Changeset 3325 for code/trunk/src/core
- Timestamp:
- Jul 19, 2009, 3:48:00 PM (15 years ago)
- Location:
- code/trunk
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/core4 merged: 3222-3224,3238
- Property svn:mergeinfo changed
-
code/trunk/src/core/CoreIncludes.h
r3196 r3325 56 56 */ 57 57 #define InternRegisterObject(ClassName, bRootClass) \ 58 this->setIdentifier(orxonox::ClassIdentifier<ClassName>::getIdentifier(#ClassName)); \ 59 if (orxonox::Identifier::isCreatingHierarchy()) \ 60 { \ 61 if (this->getParents()) \ 62 { \ 63 orxonox::ClassIdentifier<ClassName>::getIdentifier(#ClassName)->initializeClassHierarchy(this->getParents(), bRootClass); \ 64 this->getParents()->insert(this->getParents()->end(), this->getIdentifier()); \ 65 } \ 66 this->setConfigValues(); \ 58 if (ClassIdentifier<ClassName>::getIdentifier(#ClassName)->initialiseObject(this, #ClassName, bRootClass)) \ 67 59 return; \ 68 } \ 69 orxonox::ClassIdentifier<ClassName>::getIdentifier()->addObject(this) 70 71 /** 72 @brief Intern macro, containing the specific part of RegisterRootObject. 73 @param ClassName The name of the class 74 */ 75 #define InternRegisterRootObject(ClassName) \ 76 if (orxonox::Identifier::isCreatingHierarchy() && !this->getParents()) \ 77 this->createParents(); \ 78 InternRegisterObject(ClassName, true) 60 else \ 61 ((void)0) 79 62 80 63 /** … … 83 66 */ 84 67 #define RegisterObject(ClassName) \ 85 COUT(5) << "*** Register Object: " << #ClassName << std::endl; \86 68 InternRegisterObject(ClassName, false) 87 69 … … 91 73 */ 92 74 #define RegisterRootObject(ClassName) \ 93 COUT(5) << "*** Register Root-Object: " << #ClassName << std::endl; \ 94 InternRegisterRootObject(ClassName) 75 InternRegisterObject(ClassName, true) 95 76 96 77 /** … … 115 96 orxonox::ClassIdentifier<ClassName>::getIdentifier() 116 97 117 /**118 @brief Returns the Identifier with a given name through the factory.119 @param String The name of the class120 */121 #define ClassByString(String) \122 orxonox::Factory::getIdentifier(String)123 98 124 /** 125 @brief Returns the Identifier with a given network ID through the factory. 126 @param networkID The network ID of the class 127 */ 128 #define ClassByID(networkID) \ 129 orxonox::Factory::getIdentifier(networkID) 99 namespace orxonox 100 { 101 /** 102 @brief Returns the Identifier with a given name through the factory. 103 @param String The name of the class 104 */ 105 inline Identifier* ClassByString(const std::string& name) 106 { 107 return Factory::getIdentifier(name); 108 } 109 110 /** 111 @brief Returns the Identifier with a given network ID through the factory. 112 @param networkID The network ID of the class 113 */ 114 inline Identifier* ClassByID(uint32_t id) 115 { 116 return Factory::getIdentifier(id); 117 } 118 } 130 119 131 120 #endif /* _CoreIncludes_H__ */ -
code/trunk/src/core/EventIncludes.h
r3196 r3325 54 54 this->addEventContainer(eventname, containername); \ 55 55 } \ 56 event.castedOriginator_ = dynamic_cast<subclassname*>(event.originator_); \56 event.castedOriginator_ = orxonox::orxonox_cast<subclassname*>(event.originator_); \ 57 57 containername->process(this, event) 58 58 … … 66 66 this->addEventContainer(eventname, containername); \ 67 67 } \ 68 event.castedOriginator_ = dynamic_cast<subclassname*>(event.originator_); \68 event.castedOriginator_ = orxonox::orxonox_cast<subclassname*>(event.originator_); \ 69 69 containername->process(this, event) 70 70 -
code/trunk/src/core/Identifier.cc
r3280 r3325 47 47 // ### Identifier ### 48 48 // ############################### 49 int Identifier::hierarchyCreatingCounter_s = 0; // Set the static member variable hierarchyCreatingCounter_s to zero (this static member variable is ok: it's used in main(), not before) 49 int Identifier::hierarchyCreatingCounter_s = 0; 50 unsigned int Identifier::classIDCounter_s = 0; 50 51 51 52 /** … … 53 54 */ 54 55 Identifier::Identifier() 56 : classID_(classIDCounter_s++) 55 57 { 56 58 this->objects_ = new ObjectListBase(this); … … 67 69 this->directChildren_ = new std::set<const Identifier*>(); 68 70 69 // Use a static variable because the classID gets created before main() and that's why we should avoid static member variables 70 static unsigned int classIDcounter_s = 0; 71 this->classID_ = classIDcounter_s++; 71 // Default network ID is the class ID 72 this->networkID_ = this->classID_; 72 73 } 73 74 … … 244 245 void Identifier::setNetworkID(uint32_t id) 245 246 { 246 Factory::changeNetworkID(this, this-> classID_, id);247 this-> classID_ = id;247 Factory::changeNetworkID(this, this->networkID_, id); 248 this->networkID_ = id; 248 249 } 249 250 -
code/trunk/src/core/Identifier.h
r3196 r3325 224 224 225 225 /** @brief Returns the network ID to identify a class through the network. @return the network ID */ 226 inline const uint32_t getNetworkID() const { return this-> classID_; }226 inline const uint32_t getNetworkID() const { return this->networkID_; } 227 227 228 228 /** @brief Sets the network ID to a new value. @param id The new value */ 229 229 void setNetworkID(uint32_t id); 230 231 /** @brief Returns the unique ID of the class */ 232 FORCEINLINE unsigned int getClassID() const { return this->classID_; } 230 233 231 234 void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container); … … 305 308 BaseFactory* factory_; //!< The Factory, able to create new objects of the given class (if available) 306 309 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) 307 uint32_t classID_; //!< The network ID to identify a class through the network 310 uint32_t networkID_; //!< The network ID to identify a class through the network 311 const unsigned int classID_; //!< Uniquely identifies a class (might not be the same as the networkID_) 312 static unsigned int classIDCounter_s; //!< Static counter for the unique classIDs 308 313 309 314 bool bHasConfigValues_; //!< True if this class has at least one assigned config value … … 344 349 static ClassIdentifier<T> *getIdentifier(); 345 350 static ClassIdentifier<T> *getIdentifier(const std::string& name); 346 void addObject(T* object); 351 352 bool initialiseObject(T* object, const std::string& className, bool bRootClass); 347 353 348 354 void updateConfigValues(bool updateChildren = true) const; … … 423 429 */ 424 430 template <class T> 425 inline void ClassIdentifier<T>::addObject(T* object) 426 { 427 COUT(5) << "*** ClassIdentifier: Added object to " << this->getName() << "-list." << std::endl; 428 object->getMetaList().add(this->objects_, this->objects_->add(new ObjectListElement<T>(object))); 431 bool ClassIdentifier<T>::initialiseObject(T* object, const std::string& className, bool bRootClass) 432 { 433 if (bRootClass) 434 COUT(5) << "*** Register Root-Object: " << className << std::endl; 435 else 436 COUT(5) << "*** Register Object: " << className << std::endl; 437 438 object->identifier_ = this; 439 if (Identifier::isCreatingHierarchy()) 440 { 441 if (bRootClass && !object->parents_) 442 object->parents_ = new std::set<const Identifier*>(); 443 444 if (object->parents_) 445 { 446 this->initializeClassHierarchy(object->parents_, bRootClass); 447 object->parents_->insert(object->parents_->end(), this); 448 } 449 450 object->setConfigValues(); 451 return true; 452 } 453 else 454 { 455 COUT(5) << "*** ClassIdentifier: Added object to " << this->getName() << "-list." << std::endl; 456 object->metaList_->add(this->objects_, this->objects_->add(new ObjectListElement<T>(object))); 457 458 // Add pointer of type T to the map in the OrxonoxClass instance that enables "dynamic_casts" 459 object->objectPointers_.push_back(std::make_pair(this->getClassID(), reinterpret_cast<void*>(object))); 460 return false; 461 } 429 462 } 430 463 … … 444 477 for (std::set<const Identifier*>::const_iterator it = this->getChildrenBegin(); it != this->getChildrenEnd(); ++it) 445 478 (*it)->updateConfigValues(false); 479 } 480 481 482 // ############################### 483 // ### orxonox_cast ### 484 // ############################### 485 //! Helper struct to have orxonox_cast<T*> instead of orxonox_cast<T> 486 template <class T, class U> 487 struct OrxonoxCaster 488 { 489 static T* cast(U* source) 490 { 491 // If you see this function in a compiler error description, it means 492 // you were misusing orxonox_cast. You must always cast to a pointer type! 493 *****T(); 494 } 495 }; 496 497 //! Helper struct to have orxonox_cast<T*> instead of orxonox_cast<T*> 498 template <class T, class U> 499 struct OrxonoxCaster<T*, U> 500 { 501 FORCEINLINE static T* cast(U* source) 502 { 503 #ifdef ORXONOX_COMPILER_MSVC 504 return source->template getDerivedPointer<T>(ClassIdentifier<T>::getIdentifier()->getClassID()); 505 #else 506 return dynamic_cast<T*>(source); 507 #endif 508 } 509 }; 510 511 /** 512 @brief 513 Casts on object of type OrxonoxClass to any derived type that is 514 registered in the class hierarchy. 515 @return 516 Returns NULL if the cast is not possible 517 @note 518 In case of NULL return (and using MSVC), a dynamic_cast might still be possible if 519 a class forgot to register its objects. 520 Also note that the function is implemented differently for GCC/MSVC. 521 */ 522 template <class T, class U> 523 FORCEINLINE T orxonox_cast(U* source) 524 { 525 return OrxonoxCaster<T, U>::cast(source); 446 526 } 447 527 … … 539 619 if (newObject) 540 620 { 541 return dynamic_cast<T*>(newObject);621 return orxonox_cast<T*>(newObject); 542 622 } 543 623 else -
code/trunk/src/core/Iterator.h
r3196 r3325 240 240 { 241 241 if (this->element_) 242 return dynamic_cast<T*>(this->element_->objectBase_);242 return orxonox_cast<T*>(this->element_->objectBase_); 243 243 else 244 244 return 0; … … 252 252 { 253 253 if (this->element_) 254 return dynamic_cast<T*>(this->element_->objectBase_);254 return orxonox_cast<T*>(this->element_->objectBase_); 255 255 else 256 256 return 0; -
code/trunk/src/core/OrxonoxClass.h
r3280 r3325 50 50 class _CoreExport OrxonoxClass 51 51 { 52 template <class T> 53 friend class ClassIdentifier; 54 52 55 public: 53 56 OrxonoxClass(); … … 59 62 /** @brief Returns the Identifier of the object. @return The Identifier */ 60 63 inline Identifier* getIdentifier() const { return this->identifier_; } 61 62 /** @brief Sets the Identifier of the object. Used by the RegisterObject-macro. */63 inline void setIdentifier(Identifier* identifier) { this->identifier_ = identifier; }64 65 /** @brief Returns the list of all parents of the object. @return The list */66 inline std::set<const Identifier*>* getParents() const { return this->parents_; }67 68 /** @brief Creates the parents-list. */69 inline void createParents() { this->parents_ = new std::set<const Identifier*>(); }70 71 /** @brief Returns the MetaObjectList of the object, containing a link to all ObjectLists and ObjectListElements the object is registered in. @return The list */72 inline MetaObjectList& getMetaList() { return (*this->metaList_); }73 74 64 75 65 bool isA(const Identifier* identifier); … … 101 91 bool isDirectParentOf(const OrxonoxClass* object); 102 92 93 /** 94 @brief 95 Returns a valid pointer of any derived type that is 96 registered in the class hierarchy. 97 @return 98 Returns NULL if the no pointer was found. 99 */ 100 template <class T> 101 FORCEINLINE T* getDerivedPointer(unsigned int classID) const 102 { 103 for (int i = this->objectPointers_.size() - 1; i >= 0; --i) 104 { 105 if (this->objectPointers_[i].first == classID) 106 return reinterpret_cast<T*>(this->objectPointers_[i].second); 107 } 108 return NULL; 109 } 110 103 111 private: 104 112 Identifier* identifier_; //!< The Identifier of the object 105 113 std::set<const Identifier*>* parents_; //!< List of all parents of the object 106 114 MetaObjectList* metaList_; //!< MetaObjectList, containing all ObjectLists and ObjectListElements the object is registered in 115 //! 'Fast map' that holds this-pointers of all derived types 116 std::vector<std::pair<unsigned int, void*> > objectPointers_; 107 117 }; 108 118 } -
code/trunk/src/core/Super.h
r3301 r3325 206 206 207 207 // SUPER-macro: Calls Parent::functionname() where Parent is the direct parent of classname 208 #define SUPER(classname, functionname, ...) \ 209 SUPER_##functionname(classname, functionname, __VA_ARGS__) 208 #ifdef ORXONOX_COMPILER_MSVC 209 #define SUPER(classname, functionname, ...) \ 210 __super::functionname(__VA_ARGS__) 211 #else 212 #define SUPER(classname, functionname, ...) \ 213 SUPER_##functionname(classname, functionname, __VA_ARGS__) 214 #endif 210 215 211 216 // helper macro: for functions without arguments -
code/trunk/src/core/XMLPort.h
r3301 r3325 565 565 newObject->setLoaderIndentation(object->getLoaderIndentation() + " "); 566 566 567 O* castedObject = dynamic_cast<O*>(newObject);567 O* castedObject = orxonox_cast<O*>(newObject); 568 568 assert(castedObject); 569 569
Note: See TracChangeset
for help on using the changeset viewer.