9#define DECLARE_FACTORY_MANAGER(Base) \
10 static std::vector<Factory*>& GetFactoryList(); \
11 static TypeId RegisterFactory(Factory* factory, uint32_t typeIdMod = 0); \
12 static Base* CreateInstance(const char* typeName); \
13 static Base* CreateInstance(TypeId typeId);
15#define DEFINE_FACTORY_MANAGER(Base) \
16 std::vector<Factory*>& Base::GetFactoryList() \
18 static std::vector<Factory*> sFactoryList; \
19 return sFactoryList; \
22 TypeId Base::RegisterFactory(Factory* factory, uint32_t typeIdMod) \
24 std::vector<Factory*>& factoryList = GetFactoryList(); \
25 const char* name = factory->GetClassName(); \
26 TypeId typeId = (OctHashString(name) + typeIdMod); \
27 if (typeId == 0) { typeId++; } \
28 for (uint32_t i = 0; i < factoryList.size(); ++i) { \
29 if (strncmp(factoryList[i]->GetClassName(), name, MAX_PATH_SIZE) == 0) { \
30 LogError("Conflicting class name found in factory's RegisterClass() - %s", name); OCT_ASSERT(0); typeId = 0; break; } \
31 if (factoryList[i]->GetType() == typeId) { \
32 LogError("Conflicting TypeId %x encountered in " #Base " factory manager's RegisterClass() - [%s] and [%s]", (uint32_t)typeId, factoryList[i]->GetClassName(), name); \
33 LogError("Use special case of XXXXX_FACTORY() with hash add number to avoid conflict."); OCT_ASSERT(0); typeId = 0; break; } \
35 if (typeId != 0) { factoryList.push_back(factory); } \
39 Base* Base::CreateInstance(const char* typeName) \
41 std::vector<Factory*>& factoryList = GetFactoryList(); \
42 Base* retObject = nullptr; \
43 for (uint32_t i = 0; i < factoryList.size(); ++i) { \
44 if (strncmp(factoryList[i]->GetClassName(), typeName, MAX_PATH_SIZE) == 0) { \
45 retObject = (Base*) factoryList[i]->Create(); break; } \
50 Base* Base::CreateInstance(TypeId typeId) \
52 std::vector<Factory*>& factoryList = GetFactoryList(); \
53 Base* retObject = nullptr; \
54 for (uint32_t i = 0; i < factoryList.size(); ++i) { \
55 if (factoryList[i]->GetType() == typeId) { \
56 retObject = (Base*) factoryList[i]->Create(); break; } \
90#define DECLARE_FACTORY(Class, BaseClass) \
91 class Factory_##Class; \
92 virtual TypeId GetType() const; \
93 virtual const char* GetClassName() const; \
94 static TypeId GetStaticType();
96#define DEFINE_FACTORY_EX(Class, BaseClass, TypeMod) \
97 class Factory_##Class : public Factory \
100 Factory_##Class() { mType = BaseClass::RegisterFactory(this, TypeMod); } \
101 virtual void* Create() override { return new Class(); } \
102 virtual const char* GetClassName() const override { return #Class; } \
104 static Factory_##Class sFactory_##Class; \
105 TypeId Class::GetType() const { return sFactory_##Class.GetType(); } \
106 const char* Class::GetClassName() const { return sFactory_##Class.GetClassName(); } \
107 TypeId Class::GetStaticType() { return sFactory_##Class.GetType(); }
109#define DEFINE_FACTORY(Class, BaseClass) DEFINE_FACTORY_EX(Class, BaseClass, 0)
uint32_t TypeId
Definition EngineTypes.h:64
TypeId mType
Definition Factory.h:85
TypeId GetType()
Definition Factory.h:69
virtual const char * GetClassName() const
Definition Factory.h:79
Factory()
Definition Factory.h:64
virtual void * Create()
Definition Factory.h:74