Polyphase Game Engine
Loading...
Searching...
No Matches
Factory.h
Go to the documentation of this file.
1#pragma once
2
3#include "Utilities.h"
4
5#ifdef GetClassName
6#undef GetClassName
7#endif
8
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);
14
15#define DEFINE_FACTORY_MANAGER(Base) \
16 std::vector<Factory*>& Base::GetFactoryList() \
17 { \
18 static std::vector<Factory*> sFactoryList; \
19 return sFactoryList; \
20 } \
21 \
22 TypeId Base::RegisterFactory(Factory* factory, uint32_t typeIdMod) \
23 { \
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; } \
34 } \
35 if (typeId != 0) { factoryList.push_back(factory); } \
36 return typeId; \
37 } \
38 \
39 Base* Base::CreateInstance(const char* typeName) \
40 { \
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; } \
46 } \
47 return retObject; \
48 }\
49 \
50 Base* Base::CreateInstance(TypeId typeId) \
51 { \
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; } \
57 } \
58 return retObject; \
59 }
60
62{
63public:
65 {
66
67 }
68
70 {
71 return mType;
72 }
73
74 virtual void* Create()
75 {
76 return nullptr;
77 }
78
79 virtual const char* GetClassName() const
80 {
81 return "Class";
82 }
83
84protected:
86};
87
88// In the future, DECLARE_FACTORY might do more, but right now it just creates a GetType() func
89// The "override" qualifier is left out so that the base class can also use this macro.
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();
95
96#define DEFINE_FACTORY_EX(Class, BaseClass, TypeMod) \
97 class Factory_##Class : public Factory \
98 { \
99 public: \
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; } \
103 }; \
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(); }
108
109#define DEFINE_FACTORY(Class, BaseClass) DEFINE_FACTORY_EX(Class, BaseClass, 0)
uint32_t TypeId
Definition EngineTypes.h:64
Definition Factory.h:62
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