[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 | |
---|
[10768] | 58 | Identifiable* other = object->getIdentifier()->fabricate(nullptr); // 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" |
---|
[10624] | 84 | #include "util/OrxAssert.h" |
---|
[9563] | 85 | #include "core/object/ObjectList.h" |
---|
[9667] | 86 | #include "core/object/Listable.h" |
---|
| 87 | #include "core/object/Context.h" |
---|
| 88 | #include "core/object/Destroyable.h" |
---|
| 89 | #include "core/object/WeakPtr.h" |
---|
[9564] | 90 | #include "IdentifierManager.h" |
---|
[5781] | 91 | #include "Super.h" |
---|
[790] | 92 | |
---|
| 93 | namespace orxonox |
---|
| 94 | { |
---|
| 95 | // ############################### |
---|
| 96 | // ### Identifier ### |
---|
| 97 | // ############################### |
---|
| 98 | /** |
---|
[7401] | 99 | @brief The Identifier is used to identify the class of an object and to store information about the class. |
---|
[790] | 100 | |
---|
[7401] | 101 | Each Identifier stores information about one class. The Identifier can then be used to identify |
---|
| 102 | this class. On the other hand it's also possible to get the corresponding Identifier of a class, |
---|
| 103 | for example by using the macro Class(). |
---|
[790] | 104 | |
---|
[7401] | 105 | @see See @ref IdentifierExample "Identifier.h" for more information and some examples. |
---|
| 106 | |
---|
| 107 | @note You can't directly create an Identifier, it's just the base-class of ClassIdentifier<T>. |
---|
[790] | 108 | */ |
---|
[9667] | 109 | class _CoreExport Identifier : public Destroyable |
---|
[790] | 110 | { |
---|
[9667] | 111 | public: |
---|
[10624] | 112 | struct InheritsFrom //! helper class to manually define inheritance |
---|
| 113 | { |
---|
| 114 | virtual ~InheritsFrom() {} |
---|
| 115 | virtual Identifier* getParent() const = 0; |
---|
| 116 | }; |
---|
| 117 | |
---|
| 118 | public: |
---|
| 119 | Identifier(const std::string& name, Factory* factory, bool bLoadable); |
---|
[10990] | 120 | Identifier(const Identifier&) = delete; |
---|
[9667] | 121 | virtual ~Identifier(); |
---|
[9564] | 122 | |
---|
[7401] | 123 | /// Returns the name of the class the Identifier belongs to. |
---|
[5929] | 124 | inline const std::string& getName() const { return this->name_; } |
---|
[790] | 125 | |
---|
[10624] | 126 | /// Returns the type_info of the class as it is returned by typeid(T) |
---|
| 127 | virtual const std::type_info& getTypeInfo() = 0; |
---|
[9667] | 128 | |
---|
[7401] | 129 | /// Returns the network ID to identify a class through the network. |
---|
[8706] | 130 | inline uint32_t getNetworkID() const { return this->networkID_; } |
---|
[5929] | 131 | void setNetworkID(uint32_t id); |
---|
[790] | 132 | |
---|
[7401] | 133 | /// Returns the unique ID of the class. |
---|
[8351] | 134 | ORX_FORCEINLINE unsigned int getClassID() const { return this->classID_; } |
---|
[5929] | 135 | |
---|
[7401] | 136 | /// Returns true if the Identifier has a Factory. |
---|
[10768] | 137 | inline bool hasFactory() const { return (this->factory_ != nullptr); } |
---|
[790] | 138 | |
---|
[9667] | 139 | Identifiable* fabricate(Context* context); |
---|
[5929] | 140 | |
---|
[7401] | 141 | /// Returns true if the class can be loaded through XML. |
---|
[5929] | 142 | inline bool isLoadable() const { return this->bLoadable_; } |
---|
| 143 | |
---|
[10624] | 144 | /// Returns true if child classes should inherit virtually from this class. |
---|
| 145 | inline bool isVirtualBase() const { return this->bIsVirtualBase_; } |
---|
| 146 | /// Defines if child classes should inherit virtually from this class. |
---|
| 147 | inline void setVirtualBase(bool bIsVirtualBase) { this->bIsVirtualBase_ = bIsVirtualBase; } |
---|
| 148 | |
---|
[9667] | 149 | /// Returns true if the Identifier was completely initialized. |
---|
| 150 | inline bool isInitialized() const { return this->bInitialized_; } |
---|
| 151 | |
---|
[10624] | 152 | virtual void destroyObjects() = 0; |
---|
[9667] | 153 | |
---|
[10624] | 154 | virtual bool canDynamicCastObjectToIdentifierClass(Identifiable* object) const = 0; |
---|
| 155 | |
---|
| 156 | static bool initConfigValues_s; // TODO: this is a hack - remove it as soon as possible |
---|
| 157 | |
---|
| 158 | |
---|
[9667] | 159 | ///////////////////////////// |
---|
| 160 | ////// Class Hierarchy ////// |
---|
| 161 | ///////////////////////////// |
---|
[10624] | 162 | Identifier& inheritsFrom(InheritsFrom* directParent); |
---|
[9667] | 163 | |
---|
[10624] | 164 | void initializeParents(const std::list<const Identifier*>& initializationTrace); |
---|
[9667] | 165 | void finishInitialization(); |
---|
[10624] | 166 | void reset(); |
---|
[9667] | 167 | |
---|
[790] | 168 | bool isA(const Identifier* identifier) const; |
---|
[871] | 169 | bool isExactlyA(const Identifier* identifier) const; |
---|
[1052] | 170 | bool isChildOf(const Identifier* identifier) const; |
---|
[871] | 171 | bool isDirectChildOf(const Identifier* identifier) const; |
---|
[1052] | 172 | bool isParentOf(const Identifier* identifier) const; |
---|
[871] | 173 | bool isDirectParentOf(const Identifier* identifier) const; |
---|
[790] | 174 | |
---|
[10624] | 175 | /// Returns the direct parents of the class the Identifier belongs to. |
---|
| 176 | inline const std::list<const Identifier*>& getDirectParents() const { return this->directParents_; } |
---|
[7401] | 177 | /// Returns the parents of the class the Identifier belongs to. |
---|
[10624] | 178 | inline const std::list<const Identifier*>& getParents() const { return this->parents_; } |
---|
[1052] | 179 | |
---|
[10624] | 180 | /// Returns the direct children the class the Identifier belongs to. |
---|
| 181 | inline const std::set<const Identifier*>& getDirectChildren() const { return this->directChildren_; } |
---|
[7401] | 182 | /// Returns the children of the class the Identifier belongs to. |
---|
[5929] | 183 | inline const std::set<const Identifier*>& getChildren() const { return this->children_; } |
---|
[1052] | 184 | |
---|
| 185 | |
---|
[5929] | 186 | ///////////////////////// |
---|
| 187 | ///// Config Values ///// |
---|
| 188 | ///////////////////////// |
---|
| 189 | virtual void updateConfigValues(bool updateChildren = true) const = 0; |
---|
| 190 | |
---|
[7401] | 191 | /// Returns true if this class has at least one config value. |
---|
[5929] | 192 | inline bool hasConfigValues() const { return this->bHasConfigValues_; } |
---|
| 193 | |
---|
| 194 | void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container); |
---|
| 195 | ConfigValueContainer* getConfigValueContainer(const std::string& varname); |
---|
[1052] | 196 | |
---|
[5929] | 197 | |
---|
| 198 | /////////////////// |
---|
| 199 | ///// XMLPort ///// |
---|
| 200 | /////////////////// |
---|
[7401] | 201 | /// Returns the map that stores all XMLPort params. |
---|
[5781] | 202 | inline const std::map<std::string, XMLPortParamContainer*>& getXMLPortParamMap() const { return this->xmlportParamContainers_; } |
---|
[7401] | 203 | /// Returns a const_iterator to the beginning of the map that stores all XMLPort params. |
---|
[5781] | 204 | inline std::map<std::string, XMLPortParamContainer*>::const_iterator getXMLPortParamMapBegin() const { return this->xmlportParamContainers_.begin(); } |
---|
[7401] | 205 | /// Returns a const_iterator to the end of the map that stores all XMLPort params. |
---|
[5781] | 206 | inline std::map<std::string, XMLPortParamContainer*>::const_iterator getXMLPortParamMapEnd() const { return this->xmlportParamContainers_.end(); } |
---|
| 207 | |
---|
[7401] | 208 | /// Returns the map that stores all XMLPort objects. |
---|
[5781] | 209 | inline const std::map<std::string, XMLPortObjectContainer*>& getXMLPortObjectMap() const { return this->xmlportObjectContainers_; } |
---|
[7401] | 210 | /// Returns a const_iterator to the beginning of the map that stores all XMLPort objects. |
---|
[5781] | 211 | inline std::map<std::string, XMLPortObjectContainer*>::const_iterator getXMLPortObjectMapBegin() const { return this->xmlportObjectContainers_.begin(); } |
---|
[7401] | 212 | /// Returns a const_iterator to the end of the map that stores all XMLPort objects. |
---|
[5781] | 213 | inline std::map<std::string, XMLPortObjectContainer*>::const_iterator getXMLPortObjectMapEnd() const { return this->xmlportObjectContainers_.end(); } |
---|
| 214 | |
---|
| 215 | void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container); |
---|
| 216 | XMLPortParamContainer* getXMLPortParamContainer(const std::string& paramname); |
---|
| 217 | |
---|
| 218 | void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container); |
---|
| 219 | XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname); |
---|
| 220 | |
---|
[1052] | 221 | protected: |
---|
[5781] | 222 | virtual void createSuperFunctionCaller() const = 0; |
---|
[1747] | 223 | |
---|
| 224 | private: |
---|
[10624] | 225 | void verifyIdentifierTrace() const; |
---|
| 226 | void addIfNotExists(std::list<const Identifier*>& list, const Identifier* identifierToAdd) const; |
---|
[790] | 227 | |
---|
[10624] | 228 | std::list<const InheritsFrom*> manualDirectParents_; //!< Manually defined direct parents |
---|
| 229 | std::list<const Identifier*> directParents_; //!< The direct parents of the class the Identifier belongs to (sorted by their order of initialization) |
---|
| 230 | std::list<const Identifier*> parents_; //!< The parents of the class the Identifier belongs to (sorted by their order of initialization) |
---|
| 231 | |
---|
[9667] | 232 | std::set<const Identifier*> directChildren_; //!< The direct children of the class the Identifier belongs to |
---|
[10624] | 233 | std::set<const Identifier*> children_; //!< The children of the class the Identifier belongs to |
---|
[1052] | 234 | |
---|
[9667] | 235 | bool bInitialized_; //!< Is true if the Identifier was completely initialized |
---|
[5781] | 236 | bool bLoadable_; //!< False = it's not permitted to load the object through XML |
---|
[10624] | 237 | bool bIsVirtualBase_; //!< If true, it is recommended to inherit virtually from this class. This changes the order of initialization of child classes, thus this information is necessary to check the class hierarchy. |
---|
[1052] | 238 | std::string name_; //!< The name of the class the Identifier belongs to |
---|
[5929] | 239 | Factory* factory_; //!< The Factory, able to create new objects of the given class (if available) |
---|
[5781] | 240 | uint32_t networkID_; //!< The network ID to identify a class through the network |
---|
[10624] | 241 | unsigned int classID_; //!< Uniquely identifies a class (might not be the same as the networkID_) |
---|
[1052] | 242 | |
---|
| 243 | bool bHasConfigValues_; //!< True if this class has at least one assigned config value |
---|
| 244 | std::map<std::string, ConfigValueContainer*> configValues_; //!< A map to link the string of configurable variables with their ConfigValueContainer |
---|
[5781] | 245 | |
---|
| 246 | std::map<std::string, XMLPortParamContainer*> xmlportParamContainers_; //!< All loadable parameters |
---|
| 247 | std::map<std::string, XMLPortObjectContainer*> xmlportObjectContainers_; //!< All attachable objects |
---|
[790] | 248 | }; |
---|
| 249 | |
---|
[1052] | 250 | _CoreExport std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list); |
---|
[790] | 251 | |
---|
[1052] | 252 | |
---|
[790] | 253 | // ############################### |
---|
| 254 | // ### ClassIdentifier ### |
---|
| 255 | // ############################### |
---|
| 256 | /** |
---|
[7401] | 257 | @brief The ClassIdentifier is derived from Identifier and holds all class-specific functions and variables the Identifier cannot have. |
---|
| 258 | |
---|
| 259 | ClassIdentifier is a Singleton, which means that only one ClassIdentifier for a given type T exists. |
---|
[5695] | 260 | This makes it possible to store information about a class, sharing them with all |
---|
[1052] | 261 | objects of that class without defining static variables in every class. |
---|
| 262 | |
---|
| 263 | To be really sure that not more than exactly one object exists (even with libraries), |
---|
[7401] | 264 | ClassIdentifiers are stored in a static map in Identifier. |
---|
[790] | 265 | */ |
---|
| 266 | template <class T> |
---|
| 267 | class ClassIdentifier : public Identifier |
---|
[1052] | 268 | { |
---|
[10774] | 269 | static_assert(std::is_base_of<Identifiable, T>::value, "ClassIdentifier can only be used with Identifiables"); |
---|
[10624] | 270 | |
---|
[7401] | 271 | #ifndef DOXYGEN_SHOULD_SKIP_THIS |
---|
| 272 | #define SUPER_INTRUSIVE_DECLARATION_INCLUDE |
---|
| 273 | #include "Super.h" |
---|
| 274 | #endif |
---|
[5781] | 275 | |
---|
[790] | 276 | public: |
---|
[10624] | 277 | ClassIdentifier(const std::string& name, Factory* factory, bool bLoadable) : Identifier(name, factory, bLoadable) |
---|
| 278 | { |
---|
[10765] | 279 | OrxVerify(ClassIdentifier<T>::classIdentifier_s == nullptr, "Assertion failed in ClassIdentifier of type " << typeid(T).name()); |
---|
[10624] | 280 | ClassIdentifier<T>::classIdentifier_s = this; |
---|
[1052] | 281 | |
---|
[10624] | 282 | SuperFunctionInitialization<0, T>::initialize(this); |
---|
| 283 | } |
---|
| 284 | ~ClassIdentifier() |
---|
| 285 | { |
---|
| 286 | SuperFunctionDestruction<0, T>::destroy(this); |
---|
| 287 | } |
---|
| 288 | |
---|
[9667] | 289 | bool initializeObject(T* object); |
---|
[3325] | 290 | |
---|
[10817] | 291 | virtual void updateConfigValues(bool updateChildren = true) const override; |
---|
[10624] | 292 | |
---|
[10817] | 293 | virtual const std::type_info& getTypeInfo() override |
---|
[10624] | 294 | { return typeid(T); } |
---|
| 295 | |
---|
[10817] | 296 | virtual bool canDynamicCastObjectToIdentifierClass(Identifiable* object) const override |
---|
[10768] | 297 | { return dynamic_cast<T*>(object) != nullptr; } |
---|
[10624] | 298 | |
---|
[10817] | 299 | virtual void destroyObjects() override; |
---|
[10624] | 300 | |
---|
| 301 | static ClassIdentifier<T>* getIdentifier(); |
---|
| 302 | |
---|
| 303 | private: |
---|
[10990] | 304 | ClassIdentifier(const ClassIdentifier<T>&) = delete; |
---|
[10624] | 305 | |
---|
[9586] | 306 | void setConfigValues(T* object, Configurable*) const; |
---|
| 307 | void setConfigValues(T* object, Identifiable*) const; |
---|
| 308 | |
---|
[9572] | 309 | void addObjectToList(T* object, Listable*); |
---|
| 310 | void addObjectToList(T* object, Identifiable*); |
---|
| 311 | |
---|
[10624] | 312 | void destroyObjects(Listable*); |
---|
| 313 | void destroyObjects(void*); |
---|
[1052] | 314 | |
---|
[10624] | 315 | void destroyObject(Destroyable* object); |
---|
| 316 | void destroyObject(void* object); |
---|
[9667] | 317 | |
---|
| 318 | void updateConfigValues(bool updateChildren, Listable*) const; |
---|
| 319 | void updateConfigValues(bool updateChildren, Identifiable*) const; |
---|
| 320 | |
---|
[10769] | 321 | static WeakPtr<ClassIdentifier<T>> classIdentifier_s; |
---|
[790] | 322 | }; |
---|
| 323 | |
---|
[1543] | 324 | template <class T> |
---|
[10769] | 325 | WeakPtr<ClassIdentifier<T>> ClassIdentifier<T>::classIdentifier_s; |
---|
[1543] | 326 | |
---|
[790] | 327 | /** |
---|
[1747] | 328 | @brief Returns the only instance of this class. |
---|
[1543] | 329 | @return The unique Identifier |
---|
| 330 | */ |
---|
| 331 | template <class T> |
---|
[10624] | 332 | /*static*/ inline ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier() |
---|
[1543] | 333 | { |
---|
[10765] | 334 | if (ClassIdentifier<T>::classIdentifier_s == nullptr) |
---|
[10624] | 335 | ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*) IdentifierManager::getInstance().getIdentifierByTypeInfo(typeid(T)); |
---|
[1543] | 336 | |
---|
[10765] | 337 | OrxVerify(ClassIdentifier<T>::classIdentifier_s != nullptr, "Did you forget to register the class of type " << typeid(T).name() << "?"); |
---|
[1543] | 338 | return ClassIdentifier<T>::classIdentifier_s; |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | /** |
---|
[790] | 342 | @brief Adds an object of the given type to the ObjectList. |
---|
| 343 | @param object The object to add |
---|
| 344 | */ |
---|
| 345 | template <class T> |
---|
[9667] | 346 | bool ClassIdentifier<T>::initializeObject(T* object) |
---|
[790] | 347 | { |
---|
[9667] | 348 | orxout(verbose, context::object_list) << "Register Object: " << this->getName() << endl; |
---|
[3325] | 349 | |
---|
| 350 | object->identifier_ = this; |
---|
[9667] | 351 | if (IdentifierManager::getInstance().isCreatingHierarchy()) |
---|
[3325] | 352 | { |
---|
[9667] | 353 | IdentifierManager::getInstance().createdObject(object); |
---|
[3325] | 354 | |
---|
[10624] | 355 | if (Identifier::initConfigValues_s) |
---|
| 356 | this->setConfigValues(object, object); |
---|
| 357 | |
---|
[3325] | 358 | return true; |
---|
| 359 | } |
---|
| 360 | else |
---|
| 361 | { |
---|
[9572] | 362 | this->addObjectToList(object, object); |
---|
[3325] | 363 | |
---|
[9566] | 364 | // Add pointer of type T to the map in the Identifiable instance that enables "dynamic_casts" |
---|
[10918] | 365 | object->objectPointers_.emplace_back(this->getClassID(), static_cast<void*>(object)); |
---|
[3325] | 366 | return false; |
---|
| 367 | } |
---|
[790] | 368 | } |
---|
| 369 | |
---|
| 370 | /** |
---|
[9586] | 371 | * @brief Only configures the object if is a @ref Configurable |
---|
| 372 | */ |
---|
| 373 | template <class T> |
---|
| 374 | void ClassIdentifier<T>::setConfigValues(T* object, Configurable*) const |
---|
| 375 | { |
---|
| 376 | object->setConfigValues(); |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | template <class T> |
---|
| 380 | void ClassIdentifier<T>::setConfigValues(T*, Identifiable*) const |
---|
| 381 | { |
---|
| 382 | // no action |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | /** |
---|
[9572] | 386 | * @brief Only adds the object to the object list if is a @ref Listable |
---|
| 387 | */ |
---|
| 388 | template <class T> |
---|
[9667] | 389 | void ClassIdentifier<T>::addObjectToList(T* object, Listable* listable) |
---|
[9572] | 390 | { |
---|
[9667] | 391 | if (listable->getContext()) |
---|
| 392 | listable->getContext()->addObject(object); |
---|
[9572] | 393 | } |
---|
| 394 | |
---|
| 395 | template <class T> |
---|
| 396 | void ClassIdentifier<T>::addObjectToList(T*, Identifiable*) |
---|
| 397 | { |
---|
| 398 | // no action |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | /** |
---|
[10624] | 402 | * @brief Destroy all objects of this class (must be Listable). |
---|
| 403 | * Destroyables are destroyed with destroy(), all other classes with delete. |
---|
| 404 | */ |
---|
| 405 | template <class T> |
---|
| 406 | void ClassIdentifier<T>::destroyObjects() |
---|
| 407 | { |
---|
[10768] | 408 | this->destroyObjects((T*)nullptr); |
---|
[10624] | 409 | } |
---|
| 410 | |
---|
| 411 | /** |
---|
| 412 | * @brief Only searches and destroys objects if is a @ref Listable |
---|
| 413 | */ |
---|
| 414 | template <class T> |
---|
| 415 | void ClassIdentifier<T>::destroyObjects(Listable*) |
---|
| 416 | { |
---|
[10920] | 417 | ObjectList<T> list(Context::getRootContext()->getObjectList(this)); |
---|
| 418 | for (typename ObjectList<T>::iterator it = list.begin(); it != list.end(); ) |
---|
[10624] | 419 | this->destroyObject(*(it++)); |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | template <class T> |
---|
| 423 | void ClassIdentifier<T>::destroyObjects(void*) |
---|
| 424 | { |
---|
| 425 | // no action |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | /** |
---|
| 429 | * @brief Call 'object->destroy()' for Destroyables and 'delete object' for all other types. |
---|
| 430 | */ |
---|
| 431 | template <class T> |
---|
| 432 | void ClassIdentifier<T>::destroyObject(Destroyable* object) |
---|
| 433 | { |
---|
| 434 | object->destroy(); |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | template <class T> |
---|
| 438 | void ClassIdentifier<T>::destroyObject(void* object) |
---|
| 439 | { |
---|
| 440 | delete static_cast<Identifiable*>(object); |
---|
| 441 | } |
---|
| 442 | |
---|
| 443 | /** |
---|
[1052] | 444 | @brief Updates the config-values of all existing objects of this class by calling their setConfigValues() function. |
---|
[790] | 445 | */ |
---|
| 446 | template <class T> |
---|
[1747] | 447 | void ClassIdentifier<T>::updateConfigValues(bool updateChildren) const |
---|
[790] | 448 | { |
---|
[10765] | 449 | this->updateConfigValues(updateChildren, static_cast<T*>(nullptr)); |
---|
[9667] | 450 | } |
---|
| 451 | |
---|
| 452 | template <class T> |
---|
| 453 | void ClassIdentifier<T>::updateConfigValues(bool updateChildren, Listable*) const |
---|
| 454 | { |
---|
[1747] | 455 | if (!this->hasConfigValues()) |
---|
| 456 | return; |
---|
[790] | 457 | |
---|
[10919] | 458 | for (T* object : ObjectList<T>()) |
---|
| 459 | this->setConfigValues(object, object); |
---|
[1052] | 460 | |
---|
[1747] | 461 | if (updateChildren) |
---|
[10916] | 462 | for (const Identifier* child : this->getChildren()) |
---|
| 463 | child->updateConfigValues(false); |
---|
[1052] | 464 | } |
---|
| 465 | |
---|
[9667] | 466 | template <class T> |
---|
| 467 | void ClassIdentifier<T>::updateConfigValues(bool updateChildren, Identifiable*) const |
---|
| 468 | { |
---|
| 469 | // no action |
---|
| 470 | } |
---|
[1052] | 471 | |
---|
[9667] | 472 | |
---|
[790] | 473 | // ############################### |
---|
[3325] | 474 | // ### orxonox_cast ### |
---|
| 475 | // ############################### |
---|
| 476 | /** |
---|
| 477 | @brief |
---|
[9566] | 478 | Casts on object of type Identifiable to any derived type that is |
---|
[3325] | 479 | registered in the class hierarchy. |
---|
| 480 | @return |
---|
[10765] | 481 | Returns nullptr if the cast is not possible |
---|
[3325] | 482 | @note |
---|
[10765] | 483 | In case of nullptr return (and using MSVC), a dynamic_cast might still be possible if |
---|
[3325] | 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; |
---|
[10765] | 492 | if (source != nullptr) |
---|
[3370] | 493 | return source->template getDerivedPointer<ClassType>(ClassIdentifier<ClassType>::getIdentifier()->getClassID()); |
---|
| 494 | else |
---|
[10765] | 495 | return nullptr; |
---|
[3332] | 496 | #else |
---|
| 497 | return dynamic_cast<T>(source); |
---|
| 498 | #endif |
---|
[3325] | 499 | } |
---|
[790] | 500 | } |
---|
| 501 | |
---|
| 502 | #endif /* _Identifier_H__ */ |
---|