Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core6/src/libraries/core/class/Identifiable.h @ 9565

Last change on this file since 9565 was 9565, checked in by landauf, 11 years ago

moved functions and attributes needed to identify the class from OrxonoxClass to Identifiable

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