1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * Fabian 'x3n' Landau |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | /*! |
---|
29 | @file Identifier.h |
---|
30 | @brief Definition of the Identifier, ClassIdentifier and SubclassIdentifier classes, implementation of the ClassIdentifier and SubclassIdentifier classes. |
---|
31 | |
---|
32 | The Identifier contains all needed informations about the class it belongs to: |
---|
33 | - the name |
---|
34 | - a list with all objects |
---|
35 | - parents and childs |
---|
36 | - the factory (if available) |
---|
37 | - the networkID that can be synchronised with the server |
---|
38 | - all configurable variables (if available) |
---|
39 | |
---|
40 | Every object has a pointer to the Identifier of its class. This allows the use isA(...), |
---|
41 | isDirectlyA(...), isChildOf(...) and isParentOf(...). |
---|
42 | |
---|
43 | To create the class-hierarchy, the Identifier has some intern functions and variables. |
---|
44 | |
---|
45 | Every Identifier is in fact a ClassIdentifier, but they are derived from Identifier. |
---|
46 | |
---|
47 | SubclassIdentifier is a separated class, acting like an Identifier, but has a given class. |
---|
48 | You can only assign Identifiers of exactly the given class or of a derivative to a SubclassIdentifier. |
---|
49 | */ |
---|
50 | |
---|
51 | #ifndef _Identifier_H__ |
---|
52 | #define _Identifier_H__ |
---|
53 | |
---|
54 | #include <map> |
---|
55 | #include <string> |
---|
56 | |
---|
57 | #include "CorePrereqs.h" |
---|
58 | |
---|
59 | #include "ObjectList.h" |
---|
60 | #include "IdentifierList.h" |
---|
61 | #include "Factory.h" |
---|
62 | #include "Debug.h" |
---|
63 | // These two files would actually be need, but they would produce |
---|
64 | // circular dependencies. Anyway, it does compile without them |
---|
65 | //#include "OrxonoxClass.h" |
---|
66 | //#include "MetaObjectList.h" |
---|
67 | |
---|
68 | namespace orxonox |
---|
69 | { |
---|
70 | class BaseObject; // Forward declaration |
---|
71 | |
---|
72 | // ############################### |
---|
73 | // ### Identifier ### |
---|
74 | // ############################### |
---|
75 | //! The Identifier is used to identify the class of an object and to store informations about the class. |
---|
76 | /** |
---|
77 | The Identifier contains all needed informations about the class it belongs to: |
---|
78 | - the name |
---|
79 | - a list with all objects |
---|
80 | - parents and childs |
---|
81 | - the factory (if available) |
---|
82 | - the networkID that can be synchronised with the server |
---|
83 | - all configurable variables (if available) |
---|
84 | |
---|
85 | Every object has a pointer to the Identifier of its class. This allows the use isA(...), |
---|
86 | isDirectlyA(...), isChildOf(...) and isParentOf(...). |
---|
87 | |
---|
88 | You can't directly create an Identifier, it's just the base-class for ClassIdentifier. |
---|
89 | */ |
---|
90 | class _CoreExport Identifier |
---|
91 | { |
---|
92 | template <class T> |
---|
93 | friend class ClassIdentifier; // Forward declaration |
---|
94 | |
---|
95 | template <class T> |
---|
96 | friend class SubclassIdentifier; // Forward declaration |
---|
97 | |
---|
98 | friend class Factory; // Forward declaration |
---|
99 | |
---|
100 | public: |
---|
101 | /** @brief Sets the Factory. |
---|
102 | * @param factory The factory to assign |
---|
103 | */ |
---|
104 | inline void addFactory(BaseFactory* factory) { this->factory_ = factory; } |
---|
105 | |
---|
106 | BaseObject* fabricate(); |
---|
107 | |
---|
108 | bool isA(const Identifier* identifier) const; |
---|
109 | bool isDirectlyA(const Identifier* identifier) const; |
---|
110 | bool isChildOf(const Identifier* identifier) const; |
---|
111 | bool isParentOf(const Identifier* identifier) const; |
---|
112 | |
---|
113 | /** @returns the name of the class the Identifier belongs to. */ |
---|
114 | inline const std::string& getName() const { return this->name_; } |
---|
115 | |
---|
116 | /** @returns the parents of the class the Identifier belongs to. */ |
---|
117 | inline const IdentifierList& getParents() const { return this->parents_; } |
---|
118 | |
---|
119 | /** @returns the children of the class the Identifier belongs to. */ |
---|
120 | inline IdentifierList& getChildren() const { return *this->children_; } |
---|
121 | |
---|
122 | /** @returns true, if a branch of the class-hierarchy is being created, causing all new objects to store their parents. */ |
---|
123 | inline static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); } |
---|
124 | |
---|
125 | /** @returns the network ID to identify a class through the network. */ |
---|
126 | inline const unsigned int getNetworkID() const { return this->classID_; } |
---|
127 | |
---|
128 | /** @brief Sets the network ID to a new value. @param id The new value */ |
---|
129 | void setNetworkID(unsigned int id); |
---|
130 | |
---|
131 | /** @returns the ConfigValueContainer of a variable, given by the string of its name. @param varname The name of the variable */ |
---|
132 | inline ConfigValueContainer* getConfigValueContainer(const std::string& varname) |
---|
133 | { return this->configValues_[varname]; } |
---|
134 | |
---|
135 | /** @brief Sets the ConfigValueContainer of a variable, given by the string of its name. @param varname The name of the variablee @param container The container */ |
---|
136 | inline void setConfigValueContainer(const std::string& varname, ConfigValueContainer* container) |
---|
137 | { this->configValues_[varname] = container; } |
---|
138 | |
---|
139 | static std::map<std::string, Identifier*>& getIdentifierMap(); |
---|
140 | |
---|
141 | private: |
---|
142 | Identifier(); |
---|
143 | Identifier(const Identifier& identifier) {} // don't copy |
---|
144 | virtual ~Identifier(); |
---|
145 | void initialize(const IdentifierList* parents); |
---|
146 | |
---|
147 | /** |
---|
148 | @brief Increases the hierarchyCreatingCounter_s variable, causing all new objects to store their parents. |
---|
149 | */ |
---|
150 | inline static void startCreatingHierarchy() |
---|
151 | { |
---|
152 | hierarchyCreatingCounter_s++; |
---|
153 | COUT(4) << "*** Increased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl; |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | @brief Decreases the hierarchyCreatingCounter_s variable, causing the objects to stop storing their parents. |
---|
158 | */ |
---|
159 | inline static void stopCreatingHierarchy() |
---|
160 | { |
---|
161 | hierarchyCreatingCounter_s--; |
---|
162 | COUT(4) << "*** Decreased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl; |
---|
163 | } |
---|
164 | |
---|
165 | IdentifierList parents_; //!< The Parents of the class the Identifier belongs to |
---|
166 | IdentifierList* children_; //!< The Children of the class the Identifier belongs to |
---|
167 | |
---|
168 | std::string name_; //!< The name of the class the Identifier belongs to |
---|
169 | |
---|
170 | BaseFactory* factory_; //!< The Factory, able to create new objects of the given class (if available) |
---|
171 | bool bCreatedOneObject_; //!< True if at least one object of the given type was created (used to determine the need of storing the parents) |
---|
172 | static int hierarchyCreatingCounter_s; //!< Bigger than zero if at least one Identifier stores its parents (its an int instead of a bool to avoid conflicts with multithreading) |
---|
173 | unsigned int classID_; //!< The network ID to identify a class through the network |
---|
174 | std::map<std::string, ConfigValueContainer*> configValues_; //!< A map to link the string of configurable variables with their ConfigValueContainer |
---|
175 | }; |
---|
176 | |
---|
177 | |
---|
178 | // ############################### |
---|
179 | // ### ClassIdentifier ### |
---|
180 | // ############################### |
---|
181 | //! The ClassIdentifier is derived from Identifier and holds all class-specific functions and variables the Identifier cannot have. |
---|
182 | /** |
---|
183 | ClassIdentifier is a Singleton, which means that only one object of a given type T exists. |
---|
184 | This makes it possible to store informations about a class, sharing them with all |
---|
185 | objects of that class without defining static variables in every class. |
---|
186 | */ |
---|
187 | template <class T> |
---|
188 | class ClassIdentifier : public Identifier |
---|
189 | { |
---|
190 | public: |
---|
191 | static ClassIdentifier<T>* registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass); |
---|
192 | static void addObject(T* object); |
---|
193 | static ClassIdentifier<T>* getIdentifier(); |
---|
194 | void setName(const std::string& name); |
---|
195 | |
---|
196 | private: |
---|
197 | ClassIdentifier(); |
---|
198 | ClassIdentifier(const ClassIdentifier<T>& identifier) {} // don't copy |
---|
199 | ~ClassIdentifier() {} // don't delete |
---|
200 | |
---|
201 | ObjectList<T>* objects_; //!< The ObjectList, containing all objects of type T |
---|
202 | bool bSetName_; //!< True if the name is set |
---|
203 | }; |
---|
204 | |
---|
205 | /** |
---|
206 | @brief Constructor: Creates the ObjectList. |
---|
207 | */ |
---|
208 | template <class T> |
---|
209 | ClassIdentifier<T>::ClassIdentifier() |
---|
210 | { |
---|
211 | this->objects_ = ObjectList<T>::getList(); |
---|
212 | this->bSetName_ = false; |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | @brief Registers a class, which means that the name and the parents get stored. |
---|
217 | @param parents An IdentifierList, containing the Identifiers of all parents of the class |
---|
218 | @param name A string, containing exactly the name of the class |
---|
219 | @param bRootClass True if the class is either an Interface or the BaseObject itself |
---|
220 | @return The ClassIdentifier itself |
---|
221 | */ |
---|
222 | template <class T> |
---|
223 | ClassIdentifier<T>* ClassIdentifier<T>::registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass) |
---|
224 | { |
---|
225 | COUT(4) << "*** Register Class in " << name << "-Singleton." << std::endl; |
---|
226 | |
---|
227 | // Check if at least one object of the given type was created |
---|
228 | if (!getIdentifier()->bCreatedOneObject_) |
---|
229 | { |
---|
230 | // If no: We have to store the informations and initialize the Identifier |
---|
231 | getIdentifier()->setName(name); |
---|
232 | |
---|
233 | COUT(4) << "*** Register Class in " << name << "-Singleton -> Initialize Singleton." << std::endl; |
---|
234 | if (bRootClass) |
---|
235 | getIdentifier()->initialize(NULL); // If a class is derived from two interfaces, the second interface might think it's derived from the first because of the order of constructor-calls. Thats why we set parents to zero in that case. |
---|
236 | else |
---|
237 | getIdentifier()->initialize(parents); |
---|
238 | } |
---|
239 | |
---|
240 | return getIdentifier(); |
---|
241 | } |
---|
242 | |
---|
243 | /** |
---|
244 | @brief Creates the only instance of this class for the template class T. |
---|
245 | @return The Identifier itself |
---|
246 | */ |
---|
247 | template <class T> |
---|
248 | ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier() |
---|
249 | { |
---|
250 | static ClassIdentifier<T> theOneAndOnlyInstance = ClassIdentifier<T>(); |
---|
251 | static bool bIdentifierCreated = false; |
---|
252 | |
---|
253 | if (!bIdentifierCreated) |
---|
254 | { |
---|
255 | COUT(4) << "*** Create Identifier Singleton." << std::endl; |
---|
256 | bIdentifierCreated = true; |
---|
257 | } |
---|
258 | |
---|
259 | return &theOneAndOnlyInstance; |
---|
260 | } |
---|
261 | |
---|
262 | /** |
---|
263 | @brief Sets the name of the class. |
---|
264 | @param name The name |
---|
265 | */ |
---|
266 | template <class T> |
---|
267 | void ClassIdentifier<T>::setName(const std::string& name) |
---|
268 | { |
---|
269 | // Make sure we didn't already set the name, to avoid duplicate entries in the Identifier map |
---|
270 | if (!this->bSetName_) |
---|
271 | { |
---|
272 | this->name_ = name; |
---|
273 | this->getIdentifierMap().insert(std::pair<std::string, Identifier*>(name, this)); |
---|
274 | this->bSetName_ = true; |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | /** |
---|
279 | @brief Adds an object of the given type to the ObjectList. |
---|
280 | @param object The object to add |
---|
281 | */ |
---|
282 | template <class T> |
---|
283 | void ClassIdentifier<T>::addObject(T* object) |
---|
284 | { |
---|
285 | COUT(4) << "*** Added object to " << ClassIdentifier<T>::getIdentifier()->getName() << "-list." << std::endl; |
---|
286 | object->getMetaList().add(ClassIdentifier<T>::getIdentifier()->objects_, ClassIdentifier<T>::getIdentifier()->objects_->add(object)); |
---|
287 | } |
---|
288 | |
---|
289 | |
---|
290 | // ############################### |
---|
291 | // ### SubclassIdentifier ### |
---|
292 | // ############################### |
---|
293 | //! The SubclassIdentifier acts almost like an Identifier, but has some prerequisites. |
---|
294 | /** |
---|
295 | You can only assign an Identifier that belongs to a class T (or derived) to a SubclassIdentifier<T>. |
---|
296 | If you assign something else, the program aborts. |
---|
297 | Because we know the minimal type, a dynamic_cast is done, which makes it easier to create a new object. |
---|
298 | */ |
---|
299 | template <class T> |
---|
300 | class SubclassIdentifier |
---|
301 | { |
---|
302 | public: |
---|
303 | /** |
---|
304 | @brief Constructor: Automaticaly assigns the Identifier of the given class. |
---|
305 | */ |
---|
306 | SubclassIdentifier() |
---|
307 | { |
---|
308 | this->identifier_ = ClassIdentifier<T>::getIdentifier(); |
---|
309 | } |
---|
310 | |
---|
311 | /** |
---|
312 | @brief Overloading of the = operator: assigns the identifier and checks its type. |
---|
313 | @param identifier The Identifier to assign |
---|
314 | @return The SubclassIdentifier itself |
---|
315 | */ |
---|
316 | SubclassIdentifier<T>& operator=(Identifier* identifier) |
---|
317 | { |
---|
318 | if (!identifier->isA(ClassIdentifier<T>::getIdentifier())) |
---|
319 | { |
---|
320 | COUT(1) << "Error: Class " << identifier->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!" << std::endl; |
---|
321 | COUT(1) << "Error: SubclassIdentifier<" << ClassIdentifier<T>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden." << std::endl; |
---|
322 | COUT(1) << "Aborting..." << std::endl; |
---|
323 | abort(); |
---|
324 | } |
---|
325 | this->identifier_ = identifier; |
---|
326 | return *this; |
---|
327 | } |
---|
328 | |
---|
329 | /** |
---|
330 | @brief Overloading of the * operator: returns the assigned identifier. |
---|
331 | @return The assigned identifier |
---|
332 | */ |
---|
333 | Identifier* operator*() |
---|
334 | { |
---|
335 | return this->identifier_; |
---|
336 | } |
---|
337 | |
---|
338 | /** |
---|
339 | @brief Overloading of the -> operator: returns the assigned identifier. |
---|
340 | @return The assigned identifier |
---|
341 | */ |
---|
342 | Identifier* operator->() const |
---|
343 | { |
---|
344 | return this->identifier_; |
---|
345 | } |
---|
346 | |
---|
347 | /** |
---|
348 | @brief Creates a new object of the type of the assigned Identifier and dynamic_casts it to the minimal type given by T. |
---|
349 | @return The new object |
---|
350 | */ |
---|
351 | T* fabricate() |
---|
352 | { |
---|
353 | BaseObject* newObject = this->identifier_->fabricate(); |
---|
354 | |
---|
355 | // Check if the creation was successful |
---|
356 | if (newObject) |
---|
357 | { |
---|
358 | // Do a dynamic_cast, because an object of type T is much better than of type BaseObject |
---|
359 | return (T*)(newObject); |
---|
360 | } |
---|
361 | else |
---|
362 | { |
---|
363 | // Something went terribly wrong |
---|
364 | if (this->identifier_) |
---|
365 | { |
---|
366 | COUT(1) << "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<T>::getIdentifier()->getName() << "!" << std::endl; |
---|
367 | COUT(1) << "Error: Couldn't fabricate a new Object." << std::endl; |
---|
368 | COUT(1) << "Aborting..." << std::endl; |
---|
369 | } |
---|
370 | else |
---|
371 | { |
---|
372 | COUT(1) << "Error: Couldn't fabricate a new Object - Identifier is undefined." << std::endl; |
---|
373 | COUT(1) << "Aborting..." << std::endl; |
---|
374 | } |
---|
375 | |
---|
376 | abort(); |
---|
377 | } |
---|
378 | } |
---|
379 | |
---|
380 | /** @returns the assigned identifier. */ |
---|
381 | inline const Identifier* getIdentifier() const |
---|
382 | { return this->identifier_; } |
---|
383 | |
---|
384 | /** @returns true, if the assigned identifier is at least of the given type. @param identifier The identifier to compare with */ |
---|
385 | inline bool isA(const Identifier* identifier) const |
---|
386 | { return this->identifier_->isA(identifier); } |
---|
387 | |
---|
388 | /** @returns true, if the assigned identifier is exactly of the given type. @param identifier The identifier to compare with */ |
---|
389 | inline bool isDirectlyA(const Identifier* identifier) const |
---|
390 | { return this->identifier_->isDirectlyA(identifier); } |
---|
391 | |
---|
392 | /** @returns true, if the assigned identifier is a child of the given identifier. @param identifier The identifier to compare with */ |
---|
393 | inline bool isChildOf(const Identifier* identifier) const |
---|
394 | { return this->identifier_->isChildOf(identifier); } |
---|
395 | |
---|
396 | /** @returns true, if the assigned identifier is a parent of the given identifier. @param identifier The identifier to compare with */ |
---|
397 | inline bool isParentOf(const Identifier* identifier) const |
---|
398 | { return this->identifier_->isParentOf(identifier); } |
---|
399 | |
---|
400 | private: |
---|
401 | Identifier* identifier_; //!< The assigned identifier |
---|
402 | }; |
---|
403 | } |
---|
404 | |
---|
405 | #endif /* _Identifier_H__ */ |
---|