[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 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 | |
---|
| 29 | /** |
---|
[7401] | 30 | @defgroup OrxonoxClass OrxonoxClass |
---|
| 31 | @ingroup Class |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | /** |
---|
[2171] | 35 | @file |
---|
[7401] | 36 | @ingroup Class OrxonoxClass |
---|
| 37 | @brief Declaration of OrxonoxClass, the base class of all objects and interfaces in Orxonox. |
---|
[1505] | 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 | */ |
---|
| 42 | |
---|
| 43 | #ifndef _OrxonoxClass_H__ |
---|
| 44 | #define _OrxonoxClass_H__ |
---|
| 45 | |
---|
| 46 | #include "CorePrereqs.h" |
---|
[6524] | 47 | #include "Super.h" |
---|
[3327] | 48 | |
---|
[1505] | 49 | #include <set> |
---|
[3327] | 50 | #include <vector> |
---|
[1505] | 51 | |
---|
[6105] | 52 | /** |
---|
| 53 | @def CCOUT |
---|
| 54 | Acts almost exactly like COUT(x), but prepends "ClassName: " |
---|
| 55 | */ |
---|
| 56 | #define CCOUT(level) \ |
---|
| 57 | COUT(level) << this->getIdentifier()->getName() << ": " |
---|
| 58 | |
---|
[1505] | 59 | namespace orxonox |
---|
| 60 | { |
---|
| 61 | /** |
---|
[7401] | 62 | @brief The class all objects and interfaces of the game-logic (not the engine) are derived from. |
---|
| 63 | |
---|
| 64 | The BaseObject and Interfaces are derived with @c virtual @c public @c OrxonoxClass from OrxonoxClass. |
---|
| 65 | OrxonoxClass is needed to create the class-hierarchy at startup and to store the Identifier and the |
---|
| 66 | MetaObjectList, as well as to provide an interface for SmartPtr and WeakPtr. |
---|
[1505] | 67 | */ |
---|
| 68 | class _CoreExport OrxonoxClass |
---|
| 69 | { |
---|
[3325] | 70 | template <class T> |
---|
| 71 | friend class ClassIdentifier; |
---|
| 72 | |
---|
[5929] | 73 | template <class T> |
---|
| 74 | friend class SmartPtr; |
---|
| 75 | |
---|
[7849] | 76 | friend class DestructionListener; |
---|
[5929] | 77 | |
---|
[1505] | 78 | public: |
---|
| 79 | OrxonoxClass(); |
---|
| 80 | virtual ~OrxonoxClass(); |
---|
| 81 | |
---|
[5929] | 82 | void destroy(); |
---|
[6417] | 83 | void unregisterObject(); |
---|
[5929] | 84 | |
---|
[7401] | 85 | /// Function to collect the SetConfigValue-macro calls. |
---|
[1505] | 86 | void setConfigValues() {}; |
---|
| 87 | |
---|
[7401] | 88 | /// Returns the Identifier of the object. |
---|
[1505] | 89 | inline Identifier* getIdentifier() const { return this->identifier_; } |
---|
| 90 | |
---|
| 91 | bool isA(const Identifier* identifier); |
---|
| 92 | bool isExactlyA(const Identifier* identifier); |
---|
| 93 | bool isChildOf(const Identifier* identifier); |
---|
| 94 | bool isDirectChildOf(const Identifier* identifier); |
---|
| 95 | bool isParentOf(const Identifier* identifier); |
---|
| 96 | bool isDirectParentOf(const Identifier* identifier); |
---|
| 97 | |
---|
[7401] | 98 | /// Returns true if the object's class is of the given type or a derivative. |
---|
[5929] | 99 | template <class B> inline bool isA(const SubclassIdentifier<B>* identifier) |
---|
| 100 | { return this->isA(*identifier); } |
---|
[7401] | 101 | /// Returns true if the object's class is exactly of the given type. |
---|
[5929] | 102 | template <class B> inline bool isExactlyA(const SubclassIdentifier<B>* identifier) |
---|
| 103 | { return this->isExactlyA(*identifier); } |
---|
[7401] | 104 | /// Returns true if the object's class is a child of the given type. |
---|
[5929] | 105 | template <class B> inline bool isChildOf(const SubclassIdentifier<B>* identifier) |
---|
| 106 | { return this->isChildOf(*identifier); } |
---|
[7401] | 107 | /// Returns true if the object's class is a direct child of the given type. |
---|
[5929] | 108 | template <class B> inline bool isDirectChildOf(const SubclassIdentifier<B>* identifier) |
---|
| 109 | { return this->isDirectChildOf(*identifier); } |
---|
[7401] | 110 | /// Returns true if the object's class is a parent of the given type. |
---|
[5929] | 111 | template <class B> inline bool isParentOf(const SubclassIdentifier<B>* identifier) |
---|
| 112 | { return this->isParentOf(*identifier); } |
---|
[7401] | 113 | /// Returns true if the object's class is a direct parent of the given type. |
---|
[5929] | 114 | template <class B> inline bool isDirectParentOf(const SubclassIdentifier<B>* identifier) |
---|
| 115 | { return this->isDirectParentOf(*identifier); } |
---|
[1505] | 116 | |
---|
| 117 | bool isA(const OrxonoxClass* object); |
---|
| 118 | bool isExactlyA(const OrxonoxClass* object); |
---|
| 119 | bool isChildOf(const OrxonoxClass* object); |
---|
| 120 | bool isDirectChildOf(const OrxonoxClass* object); |
---|
| 121 | bool isParentOf(const OrxonoxClass* object); |
---|
| 122 | bool isDirectParentOf(const OrxonoxClass* object); |
---|
[7163] | 123 | |
---|
[6524] | 124 | virtual void clone(OrxonoxClass*& item) {} |
---|
[1505] | 125 | |
---|
[7401] | 126 | /// Returns the number of @ref orxonox::SmartPtr "smart pointers" that point to this object. |
---|
[5929] | 127 | inline unsigned int getReferenceCount() const |
---|
| 128 | { return this->referenceCount_; } |
---|
| 129 | |
---|
[3325] | 130 | /** |
---|
| 131 | @brief |
---|
| 132 | Returns a valid pointer of any derived type that is |
---|
| 133 | registered in the class hierarchy. |
---|
| 134 | @return |
---|
| 135 | Returns NULL if the no pointer was found. |
---|
| 136 | */ |
---|
[8351] | 137 | ORX_FORCEINLINE void* getDerivedPointer(unsigned int classID) |
---|
[3325] | 138 | { |
---|
| 139 | for (int i = this->objectPointers_.size() - 1; i >= 0; --i) |
---|
| 140 | { |
---|
| 141 | if (this->objectPointers_[i].first == classID) |
---|
[5929] | 142 | return this->objectPointers_[i].second; |
---|
[3325] | 143 | } |
---|
| 144 | return NULL; |
---|
| 145 | } |
---|
[5929] | 146 | |
---|
[7401] | 147 | /// Version of getDerivedPointer with template |
---|
[8351] | 148 | template <class T> ORX_FORCEINLINE T* getDerivedPointer(unsigned int classID) |
---|
[5929] | 149 | { return static_cast<T*>(this->getDerivedPointer(classID)); } |
---|
[7401] | 150 | /// Const version of getDerivedPointer with template |
---|
[8351] | 151 | template <class T> ORX_FORCEINLINE const T* getDerivedPointer(unsigned int classID) const |
---|
[5929] | 152 | { return const_cast<OrxonoxClass*>(this)->getDerivedPointer<T>(classID); } |
---|
| 153 | |
---|
[6417] | 154 | protected: |
---|
[7401] | 155 | /// 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. |
---|
[6417] | 156 | virtual void preDestroy() {} |
---|
| 157 | |
---|
[5929] | 158 | private: |
---|
[7401] | 159 | /// Increments the reference counter (for smart pointers). |
---|
[5929] | 160 | inline void incrementReferenceCount() |
---|
| 161 | { ++this->referenceCount_; } |
---|
[7401] | 162 | /// Decrements the reference counter (for smart pointers). |
---|
[5929] | 163 | inline void decrementReferenceCount() |
---|
[6417] | 164 | { |
---|
| 165 | --this->referenceCount_; |
---|
| 166 | if (this->referenceCount_ == 0 && this->requestedDestruction_) |
---|
| 167 | this->destroy(); |
---|
| 168 | } |
---|
| 169 | |
---|
[7849] | 170 | /// Register a destruction listener (for example a weak pointer which points to this object). |
---|
| 171 | inline void registerDestructionListener(DestructionListener* pointer) |
---|
| 172 | { this->destructionListeners_.insert(pointer); } |
---|
| 173 | /// Unegister a destruction listener (for example a weak pointer which pointed to this object before). |
---|
| 174 | inline void unregisterDestructionListener(DestructionListener* pointer) |
---|
| 175 | { this->destructionListeners_.erase(pointer); } |
---|
[3325] | 176 | |
---|
[7849] | 177 | Identifier* identifier_; //!< The Identifier of the object |
---|
| 178 | std::set<const Identifier*>* parents_; //!< List of all parents of the object |
---|
| 179 | MetaObjectList* metaList_; //!< MetaObjectList, containing all ObjectLists and ObjectListElements the object is registered in |
---|
| 180 | int referenceCount_; //!< Counts the references from smart pointers to this object |
---|
| 181 | bool requestedDestruction_; //!< Becomes true after someone called delete on this object |
---|
| 182 | std::set<DestructionListener*> destructionListeners_; //!< All destruction listeners (for example weak pointers which point to this object and like to get notified if it dies) |
---|
[5929] | 183 | |
---|
[7401] | 184 | /// 'Fast map' that holds this-pointers of all derived types |
---|
[3325] | 185 | std::vector<std::pair<unsigned int, void*> > objectPointers_; |
---|
[1505] | 186 | }; |
---|
[7163] | 187 | |
---|
[7849] | 188 | /** |
---|
| 189 | @brief This listener is used to inform weak pointers if an object of type OrxonoxClass gets destroyed. |
---|
| 190 | */ |
---|
| 191 | class _CoreExport DestructionListener |
---|
| 192 | { |
---|
| 193 | friend class OrxonoxClass; |
---|
| 194 | |
---|
| 195 | protected: |
---|
[8079] | 196 | virtual ~DestructionListener() {} |
---|
| 197 | |
---|
[7849] | 198 | inline void registerAsDestructionListener(OrxonoxClass* object) |
---|
[7850] | 199 | { if (object) { object->registerDestructionListener(this); } } |
---|
[7849] | 200 | inline void unregisterAsDestructionListener(OrxonoxClass* object) |
---|
[7850] | 201 | { if (object) { object->unregisterDestructionListener(this); } } |
---|
[7849] | 202 | |
---|
| 203 | virtual void objectDeleted() = 0; |
---|
| 204 | }; |
---|
| 205 | |
---|
[7163] | 206 | SUPER_FUNCTION(11, OrxonoxClass, clone, false); |
---|
[1505] | 207 | } |
---|
| 208 | |
---|
| 209 | #endif /* _OrxonoxClass_H__ */ |
---|