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 | /** |
---|
30 | @file |
---|
31 | @brief Declaration of the OrxonoxClass Class. |
---|
32 | |
---|
33 | All objects and interfaces of the game-logic (not the engine) are derived from OrxonoxClass. |
---|
34 | It stores the Identifier and the MetaObjectList and has all needed functions to create and use the class-hierarchy. |
---|
35 | */ |
---|
36 | |
---|
37 | #ifndef _OrxonoxClass_H__ |
---|
38 | #define _OrxonoxClass_H__ |
---|
39 | |
---|
40 | #include "CorePrereqs.h" |
---|
41 | |
---|
42 | #include <set> |
---|
43 | #include <vector> |
---|
44 | |
---|
45 | /** |
---|
46 | @def CCOUT |
---|
47 | Acts almost exactly like COUT(x), but prepends "ClassName: " |
---|
48 | */ |
---|
49 | #define CCOUT(level) \ |
---|
50 | COUT(level) << this->getIdentifier()->getName() << ": " |
---|
51 | |
---|
52 | namespace orxonox |
---|
53 | { |
---|
54 | //! The class all objects and interfaces of the game-logic (not the engine) are derived from. |
---|
55 | /** |
---|
56 | The BaseObject and Interfaces are derived with 'virtual public OrxonoxClass' from OrxonoxClass. |
---|
57 | OrxonoxClass is needed to create the class-hierarchy at startup and to store the Identifier and the MetaObjectList. |
---|
58 | */ |
---|
59 | class _CoreExport OrxonoxClass |
---|
60 | { |
---|
61 | template <class T> |
---|
62 | friend class ClassIdentifier; |
---|
63 | |
---|
64 | template <class T> |
---|
65 | friend class SmartPtr; |
---|
66 | |
---|
67 | template <class T> |
---|
68 | friend class WeakPtr; |
---|
69 | |
---|
70 | public: |
---|
71 | OrxonoxClass(); |
---|
72 | virtual ~OrxonoxClass(); |
---|
73 | |
---|
74 | void destroy(); |
---|
75 | void unregisterObject(); |
---|
76 | |
---|
77 | /** @brief Function to collect the SetConfigValue-macro calls. */ |
---|
78 | void setConfigValues() {}; |
---|
79 | |
---|
80 | /** @brief Returns the Identifier of the object. @return The Identifier */ |
---|
81 | inline Identifier* getIdentifier() const { return this->identifier_; } |
---|
82 | |
---|
83 | bool isA(const Identifier* identifier); |
---|
84 | bool isExactlyA(const Identifier* identifier); |
---|
85 | bool isChildOf(const Identifier* identifier); |
---|
86 | bool isDirectChildOf(const Identifier* identifier); |
---|
87 | bool isParentOf(const Identifier* identifier); |
---|
88 | bool isDirectParentOf(const Identifier* identifier); |
---|
89 | |
---|
90 | template <class B> inline bool isA(const SubclassIdentifier<B>* identifier) |
---|
91 | { return this->isA(*identifier); } |
---|
92 | template <class B> inline bool isExactlyA(const SubclassIdentifier<B>* identifier) |
---|
93 | { return this->isExactlyA(*identifier); } |
---|
94 | template <class B> inline bool isChildOf(const SubclassIdentifier<B>* identifier) |
---|
95 | { return this->isChildOf(*identifier); } |
---|
96 | template <class B> inline bool isDirectChildOf(const SubclassIdentifier<B>* identifier) |
---|
97 | { return this->isDirectChildOf(*identifier); } |
---|
98 | template <class B> inline bool isParentOf(const SubclassIdentifier<B>* identifier) |
---|
99 | { return this->isParentOf(*identifier); } |
---|
100 | template <class B> inline bool isDirectParentOf(const SubclassIdentifier<B>* identifier) |
---|
101 | { return this->isDirectParentOf(*identifier); } |
---|
102 | |
---|
103 | bool isA(const OrxonoxClass* object); |
---|
104 | bool isExactlyA(const OrxonoxClass* object); |
---|
105 | bool isChildOf(const OrxonoxClass* object); |
---|
106 | bool isDirectChildOf(const OrxonoxClass* object); |
---|
107 | bool isParentOf(const OrxonoxClass* object); |
---|
108 | bool isDirectParentOf(const OrxonoxClass* object); |
---|
109 | |
---|
110 | inline unsigned int getReferenceCount() const |
---|
111 | { return this->referenceCount_; } |
---|
112 | |
---|
113 | /** |
---|
114 | @brief |
---|
115 | Returns a valid pointer of any derived type that is |
---|
116 | registered in the class hierarchy. |
---|
117 | @return |
---|
118 | Returns NULL if the no pointer was found. |
---|
119 | */ |
---|
120 | FORCEINLINE void* getDerivedPointer(unsigned int classID) |
---|
121 | { |
---|
122 | for (int i = this->objectPointers_.size() - 1; i >= 0; --i) |
---|
123 | { |
---|
124 | if (this->objectPointers_[i].first == classID) |
---|
125 | return this->objectPointers_[i].second; |
---|
126 | } |
---|
127 | return NULL; |
---|
128 | } |
---|
129 | |
---|
130 | //! Version of getDerivedPointer with template |
---|
131 | template <class T> FORCEINLINE T* getDerivedPointer(unsigned int classID) |
---|
132 | { return static_cast<T*>(this->getDerivedPointer(classID)); } |
---|
133 | //! Const version of getDerivedPointer with template |
---|
134 | template <class T> FORCEINLINE const T* getDerivedPointer(unsigned int classID) const |
---|
135 | { return const_cast<OrxonoxClass*>(this)->getDerivedPointer<T>(classID); } |
---|
136 | |
---|
137 | protected: |
---|
138 | virtual void preDestroy() {} |
---|
139 | |
---|
140 | private: |
---|
141 | /** @brief Increments the reference counter (for smart pointers). */ |
---|
142 | inline void incrementReferenceCount() |
---|
143 | { ++this->referenceCount_; } |
---|
144 | /** @brief Decrements the reference counter (for smart pointers). */ |
---|
145 | inline void decrementReferenceCount() |
---|
146 | { |
---|
147 | --this->referenceCount_; |
---|
148 | if (this->referenceCount_ == 0 && this->requestedDestruction_) |
---|
149 | this->destroy(); |
---|
150 | } |
---|
151 | |
---|
152 | /** @brief Register a weak pointer which points to this object. */ |
---|
153 | template <class T> |
---|
154 | inline void registerWeakPtr(WeakPtr<T>* pointer) |
---|
155 | { this->weakPointers_.insert(reinterpret_cast<WeakPtr<OrxonoxClass>*>(pointer)); } |
---|
156 | /** @brief Unegister a weak pointer which pointed to this object before. */ |
---|
157 | template <class T> |
---|
158 | inline void unregisterWeakPtr(WeakPtr<T>* pointer) |
---|
159 | { this->weakPointers_.erase(reinterpret_cast<WeakPtr<OrxonoxClass>*>(pointer)); } |
---|
160 | |
---|
161 | Identifier* identifier_; //!< The Identifier of the object |
---|
162 | std::set<const Identifier*>* parents_; //!< List of all parents of the object |
---|
163 | MetaObjectList* metaList_; //!< MetaObjectList, containing all ObjectLists and ObjectListElements the object is registered in |
---|
164 | int referenceCount_; //!< Counts the references from smart pointers to this object |
---|
165 | bool requestedDestruction_; //!< Becomes true after someone called delete on this object |
---|
166 | std::set<WeakPtr<OrxonoxClass>*> weakPointers_; //!< All weak pointers which point to this object (and like to get notified if it dies) |
---|
167 | |
---|
168 | //! 'Fast map' that holds this-pointers of all derived types |
---|
169 | std::vector<std::pair<unsigned int, void*> > objectPointers_; |
---|
170 | }; |
---|
171 | } |
---|
172 | |
---|
173 | #endif /* _OrxonoxClass_H__ */ |
---|