Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/libraries/core/OrxonoxClass.h @ 5821

Last change on this file since 5821 was 5805, checked in by landauf, 16 years ago
  • Enhanced SmartPtr: a) It stores now two pointers, one OrxonoxClass* (for reference counting) and one T* (for normal use). b) Incrementing the reference is now optional in the constructor for raw pointers. This is needed because Scene has a selfreference but should still be destroyable.
  • Changed BaseObject to store a SmartPtr to it's Scene instead of a normal pointer.
  • Changed setScene and getScene to deal directly with SmartPtr instead of a Scene* pointer to avoid casting-to-OrxonoxClass issues.
  • Fixed two problems with lost SceneManagers in CameraManger: a) added a SmartPtr to the Scene for the fallback camera b) added a call to GUIManager in the destructor to release the scene manager
  • Enabled unloading in GSLevel again (after years). Works if playing without bots.
  • Property svn:eol-style set to native
File size: 6.0 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    @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
45namespace orxonox
46{
47    //! The class all objects and interfaces of the game-logic (not the engine) are derived from.
48    /**
49        The BaseObject and Interfaces are derived with 'virtual public OrxonoxClass' from OrxonoxClass.
50        OrxonoxClass is needed to create the class-hierarchy at startup and to store the Identifier and the MetaObjectList.
51    */
52    class _CoreExport OrxonoxClass
53    {
54        template <class T>
55        friend class ClassIdentifier;
56
57        template <class T>
58        friend class SmartPtr;
59
60        public:
61            OrxonoxClass();
62            virtual ~OrxonoxClass();
63
64            void destroy();
65
66            /** @brief Function to collect the SetConfigValue-macro calls. */
67            void setConfigValues() {};
68
69            /** @brief Returns the Identifier of the object. @return The Identifier */
70            inline Identifier* getIdentifier() const { return this->identifier_; }
71
72            bool isA(const Identifier* identifier);
73            bool isExactlyA(const Identifier* identifier);
74            bool isChildOf(const Identifier* identifier);
75            bool isDirectChildOf(const Identifier* identifier);
76            bool isParentOf(const Identifier* identifier);
77            bool isDirectParentOf(const Identifier* identifier);
78
79            template <class B> inline bool isA(const SubclassIdentifier<B>* identifier)
80                { return this->isA(*identifier); }
81            template <class B> inline bool isExactlyA(const SubclassIdentifier<B>* identifier)
82                { return this->isExactlyA(*identifier); }
83            template <class B> inline bool isChildOf(const SubclassIdentifier<B>* identifier)
84                { return this->isChildOf(*identifier); }
85            template <class B> inline bool isDirectChildOf(const SubclassIdentifier<B>* identifier)
86                { return this->isDirectChildOf(*identifier); }
87            template <class B> inline bool isParentOf(const SubclassIdentifier<B>* identifier)
88                { return this->isParentOf(*identifier); }
89            template <class B> inline bool isDirectParentOf(const SubclassIdentifier<B>* identifier)
90                { return this->isDirectParentOf(*identifier); }
91
92            bool isA(const OrxonoxClass* object);
93            bool isExactlyA(const OrxonoxClass* object);
94            bool isChildOf(const OrxonoxClass* object);
95            bool isDirectChildOf(const OrxonoxClass* object);
96            bool isParentOf(const OrxonoxClass* object);
97            bool isDirectParentOf(const OrxonoxClass* object);
98
99            inline unsigned int getReferenceCount() const
100                { return this->referenceCount_; }
101
102            /**
103            @brief
104                Returns a valid pointer of any derived type that is
105                registered in the class hierarchy.
106            @return
107                Returns NULL if the no pointer was found.
108            */
109            template <class T>
110            FORCEINLINE T* getDerivedPointer(unsigned int classID)
111            {
112                for (int i = this->objectPointers_.size() - 1; i >= 0; --i)
113                {
114                    if (this->objectPointers_[i].first == classID)
115                        return static_cast<T*>(this->objectPointers_[i].second);
116                }
117                return NULL;
118            }
119            //! Const version of getDerivedPointer
120            template <class T>
121            FORCEINLINE const T* getDerivedPointer(unsigned int classID) const
122            {
123                return const_cast<OrxonoxClass*>(this)->getDerivedPointer<T>(classID);
124            }
125
126        private:
127            /** @brief Increments the reference counter (for smart pointers). */
128            inline void incrementReferenceCount()
129                { ++this->referenceCount_; }
130            /** @brief Decrements the reference counter (for smart pointers). */
131            inline void decrementReferenceCount()
132                { --this->referenceCount_; if (this->referenceCount_ == 0 && this->requestedDestruction_) { delete this; } }
133
134            Identifier* identifier_;                   //!< The Identifier of the object
135            std::set<const Identifier*>* parents_;     //!< List of all parents of the object
136            MetaObjectList* metaList_;                 //!< MetaObjectList, containing all ObjectLists and ObjectListElements the object is registered in
137            int referenceCount_;                       //!< Counts the references from smart pointers to this object
138            bool requestedDestruction_;                //!< Becomes true after someone called delete on this object
139
140            //! 'Fast map' that holds this-pointers of all derived types
141            std::vector<std::pair<unsigned int, void*> > objectPointers_;
142    };
143}
144
145#endif /* _OrxonoxClass_H__ */
Note: See TracBrowser for help on using the repository browser.