Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/libraries/core/OrxonoxClass.h @ 7363

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

assigned a group to each header file in the core library

  • Property svn:eol-style set to native
File size: 7.5 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 the OrxonoxClass Class.
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    //! The class all objects and interfaces of the game-logic (not the engine) are derived from.
62    /**
63        The BaseObject and Interfaces are derived with 'virtual public OrxonoxClass' from OrxonoxClass.
64        OrxonoxClass is needed to create the class-hierarchy at startup and to store the Identifier and the MetaObjectList.
65    */
66    class _CoreExport OrxonoxClass
67    {
68        template <class T>
69        friend class ClassIdentifier;
70
71        template <class T>
72        friend class SmartPtr;
73
74        template <class T>
75        friend class WeakPtr;
76
77        public:
78            OrxonoxClass();
79            virtual ~OrxonoxClass();
80
81            void destroy();
82            void unregisterObject();
83
84            /** @brief Function to collect the SetConfigValue-macro calls. */
85            void setConfigValues() {};
86
87            /** @brief Returns the Identifier of the object. @return The Identifier */
88            inline Identifier* getIdentifier() const { return this->identifier_; }
89
90            bool isA(const Identifier* identifier);
91            bool isExactlyA(const Identifier* identifier);
92            bool isChildOf(const Identifier* identifier);
93            bool isDirectChildOf(const Identifier* identifier);
94            bool isParentOf(const Identifier* identifier);
95            bool isDirectParentOf(const Identifier* identifier);
96
97            template <class B> inline bool isA(const SubclassIdentifier<B>* identifier)
98                { return this->isA(*identifier); }
99            template <class B> inline bool isExactlyA(const SubclassIdentifier<B>* identifier)
100                { return this->isExactlyA(*identifier); }
101            template <class B> inline bool isChildOf(const SubclassIdentifier<B>* identifier)
102                { return this->isChildOf(*identifier); }
103            template <class B> inline bool isDirectChildOf(const SubclassIdentifier<B>* identifier)
104                { return this->isDirectChildOf(*identifier); }
105            template <class B> inline bool isParentOf(const SubclassIdentifier<B>* identifier)
106                { return this->isParentOf(*identifier); }
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            virtual void clone(OrxonoxClass*& item) {}
118
119            inline unsigned int getReferenceCount() const
120                { return this->referenceCount_; }
121
122            /**
123            @brief
124                Returns a valid pointer of any derived type that is
125                registered in the class hierarchy.
126            @return
127                Returns NULL if the no pointer was found.
128            */
129            FORCEINLINE void* getDerivedPointer(unsigned int classID)
130            {
131                for (int i = this->objectPointers_.size() - 1; i >= 0; --i)
132                {
133                    if (this->objectPointers_[i].first == classID)
134                        return this->objectPointers_[i].second;
135                }
136                return NULL;
137            }
138
139            //! Version of getDerivedPointer with template
140            template <class T> FORCEINLINE T* getDerivedPointer(unsigned int classID)
141            {   return static_cast<T*>(this->getDerivedPointer(classID));   }
142            //! Const version of getDerivedPointer with template
143            template <class T> FORCEINLINE const T* getDerivedPointer(unsigned int classID) const
144            {   return const_cast<OrxonoxClass*>(this)->getDerivedPointer<T>(classID);   }
145
146        protected:
147            virtual void preDestroy() {}
148
149        private:
150            /** @brief Increments the reference counter (for smart pointers). */
151            inline void incrementReferenceCount()
152                { ++this->referenceCount_; }
153            /** @brief 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            /** @brief Register a weak pointer which points to this object. */
162            template <class T>
163            inline void registerWeakPtr(WeakPtr<T>* pointer)
164                { this->weakPointers_.insert(reinterpret_cast<WeakPtr<OrxonoxClass>*>(pointer)); }
165            /** @brief Unegister a weak pointer which pointed to this object before. */
166            template <class T>
167            inline void unregisterWeakPtr(WeakPtr<T>* pointer)
168                { this->weakPointers_.erase(reinterpret_cast<WeakPtr<OrxonoxClass>*>(pointer)); }
169
170            Identifier* identifier_;                   //!< The Identifier of the object
171            std::set<const Identifier*>* parents_;     //!< List of all parents of the object
172            MetaObjectList* metaList_;                 //!< MetaObjectList, containing all ObjectLists and ObjectListElements the object is registered in
173            int referenceCount_;                       //!< Counts the references from smart pointers to this object
174            bool requestedDestruction_;                //!< Becomes true after someone called delete on this object
175            std::set<WeakPtr<OrxonoxClass>*> weakPointers_; //!< All weak pointers which point to this object (and like to get notified if it dies)
176
177            //! 'Fast map' that holds this-pointers of all derived types
178            std::vector<std::pair<unsigned int, void*> > objectPointers_;
179    };
180
181    SUPER_FUNCTION(11, OrxonoxClass, clone, false);
182
183}
184
185#endif /* _OrxonoxClass_H__ */
Note: See TracBrowser for help on using the repository browser.