Factory/CCClassFactory.h

37 lines
1.3 KiB
C
Raw Normal View History

2025-12-06 17:11:34 +08:00
#ifndef REFLECTION_CC_CLASS_FACTORY_H
#define REFLECTION_CC_CLASS_FACTORY_H
#include <string>
#include <map>
#include <functional>
#include "iostream"
#include "CCClassFactoryInfo.h"
#include "CCObject.h"
namespace CTL {
class ClassFactory {
std::map<std::string, std::function<Object*()>> m_map;
std::map<std::string, std::vector<ClassFactoryInfo*>> m_map_info;
ClassFactory();
~ClassFactory();
public:
static ClassFactory* getInstance();
void register_class(const std::string& name, const std::function<Object*()> &create_func);
Object* create_class(const std::string& name);
void register_info(const std::string& name, const std::string& infoName, const std::string& type,size_t offset);
int get_info_count(const std::string& className);
ClassFactoryInfo* get_info(const std::string& className, int pos);
ClassFactoryInfo* get_info(const std::string& className, const std::string& infoName);
};
#define CC_CREATE_CLASS(className) (className*)CTL::ClassFactory::getInstance()->create_class(#className)
#define CC_CREATE_CLASS_FROM_STRING(classNameStr) \
[](const std::string& str) -> void* { \
return CTL::ClassFactory::getInstance()->create_class(str); \
}(classNameStr)
}
#endif