Changeset 9565 for code/branches/core6/src/libraries
- Timestamp:
- Mar 24, 2013, 6:34:23 PM (12 years ago)
- Location:
- code/branches/core6/src/libraries/core
- Files:
-
- 4 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core6/src/libraries/core/CorePrereqs.h
r9561 r9565 155 155 class GraphicsManager; 156 156 class GUIManager; 157 class Identifiable; 157 158 class Identifier; 158 159 template <class T> -
code/branches/core6/src/libraries/core/class/CMakeLists.txt
r9564 r9565 1 1 ADD_SOURCE_FILES(CORE_SRC_FILES 2 Identifiable.cc 2 3 Identifier.cc 3 4 IdentifierManager.cc -
code/branches/core6/src/libraries/core/class/Identifiable.cc
r9563 r9565 32 32 */ 33 33 34 #include " OrxonoxClass.h"34 #include "Identifiable.h" 35 35 36 36 #include <cassert> … … 44 44 @brief Constructor: Sets the default values. 45 45 */ 46 OrxonoxClass::OrxonoxClass()46 Identifiable::Identifiable() 47 47 { 48 48 this->identifier_ = 0; 49 49 this->parents_ = 0; 50 50 this->metaList_ = new MetaObjectList(); 51 this->referenceCount_ = 0;52 this->requestedDestruction_ = false;53 51 // Optimisation 54 52 this->objectPointers_.reserve(6); … … 56 54 57 55 /** 58 @brief Destructor: Removes the object from the object-lists , notifies all DestructionListener (for example @ref WeakPtr "weak pointers") that this object is being deleted.56 @brief Destructor: Removes the object from the object-lists 59 57 */ 60 OrxonoxClass::~OrxonoxClass()58 Identifiable::~Identifiable() 61 59 { 62 60 // if (!this->requestedDestruction_) 63 61 // orxout(internal_warning) << "Destroyed object without destroy() (" << this->getIdentifier()->getName() << ')' << endl; 64 65 assert(this->referenceCount_ <= 0);66 62 67 63 this->unregisterObject(); … … 70 66 if (this->parents_) 71 67 delete this->parents_; 72 73 // notify all destruction listeners74 for (std::set<DestructionListener*>::iterator it = this->destructionListeners_.begin(); it != this->destructionListeners_.end(); )75 (*(it++))->objectDeleted();76 }77 78 /**79 @brief Deletes the object if no @ref orxonox::SmartPtr "smart pointers" point to this object. Otherwise schedules the object to be deleted as soon as possible.80 */81 void OrxonoxClass::destroy()82 {83 assert(this); // Just in case someone tries to delete a NULL pointer84 this->requestedDestruction_ = true;85 if (this->referenceCount_ == 0)86 {87 this->preDestroy();88 if (this->referenceCount_ == 0)89 delete this;90 }91 68 } 92 69 … … 94 71 @brief Removes this object from the object-lists. 95 72 */ 96 void OrxonoxClass::unregisterObject()73 void Identifiable::unregisterObject() 97 74 { 98 75 if (this->metaList_) … … 102 79 103 80 /// Returns true if the object's class is of the given type or a derivative. 104 bool OrxonoxClass::isA(const Identifier* identifier)81 bool Identifiable::isA(const Identifier* identifier) 105 82 { return this->getIdentifier()->isA(identifier); } 106 83 /// Returns true if the object's class is exactly of the given type. 107 bool OrxonoxClass::isExactlyA(const Identifier* identifier)84 bool Identifiable::isExactlyA(const Identifier* identifier) 108 85 { return this->getIdentifier()->isExactlyA(identifier); } 109 86 /// Returns true if the object's class is a child of the given type. 110 bool OrxonoxClass::isChildOf(const Identifier* identifier)87 bool Identifiable::isChildOf(const Identifier* identifier) 111 88 { return this->getIdentifier()->isChildOf(identifier); } 112 89 /// Returns true if the object's class is a direct child of the given type. 113 bool OrxonoxClass::isDirectChildOf(const Identifier* identifier)90 bool Identifiable::isDirectChildOf(const Identifier* identifier) 114 91 { return this->getIdentifier()->isDirectChildOf(identifier); } 115 92 /// Returns true if the object's class is a parent of the given type. 116 bool OrxonoxClass::isParentOf(const Identifier* identifier)93 bool Identifiable::isParentOf(const Identifier* identifier) 117 94 { return this->getIdentifier()->isParentOf(identifier); } 118 95 /// Returns true if the object's class is a direct parent of the given type. 119 bool OrxonoxClass::isDirectParentOf(const Identifier* identifier)96 bool Identifiable::isDirectParentOf(const Identifier* identifier) 120 97 { return this->getIdentifier()->isDirectParentOf(identifier); } 121 98 122 99 123 100 /// Returns true if the object's class is of the given type or a derivative. 124 bool OrxonoxClass::isA(const OrxonoxClass* object)101 bool Identifiable::isA(const Identifiable* object) 125 102 { return this->getIdentifier()->isA(object->getIdentifier()); } 126 103 /// Returns true if the object's class is exactly of the given type. 127 bool OrxonoxClass::isExactlyA(const OrxonoxClass* object)104 bool Identifiable::isExactlyA(const Identifiable* object) 128 105 { return this->getIdentifier()->isExactlyA(object->getIdentifier()); } 129 106 /// Returns true if the object's class is a child of the given type. 130 bool OrxonoxClass::isChildOf(const OrxonoxClass* object)107 bool Identifiable::isChildOf(const Identifiable* object) 131 108 { return this->getIdentifier()->isChildOf(object->getIdentifier()); } 132 109 /// Returns true if the object's class is a direct child of the given type. 133 bool OrxonoxClass::isDirectChildOf(const OrxonoxClass* object)110 bool Identifiable::isDirectChildOf(const Identifiable* object) 134 111 { return this->getIdentifier()->isDirectChildOf(object->getIdentifier()); } 135 112 /// Returns true if the object's class is a parent of the given type. 136 bool OrxonoxClass::isParentOf(const OrxonoxClass* object)113 bool Identifiable::isParentOf(const Identifiable* object) 137 114 { return this->getIdentifier()->isParentOf(object->getIdentifier()); } 138 115 /// Returns true if the object's class is a direct child of the given type. 139 bool OrxonoxClass::isDirectParentOf(const OrxonoxClass* object)116 bool Identifiable::isDirectParentOf(const Identifiable* object) 140 117 { return this->getIdentifier()->isDirectParentOf(object->getIdentifier()); } 141 118 } -
code/branches/core6/src/libraries/core/class/Identifiable.h
r9563 r9565 28 28 29 29 /** 30 @ defgroup OrxonoxClass OrxonoxClass31 @ingroup Class 32 */ 30 @file 31 @ingroup Class Identifier 32 @brief Declaration of Identifiable, the base of all classes that should own an Identifier. 33 33 34 /**35 @file36 @ingroup Class OrxonoxClass37 @brief Declaration of OrxonoxClass, the base class of all objects and interfaces in Orxonox.38 39 All objects and interfaces of the game-logic (not the engine) are derived from OrxonoxClass.40 34 It stores the Identifier and the MetaObjectList and has all needed functions to create and use the class-hierarchy. 41 35 */ 42 36 43 #ifndef _ OrxonoxClass_H__44 #define _ OrxonoxClass_H__37 #ifndef _Identifiable_H__ 38 #define _Identifiable_H__ 45 39 46 40 #include "core/CorePrereqs.h" … … 48 42 #include <set> 49 43 #include <vector> 50 #include "Super.h"51 44 52 45 namespace orxonox 53 46 { 54 47 /** 55 @brief The class all objects and interfaces of the game-logic (not the engine) are derived from. 56 57 The BaseObject and Interfaces are derived with @c virtual @c public @c OrxonoxClass from OrxonoxClass. 58 OrxonoxClass is needed to create the class-hierarchy at startup and to store the Identifier and the 59 MetaObjectList, as well as to provide an interface for SmartPtr and WeakPtr. 48 @brief Identifiable is needed to create the class-hierarchy at startup and to store the Identifier and the MetaObjectList. 60 49 */ 61 class _CoreExport OrxonoxClass50 class _CoreExport Identifiable 62 51 { 63 52 template <class T> 64 53 friend class ClassIdentifier; 65 54 66 template <class T>67 friend class SmartPtr;68 69 friend class DestructionListener;70 71 55 public: 72 OrxonoxClass();73 virtual ~ OrxonoxClass();56 Identifiable(); 57 virtual ~Identifiable(); 74 58 75 59 void destroy(); 76 60 void unregisterObject(); 77 78 /// Function to collect the SetConfigValue-macro calls.79 void setConfigValues() {};80 61 81 62 /// Returns the Identifier of the object. … … 108 89 { return this->isDirectParentOf(*identifier); } 109 90 110 bool isA(const OrxonoxClass* object); 111 bool isExactlyA(const OrxonoxClass* object); 112 bool isChildOf(const OrxonoxClass* object); 113 bool isDirectChildOf(const OrxonoxClass* object); 114 bool isParentOf(const OrxonoxClass* object); 115 bool isDirectParentOf(const OrxonoxClass* object); 116 117 /// Returns the number of @ref orxonox::SmartPtr "smart pointers" that point to this object. 118 inline unsigned int getReferenceCount() const 119 { return this->referenceCount_; } 91 bool isA(const Identifiable* object); 92 bool isExactlyA(const Identifiable* object); 93 bool isChildOf(const Identifiable* object); 94 bool isDirectChildOf(const Identifiable* object); 95 bool isParentOf(const Identifiable* object); 96 bool isDirectParentOf(const Identifiable* object); 120 97 121 98 /** … … 141 118 /// Const version of getDerivedPointer with template 142 119 template <class T> ORX_FORCEINLINE const T* getDerivedPointer(unsigned int classID) const 143 { return const_cast<OrxonoxClass*>(this)->getDerivedPointer<T>(classID); } 144 145 protected: 146 /// This virtual function is called if destroy() is called and no SmartPtr points to this object. Used in some cases to create a new SmartPtr to prevent destruction. 147 virtual void preDestroy() {} 120 { return const_cast<Identifiable*>(this)->getDerivedPointer<T>(classID); } 148 121 149 122 private: 150 /// Increments the reference counter (for smart pointers).151 inline void incrementReferenceCount()152 { ++this->referenceCount_; }153 /// Decrements the reference counter (for smart pointers).154 inline void decrementReferenceCount()155 {156 --this->referenceCount_;157 if (this->referenceCount_ == 0 && this->requestedDestruction_)158 this->destroy();159 }160 161 /// Register a destruction listener (for example a weak pointer which points to this object).162 inline void registerDestructionListener(DestructionListener* pointer)163 { this->destructionListeners_.insert(pointer); }164 /// Unegister a destruction listener (for example a weak pointer which pointed to this object before).165 inline void unregisterDestructionListener(DestructionListener* pointer)166 { this->destructionListeners_.erase(pointer); }167 168 123 Identifier* identifier_; //!< The Identifier of the object 169 124 std::set<const Identifier*>* parents_; //!< List of all parents of the object 170 125 MetaObjectList* metaList_; //!< MetaObjectList, containing all ObjectLists and ObjectListElements the object is registered in 171 int referenceCount_; //!< Counts the references from smart pointers to this object172 bool requestedDestruction_; //!< Becomes true after someone called delete on this object173 std::set<DestructionListener*> destructionListeners_; //!< All destruction listeners (for example weak pointers which point to this object and like to get notified if it dies)174 126 175 127 /// 'Fast map' that holds this-pointers of all derived types 176 128 std::vector<std::pair<unsigned int, void*> > objectPointers_; 177 129 }; 178 179 /**180 @brief This listener is used to inform weak pointers if an object of type OrxonoxClass gets destroyed.181 */182 class _CoreExport DestructionListener183 {184 friend class OrxonoxClass;185 186 protected:187 virtual ~DestructionListener() {}188 189 inline void registerAsDestructionListener(OrxonoxClass* object)190 { if (object) { object->registerDestructionListener(this); } }191 inline void unregisterAsDestructionListener(OrxonoxClass* object)192 { if (object) { object->unregisterDestructionListener(this); } }193 194 virtual void objectDeleted() = 0;195 };196 197 130 } 198 131 199 #endif /* _ OrxonoxClass_H__ */132 #endif /* _Identifiable_H__ */ -
code/branches/core6/src/libraries/core/class/OrxonoxClass.cc
r9563 r9565 46 46 OrxonoxClass::OrxonoxClass() 47 47 { 48 this->identifier_ = 0;49 this->parents_ = 0;50 this->metaList_ = new MetaObjectList();51 48 this->referenceCount_ = 0; 52 49 this->requestedDestruction_ = false; 53 // Optimisation54 this->objectPointers_.reserve(6);55 50 } 56 51 57 52 /** 58 @brief Destructor: Removes the object from the object-lists, notifies all DestructionListener (for example @ref WeakPtr "weak pointers") that this object is being deleted.53 @brief Destructor: Notifies all DestructionListener (for example @ref WeakPtr "weak pointers") that this object is being deleted. 59 54 */ 60 55 OrxonoxClass::~OrxonoxClass() 61 56 { 62 // if (!this->requestedDestruction_)63 // orxout(internal_warning) << "Destroyed object without destroy() (" << this->getIdentifier()->getName() << ')' << endl;64 65 57 assert(this->referenceCount_ <= 0); 66 67 this->unregisterObject();68 69 // parents_ exists only if isCreatingHierarchy() of the associated Identifier returned true while creating the class70 if (this->parents_)71 delete this->parents_;72 58 73 59 // notify all destruction listeners … … 90 76 } 91 77 } 92 93 /**94 @brief Removes this object from the object-lists.95 */96 void OrxonoxClass::unregisterObject()97 {98 if (this->metaList_)99 delete this->metaList_;100 this->metaList_ = 0;101 }102 103 /// Returns true if the object's class is of the given type or a derivative.104 bool OrxonoxClass::isA(const Identifier* identifier)105 { return this->getIdentifier()->isA(identifier); }106 /// Returns true if the object's class is exactly of the given type.107 bool OrxonoxClass::isExactlyA(const Identifier* identifier)108 { return this->getIdentifier()->isExactlyA(identifier); }109 /// Returns true if the object's class is a child of the given type.110 bool OrxonoxClass::isChildOf(const Identifier* identifier)111 { return this->getIdentifier()->isChildOf(identifier); }112 /// Returns true if the object's class is a direct child of the given type.113 bool OrxonoxClass::isDirectChildOf(const Identifier* identifier)114 { return this->getIdentifier()->isDirectChildOf(identifier); }115 /// Returns true if the object's class is a parent of the given type.116 bool OrxonoxClass::isParentOf(const Identifier* identifier)117 { return this->getIdentifier()->isParentOf(identifier); }118 /// Returns true if the object's class is a direct parent of the given type.119 bool OrxonoxClass::isDirectParentOf(const Identifier* identifier)120 { return this->getIdentifier()->isDirectParentOf(identifier); }121 122 123 /// Returns true if the object's class is of the given type or a derivative.124 bool OrxonoxClass::isA(const OrxonoxClass* object)125 { return this->getIdentifier()->isA(object->getIdentifier()); }126 /// Returns true if the object's class is exactly of the given type.127 bool OrxonoxClass::isExactlyA(const OrxonoxClass* object)128 { return this->getIdentifier()->isExactlyA(object->getIdentifier()); }129 /// Returns true if the object's class is a child of the given type.130 bool OrxonoxClass::isChildOf(const OrxonoxClass* object)131 { return this->getIdentifier()->isChildOf(object->getIdentifier()); }132 /// Returns true if the object's class is a direct child of the given type.133 bool OrxonoxClass::isDirectChildOf(const OrxonoxClass* object)134 { return this->getIdentifier()->isDirectChildOf(object->getIdentifier()); }135 /// Returns true if the object's class is a parent of the given type.136 bool OrxonoxClass::isParentOf(const OrxonoxClass* object)137 { return this->getIdentifier()->isParentOf(object->getIdentifier()); }138 /// Returns true if the object's class is a direct child of the given type.139 bool OrxonoxClass::isDirectParentOf(const OrxonoxClass* object)140 { return this->getIdentifier()->isDirectParentOf(object->getIdentifier()); }141 78 } -
code/branches/core6/src/libraries/core/class/OrxonoxClass.h
r9563 r9565 36 36 @ingroup Class OrxonoxClass 37 37 @brief Declaration of OrxonoxClass, the base class of all objects and interfaces in Orxonox. 38 39 All objects and interfaces of the game-logic (not the engine) are derived from OrxonoxClass.40 It stores the Identifier and the MetaObjectList and has all needed functions to create and use the class-hierarchy.41 38 */ 42 39 … … 47 44 48 45 #include <set> 49 #include <vector> 50 #include " Super.h"46 //#include "Super.h" 47 #include "Identifiable.h" 51 48 52 49 namespace orxonox … … 56 53 57 54 The BaseObject and Interfaces are derived with @c virtual @c public @c OrxonoxClass from OrxonoxClass. 58 OrxonoxClass is needed to create the class-hierarchy at startup and to store the Identifier and the59 MetaObjectList, as well as to provide an interface for SmartPtr and WeakPtr.60 55 */ 61 class _CoreExport OrxonoxClass 56 class _CoreExport OrxonoxClass : public Identifiable 62 57 { 63 template <class T>64 friend class ClassIdentifier;65 66 58 template <class T> 67 59 friend class SmartPtr; … … 74 66 75 67 void destroy(); 76 void unregisterObject();77 68 78 69 /// Function to collect the SetConfigValue-macro calls. 79 70 void setConfigValues() {}; 80 71 81 /// Returns the Identifier of the object.82 inline Identifier* getIdentifier() const { return this->identifier_; }83 84 bool isA(const Identifier* identifier);85 bool isExactlyA(const Identifier* identifier);86 bool isChildOf(const Identifier* identifier);87 bool isDirectChildOf(const Identifier* identifier);88 bool isParentOf(const Identifier* identifier);89 bool isDirectParentOf(const Identifier* identifier);90 91 /// Returns true if the object's class is of the given type or a derivative.92 template <class B> inline bool isA(const SubclassIdentifier<B>* identifier)93 { return this->isA(*identifier); }94 /// Returns true if the object's class is exactly of the given type.95 template <class B> inline bool isExactlyA(const SubclassIdentifier<B>* identifier)96 { return this->isExactlyA(*identifier); }97 /// Returns true if the object's class is a child of the given type.98 template <class B> inline bool isChildOf(const SubclassIdentifier<B>* identifier)99 { return this->isChildOf(*identifier); }100 /// Returns true if the object's class is a direct child of the given type.101 template <class B> inline bool isDirectChildOf(const SubclassIdentifier<B>* identifier)102 { return this->isDirectChildOf(*identifier); }103 /// Returns true if the object's class is a parent of the given type.104 template <class B> inline bool isParentOf(const SubclassIdentifier<B>* identifier)105 { return this->isParentOf(*identifier); }106 /// Returns true if the object's class is a direct parent of the given type.107 template <class B> inline bool isDirectParentOf(const SubclassIdentifier<B>* identifier)108 { return this->isDirectParentOf(*identifier); }109 110 bool isA(const OrxonoxClass* object);111 bool isExactlyA(const OrxonoxClass* object);112 bool isChildOf(const OrxonoxClass* object);113 bool isDirectChildOf(const OrxonoxClass* object);114 bool isParentOf(const OrxonoxClass* object);115 bool isDirectParentOf(const OrxonoxClass* object);116 117 72 /// Returns the number of @ref orxonox::SmartPtr "smart pointers" that point to this object. 118 73 inline unsigned int getReferenceCount() const 119 74 { return this->referenceCount_; } 120 121 /**122 @brief123 Returns a valid pointer of any derived type that is124 registered in the class hierarchy.125 @return126 Returns NULL if the no pointer was found.127 */128 ORX_FORCEINLINE void* getDerivedPointer(unsigned int classID)129 {130 for (int i = this->objectPointers_.size() - 1; i >= 0; --i)131 {132 if (this->objectPointers_[i].first == classID)133 return this->objectPointers_[i].second;134 }135 return NULL;136 }137 138 /// Version of getDerivedPointer with template139 template <class T> ORX_FORCEINLINE T* getDerivedPointer(unsigned int classID)140 { return static_cast<T*>(this->getDerivedPointer(classID)); }141 /// Const version of getDerivedPointer with template142 template <class T> ORX_FORCEINLINE const T* getDerivedPointer(unsigned int classID) const143 { return const_cast<OrxonoxClass*>(this)->getDerivedPointer<T>(classID); }144 75 145 76 protected: … … 166 97 { this->destructionListeners_.erase(pointer); } 167 98 168 Identifier* identifier_; //!< The Identifier of the object169 std::set<const Identifier*>* parents_; //!< List of all parents of the object170 MetaObjectList* metaList_; //!< MetaObjectList, containing all ObjectLists and ObjectListElements the object is registered in171 99 int referenceCount_; //!< Counts the references from smart pointers to this object 172 100 bool requestedDestruction_; //!< Becomes true after someone called delete on this object 173 101 std::set<DestructionListener*> destructionListeners_; //!< All destruction listeners (for example weak pointers which point to this object and like to get notified if it dies) 174 175 /// 'Fast map' that holds this-pointers of all derived types176 std::vector<std::pair<unsigned int, void*> > objectPointers_;177 102 }; 178 103
Note: See TracChangeset
for help on using the changeset viewer.