[790] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1505] | 3 | * > www.orxonox.net < |
---|
[790] | 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[871] | 29 | /** |
---|
[7401] | 30 | @defgroup Identifier Identifier |
---|
| 31 | @ingroup Class |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | /** |
---|
[2171] | 35 | @file |
---|
[7401] | 36 | @ingroup Class Identifier |
---|
| 37 | @brief Declaration of Identifier, definition of ClassIdentifier<T>; used to identify the class of an object. |
---|
[790] | 38 | |
---|
[7401] | 39 | @anchor IdentifierExample |
---|
[790] | 40 | |
---|
[7401] | 41 | An Identifier "identifies" the class of an object. It contains different information about |
---|
| 42 | the class: Its name and ID, a list of all instances of this class, a factory to create new |
---|
| 43 | instances of this class, and more. |
---|
[790] | 44 | |
---|
[7401] | 45 | It also contains information about the inheritance of this class: It stores a list of the |
---|
| 46 | Identifiers of all parent-classes as well as a list of all child-classes. These relationships |
---|
| 47 | can be tested using functions like @c isA(), @c isChildOf(), @c isParentOf(), and more. |
---|
[790] | 48 | |
---|
[7401] | 49 | Every Identifier is in fact a ClassIdentifier<T> (where T is the class that is identified |
---|
| 50 | by the Identifier), Identifier is just the common base-class. |
---|
| 51 | |
---|
| 52 | Example: |
---|
| 53 | @code |
---|
| 54 | MyClass* object = new MyClass(); // create an instance of MyClass |
---|
| 55 | |
---|
| 56 | object->getIdentifier()->getName(); // returns "MyClass" |
---|
| 57 | |
---|
| 58 | BaseObject* other = object->getIdentifier()->fabricate(0); // fabricates a new instance of MyClass |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | // iterate through all objects of type MyClass: |
---|
| 62 | ObjectListBase* objects = object->getIdentifier()->getObjects(); // get a pointer to the object-list |
---|
| 63 | int count; |
---|
| 64 | for (Iterator<BaseObject> it = objects.begin(); it != objects.end(); ++it) // iterate through the objects |
---|
| 65 | ++count; |
---|
| 66 | COUT(0) << count << std::endl; // prints "2" because we created 2 instances of MyClass so far |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | // test the class hierarchy |
---|
| 70 | object->getIdentifier()->isA(Class(MyClass)); // returns true |
---|
| 71 | object->isA(Class(MyClass)); // returns true (short version) |
---|
| 72 | |
---|
| 73 | object->isA(Class(BaseClass)); // returns true if MyClass is a child of BaseClass |
---|
| 74 | |
---|
| 75 | Class(ChildClass)->isChildOf(object->getIdentifier()); // returns true if ChildClass is a child of MyClass |
---|
| 76 | @endcode |
---|
[790] | 77 | */ |
---|
| 78 | |
---|
| 79 | #ifndef _Identifier_H__ |
---|
| 80 | #define _Identifier_H__ |
---|
[1052] | 81 | |
---|
[1062] | 82 | #include "CorePrereqs.h" |
---|
| 83 | |
---|
[3196] | 84 | #include <cassert> |
---|
| 85 | #include <map> |
---|
[1052] | 86 | #include <set> |
---|
[790] | 87 | #include <string> |
---|
[1639] | 88 | #include <typeinfo> |
---|
[7266] | 89 | #include <loki/TypeTraits.h> |
---|
[790] | 90 | |
---|
[3196] | 91 | #include "util/Debug.h" |
---|
[1747] | 92 | #include "MetaObjectList.h" |
---|
[3196] | 93 | #include "ObjectList.h" |
---|
| 94 | #include "ObjectListBase.h" |
---|
[5781] | 95 | #include "Super.h" |
---|
[790] | 96 | |
---|
| 97 | namespace orxonox |
---|
| 98 | { |
---|
| 99 | // ############################### |
---|
| 100 | // ### Identifier ### |
---|
| 101 | // ############################### |
---|
| 102 | /** |
---|
[7401] | 103 | @brief The Identifier is used to identify the class of an object and to store information about the class. |
---|
[790] | 104 | |
---|
[7401] | 105 | Each Identifier stores information about one class. The Identifier can then be used to identify |
---|
| 106 | this class. On the other hand it's also possible to get the corresponding Identifier of a class, |
---|
| 107 | for example by using the macro Class(). |
---|
[790] | 108 | |
---|
[7401] | 109 | @see See @ref IdentifierExample "Identifier.h" for more information and some examples. |
---|
| 110 | |
---|
| 111 | @note You can't directly create an Identifier, it's just the base-class of ClassIdentifier<T>. |
---|
[790] | 112 | */ |
---|
| 113 | class _CoreExport Identifier |
---|
| 114 | { |
---|
[5929] | 115 | public: |
---|
[7401] | 116 | /// Returns the name of the class the Identifier belongs to. |
---|
[5929] | 117 | inline const std::string& getName() const { return this->name_; } |
---|
| 118 | void setName(const std::string& name); |
---|
[790] | 119 | |
---|
[7401] | 120 | /// Returns the network ID to identify a class through the network. |
---|
[8706] | 121 | inline uint32_t getNetworkID() const { return this->networkID_; } |
---|
[5929] | 122 | void setNetworkID(uint32_t id); |
---|
[790] | 123 | |
---|
[7401] | 124 | /// Returns the unique ID of the class. |
---|
[8351] | 125 | ORX_FORCEINLINE unsigned int getClassID() const { return this->classID_; } |
---|
[5929] | 126 | |
---|
[7401] | 127 | /// Returns the list of all existing objects of this class. |
---|
[5929] | 128 | inline ObjectListBase* getObjects() const { return this->objects_; } |
---|
| 129 | |
---|
[7401] | 130 | /// Sets the Factory. |
---|
[5929] | 131 | inline void addFactory(Factory* factory) { this->factory_ = factory; } |
---|
[7401] | 132 | /// Returns true if the Identifier has a Factory. |
---|
[5929] | 133 | inline bool hasFactory() const { return (this->factory_ != 0); } |
---|
[790] | 134 | |
---|
[2087] | 135 | BaseObject* fabricate(BaseObject* creator); |
---|
[5929] | 136 | |
---|
[7401] | 137 | /// Returns true if the class can be loaded through XML. |
---|
[5929] | 138 | inline bool isLoadable() const { return this->bLoadable_; } |
---|
[7401] | 139 | /// Set the class to be loadable through XML or not. |
---|
[5929] | 140 | inline void setLoadable(bool bLoadable) { this->bLoadable_ = bLoadable; } |
---|
| 141 | |
---|
[790] | 142 | bool isA(const Identifier* identifier) const; |
---|
[871] | 143 | bool isExactlyA(const Identifier* identifier) const; |
---|
[1052] | 144 | bool isChildOf(const Identifier* identifier) const; |
---|
[871] | 145 | bool isDirectChildOf(const Identifier* identifier) const; |
---|
[1052] | 146 | bool isParentOf(const Identifier* identifier) const; |
---|
[871] | 147 | bool isDirectParentOf(const Identifier* identifier) const; |
---|
[790] | 148 | |
---|
[5781] | 149 | |
---|
[5929] | 150 | ///////////////////////////// |
---|
| 151 | ////// Class Hierarchy ////// |
---|
| 152 | ///////////////////////////// |
---|
| 153 | static void createClassHierarchy(); |
---|
[790] | 154 | |
---|
[7401] | 155 | /// Returns true, if a branch of the class-hierarchy is being created, causing all new objects to store their parents. |
---|
[5929] | 156 | inline static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); } |
---|
[790] | 157 | |
---|
[7401] | 158 | /// Returns the parents of the class the Identifier belongs to. |
---|
[1052] | 159 | inline const std::set<const Identifier*>& getParents() const { return this->parents_; } |
---|
[7401] | 160 | /// Returns the begin-iterator of the parents-list. |
---|
[1052] | 161 | inline std::set<const Identifier*>::const_iterator getParentsBegin() const { return this->parents_.begin(); } |
---|
[7401] | 162 | /// Returns the end-iterator of the parents-list. |
---|
[1052] | 163 | inline std::set<const Identifier*>::const_iterator getParentsEnd() const { return this->parents_.end(); } |
---|
| 164 | |
---|
[7401] | 165 | /// Returns the children of the class the Identifier belongs to. |
---|
[5929] | 166 | inline const std::set<const Identifier*>& getChildren() const { return this->children_; } |
---|
[7401] | 167 | /// Returns the begin-iterator of the children-list. |
---|
[5929] | 168 | inline std::set<const Identifier*>::const_iterator getChildrenBegin() const { return this->children_.begin(); } |
---|
[7401] | 169 | /// Returns the end-iterator of the children-list. |
---|
[5929] | 170 | inline std::set<const Identifier*>::const_iterator getChildrenEnd() const { return this->children_.end(); } |
---|
[1052] | 171 | |
---|
[7401] | 172 | /// Returns the direct parents of the class the Identifier belongs to. |
---|
[1052] | 173 | inline const std::set<const Identifier*>& getDirectParents() const { return this->directParents_; } |
---|
[7401] | 174 | /// Returns the begin-iterator of the direct-parents-list. |
---|
[1052] | 175 | inline std::set<const Identifier*>::const_iterator getDirectParentsBegin() const { return this->directParents_.begin(); } |
---|
[7401] | 176 | /// Returns the end-iterator of the direct-parents-list. |
---|
[1052] | 177 | inline std::set<const Identifier*>::const_iterator getDirectParentsEnd() const { return this->directParents_.end(); } |
---|
| 178 | |
---|
[7401] | 179 | /// Returns the direct children the class the Identifier belongs to. |
---|
[5929] | 180 | inline const std::set<const Identifier*>& getDirectChildren() const { return this->directChildren_; } |
---|
[7401] | 181 | /// Returns the begin-iterator of the direct-children-list. |
---|
[5929] | 182 | inline std::set<const Identifier*>::const_iterator getDirectChildrenBegin() const { return this->directChildren_.begin(); } |
---|
[7401] | 183 | /// Returns the end-iterator of the direct-children-list. |
---|
[5929] | 184 | inline std::set<const Identifier*>::const_iterator getDirectChildrenEnd() const { return this->directChildren_.end(); } |
---|
[1052] | 185 | |
---|
| 186 | |
---|
[5929] | 187 | ////////////////////////// |
---|
| 188 | ///// Identifier Map ///// |
---|
| 189 | ////////////////////////// |
---|
| 190 | static void destroyAllIdentifiers(); |
---|
[1052] | 191 | |
---|
[5929] | 192 | static Identifier* getIdentifierByString(const std::string& name); |
---|
| 193 | static Identifier* getIdentifierByLowercaseString(const std::string& name); |
---|
| 194 | static Identifier* getIdentifierByID(uint32_t id); |
---|
| 195 | |
---|
| 196 | static void clearNetworkIDs(); |
---|
| 197 | |
---|
[7401] | 198 | /// Returns the map that stores all Identifiers with their names. |
---|
[5929] | 199 | static inline const std::map<std::string, Identifier*>& getStringIdentifierMap() { return Identifier::getStringIdentifierMapIntern(); } |
---|
[7401] | 200 | /// Returns a const_iterator to the beginning of the map that stores all Identifiers with their names. |
---|
[5929] | 201 | static inline std::map<std::string, Identifier*>::const_iterator getStringIdentifierMapBegin() { return Identifier::getStringIdentifierMap().begin(); } |
---|
[7401] | 202 | /// Returns a const_iterator to the end of the map that stores all Identifiers with their names. |
---|
[5929] | 203 | static inline std::map<std::string, Identifier*>::const_iterator getStringIdentifierMapEnd() { return Identifier::getStringIdentifierMap().end(); } |
---|
| 204 | |
---|
[7401] | 205 | /// Returns the map that stores all Identifiers with their names in lowercase. |
---|
[5929] | 206 | static inline const std::map<std::string, Identifier*>& getLowercaseStringIdentifierMap() { return Identifier::getLowercaseStringIdentifierMapIntern(); } |
---|
[7401] | 207 | /// Returns a const_iterator to the beginning of the map that stores all Identifiers with their names in lowercase. |
---|
[5929] | 208 | static inline std::map<std::string, Identifier*>::const_iterator getLowercaseStringIdentifierMapBegin() { return Identifier::getLowercaseStringIdentifierMap().begin(); } |
---|
[7401] | 209 | /// Returns a const_iterator to the end of the map that stores all Identifiers with their names in lowercase. |
---|
[5929] | 210 | static inline std::map<std::string, Identifier*>::const_iterator getLowercaseStringIdentifierMapEnd() { return Identifier::getLowercaseStringIdentifierMap().end(); } |
---|
[1052] | 211 | |
---|
[7401] | 212 | /// Returns the map that stores all Identifiers with their IDs. |
---|
[5929] | 213 | static inline const std::map<uint32_t, Identifier*>& getIDIdentifierMap() { return Identifier::getIDIdentifierMapIntern(); } |
---|
[7401] | 214 | /// Returns a const_iterator to the beginning of the map that stores all Identifiers with their IDs. |
---|
[5929] | 215 | static inline std::map<uint32_t, Identifier*>::const_iterator getIDIdentifierMapBegin() { return Identifier::getIDIdentifierMap().begin(); } |
---|
[7401] | 216 | /// Returns a const_iterator to the end of the map that stores all Identifiers with their IDs. |
---|
[5929] | 217 | static inline std::map<uint32_t, Identifier*>::const_iterator getIDIdentifierMapEnd() { return Identifier::getIDIdentifierMap().end(); } |
---|
[1052] | 218 | |
---|
[5929] | 219 | |
---|
| 220 | ///////////////////////// |
---|
| 221 | ///// Config Values ///// |
---|
| 222 | ///////////////////////// |
---|
| 223 | virtual void updateConfigValues(bool updateChildren = true) const = 0; |
---|
| 224 | |
---|
[7401] | 225 | /// Returns true if this class has at least one config value. |
---|
[5929] | 226 | inline bool hasConfigValues() const { return this->bHasConfigValues_; } |
---|
| 227 | |
---|
| 228 | void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container); |
---|
| 229 | ConfigValueContainer* getConfigValueContainer(const std::string& varname); |
---|
[1052] | 230 | |
---|
[5929] | 231 | |
---|
| 232 | /////////////////// |
---|
| 233 | ///// XMLPort ///// |
---|
| 234 | /////////////////// |
---|
[7401] | 235 | /// Returns the map that stores all XMLPort params. |
---|
[5781] | 236 | inline const std::map<std::string, XMLPortParamContainer*>& getXMLPortParamMap() const { return this->xmlportParamContainers_; } |
---|
[7401] | 237 | /// Returns a const_iterator to the beginning of the map that stores all XMLPort params. |
---|
[5781] | 238 | inline std::map<std::string, XMLPortParamContainer*>::const_iterator getXMLPortParamMapBegin() const { return this->xmlportParamContainers_.begin(); } |
---|
[7401] | 239 | /// Returns a const_iterator to the end of the map that stores all XMLPort params. |
---|
[5781] | 240 | inline std::map<std::string, XMLPortParamContainer*>::const_iterator getXMLPortParamMapEnd() const { return this->xmlportParamContainers_.end(); } |
---|
| 241 | |
---|
[7401] | 242 | /// Returns the map that stores all XMLPort objects. |
---|
[5781] | 243 | inline const std::map<std::string, XMLPortObjectContainer*>& getXMLPortObjectMap() const { return this->xmlportObjectContainers_; } |
---|
[7401] | 244 | /// Returns a const_iterator to the beginning of the map that stores all XMLPort objects. |
---|
[5781] | 245 | inline std::map<std::string, XMLPortObjectContainer*>::const_iterator getXMLPortObjectMapBegin() const { return this->xmlportObjectContainers_.begin(); } |
---|
[7401] | 246 | /// Returns a const_iterator to the end of the map that stores all XMLPort objects. |
---|
[5781] | 247 | inline std::map<std::string, XMLPortObjectContainer*>::const_iterator getXMLPortObjectMapEnd() const { return this->xmlportObjectContainers_.end(); } |
---|
| 248 | |
---|
| 249 | void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container); |
---|
| 250 | XMLPortParamContainer* getXMLPortParamContainer(const std::string& paramname); |
---|
| 251 | |
---|
| 252 | void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container); |
---|
| 253 | XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname); |
---|
| 254 | |
---|
| 255 | |
---|
[1052] | 256 | protected: |
---|
[1747] | 257 | Identifier(); |
---|
| 258 | Identifier(const Identifier& identifier); // don't copy |
---|
| 259 | virtual ~Identifier(); |
---|
| 260 | |
---|
| 261 | static Identifier* getIdentifierSingleton(const std::string& name, Identifier* proposal); |
---|
[5781] | 262 | virtual void createSuperFunctionCaller() const = 0; |
---|
[1747] | 263 | |
---|
[5929] | 264 | void initializeClassHierarchy(std::set<const Identifier*>* parents, bool bRootClass); |
---|
| 265 | |
---|
[7401] | 266 | /// Returns the map that stores all Identifiers with their names. |
---|
[5929] | 267 | static std::map<std::string, Identifier*>& getStringIdentifierMapIntern(); |
---|
[7401] | 268 | /// Returns the map that stores all Identifiers with their names in lowercase. |
---|
[5929] | 269 | static std::map<std::string, Identifier*>& getLowercaseStringIdentifierMapIntern(); |
---|
[7401] | 270 | /// Returns the map that stores all Identifiers with their network IDs. |
---|
[5929] | 271 | static std::map<uint32_t, Identifier*>& getIDIdentifierMapIntern(); |
---|
[1052] | 272 | |
---|
[7401] | 273 | /// Returns the children of the class the Identifier belongs to. |
---|
[5929] | 274 | inline std::set<const Identifier*>& getChildrenIntern() const { return this->children_; } |
---|
[7401] | 275 | /// Returns the direct children of the class the Identifier belongs to. |
---|
[5929] | 276 | inline std::set<const Identifier*>& getDirectChildrenIntern() const { return this->directChildren_; } |
---|
[1052] | 277 | |
---|
[1747] | 278 | ObjectListBase* objects_; //!< The list of all objects of this class |
---|
| 279 | |
---|
| 280 | private: |
---|
[7401] | 281 | /// Increases the hierarchyCreatingCounter_s variable, causing all new objects to store their parents. |
---|
[5929] | 282 | inline static void startCreatingHierarchy() { hierarchyCreatingCounter_s++; } |
---|
[7401] | 283 | /// Decreases the hierarchyCreatingCounter_s variable, causing the objects to stop storing their parents. |
---|
[5929] | 284 | inline static void stopCreatingHierarchy() { hierarchyCreatingCounter_s--; } |
---|
[790] | 285 | |
---|
[2662] | 286 | static std::map<std::string, Identifier*>& getTypeIDIdentifierMap(); |
---|
| 287 | |
---|
[1856] | 288 | void initialize(std::set<const Identifier*>* parents); |
---|
| 289 | |
---|
[1052] | 290 | std::set<const Identifier*> parents_; //!< The parents of the class the Identifier belongs to |
---|
[5929] | 291 | mutable std::set<const Identifier*> children_; //!< The children of the class the Identifier belongs to |
---|
[790] | 292 | |
---|
[1052] | 293 | std::set<const Identifier*> directParents_; //!< The direct parents of the class the Identifier belongs to |
---|
[5929] | 294 | mutable std::set<const Identifier*> directChildren_; //!< The direct children of the class the Identifier belongs to |
---|
[1052] | 295 | |
---|
[1856] | 296 | bool bCreatedOneObject_; //!< True if at least one object of the given type was created (used to determine the need of storing the parents) |
---|
[1747] | 297 | bool bSetName_; //!< True if the name is set |
---|
[5781] | 298 | bool bLoadable_; //!< False = it's not permitted to load the object through XML |
---|
[1052] | 299 | std::string name_; //!< The name of the class the Identifier belongs to |
---|
[5929] | 300 | Factory* factory_; //!< The Factory, able to create new objects of the given class (if available) |
---|
[1052] | 301 | 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) |
---|
[5781] | 302 | uint32_t networkID_; //!< The network ID to identify a class through the network |
---|
[3325] | 303 | const unsigned int classID_; //!< Uniquely identifies a class (might not be the same as the networkID_) |
---|
| 304 | static unsigned int classIDCounter_s; //!< Static counter for the unique classIDs |
---|
[1052] | 305 | |
---|
| 306 | bool bHasConfigValues_; //!< True if this class has at least one assigned config value |
---|
| 307 | std::map<std::string, ConfigValueContainer*> configValues_; //!< A map to link the string of configurable variables with their ConfigValueContainer |
---|
[5781] | 308 | |
---|
| 309 | std::map<std::string, XMLPortParamContainer*> xmlportParamContainers_; //!< All loadable parameters |
---|
| 310 | std::map<std::string, XMLPortObjectContainer*> xmlportObjectContainers_; //!< All attachable objects |
---|
[790] | 311 | }; |
---|
| 312 | |
---|
[1052] | 313 | _CoreExport std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list); |
---|
[790] | 314 | |
---|
[1052] | 315 | |
---|
[790] | 316 | // ############################### |
---|
| 317 | // ### ClassIdentifier ### |
---|
| 318 | // ############################### |
---|
| 319 | /** |
---|
[7401] | 320 | @brief The ClassIdentifier is derived from Identifier and holds all class-specific functions and variables the Identifier cannot have. |
---|
| 321 | |
---|
| 322 | ClassIdentifier is a Singleton, which means that only one ClassIdentifier for a given type T exists. |
---|
[5695] | 323 | This makes it possible to store information about a class, sharing them with all |
---|
[1052] | 324 | objects of that class without defining static variables in every class. |
---|
| 325 | |
---|
| 326 | To be really sure that not more than exactly one object exists (even with libraries), |
---|
[7401] | 327 | ClassIdentifiers are stored in a static map in Identifier. |
---|
[790] | 328 | */ |
---|
| 329 | template <class T> |
---|
| 330 | class ClassIdentifier : public Identifier |
---|
[1052] | 331 | { |
---|
[7401] | 332 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
---|
| 333 | #define SUPER_INTRUSIVE_DECLARATION_INCLUDE |
---|
| 334 | #include "Super.h" |
---|
| 335 | #endif |
---|
[5781] | 336 | |
---|
[790] | 337 | public: |
---|
[5929] | 338 | static ClassIdentifier<T>* getIdentifier(); |
---|
| 339 | static ClassIdentifier<T>* getIdentifier(const std::string& name); |
---|
[1052] | 340 | |
---|
[3325] | 341 | bool initialiseObject(T* object, const std::string& className, bool bRootClass); |
---|
| 342 | |
---|
[1747] | 343 | void updateConfigValues(bool updateChildren = true) const; |
---|
[1052] | 344 | |
---|
[790] | 345 | private: |
---|
[2784] | 346 | static void initialiseIdentifier(); |
---|
[790] | 347 | ClassIdentifier(const ClassIdentifier<T>& identifier) {} // don't copy |
---|
[1747] | 348 | ClassIdentifier() |
---|
| 349 | { |
---|
[5781] | 350 | SuperFunctionInitialization<0, T>::initialize(this); |
---|
[1747] | 351 | } |
---|
| 352 | ~ClassIdentifier() |
---|
| 353 | { |
---|
[5781] | 354 | SuperFunctionDestruction<0, T>::destroy(this); |
---|
[1747] | 355 | } |
---|
[790] | 356 | |
---|
[1856] | 357 | static ClassIdentifier<T>* classIdentifier_s; |
---|
[790] | 358 | }; |
---|
| 359 | |
---|
[1543] | 360 | template <class T> |
---|
[2784] | 361 | ClassIdentifier<T>* ClassIdentifier<T>::classIdentifier_s = 0; |
---|
[1543] | 362 | |
---|
[790] | 363 | /** |
---|
[1747] | 364 | @brief Returns the only instance of this class. |
---|
[1543] | 365 | @return The unique Identifier |
---|
| 366 | */ |
---|
| 367 | template <class T> |
---|
[3196] | 368 | inline ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier() |
---|
[1543] | 369 | { |
---|
[5929] | 370 | // check if the Identifier already exists |
---|
| 371 | if (!ClassIdentifier<T>::classIdentifier_s) |
---|
[2784] | 372 | ClassIdentifier<T>::initialiseIdentifier(); |
---|
[1543] | 373 | |
---|
| 374 | return ClassIdentifier<T>::classIdentifier_s; |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | /** |
---|
[1747] | 378 | @brief Does the same as getIdentifier() but sets the name if this wasn't done yet. |
---|
| 379 | @param name The name of this Identifier |
---|
| 380 | @return The Identifier |
---|
[790] | 381 | */ |
---|
| 382 | template <class T> |
---|
[3196] | 383 | inline ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier(const std::string& name) |
---|
[790] | 384 | { |
---|
[1747] | 385 | ClassIdentifier<T>* identifier = ClassIdentifier<T>::getIdentifier(); |
---|
| 386 | identifier->setName(name); |
---|
| 387 | return identifier; |
---|
[790] | 388 | } |
---|
| 389 | |
---|
| 390 | /** |
---|
[2784] | 391 | @brief Assigns the static field for the identifier singleton. |
---|
| 392 | */ |
---|
| 393 | template <class T> |
---|
| 394 | void ClassIdentifier<T>::initialiseIdentifier() |
---|
| 395 | { |
---|
| 396 | // Get the name of the class |
---|
[8351] | 397 | std::string name = typeid(T).name(); |
---|
[2784] | 398 | |
---|
| 399 | // create a new identifier anyway. Will be deleted in Identifier::getIdentifier if not used. |
---|
| 400 | ClassIdentifier<T>* proposal = new ClassIdentifier<T>(); |
---|
| 401 | |
---|
| 402 | // Get the entry from the map |
---|
| 403 | ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*)Identifier::getIdentifierSingleton(name, proposal); |
---|
| 404 | |
---|
| 405 | if (ClassIdentifier<T>::classIdentifier_s == proposal) |
---|
| 406 | { |
---|
| 407 | COUT(4) << "*** Identifier: Requested Identifier for " << name << " was not yet existing and got created." << std::endl; |
---|
| 408 | } |
---|
| 409 | else |
---|
| 410 | { |
---|
| 411 | COUT(4) << "*** Identifier: Requested Identifier for " << name << " was already existing and got assigned." << std::endl; |
---|
| 412 | } |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | /** |
---|
[790] | 416 | @brief Adds an object of the given type to the ObjectList. |
---|
| 417 | @param object The object to add |
---|
[7401] | 418 | @param className The name of the class T |
---|
| 419 | @param bRootClass True if this is a root class (i.e. it inherits directly from OrxonoxClass) |
---|
[790] | 420 | */ |
---|
| 421 | template <class T> |
---|
[3325] | 422 | bool ClassIdentifier<T>::initialiseObject(T* object, const std::string& className, bool bRootClass) |
---|
[790] | 423 | { |
---|
[3325] | 424 | if (bRootClass) |
---|
| 425 | COUT(5) << "*** Register Root-Object: " << className << std::endl; |
---|
| 426 | else |
---|
| 427 | COUT(5) << "*** Register Object: " << className << std::endl; |
---|
| 428 | |
---|
| 429 | object->identifier_ = this; |
---|
| 430 | if (Identifier::isCreatingHierarchy()) |
---|
| 431 | { |
---|
| 432 | if (bRootClass && !object->parents_) |
---|
| 433 | object->parents_ = new std::set<const Identifier*>(); |
---|
| 434 | |
---|
| 435 | if (object->parents_) |
---|
| 436 | { |
---|
| 437 | this->initializeClassHierarchy(object->parents_, bRootClass); |
---|
| 438 | object->parents_->insert(object->parents_->end(), this); |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | object->setConfigValues(); |
---|
| 442 | return true; |
---|
| 443 | } |
---|
| 444 | else |
---|
| 445 | { |
---|
| 446 | COUT(5) << "*** ClassIdentifier: Added object to " << this->getName() << "-list." << std::endl; |
---|
| 447 | object->metaList_->add(this->objects_, this->objects_->add(new ObjectListElement<T>(object))); |
---|
| 448 | |
---|
| 449 | // Add pointer of type T to the map in the OrxonoxClass instance that enables "dynamic_casts" |
---|
[3333] | 450 | object->objectPointers_.push_back(std::make_pair(this->getClassID(), static_cast<void*>(object))); |
---|
[3325] | 451 | return false; |
---|
| 452 | } |
---|
[790] | 453 | } |
---|
| 454 | |
---|
| 455 | /** |
---|
[1052] | 456 | @brief Updates the config-values of all existing objects of this class by calling their setConfigValues() function. |
---|
[790] | 457 | */ |
---|
| 458 | template <class T> |
---|
[1747] | 459 | void ClassIdentifier<T>::updateConfigValues(bool updateChildren) const |
---|
[790] | 460 | { |
---|
[1747] | 461 | if (!this->hasConfigValues()) |
---|
| 462 | return; |
---|
[790] | 463 | |
---|
[1747] | 464 | for (ObjectListIterator<T> it = ObjectList<T>::begin(); it; ++it) |
---|
| 465 | it->setConfigValues(); |
---|
[1052] | 466 | |
---|
[1747] | 467 | if (updateChildren) |
---|
| 468 | for (std::set<const Identifier*>::const_iterator it = this->getChildrenBegin(); it != this->getChildrenEnd(); ++it) |
---|
| 469 | (*it)->updateConfigValues(false); |
---|
[1052] | 470 | } |
---|
| 471 | |
---|
| 472 | |
---|
[790] | 473 | // ############################### |
---|
[3325] | 474 | // ### orxonox_cast ### |
---|
| 475 | // ############################### |
---|
| 476 | /** |
---|
| 477 | @brief |
---|
| 478 | Casts on object of type OrxonoxClass to any derived type that is |
---|
| 479 | registered in the class hierarchy. |
---|
| 480 | @return |
---|
| 481 | Returns NULL if the cast is not possible |
---|
| 482 | @note |
---|
| 483 | In case of NULL return (and using MSVC), a dynamic_cast might still be possible if |
---|
| 484 | a class forgot to register its objects. |
---|
| 485 | Also note that the function is implemented differently for GCC/MSVC. |
---|
| 486 | */ |
---|
| 487 | template <class T, class U> |
---|
[8351] | 488 | ORX_FORCEINLINE T orxonox_cast(U* source) |
---|
[3325] | 489 | { |
---|
[3332] | 490 | #ifdef ORXONOX_COMPILER_MSVC |
---|
[3333] | 491 | typedef Loki::TypeTraits<typename Loki::TypeTraits<T>::PointeeType>::NonConstType ClassType; |
---|
[3370] | 492 | if (source != NULL) |
---|
| 493 | return source->template getDerivedPointer<ClassType>(ClassIdentifier<ClassType>::getIdentifier()->getClassID()); |
---|
| 494 | else |
---|
| 495 | return NULL; |
---|
[3332] | 496 | #else |
---|
| 497 | return dynamic_cast<T>(source); |
---|
| 498 | #endif |
---|
[3325] | 499 | } |
---|
[790] | 500 | } |
---|
| 501 | |
---|
| 502 | #endif /* _Identifier_H__ */ |
---|