1 | #ifndef _Identifier_H__ |
---|
2 | #define _Identifier_H__ |
---|
3 | |
---|
4 | #include <iostream> |
---|
5 | |
---|
6 | #include "IdentifierList.h" |
---|
7 | #include "ObjectList.h" |
---|
8 | #include "Factory.h" |
---|
9 | |
---|
10 | #define HIERARCHY_VERBOSE false |
---|
11 | |
---|
12 | |
---|
13 | namespace orxonox |
---|
14 | { |
---|
15 | class BaseObject; |
---|
16 | |
---|
17 | // ############################### |
---|
18 | // ### Identifier ### |
---|
19 | // ############################### |
---|
20 | class Identifier |
---|
21 | { |
---|
22 | template <class T> |
---|
23 | friend class ClassIdentifier; |
---|
24 | |
---|
25 | template <class T> |
---|
26 | friend class SubclassIdentifier; |
---|
27 | |
---|
28 | template <class T> |
---|
29 | friend class ClassFactory; |
---|
30 | |
---|
31 | public: |
---|
32 | inline void addFactory(BaseFactory* factory) { this->factory_ = factory; } |
---|
33 | BaseObject* fabricate(); |
---|
34 | |
---|
35 | bool isA(const Identifier* identifier) const; |
---|
36 | bool isDirectlyA(const Identifier* identifier) const; |
---|
37 | bool isChildOf(const Identifier* identifier) const; |
---|
38 | bool isParentOf(const Identifier* identifier) const; |
---|
39 | |
---|
40 | inline const std::string& getName() const { return this->name_; } |
---|
41 | inline const IdentifierList& getParents() const { return this->parents_; } |
---|
42 | inline IdentifierList& getChildren() const { return *this->children_; } |
---|
43 | |
---|
44 | inline static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); } |
---|
45 | |
---|
46 | private: |
---|
47 | Identifier(); |
---|
48 | Identifier(const Identifier& identifier) {} |
---|
49 | virtual ~Identifier(); |
---|
50 | void initialize(const IdentifierList* parents); |
---|
51 | |
---|
52 | inline static void startCreatingHierarchy() |
---|
53 | { |
---|
54 | hierarchyCreatingCounter_s++; |
---|
55 | #if HIERARCHY_VERBOSE |
---|
56 | std::cout << "*** Increased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << "\n"; |
---|
57 | #endif |
---|
58 | } |
---|
59 | |
---|
60 | inline static void stopCreatingHierarchy() |
---|
61 | { |
---|
62 | hierarchyCreatingCounter_s--; |
---|
63 | #if HIERARCHY_VERBOSE |
---|
64 | std::cout << "*** Decreased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << "\n"; |
---|
65 | #endif |
---|
66 | } |
---|
67 | |
---|
68 | IdentifierList parents_; |
---|
69 | IdentifierList* children_; |
---|
70 | |
---|
71 | std::string name_; |
---|
72 | |
---|
73 | BaseFactory* factory_; |
---|
74 | bool bCreatedOneObject_; |
---|
75 | static int hierarchyCreatingCounter_s; |
---|
76 | }; |
---|
77 | |
---|
78 | |
---|
79 | // ############################### |
---|
80 | // ### ClassIdentifier ### |
---|
81 | // ############################### |
---|
82 | template <class T> |
---|
83 | class ClassIdentifier : public Identifier |
---|
84 | { |
---|
85 | template <class U> |
---|
86 | friend class Iterator; |
---|
87 | |
---|
88 | public: |
---|
89 | static ClassIdentifier<T>* registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass); |
---|
90 | static ClassIdentifier<T>* getIdentifier(); |
---|
91 | static void addObject(T* object); |
---|
92 | |
---|
93 | private: |
---|
94 | ClassIdentifier(); |
---|
95 | ClassIdentifier(const ClassIdentifier<T>& identifier) {} |
---|
96 | ~ClassIdentifier(); |
---|
97 | |
---|
98 | static ClassIdentifier<T>* pointer_s; |
---|
99 | ObjectList<T>* objects_; |
---|
100 | }; |
---|
101 | |
---|
102 | template <class T> |
---|
103 | ClassIdentifier<T>* ClassIdentifier<T>::pointer_s = NULL; |
---|
104 | |
---|
105 | template <class T> |
---|
106 | ClassIdentifier<T>::ClassIdentifier() |
---|
107 | { |
---|
108 | this->objects_ = new ObjectList<T>; |
---|
109 | } |
---|
110 | |
---|
111 | template <class T> |
---|
112 | ClassIdentifier<T>::~ClassIdentifier() |
---|
113 | { |
---|
114 | delete this->objects_; |
---|
115 | this->pointer_s = NULL; |
---|
116 | } |
---|
117 | |
---|
118 | template <class T> |
---|
119 | ClassIdentifier<T>* ClassIdentifier<T>::registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass) |
---|
120 | { |
---|
121 | #if HIERARCHY_VERBOSE |
---|
122 | std::cout << "*** Register Class in " << name << "-Singleton.\n"; |
---|
123 | #endif |
---|
124 | if (!pointer_s) |
---|
125 | { |
---|
126 | #if HIERARCHY_VERBOSE |
---|
127 | std::cout << "*** Register Class in " << name << "-Singleton -> Create Singleton.\n"; |
---|
128 | #endif |
---|
129 | pointer_s = new ClassIdentifier(); |
---|
130 | } |
---|
131 | |
---|
132 | if (!pointer_s->bCreatedOneObject_) |
---|
133 | { |
---|
134 | #if HIERARCHY_VERBOSE |
---|
135 | std::cout << "*** Register Class in " << name << "-Singleton -> Initialize Singleton.\n"; |
---|
136 | #endif |
---|
137 | pointer_s->name_ = name; |
---|
138 | Factory::add(name, pointer_s); |
---|
139 | |
---|
140 | if (bRootClass) |
---|
141 | pointer_s->initialize(NULL); |
---|
142 | else |
---|
143 | pointer_s->initialize(parents); |
---|
144 | } |
---|
145 | |
---|
146 | return pointer_s; |
---|
147 | } |
---|
148 | |
---|
149 | template <class T> |
---|
150 | ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier() |
---|
151 | { |
---|
152 | if (!pointer_s) |
---|
153 | { |
---|
154 | #if HIERARCHY_VERBOSE |
---|
155 | std::cout << "*** Create Singleton.\n"; |
---|
156 | #endif |
---|
157 | pointer_s = new ClassIdentifier(); |
---|
158 | } |
---|
159 | |
---|
160 | return pointer_s; |
---|
161 | } |
---|
162 | |
---|
163 | template <class T> |
---|
164 | void ClassIdentifier<T>::addObject(T* object) |
---|
165 | { |
---|
166 | #if HIERARCHY_VERBOSE |
---|
167 | std::cout << "*** Added object to " << ClassIdentifier<T>::getIdentifier()->getName() << "-list.\n"; |
---|
168 | #endif |
---|
169 | object->getMetaList()->add(ClassIdentifier<T>::getIdentifier()->objects_, ClassIdentifier<T>::getIdentifier()->objects_->add(object)); |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | // ############################### |
---|
174 | // ### SubclassIdentifier ### |
---|
175 | // ############################### |
---|
176 | template <class B> |
---|
177 | class SubclassIdentifier |
---|
178 | { |
---|
179 | public: |
---|
180 | SubclassIdentifier(); |
---|
181 | |
---|
182 | SubclassIdentifier<B>& operator=(Identifier* identifier) |
---|
183 | { |
---|
184 | if (!identifier->isA(ClassIdentifier<B>::getIdentifier())) |
---|
185 | { |
---|
186 | std::cout << "Error: Class " << identifier->getName() << " is not a " << ClassIdentifier<B>::getIdentifier()->getName() << "!\n"; |
---|
187 | std::cout << "Error: SubclassIdentifier<" << ClassIdentifier<B>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden.\n"; |
---|
188 | std::cout << "Aborting...\n"; |
---|
189 | abort(); |
---|
190 | } |
---|
191 | this->identifier_ = identifier; |
---|
192 | return *this; |
---|
193 | } |
---|
194 | |
---|
195 | Identifier* operator*() |
---|
196 | { |
---|
197 | return this->identifier_; |
---|
198 | } |
---|
199 | |
---|
200 | Identifier* operator->() const |
---|
201 | { |
---|
202 | return this->identifier_; |
---|
203 | } |
---|
204 | |
---|
205 | B* fabricate() |
---|
206 | { |
---|
207 | BaseObject* newObject = this->identifier_->fabricate(); |
---|
208 | if (newObject) |
---|
209 | { |
---|
210 | return dynamic_cast<B*>(newObject); |
---|
211 | } |
---|
212 | else |
---|
213 | { |
---|
214 | if (this->identifier_) |
---|
215 | { |
---|
216 | std::cout << "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<B>::getIdentifier()->getName() << "!\n"; |
---|
217 | std::cout << "Error: Couldn't fabricate a new Object.\n"; |
---|
218 | std::cout << "Aborting...\n"; |
---|
219 | } |
---|
220 | else |
---|
221 | { |
---|
222 | std::cout << "Error: Couldn't fabricate a new Object - Identifier is undefined.\n"; |
---|
223 | std::cout << "Aborting...\n"; |
---|
224 | } |
---|
225 | |
---|
226 | abort(); |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | inline const Identifier* getIdentifier() const |
---|
231 | { return this->identifier_; } |
---|
232 | inline bool isA(const Identifier* identifier) const |
---|
233 | { return this->identifier_->isA(identifier); } |
---|
234 | inline bool isDirectlyA(const Identifier* identifier) const |
---|
235 | { return this->identifier_->isDirectlyA(identifier); } |
---|
236 | inline bool isChildOf(const Identifier* identifier) const |
---|
237 | { return this->identifier_->isChildOf(identifier); } |
---|
238 | inline bool isParentOf(const Identifier* identifier) const |
---|
239 | { return this->identifier_->isParentOf(identifier); } |
---|
240 | |
---|
241 | private: |
---|
242 | Identifier* identifier_; |
---|
243 | }; |
---|
244 | |
---|
245 | template <class B> |
---|
246 | SubclassIdentifier<B>::SubclassIdentifier() |
---|
247 | { |
---|
248 | this->identifier_ = ClassIdentifier<B>::getIdentifier(); |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | #endif |
---|