[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 | |
---|
[9632] | 58 | Identifiable* other = object->getIdentifier()->fabricate(0); // fabricates a new instance of MyClass |
---|
[7401] | 59 | |
---|
| 60 | |
---|
| 61 | // test the class hierarchy |
---|
| 62 | object->getIdentifier()->isA(Class(MyClass)); // returns true |
---|
| 63 | object->isA(Class(MyClass)); // returns true (short version) |
---|
| 64 | |
---|
| 65 | object->isA(Class(BaseClass)); // returns true if MyClass is a child of BaseClass |
---|
| 66 | |
---|
| 67 | Class(ChildClass)->isChildOf(object->getIdentifier()); // returns true if ChildClass is a child of MyClass |
---|
| 68 | @endcode |
---|
[790] | 69 | */ |
---|
| 70 | |
---|
| 71 | #ifndef _Identifier_H__ |
---|
| 72 | #define _Identifier_H__ |
---|
[1052] | 73 | |
---|
[9563] | 74 | #include "core/CorePrereqs.h" |
---|
[1062] | 75 | |
---|
[3196] | 76 | #include <cassert> |
---|
| 77 | #include <map> |
---|
[1052] | 78 | #include <set> |
---|
[790] | 79 | #include <string> |
---|
[1639] | 80 | #include <typeinfo> |
---|
[7266] | 81 | #include <loki/TypeTraits.h> |
---|
[790] | 82 | |
---|
[8858] | 83 | #include "util/Output.h" |
---|
[9563] | 84 | #include "core/object/ObjectList.h" |
---|
[9606] | 85 | #include "core/object/Listable.h" |
---|
| 86 | #include "core/object/Context.h" |
---|
[9564] | 87 | #include "IdentifierManager.h" |
---|
[5781] | 88 | #include "Super.h" |
---|
[790] | 89 | |
---|
| 90 | namespace orxonox |
---|
| 91 | { |
---|
| 92 | // ############################### |
---|
| 93 | // ### Identifier ### |
---|
| 94 | // ############################### |
---|
| 95 | /** |
---|
[7401] | 96 | @brief The Identifier is used to identify the class of an object and to store information about the class. |
---|
[790] | 97 | |
---|
[7401] | 98 | Each Identifier stores information about one class. The Identifier can then be used to identify |
---|
| 99 | this class. On the other hand it's also possible to get the corresponding Identifier of a class, |
---|
| 100 | for example by using the macro Class(). |
---|
[790] | 101 | |
---|
[7401] | 102 | @see See @ref IdentifierExample "Identifier.h" for more information and some examples. |
---|
| 103 | |
---|
| 104 | @note You can't directly create an Identifier, it's just the base-class of ClassIdentifier<T>. |
---|
[790] | 105 | */ |
---|
| 106 | class _CoreExport Identifier |
---|
| 107 | { |
---|
[9564] | 108 | friend class IdentifierManager; |
---|
| 109 | |
---|
[5929] | 110 | public: |
---|
[7401] | 111 | /// Returns the name of the class the Identifier belongs to. |
---|
[5929] | 112 | inline const std::string& getName() const { return this->name_; } |
---|
| 113 | void setName(const std::string& name); |
---|
[790] | 114 | |
---|
[7401] | 115 | /// Returns the network ID to identify a class through the network. |
---|
[8706] | 116 | inline uint32_t getNetworkID() const { return this->networkID_; } |
---|
[5929] | 117 | void setNetworkID(uint32_t id); |
---|
[790] | 118 | |
---|
[7401] | 119 | /// Returns the unique ID of the class. |
---|
[8351] | 120 | ORX_FORCEINLINE unsigned int getClassID() const { return this->classID_; } |
---|
[5929] | 121 | |
---|
[7401] | 122 | /// Sets the Factory. |
---|
[9602] | 123 | void setFactory(Factory* factory); |
---|
[7401] | 124 | /// Returns true if the Identifier has a Factory. |
---|
[5929] | 125 | inline bool hasFactory() const { return (this->factory_ != 0); } |
---|
[790] | 126 | |
---|
[9632] | 127 | Identifiable* fabricate(Context* context); |
---|
[5929] | 128 | |
---|
[7401] | 129 | /// Returns true if the class can be loaded through XML. |
---|
[5929] | 130 | inline bool isLoadable() const { return this->bLoadable_; } |
---|
[7401] | 131 | /// Set the class to be loadable through XML or not. |
---|
[5929] | 132 | inline void setLoadable(bool bLoadable) { this->bLoadable_ = bLoadable; } |
---|
| 133 | |
---|
[790] | 134 | bool isA(const Identifier* identifier) const; |
---|
[871] | 135 | bool isExactlyA(const Identifier* identifier) const; |
---|
[1052] | 136 | bool isChildOf(const Identifier* identifier) const; |
---|
[871] | 137 | bool isDirectChildOf(const Identifier* identifier) const; |
---|
[1052] | 138 | bool isParentOf(const Identifier* identifier) const; |
---|
[871] | 139 | bool isDirectParentOf(const Identifier* identifier) const; |
---|
[790] | 140 | |
---|
[5781] | 141 | |
---|
[5929] | 142 | ///////////////////////////// |
---|
| 143 | ////// Class Hierarchy ////// |
---|
| 144 | ///////////////////////////// |
---|
[7401] | 145 | /// Returns the parents of the class the Identifier belongs to. |
---|
[1052] | 146 | inline const std::set<const Identifier*>& getParents() const { return this->parents_; } |
---|
[7401] | 147 | /// Returns the begin-iterator of the parents-list. |
---|
[1052] | 148 | inline std::set<const Identifier*>::const_iterator getParentsBegin() const { return this->parents_.begin(); } |
---|
[7401] | 149 | /// Returns the end-iterator of the parents-list. |
---|
[1052] | 150 | inline std::set<const Identifier*>::const_iterator getParentsEnd() const { return this->parents_.end(); } |
---|
| 151 | |
---|
[7401] | 152 | /// Returns the children of the class the Identifier belongs to. |
---|
[5929] | 153 | inline const std::set<const Identifier*>& getChildren() const { return this->children_; } |
---|
[7401] | 154 | /// Returns the begin-iterator of the children-list. |
---|
[5929] | 155 | inline std::set<const Identifier*>::const_iterator getChildrenBegin() const { return this->children_.begin(); } |
---|
[7401] | 156 | /// Returns the end-iterator of the children-list. |
---|
[5929] | 157 | inline std::set<const Identifier*>::const_iterator getChildrenEnd() const { return this->children_.end(); } |
---|
[1052] | 158 | |
---|
[7401] | 159 | /// Returns the direct parents of the class the Identifier belongs to. |
---|
[1052] | 160 | inline const std::set<const Identifier*>& getDirectParents() const { return this->directParents_; } |
---|
[7401] | 161 | /// Returns the begin-iterator of the direct-parents-list. |
---|
[1052] | 162 | inline std::set<const Identifier*>::const_iterator getDirectParentsBegin() const { return this->directParents_.begin(); } |
---|
[7401] | 163 | /// Returns the end-iterator of the direct-parents-list. |
---|
[1052] | 164 | inline std::set<const Identifier*>::const_iterator getDirectParentsEnd() const { return this->directParents_.end(); } |
---|
| 165 | |
---|
[7401] | 166 | /// Returns the direct children the class the Identifier belongs to. |
---|
[5929] | 167 | inline const std::set<const Identifier*>& getDirectChildren() const { return this->directChildren_; } |
---|
[7401] | 168 | /// Returns the begin-iterator of the direct-children-list. |
---|
[5929] | 169 | inline std::set<const Identifier*>::const_iterator getDirectChildrenBegin() const { return this->directChildren_.begin(); } |
---|
[7401] | 170 | /// Returns the end-iterator of the direct-children-list. |
---|
[5929] | 171 | inline std::set<const Identifier*>::const_iterator getDirectChildrenEnd() const { return this->directChildren_.end(); } |
---|
[1052] | 172 | |
---|
| 173 | |
---|
[5929] | 174 | ///////////////////////// |
---|
| 175 | ///// Config Values ///// |
---|
| 176 | ///////////////////////// |
---|
| 177 | virtual void updateConfigValues(bool updateChildren = true) const = 0; |
---|
| 178 | |
---|
[7401] | 179 | /// Returns true if this class has at least one config value. |
---|
[5929] | 180 | inline bool hasConfigValues() const { return this->bHasConfigValues_; } |
---|
| 181 | |
---|
| 182 | void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container); |
---|
| 183 | ConfigValueContainer* getConfigValueContainer(const std::string& varname); |
---|
[1052] | 184 | |
---|
[5929] | 185 | |
---|
| 186 | /////////////////// |
---|
| 187 | ///// XMLPort ///// |
---|
| 188 | /////////////////// |
---|
[7401] | 189 | /// Returns the map that stores all XMLPort params. |
---|
[5781] | 190 | inline const std::map<std::string, XMLPortParamContainer*>& getXMLPortParamMap() const { return this->xmlportParamContainers_; } |
---|
[7401] | 191 | /// Returns a const_iterator to the beginning of the map that stores all XMLPort params. |
---|
[5781] | 192 | inline std::map<std::string, XMLPortParamContainer*>::const_iterator getXMLPortParamMapBegin() const { return this->xmlportParamContainers_.begin(); } |
---|
[7401] | 193 | /// Returns a const_iterator to the end of the map that stores all XMLPort params. |
---|
[5781] | 194 | inline std::map<std::string, XMLPortParamContainer*>::const_iterator getXMLPortParamMapEnd() const { return this->xmlportParamContainers_.end(); } |
---|
| 195 | |
---|
[7401] | 196 | /// Returns the map that stores all XMLPort objects. |
---|
[5781] | 197 | inline const std::map<std::string, XMLPortObjectContainer*>& getXMLPortObjectMap() const { return this->xmlportObjectContainers_; } |
---|
[7401] | 198 | /// Returns a const_iterator to the beginning of the map that stores all XMLPort objects. |
---|
[5781] | 199 | inline std::map<std::string, XMLPortObjectContainer*>::const_iterator getXMLPortObjectMapBegin() const { return this->xmlportObjectContainers_.begin(); } |
---|
[7401] | 200 | /// Returns a const_iterator to the end of the map that stores all XMLPort objects. |
---|
[5781] | 201 | inline std::map<std::string, XMLPortObjectContainer*>::const_iterator getXMLPortObjectMapEnd() const { return this->xmlportObjectContainers_.end(); } |
---|
| 202 | |
---|
| 203 | void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container); |
---|
| 204 | XMLPortParamContainer* getXMLPortParamContainer(const std::string& paramname); |
---|
| 205 | |
---|
| 206 | void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container); |
---|
| 207 | XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname); |
---|
| 208 | |
---|
| 209 | |
---|
[1052] | 210 | protected: |
---|
[1747] | 211 | Identifier(); |
---|
| 212 | Identifier(const Identifier& identifier); // don't copy |
---|
| 213 | virtual ~Identifier(); |
---|
| 214 | |
---|
[5781] | 215 | virtual void createSuperFunctionCaller() const = 0; |
---|
[1747] | 216 | |
---|
[5929] | 217 | void initializeClassHierarchy(std::set<const Identifier*>* parents, bool bRootClass); |
---|
| 218 | |
---|
[7401] | 219 | /// Returns the children of the class the Identifier belongs to. |
---|
[5929] | 220 | inline std::set<const Identifier*>& getChildrenIntern() const { return this->children_; } |
---|
[7401] | 221 | /// Returns the direct children of the class the Identifier belongs to. |
---|
[5929] | 222 | inline std::set<const Identifier*>& getDirectChildrenIntern() const { return this->directChildren_; } |
---|
[1052] | 223 | |
---|
[1747] | 224 | private: |
---|
[1856] | 225 | void initialize(std::set<const Identifier*>* parents); |
---|
| 226 | |
---|
[1052] | 227 | std::set<const Identifier*> parents_; //!< The parents of the class the Identifier belongs to |
---|
[5929] | 228 | mutable std::set<const Identifier*> children_; //!< The children of the class the Identifier belongs to |
---|
[790] | 229 | |
---|
[1052] | 230 | std::set<const Identifier*> directParents_; //!< The direct parents of the class the Identifier belongs to |
---|
[5929] | 231 | mutable std::set<const Identifier*> directChildren_; //!< The direct children of the class the Identifier belongs to |
---|
[1052] | 232 | |
---|
[1856] | 233 | bool bCreatedOneObject_; //!< True if at least one object of the given type was created (used to determine the need of storing the parents) |
---|
[1747] | 234 | bool bSetName_; //!< True if the name is set |
---|
[5781] | 235 | bool bLoadable_; //!< False = it's not permitted to load the object through XML |
---|
[1052] | 236 | std::string name_; //!< The name of the class the Identifier belongs to |
---|
[5929] | 237 | Factory* factory_; //!< The Factory, able to create new objects of the given class (if available) |
---|
[5781] | 238 | uint32_t networkID_; //!< The network ID to identify a class through the network |
---|
[3325] | 239 | const unsigned int classID_; //!< Uniquely identifies a class (might not be the same as the networkID_) |
---|
[1052] | 240 | |
---|
| 241 | bool bHasConfigValues_; //!< True if this class has at least one assigned config value |
---|
| 242 | std::map<std::string, ConfigValueContainer*> configValues_; //!< A map to link the string of configurable variables with their ConfigValueContainer |
---|
[5781] | 243 | |
---|
| 244 | std::map<std::string, XMLPortParamContainer*> xmlportParamContainers_; //!< All loadable parameters |
---|
| 245 | std::map<std::string, XMLPortObjectContainer*> xmlportObjectContainers_; //!< All attachable objects |
---|
[790] | 246 | }; |
---|
| 247 | |
---|
[1052] | 248 | _CoreExport std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list); |
---|
[790] | 249 | |
---|
[1052] | 250 | |
---|
[790] | 251 | // ############################### |
---|
| 252 | // ### ClassIdentifier ### |
---|
| 253 | // ############################### |
---|
| 254 | /** |
---|
[7401] | 255 | @brief The ClassIdentifier is derived from Identifier and holds all class-specific functions and variables the Identifier cannot have. |
---|
| 256 | |
---|
| 257 | ClassIdentifier is a Singleton, which means that only one ClassIdentifier for a given type T exists. |
---|
[5695] | 258 | This makes it possible to store information about a class, sharing them with all |
---|
[1052] | 259 | objects of that class without defining static variables in every class. |
---|
| 260 | |
---|
| 261 | To be really sure that not more than exactly one object exists (even with libraries), |
---|
[7401] | 262 | ClassIdentifiers are stored in a static map in Identifier. |
---|
[790] | 263 | */ |
---|
| 264 | template <class T> |
---|
| 265 | class ClassIdentifier : public Identifier |
---|
[1052] | 266 | { |
---|
[7401] | 267 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
---|
| 268 | #define SUPER_INTRUSIVE_DECLARATION_INCLUDE |
---|
| 269 | #include "Super.h" |
---|
| 270 | #endif |
---|
[5781] | 271 | |
---|
[790] | 272 | public: |
---|
[5929] | 273 | static ClassIdentifier<T>* getIdentifier(); |
---|
| 274 | static ClassIdentifier<T>* getIdentifier(const std::string& name); |
---|
[1052] | 275 | |
---|
[3325] | 276 | bool initialiseObject(T* object, const std::string& className, bool bRootClass); |
---|
| 277 | |
---|
[9586] | 278 | void setConfigValues(T* object, Configurable*) const; |
---|
| 279 | void setConfigValues(T* object, Identifiable*) const; |
---|
| 280 | |
---|
[9572] | 281 | void addObjectToList(T* object, Listable*); |
---|
| 282 | void addObjectToList(T* object, Identifiable*); |
---|
| 283 | |
---|
[1747] | 284 | void updateConfigValues(bool updateChildren = true) const; |
---|
[1052] | 285 | |
---|
[790] | 286 | private: |
---|
[2784] | 287 | static void initialiseIdentifier(); |
---|
[790] | 288 | ClassIdentifier(const ClassIdentifier<T>& identifier) {} // don't copy |
---|
[1747] | 289 | ClassIdentifier() |
---|
| 290 | { |
---|
[5781] | 291 | SuperFunctionInitialization<0, T>::initialize(this); |
---|
[1747] | 292 | } |
---|
| 293 | ~ClassIdentifier() |
---|
| 294 | { |
---|
[5781] | 295 | SuperFunctionDestruction<0, T>::destroy(this); |
---|
[1747] | 296 | } |
---|
[790] | 297 | |
---|
[9626] | 298 | void updateConfigValues(bool updateChildren, Listable*) const; |
---|
| 299 | void updateConfigValues(bool updateChildren, Identifiable*) const; |
---|
| 300 | |
---|
[1856] | 301 | static ClassIdentifier<T>* classIdentifier_s; |
---|
[790] | 302 | }; |
---|
| 303 | |
---|
[1543] | 304 | template <class T> |
---|
[2784] | 305 | ClassIdentifier<T>* ClassIdentifier<T>::classIdentifier_s = 0; |
---|
[1543] | 306 | |
---|
[790] | 307 | /** |
---|
[1747] | 308 | @brief Returns the only instance of this class. |
---|
[1543] | 309 | @return The unique Identifier |
---|
| 310 | */ |
---|
| 311 | template <class T> |
---|
[3196] | 312 | inline ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier() |
---|
[1543] | 313 | { |
---|
[5929] | 314 | // check if the Identifier already exists |
---|
| 315 | if (!ClassIdentifier<T>::classIdentifier_s) |
---|
[2784] | 316 | ClassIdentifier<T>::initialiseIdentifier(); |
---|
[1543] | 317 | |
---|
| 318 | return ClassIdentifier<T>::classIdentifier_s; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | /** |
---|
[1747] | 322 | @brief Does the same as getIdentifier() but sets the name if this wasn't done yet. |
---|
| 323 | @param name The name of this Identifier |
---|
| 324 | @return The Identifier |
---|
[790] | 325 | */ |
---|
| 326 | template <class T> |
---|
[3196] | 327 | inline ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier(const std::string& name) |
---|
[790] | 328 | { |
---|
[1747] | 329 | ClassIdentifier<T>* identifier = ClassIdentifier<T>::getIdentifier(); |
---|
| 330 | identifier->setName(name); |
---|
| 331 | return identifier; |
---|
[790] | 332 | } |
---|
| 333 | |
---|
| 334 | /** |
---|
[2784] | 335 | @brief Assigns the static field for the identifier singleton. |
---|
| 336 | */ |
---|
| 337 | template <class T> |
---|
| 338 | void ClassIdentifier<T>::initialiseIdentifier() |
---|
| 339 | { |
---|
| 340 | // Get the name of the class |
---|
[8351] | 341 | std::string name = typeid(T).name(); |
---|
[2784] | 342 | |
---|
| 343 | // create a new identifier anyway. Will be deleted in Identifier::getIdentifier if not used. |
---|
| 344 | ClassIdentifier<T>* proposal = new ClassIdentifier<T>(); |
---|
| 345 | |
---|
| 346 | // Get the entry from the map |
---|
[9564] | 347 | ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*)IdentifierManager::getIdentifierSingleton(name, proposal); |
---|
[2784] | 348 | |
---|
| 349 | if (ClassIdentifier<T>::classIdentifier_s == proposal) |
---|
| 350 | { |
---|
[8858] | 351 | orxout(verbose, context::identifier) << "Requested Identifier for " << name << " was not yet existing and got created." << endl; |
---|
[2784] | 352 | } |
---|
| 353 | else |
---|
| 354 | { |
---|
[8858] | 355 | orxout(verbose, context::identifier) << "Requested Identifier for " << name << " was already existing and got assigned." << endl; |
---|
[2784] | 356 | } |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | /** |
---|
[790] | 360 | @brief Adds an object of the given type to the ObjectList. |
---|
| 361 | @param object The object to add |
---|
[7401] | 362 | @param className The name of the class T |
---|
| 363 | @param bRootClass True if this is a root class (i.e. it inherits directly from OrxonoxClass) |
---|
[790] | 364 | */ |
---|
| 365 | template <class T> |
---|
[3325] | 366 | bool ClassIdentifier<T>::initialiseObject(T* object, const std::string& className, bool bRootClass) |
---|
[790] | 367 | { |
---|
[3325] | 368 | if (bRootClass) |
---|
[8858] | 369 | orxout(verbose, context::object_list) << "Register Root-Object: " << className << endl; |
---|
[3325] | 370 | else |
---|
[8858] | 371 | orxout(verbose, context::object_list) << "Register Object: " << className << endl; |
---|
[3325] | 372 | |
---|
| 373 | object->identifier_ = this; |
---|
[9564] | 374 | if (IdentifierManager::isCreatingHierarchy()) |
---|
[3325] | 375 | { |
---|
| 376 | if (bRootClass && !object->parents_) |
---|
| 377 | object->parents_ = new std::set<const Identifier*>(); |
---|
| 378 | |
---|
| 379 | if (object->parents_) |
---|
| 380 | { |
---|
| 381 | this->initializeClassHierarchy(object->parents_, bRootClass); |
---|
| 382 | object->parents_->insert(object->parents_->end(), this); |
---|
| 383 | } |
---|
| 384 | |
---|
[9586] | 385 | this->setConfigValues(object, object); |
---|
[3325] | 386 | return true; |
---|
| 387 | } |
---|
| 388 | else |
---|
| 389 | { |
---|
[9572] | 390 | this->addObjectToList(object, object); |
---|
[3325] | 391 | |
---|
[9566] | 392 | // Add pointer of type T to the map in the Identifiable instance that enables "dynamic_casts" |
---|
[3333] | 393 | object->objectPointers_.push_back(std::make_pair(this->getClassID(), static_cast<void*>(object))); |
---|
[3325] | 394 | return false; |
---|
| 395 | } |
---|
[790] | 396 | } |
---|
| 397 | |
---|
| 398 | /** |
---|
[9586] | 399 | * @brief Only configures the object if is a @ref Configurable |
---|
| 400 | */ |
---|
| 401 | template <class T> |
---|
| 402 | void ClassIdentifier<T>::setConfigValues(T* object, Configurable*) const |
---|
| 403 | { |
---|
| 404 | object->setConfigValues(); |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | template <class T> |
---|
| 408 | void ClassIdentifier<T>::setConfigValues(T*, Identifiable*) const |
---|
| 409 | { |
---|
| 410 | // no action |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | /** |
---|
[9572] | 414 | * @brief Only adds the object to the object list if is a @ref Listable |
---|
| 415 | */ |
---|
| 416 | template <class T> |
---|
[9606] | 417 | void ClassIdentifier<T>::addObjectToList(T* object, Listable* listable) |
---|
[9572] | 418 | { |
---|
[9606] | 419 | listable->getContext()->addObject(object); |
---|
[9572] | 420 | } |
---|
| 421 | |
---|
| 422 | template <class T> |
---|
| 423 | void ClassIdentifier<T>::addObjectToList(T*, Identifiable*) |
---|
| 424 | { |
---|
| 425 | // no action |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | /** |
---|
[1052] | 429 | @brief Updates the config-values of all existing objects of this class by calling their setConfigValues() function. |
---|
[790] | 430 | */ |
---|
| 431 | template <class T> |
---|
[1747] | 432 | void ClassIdentifier<T>::updateConfigValues(bool updateChildren) const |
---|
[790] | 433 | { |
---|
[9626] | 434 | this->updateConfigValues(updateChildren, static_cast<T*>(NULL)); |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | template <class T> |
---|
| 438 | void ClassIdentifier<T>::updateConfigValues(bool updateChildren, Listable*) const |
---|
| 439 | { |
---|
[1747] | 440 | if (!this->hasConfigValues()) |
---|
| 441 | return; |
---|
[790] | 442 | |
---|
[1747] | 443 | for (ObjectListIterator<T> it = ObjectList<T>::begin(); it; ++it) |
---|
[9586] | 444 | this->setConfigValues(*it, *it); |
---|
[1052] | 445 | |
---|
[1747] | 446 | if (updateChildren) |
---|
| 447 | for (std::set<const Identifier*>::const_iterator it = this->getChildrenBegin(); it != this->getChildrenEnd(); ++it) |
---|
| 448 | (*it)->updateConfigValues(false); |
---|
[1052] | 449 | } |
---|
| 450 | |
---|
[9626] | 451 | template <class T> |
---|
| 452 | void ClassIdentifier<T>::updateConfigValues(bool updateChildren, Identifiable*) const |
---|
| 453 | { |
---|
| 454 | // no action |
---|
| 455 | } |
---|
[1052] | 456 | |
---|
[9626] | 457 | |
---|
[790] | 458 | // ############################### |
---|
[3325] | 459 | // ### orxonox_cast ### |
---|
| 460 | // ############################### |
---|
| 461 | /** |
---|
| 462 | @brief |
---|
[9566] | 463 | Casts on object of type Identifiable to any derived type that is |
---|
[3325] | 464 | registered in the class hierarchy. |
---|
| 465 | @return |
---|
| 466 | Returns NULL if the cast is not possible |
---|
| 467 | @note |
---|
| 468 | In case of NULL return (and using MSVC), a dynamic_cast might still be possible if |
---|
| 469 | a class forgot to register its objects. |
---|
| 470 | Also note that the function is implemented differently for GCC/MSVC. |
---|
| 471 | */ |
---|
| 472 | template <class T, class U> |
---|
[8351] | 473 | ORX_FORCEINLINE T orxonox_cast(U* source) |
---|
[3325] | 474 | { |
---|
[3332] | 475 | #ifdef ORXONOX_COMPILER_MSVC |
---|
[3333] | 476 | typedef Loki::TypeTraits<typename Loki::TypeTraits<T>::PointeeType>::NonConstType ClassType; |
---|
[3370] | 477 | if (source != NULL) |
---|
| 478 | return source->template getDerivedPointer<ClassType>(ClassIdentifier<ClassType>::getIdentifier()->getClassID()); |
---|
| 479 | else |
---|
| 480 | return NULL; |
---|
[3332] | 481 | #else |
---|
| 482 | return dynamic_cast<T>(source); |
---|
| 483 | #endif |
---|
[3325] | 484 | } |
---|
[790] | 485 | } |
---|
| 486 | |
---|
| 487 | #endif /* _Identifier_H__ */ |
---|