Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/lastmanstanding2/src/libraries/core/OrxonoxClass.h @ 7988

Last change on this file since 7988 was 7401, checked in by landauf, 14 years ago

merged doc branch back to trunk

  • Property svn:eol-style set to native
File size: 8.3 KB
Line 
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    @defgroup OrxonoxClass OrxonoxClass
31    @ingroup Class
32*/
33
34/**
35    @file
36    @ingroup Class OrxonoxClass
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*/
42
43#ifndef _OrxonoxClass_H__
44#define _OrxonoxClass_H__
45
46#include "CorePrereqs.h"
47#include "Super.h"
48
49#include <set>
50#include <vector>
51
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
59namespace orxonox
60{
61    /**
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.
67    */
68    class _CoreExport OrxonoxClass
69    {
70        template <class T>
71        friend class ClassIdentifier;
72
73        template <class T>
74        friend class SmartPtr;
75
76        template <class T>
77        friend class WeakPtr;
78
79        public:
80            OrxonoxClass();
81            virtual ~OrxonoxClass();
82
83            void destroy();
84            void unregisterObject();
85
86            /// Function to collect the SetConfigValue-macro calls.
87            void setConfigValues() {};
88
89            /// Returns the Identifier of the object.
90            inline Identifier* getIdentifier() const { return this->identifier_; }
91
92            bool isA(const Identifier* identifier);
93            bool isExactlyA(const Identifier* identifier);
94            bool isChildOf(const Identifier* identifier);
95            bool isDirectChildOf(const Identifier* identifier);
96            bool isParentOf(const Identifier* identifier);
97            bool isDirectParentOf(const Identifier* identifier);
98
99            /// Returns true if the object's class is of the given type or a derivative.
100            template <class B> inline bool isA(const SubclassIdentifier<B>* identifier)
101                { return this->isA(*identifier); }
102            /// Returns true if the object's class is exactly of the given type.
103            template <class B> inline bool isExactlyA(const SubclassIdentifier<B>* identifier)
104                { return this->isExactlyA(*identifier); }
105            /// Returns true if the object's class is a child of the given type.
106            template <class B> inline bool isChildOf(const SubclassIdentifier<B>* identifier)
107                { return this->isChildOf(*identifier); }
108            /// Returns true if the object's class is a direct child of the given type.
109            template <class B> inline bool isDirectChildOf(const SubclassIdentifier<B>* identifier)
110                { return this->isDirectChildOf(*identifier); }
111            /// Returns true if the object's class is a parent of the given type.
112            template <class B> inline bool isParentOf(const SubclassIdentifier<B>* identifier)
113                { return this->isParentOf(*identifier); }
114            /// Returns true if the object's class is a direct parent of the given type.
115            template <class B> inline bool isDirectParentOf(const SubclassIdentifier<B>* identifier)
116                { return this->isDirectParentOf(*identifier); }
117
118            bool isA(const OrxonoxClass* object);
119            bool isExactlyA(const OrxonoxClass* object);
120            bool isChildOf(const OrxonoxClass* object);
121            bool isDirectChildOf(const OrxonoxClass* object);
122            bool isParentOf(const OrxonoxClass* object);
123            bool isDirectParentOf(const OrxonoxClass* object);
124
125            virtual void clone(OrxonoxClass*& item) {}
126
127            /// Returns the number of @ref orxonox::SmartPtr "smart pointers" that point to this object.
128            inline unsigned int getReferenceCount() const
129                { return this->referenceCount_; }
130
131            /**
132            @brief
133                Returns a valid pointer of any derived type that is
134                registered in the class hierarchy.
135            @return
136                Returns NULL if the no pointer was found.
137            */
138            FORCEINLINE void* getDerivedPointer(unsigned int classID)
139            {
140                for (int i = this->objectPointers_.size() - 1; i >= 0; --i)
141                {
142                    if (this->objectPointers_[i].first == classID)
143                        return this->objectPointers_[i].second;
144                }
145                return NULL;
146            }
147
148            /// Version of getDerivedPointer with template
149            template <class T> FORCEINLINE T* getDerivedPointer(unsigned int classID)
150            {   return static_cast<T*>(this->getDerivedPointer(classID));   }
151            /// Const version of getDerivedPointer with template
152            template <class T> FORCEINLINE const T* getDerivedPointer(unsigned int classID) const
153            {   return const_cast<OrxonoxClass*>(this)->getDerivedPointer<T>(classID);   }
154
155        protected:
156            /// 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.
157            virtual void preDestroy() {}
158
159        private:
160            /// Increments the reference counter (for smart pointers).
161            inline void incrementReferenceCount()
162                { ++this->referenceCount_; }
163            /// Decrements the reference counter (for smart pointers).
164            inline void decrementReferenceCount()
165            {
166                --this->referenceCount_;
167                if (this->referenceCount_ == 0 && this->requestedDestruction_)
168                    this->destroy();
169            }
170
171            /// Register a weak pointer which points to this object.
172            template <class T>
173            inline void registerWeakPtr(WeakPtr<T>* pointer)
174                { this->weakPointers_.insert(reinterpret_cast<WeakPtr<OrxonoxClass>*>(pointer)); }
175            /// Unegister a weak pointer which pointed to this object before.
176            template <class T>
177            inline void unregisterWeakPtr(WeakPtr<T>* pointer)
178                { this->weakPointers_.erase(reinterpret_cast<WeakPtr<OrxonoxClass>*>(pointer)); }
179
180            Identifier* identifier_;                            //!< The Identifier of the object
181            std::set<const Identifier*>* parents_;              //!< List of all parents of the object
182            MetaObjectList* metaList_;                          //!< MetaObjectList, containing all ObjectLists and ObjectListElements the object is registered in
183            int referenceCount_;                                //!< Counts the references from smart pointers to this object
184            bool requestedDestruction_;                         //!< Becomes true after someone called delete on this object
185            std::set<WeakPtr<OrxonoxClass>*> weakPointers_;     //!< All weak pointers which point to this object (and like to get notified if it dies)
186
187            /// 'Fast map' that holds this-pointers of all derived types
188            std::vector<std::pair<unsigned int, void*> > objectPointers_;
189    };
190
191    SUPER_FUNCTION(11, OrxonoxClass, clone, false);
192
193}
194
195#endif /* _OrxonoxClass_H__ */
Note: See TracBrowser for help on using the repository browser.