Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/core/class/Identifier.h @ 10460

Last change on this file since 10460 was 10422, checked in by landauf, 9 years ago

details

  • Property svn:eol-style set to native
File size: 19.1 KB
RevLine 
[790]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1505]3 *                    > www.orxonox.net <
[790]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
[871]29/**
[7401]30    @defgroup Identifier Identifier
31    @ingroup Class
32*/
33
34/**
[2171]35    @file
[7401]36    @ingroup Class Identifier
37    @brief Declaration of Identifier, definition of ClassIdentifier<T>; used to identify the class of an object.
[790]38
[7401]39    @anchor IdentifierExample
[790]40
[7401]41    An Identifier "identifies" the class of an object. It contains different information about
42    the class: Its name and ID, a list of all instances of this class, a factory to create new
43    instances of this class, and more.
[790]44
[7401]45    It also contains information about the inheritance of this class: It stores a list of the
46    Identifiers of all parent-classes as well as a list of all child-classes. These relationships
47    can be tested using functions like @c isA(), @c isChildOf(), @c isParentOf(), and more.
[790]48
[7401]49    Every Identifier is in fact a ClassIdentifier<T> (where T is the class that is identified
50    by the Identifier), Identifier is just the common base-class.
51
52    Example:
53    @code
54    MyClass* object = new MyClass();                                            // create an instance of MyClass
55
56    object->getIdentifier()->getName();                                         // returns "MyClass"
57
[9667]58    Identifiable* other = object->getIdentifier()->fabricate(0);                // fabricates a new instance of MyClass
[7401]59
60
61    // test the class hierarchy
62    object->getIdentifier()->isA(Class(MyClass));                               // returns true
63    object->isA(Class(MyClass));                                                // returns true (short version)
64
65    object->isA(Class(BaseClass));                                              // returns true if MyClass is a child of BaseClass
66
67    Class(ChildClass)->isChildOf(object->getIdentifier());                      // returns true if ChildClass is a child of MyClass
68    @endcode
[790]69*/
70
71#ifndef _Identifier_H__
72#define _Identifier_H__
[1052]73
[9563]74#include "core/CorePrereqs.h"
[1062]75
[3196]76#include <cassert>
77#include <map>
[1052]78#include <set>
[790]79#include <string>
[1639]80#include <typeinfo>
[7266]81#include <loki/TypeTraits.h>
[790]82
[8858]83#include "util/Output.h"
[10395]84#include "util/OrxAssert.h"
[9563]85#include "core/object/ObjectList.h"
[9667]86#include "core/object/Listable.h"
87#include "core/object/Context.h"
88#include "core/object/Destroyable.h"
89#include "core/object/WeakPtr.h"
[9564]90#include "IdentifierManager.h"
[5781]91#include "Super.h"
[790]92
93namespace orxonox
94{
95    // ###############################
96    // ###       Identifier        ###
97    // ###############################
98    /**
[7401]99        @brief The Identifier is used to identify the class of an object and to store information about the class.
[790]100
[7401]101        Each Identifier stores information about one class. The Identifier can then be used to identify
102        this class. On the other hand it's also possible to get the corresponding Identifier of a class,
103        for example by using the macro Class().
[790]104
[7401]105        @see See @ref IdentifierExample "Identifier.h" for more information and some examples.
106
107        @note You can't directly create an Identifier, it's just the base-class of ClassIdentifier<T>.
[790]108    */
[9667]109    class _CoreExport Identifier : public Destroyable
[790]110    {
[9667]111        public:
[10395]112            Identifier(const std::string& name, Factory* factory, bool bLoadable);
[9667]113            Identifier(const Identifier& identifier); // don't copy
114            virtual ~Identifier();
[9564]115
[7401]116            /// Returns the name of the class the Identifier belongs to.
[5929]117            inline const std::string& getName() const { return this->name_; }
[790]118
[10399]119            /// Returns the type_info of the class as it is returned by typeid(T)
120            virtual const std::type_info& getTypeInfo() = 0;
[9667]121
[7401]122            /// Returns the network ID to identify a class through the network.
[8706]123            inline uint32_t getNetworkID() const { return this->networkID_; }
[5929]124            void setNetworkID(uint32_t id);
[790]125
[7401]126            /// Returns the unique ID of the class.
[8351]127            ORX_FORCEINLINE unsigned int getClassID() const { return this->classID_; }
[5929]128
[7401]129            /// Returns true if the Identifier has a Factory.
[5929]130            inline bool hasFactory() const { return (this->factory_ != 0); }
[790]131
[9667]132            Identifiable* fabricate(Context* context);
[5929]133
[7401]134            /// Returns true if the class can be loaded through XML.
[5929]135            inline bool isLoadable() const { return this->bLoadable_; }
136
[10374]137            /// Returns true if child classes should inherit virtually from this class.
138            inline bool isVirtualBase() const { return this->bIsVirtualBase_; }
139            /// Defines if child classes should inherit virtually from this class.
140            inline void setVirtualBase(bool bIsVirtualBase) { this->bIsVirtualBase_ = bIsVirtualBase; }
141
[9667]142            /// Returns true if the Identifier was completely initialized.
143            inline bool isInitialized() const { return this->bInitialized_; }
144
145
146            /////////////////////////////
147            ////// Class Hierarchy //////
148            /////////////////////////////
149            Identifier& inheritsFrom(Identifier* directParent);
150
[10372]151            void initializeParents(const std::list<const Identifier*>& initializationTrace);
[9667]152            void finishInitialization();
[10403]153            void reset();
[9667]154
[790]155            bool isA(const Identifier* identifier) const;
[871]156            bool isExactlyA(const Identifier* identifier) const;
[1052]157            bool isChildOf(const Identifier* identifier) const;
[871]158            bool isDirectChildOf(const Identifier* identifier) const;
[1052]159            bool isParentOf(const Identifier* identifier) const;
[871]160            bool isDirectParentOf(const Identifier* identifier) const;
[790]161
[10373]162            /// Returns the direct parents of the class the Identifier belongs to.
163            inline const std::list<const Identifier*>& getDirectParents() const { return this->directParents_; }
[7401]164            /// Returns the parents of the class the Identifier belongs to.
[10372]165            inline const std::list<const Identifier*>& getParents() const { return this->parents_; }
[10373]166
167            /// Returns the direct children the class the Identifier belongs to.
168            inline const std::set<const Identifier*>& getDirectChildren() const { return this->directChildren_; }
[7401]169            /// Returns the children of the class the Identifier belongs to.
[5929]170            inline const std::set<const Identifier*>& getChildren() const { return this->children_; }
[1052]171
172
[5929]173            /////////////////////////
174            ///// Config Values /////
175            /////////////////////////
176            virtual void updateConfigValues(bool updateChildren = true) const = 0;
177
[7401]178            /// Returns true if this class has at least one config value.
[5929]179            inline bool hasConfigValues() const { return this->bHasConfigValues_; }
180
181            void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container);
182            ConfigValueContainer* getConfigValueContainer(const std::string& varname);
[1052]183
[5929]184
185            ///////////////////
186            ///// XMLPort /////
187            ///////////////////
[7401]188            /// Returns the map that stores all XMLPort params.
[5781]189            inline const std::map<std::string, XMLPortParamContainer*>& getXMLPortParamMap() const { return this->xmlportParamContainers_; }
[7401]190            /// Returns a const_iterator to the beginning of the map that stores all XMLPort params.
[5781]191            inline std::map<std::string, XMLPortParamContainer*>::const_iterator getXMLPortParamMapBegin() const { return this->xmlportParamContainers_.begin(); }
[7401]192            /// Returns a const_iterator to the end of the map that stores all XMLPort params.
[5781]193            inline std::map<std::string, XMLPortParamContainer*>::const_iterator getXMLPortParamMapEnd() const { return this->xmlportParamContainers_.end(); }
194
[7401]195            /// Returns the map that stores all XMLPort objects.
[5781]196            inline const std::map<std::string, XMLPortObjectContainer*>& getXMLPortObjectMap() const { return this->xmlportObjectContainers_; }
[7401]197            /// Returns a const_iterator to the beginning of the map that stores all XMLPort objects.
[5781]198            inline std::map<std::string, XMLPortObjectContainer*>::const_iterator getXMLPortObjectMapBegin() const { return this->xmlportObjectContainers_.begin(); }
[7401]199            /// Returns a const_iterator to the end of the map that stores all XMLPort objects.
[5781]200            inline std::map<std::string, XMLPortObjectContainer*>::const_iterator getXMLPortObjectMapEnd() const { return this->xmlportObjectContainers_.end(); }
201
202            void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container);
203            XMLPortParamContainer* getXMLPortParamContainer(const std::string& paramname);
204
205            void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container);
206            XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname);
207
[10363]208            virtual bool canDynamicCastObjectToIdentifierClass(Identifiable* object) const = 0;
[5781]209
[10400]210            static bool initConfigValues_s; // TODO: this is a hack - remove it as soon as possible
211
[1052]212        protected:
[5781]213            virtual void createSuperFunctionCaller() const = 0;
[1747]214
215        private:
[10377]216            void verifyIdentifierTrace() const;
[10376]217            void addIfNotExists(std::list<const Identifier*>& list, const Identifier* identifierToAdd) const;
218
[10373]219            std::list<const Identifier*> directParents_;                    //!< The direct parents of the class the Identifier belongs to (sorted by their order of initialization)
[10372]220            std::list<const Identifier*> parents_;                          //!< The parents of the class the Identifier belongs to (sorted by their order of initialization)
[790]221
[9667]222            std::set<const Identifier*> directChildren_;                   //!< The direct children of the class the Identifier belongs to
[10373]223            std::set<const Identifier*> children_;                         //!< The children of the class the Identifier belongs to
[1052]224
[9667]225            bool bInitialized_;                                            //!< Is true if the Identifier was completely initialized
[5781]226            bool bLoadable_;                                               //!< False = it's not permitted to load the object through XML
[10374]227            bool bIsVirtualBase_;                                          //!< If true, it is recommended to inherit virtually from this class. This changes the order of initialization of child classes, thus this information is necessary to check the class hierarchy.
[1052]228            std::string name_;                                             //!< The name of the class the Identifier belongs to
[5929]229            Factory* factory_;                                             //!< The Factory, able to create new objects of the given class (if available)
[5781]230            uint32_t networkID_;                                           //!< The network ID to identify a class through the network
[3325]231            const unsigned int classID_;                                   //!< Uniquely identifies a class (might not be the same as the networkID_)
[1052]232
233            bool bHasConfigValues_;                                        //!< True if this class has at least one assigned config value
234            std::map<std::string, ConfigValueContainer*> configValues_;    //!< A map to link the string of configurable variables with their ConfigValueContainer
[5781]235
236            std::map<std::string, XMLPortParamContainer*> xmlportParamContainers_;     //!< All loadable parameters
237            std::map<std::string, XMLPortObjectContainer*> xmlportObjectContainers_;   //!< All attachable objects
[790]238    };
239
[1052]240    _CoreExport std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list);
[790]241
[1052]242
[790]243    // ###############################
244    // ###     ClassIdentifier     ###
245    // ###############################
246    /**
[7401]247        @brief The ClassIdentifier is derived from Identifier and holds all class-specific functions and variables the Identifier cannot have.
248
249        ClassIdentifier is a Singleton, which means that only one ClassIdentifier for a given type T exists.
[5695]250        This makes it possible to store information about a class, sharing them with all
[1052]251        objects of that class without defining static variables in every class.
252
253        To be really sure that not more than exactly one object exists (even with libraries),
[7401]254        ClassIdentifiers are stored in a static map in Identifier.
[790]255    */
256    template <class T>
257    class ClassIdentifier : public Identifier
[1052]258    {
[7401]259        #ifndef DOXYGEN_SHOULD_SKIP_THIS
260          #define SUPER_INTRUSIVE_DECLARATION_INCLUDE
261          #include "Super.h"
262        #endif
[5781]263
[790]264        public:
[10395]265            ClassIdentifier(const std::string& name, Factory* factory, bool bLoadable) : Identifier(name, factory, bLoadable)
266            {
267                OrxVerify(ClassIdentifier<T>::classIdentifier_s == NULL, "Assertion failed in ClassIdentifier of type " << typeid(T).name());
268                ClassIdentifier<T>::classIdentifier_s = this;
[1052]269
[10395]270                SuperFunctionInitialization<0, T>::initialize(this);
271            }
272            ~ClassIdentifier()
273            {
274                SuperFunctionDestruction<0, T>::destroy(this);
275            }
276
[9667]277            bool initializeObject(T* object);
[3325]278
[9586]279            void setConfigValues(T* object, Configurable*) const;
280            void setConfigValues(T* object, Identifiable*) const;
281
[9572]282            void addObjectToList(T* object, Listable*);
283            void addObjectToList(T* object, Identifiable*);
284
[9667]285            virtual void updateConfigValues(bool updateChildren = true) const;
[1052]286
[10399]287            virtual const std::type_info& getTypeInfo()
288                { return typeid(T); }
[9667]289
[10363]290            virtual bool canDynamicCastObjectToIdentifierClass(Identifiable* object) const
[10361]291                { return dynamic_cast<T*>(object) != 0; }
292
[10395]293            static ClassIdentifier<T>* getIdentifier();
294
[790]295        private:
296            ClassIdentifier(const ClassIdentifier<T>& identifier) {}    // don't copy
297
[9667]298            void updateConfigValues(bool updateChildren, Listable*) const;
299            void updateConfigValues(bool updateChildren, Identifiable*) const;
300
301            static WeakPtr<ClassIdentifier<T> > classIdentifier_s;
[790]302    };
303
[1543]304    template <class T>
[9667]305    WeakPtr<ClassIdentifier<T> > ClassIdentifier<T>::classIdentifier_s;
[1543]306
[790]307    /**
[1747]308        @brief Returns the only instance of this class.
[1543]309        @return The unique Identifier
310    */
311    template <class T>
[10395]312    /*static*/ inline ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier()
[1543]313    {
[10395]314        if (ClassIdentifier<T>::classIdentifier_s == NULL)
[10399]315            ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*) IdentifierManager::getInstance().getIdentifierByTypeInfo(typeid(T));
[1543]316
[10422]317        OrxVerify(ClassIdentifier<T>::classIdentifier_s != NULL, "Did you forget to register the class of type " << typeid(T).name() << "?");
[1543]318        return ClassIdentifier<T>::classIdentifier_s;
319    }
320
321    /**
[790]322        @brief Adds an object of the given type to the ObjectList.
323        @param object The object to add
324    */
325    template <class T>
[9667]326    bool ClassIdentifier<T>::initializeObject(T* object)
[790]327    {
[9667]328        orxout(verbose, context::object_list) << "Register Object: " << this->getName() << endl;
[3325]329
330        object->identifier_ = this;
[9667]331        if (IdentifierManager::getInstance().isCreatingHierarchy())
[3325]332        {
[9667]333            IdentifierManager::getInstance().createdObject(object);
[3325]334
[10400]335            if (Identifier::initConfigValues_s)
336                this->setConfigValues(object, object);
337
[3325]338            return true;
339        }
340        else
341        {
[9572]342            this->addObjectToList(object, object);
[3325]343
[9566]344            // Add pointer of type T to the map in the Identifiable instance that enables "dynamic_casts"
[3333]345            object->objectPointers_.push_back(std::make_pair(this->getClassID(), static_cast<void*>(object)));
[3325]346            return false;
347        }
[790]348    }
349
350    /**
[9586]351     * @brief Only configures the object if is a @ref Configurable
352     */
353    template <class T>
354    void ClassIdentifier<T>::setConfigValues(T* object, Configurable*) const
355    {
356        object->setConfigValues();
357    }
358
359    template <class T>
360    void ClassIdentifier<T>::setConfigValues(T*, Identifiable*) const
361    {
362        // no action
363    }
364
365    /**
[9572]366     * @brief Only adds the object to the object list if is a @ref Listable
367     */
368    template <class T>
[9667]369    void ClassIdentifier<T>::addObjectToList(T* object, Listable* listable)
[9572]370    {
[9667]371        if (listable->getContext())
372            listable->getContext()->addObject(object);
[9572]373    }
374
375    template <class T>
376    void ClassIdentifier<T>::addObjectToList(T*, Identifiable*)
377    {
378        // no action
379    }
380
381    /**
[1052]382        @brief Updates the config-values of all existing objects of this class by calling their setConfigValues() function.
[790]383    */
384    template <class T>
[1747]385    void ClassIdentifier<T>::updateConfigValues(bool updateChildren) const
[790]386    {
[9667]387        this->updateConfigValues(updateChildren, static_cast<T*>(NULL));
388    }
389
390    template <class T>
391    void ClassIdentifier<T>::updateConfigValues(bool updateChildren, Listable*) const
392    {
[1747]393        if (!this->hasConfigValues())
394            return;
[790]395
[1747]396        for (ObjectListIterator<T> it = ObjectList<T>::begin(); it; ++it)
[9586]397            this->setConfigValues(*it, *it);
[1052]398
[1747]399        if (updateChildren)
[10367]400            for (std::set<const Identifier*>::const_iterator it = this->getChildren().begin(); it != this->getChildren().end(); ++it)
[1747]401                (*it)->updateConfigValues(false);
[1052]402    }
403
[9667]404    template <class T>
405    void ClassIdentifier<T>::updateConfigValues(bool updateChildren, Identifiable*) const
406    {
407        // no action
408    }
[1052]409
[9667]410
[790]411    // ###############################
[3325]412    // ###      orxonox_cast       ###
413    // ###############################
414    /**
415    @brief
[9566]416        Casts on object of type Identifiable to any derived type that is
[3325]417        registered in the class hierarchy.
418    @return
419        Returns NULL if the cast is not possible
420    @note
421        In case of NULL return (and using MSVC), a dynamic_cast might still be possible if
422        a class forgot to register its objects.
423        Also note that the function is implemented differently for GCC/MSVC.
424    */
425    template <class T, class U>
[8351]426    ORX_FORCEINLINE T orxonox_cast(U* source)
[3325]427    {
[3332]428#ifdef ORXONOX_COMPILER_MSVC
[3333]429        typedef Loki::TypeTraits<typename Loki::TypeTraits<T>::PointeeType>::NonConstType ClassType;
[3370]430        if (source != NULL)
431            return source->template getDerivedPointer<ClassType>(ClassIdentifier<ClassType>::getIdentifier()->getClassID());
432        else
433            return NULL;
[3332]434#else
435        return dynamic_cast<T>(source);
436#endif
[3325]437    }
[790]438}
439
440#endif /* _Identifier_H__ */
Note: See TracBrowser for help on using the repository browser.