[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 | /** |
---|
[2171] | 30 | @file |
---|
[9565] | 31 | @ingroup Class Identifier |
---|
| 32 | @brief Declaration of Identifiable, the base of all classes that should own an Identifier. |
---|
[1505] | 33 | |
---|
[9574] | 34 | It stores the Identifier and has all needed functions to create and use the class-hierarchy. |
---|
[1505] | 35 | */ |
---|
| 36 | |
---|
[9565] | 37 | #ifndef _Identifiable_H__ |
---|
| 38 | #define _Identifiable_H__ |
---|
[1505] | 39 | |
---|
[9563] | 40 | #include "core/CorePrereqs.h" |
---|
[3327] | 41 | |
---|
[1505] | 42 | #include <set> |
---|
[3327] | 43 | #include <vector> |
---|
[1505] | 44 | |
---|
| 45 | namespace orxonox |
---|
| 46 | { |
---|
| 47 | /** |
---|
[9574] | 48 | @brief Identifiable is needed to create the class-hierarchy at startup and to store the Identifier. |
---|
[1505] | 49 | */ |
---|
[9565] | 50 | class _CoreExport Identifiable |
---|
[1505] | 51 | { |
---|
[3325] | 52 | template <class T> |
---|
| 53 | friend class ClassIdentifier; |
---|
| 54 | |
---|
[1505] | 55 | public: |
---|
[9565] | 56 | Identifiable(); |
---|
[9667] | 57 | virtual ~Identifiable() {} |
---|
[1505] | 58 | |
---|
[7401] | 59 | /// Returns the Identifier of the object. |
---|
[1505] | 60 | inline Identifier* getIdentifier() const { return this->identifier_; } |
---|
| 61 | |
---|
| 62 | bool isA(const Identifier* identifier); |
---|
| 63 | bool isExactlyA(const Identifier* identifier); |
---|
| 64 | bool isChildOf(const Identifier* identifier); |
---|
| 65 | bool isDirectChildOf(const Identifier* identifier); |
---|
| 66 | bool isParentOf(const Identifier* identifier); |
---|
| 67 | bool isDirectParentOf(const Identifier* identifier); |
---|
| 68 | |
---|
[7401] | 69 | /// Returns true if the object's class is of the given type or a derivative. |
---|
[5929] | 70 | template <class B> inline bool isA(const SubclassIdentifier<B>* identifier) |
---|
| 71 | { return this->isA(*identifier); } |
---|
[7401] | 72 | /// Returns true if the object's class is exactly of the given type. |
---|
[5929] | 73 | template <class B> inline bool isExactlyA(const SubclassIdentifier<B>* identifier) |
---|
| 74 | { return this->isExactlyA(*identifier); } |
---|
[7401] | 75 | /// Returns true if the object's class is a child of the given type. |
---|
[5929] | 76 | template <class B> inline bool isChildOf(const SubclassIdentifier<B>* identifier) |
---|
| 77 | { return this->isChildOf(*identifier); } |
---|
[7401] | 78 | /// Returns true if the object's class is a direct child of the given type. |
---|
[5929] | 79 | template <class B> inline bool isDirectChildOf(const SubclassIdentifier<B>* identifier) |
---|
| 80 | { return this->isDirectChildOf(*identifier); } |
---|
[7401] | 81 | /// Returns true if the object's class is a parent of the given type. |
---|
[5929] | 82 | template <class B> inline bool isParentOf(const SubclassIdentifier<B>* identifier) |
---|
| 83 | { return this->isParentOf(*identifier); } |
---|
[7401] | 84 | /// Returns true if the object's class is a direct parent of the given type. |
---|
[5929] | 85 | template <class B> inline bool isDirectParentOf(const SubclassIdentifier<B>* identifier) |
---|
| 86 | { return this->isDirectParentOf(*identifier); } |
---|
[1505] | 87 | |
---|
[9565] | 88 | bool isA(const Identifiable* object); |
---|
| 89 | bool isExactlyA(const Identifiable* object); |
---|
| 90 | bool isChildOf(const Identifiable* object); |
---|
| 91 | bool isDirectChildOf(const Identifiable* object); |
---|
| 92 | bool isParentOf(const Identifiable* object); |
---|
| 93 | bool isDirectParentOf(const Identifiable* object); |
---|
[7163] | 94 | |
---|
[3325] | 95 | /** |
---|
| 96 | @brief |
---|
| 97 | Returns a valid pointer of any derived type that is |
---|
| 98 | registered in the class hierarchy. |
---|
| 99 | @return |
---|
[11071] | 100 | Returns nullptr if the no pointer was found. |
---|
[3325] | 101 | */ |
---|
[8351] | 102 | ORX_FORCEINLINE void* getDerivedPointer(unsigned int classID) |
---|
[3325] | 103 | { |
---|
| 104 | for (int i = this->objectPointers_.size() - 1; i >= 0; --i) |
---|
| 105 | { |
---|
| 106 | if (this->objectPointers_[i].first == classID) |
---|
[5929] | 107 | return this->objectPointers_[i].second; |
---|
[3325] | 108 | } |
---|
[11071] | 109 | return nullptr; |
---|
[3325] | 110 | } |
---|
[5929] | 111 | |
---|
[7401] | 112 | /// Version of getDerivedPointer with template |
---|
[8351] | 113 | template <class T> ORX_FORCEINLINE T* getDerivedPointer(unsigned int classID) |
---|
[5929] | 114 | { return static_cast<T*>(this->getDerivedPointer(classID)); } |
---|
[7401] | 115 | /// Const version of getDerivedPointer with template |
---|
[8351] | 116 | template <class T> ORX_FORCEINLINE const T* getDerivedPointer(unsigned int classID) const |
---|
[9565] | 117 | { return const_cast<Identifiable*>(this)->getDerivedPointer<T>(classID); } |
---|
[5929] | 118 | |
---|
| 119 | private: |
---|
[9572] | 120 | Identifier* identifier_; //!< The Identifier of the object |
---|
[5929] | 121 | |
---|
[7401] | 122 | /// 'Fast map' that holds this-pointers of all derived types |
---|
[11071] | 123 | std::vector<std::pair<unsigned int, void*>> objectPointers_; |
---|
[1505] | 124 | }; |
---|
| 125 | } |
---|
| 126 | |
---|
[9565] | 127 | #endif /* _Identifiable_H__ */ |
---|