Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core6/src/libraries/core/class/Identifier.h @ 9593

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

moved logic to remove an element from an ObjectListBase from MetaObjectList to ObjectListBase

  • Property svn:eol-style set to native
File size: 21.6 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
[9556]58    OrxonoxClass* other = object->getIdentifier()->fabricate(0);                // fabricates a new instance of MyClass
[7401]59
60
61    // iterate through all objects of type MyClass:
62    ObjectListBase* objects = object->getIdentifier()->getObjects();            // get a pointer to the object-list
63    int count;
[9556]64    for (Iterator<MyClass> it = objects.begin(); it != objects.end(); ++it)     // iterate through the objects
[7401]65        ++count;
[8858]66    orxout() << count << endl;                                                  // prints "2" because we created 2 instances of MyClass so far
[7401]67
68
69    // test the class hierarchy
70    object->getIdentifier()->isA(Class(MyClass));                               // returns true
71    object->isA(Class(MyClass));                                                // returns true (short version)
72
73    object->isA(Class(BaseClass));                                              // returns true if MyClass is a child of BaseClass
74
75    Class(ChildClass)->isChildOf(object->getIdentifier());                      // returns true if ChildClass is a child of MyClass
76    @endcode
[790]77*/
78
79#ifndef _Identifier_H__
80#define _Identifier_H__
[1052]81
[9563]82#include "core/CorePrereqs.h"
[1062]83
[3196]84#include <cassert>
85#include <map>
[1052]86#include <set>
[790]87#include <string>
[1639]88#include <typeinfo>
[7266]89#include <loki/TypeTraits.h>
[790]90
[8858]91#include "util/Output.h"
[9563]92#include "core/object/MetaObjectList.h"
93#include "core/object/ObjectList.h"
94#include "core/object/ObjectListBase.h"
[9564]95#include "IdentifierManager.h"
[5781]96#include "Super.h"
[790]97
98namespace orxonox
99{
100    // ###############################
101    // ###       Identifier        ###
102    // ###############################
103    /**
[7401]104        @brief The Identifier is used to identify the class of an object and to store information about the class.
[790]105
[7401]106        Each Identifier stores information about one class. The Identifier can then be used to identify
107        this class. On the other hand it's also possible to get the corresponding Identifier of a class,
108        for example by using the macro Class().
[790]109
[7401]110        @see See @ref IdentifierExample "Identifier.h" for more information and some examples.
111
112        @note You can't directly create an Identifier, it's just the base-class of ClassIdentifier<T>.
[790]113    */
114    class _CoreExport Identifier
115    {
[9564]116        friend class IdentifierManager;
117
[5929]118        public:
[7401]119            /// Returns the name of the class the Identifier belongs to.
[5929]120            inline const std::string& getName() const { return this->name_; }
121            void setName(const std::string& name);
[790]122
[7401]123            /// Returns the network ID to identify a class through the network.
[8706]124            inline uint32_t getNetworkID() const { return this->networkID_; }
[5929]125            void setNetworkID(uint32_t id);
[790]126
[7401]127            /// Returns the unique ID of the class.
[8351]128            ORX_FORCEINLINE unsigned int getClassID() const { return this->classID_; }
[5929]129
[7401]130            /// Returns the list of all existing objects of this class.
[5929]131            inline ObjectListBase* getObjects() const { return this->objects_; }
132
[7401]133            /// Sets the Factory.
[5929]134            inline void addFactory(Factory* factory) { this->factory_ = factory; }
[7401]135            /// Returns true if the Identifier has a Factory.
[5929]136            inline bool hasFactory() const { return (this->factory_ != 0); }
[790]137
[9556]138            OrxonoxClass* fabricate(BaseObject* creator);
[5929]139
[7401]140            /// Returns true if the class can be loaded through XML.
[5929]141            inline bool isLoadable() const { return this->bLoadable_; }
[7401]142            /// Set the class to be loadable through XML or not.
[5929]143            inline void setLoadable(bool bLoadable) { this->bLoadable_ = bLoadable; }
144
[790]145            bool isA(const Identifier* identifier) const;
[871]146            bool isExactlyA(const Identifier* identifier) const;
[1052]147            bool isChildOf(const Identifier* identifier) const;
[871]148            bool isDirectChildOf(const Identifier* identifier) const;
[1052]149            bool isParentOf(const Identifier* identifier) const;
[871]150            bool isDirectParentOf(const Identifier* identifier) const;
[790]151
[5781]152
[5929]153            /////////////////////////////
154            ////// Class Hierarchy //////
155            /////////////////////////////
[7401]156            /// Returns the parents of the class the Identifier belongs to.
[1052]157            inline const std::set<const Identifier*>& getParents() const { return this->parents_; }
[7401]158            /// Returns the begin-iterator of the parents-list.
[1052]159            inline std::set<const Identifier*>::const_iterator getParentsBegin() const { return this->parents_.begin(); }
[7401]160            /// Returns the end-iterator of the parents-list.
[1052]161            inline std::set<const Identifier*>::const_iterator getParentsEnd() const { return this->parents_.end(); }
162
[7401]163            /// Returns the children of the class the Identifier belongs to.
[5929]164            inline const std::set<const Identifier*>& getChildren() const { return this->children_; }
[7401]165            /// Returns the begin-iterator of the children-list.
[5929]166            inline std::set<const Identifier*>::const_iterator getChildrenBegin() const { return this->children_.begin(); }
[7401]167            /// Returns the end-iterator of the children-list.
[5929]168            inline std::set<const Identifier*>::const_iterator getChildrenEnd() const { return this->children_.end(); }
[1052]169
[7401]170            /// Returns the direct parents of the class the Identifier belongs to.
[1052]171            inline const std::set<const Identifier*>& getDirectParents() const { return this->directParents_; }
[7401]172            /// Returns the begin-iterator of the direct-parents-list.
[1052]173            inline std::set<const Identifier*>::const_iterator getDirectParentsBegin() const { return this->directParents_.begin(); }
[7401]174            /// Returns the end-iterator of the direct-parents-list.
[1052]175            inline std::set<const Identifier*>::const_iterator getDirectParentsEnd() const { return this->directParents_.end(); }
176
[7401]177            /// Returns the direct children the class the Identifier belongs to.
[5929]178            inline const std::set<const Identifier*>& getDirectChildren() const { return this->directChildren_; }
[7401]179            /// Returns the begin-iterator of the direct-children-list.
[5929]180            inline std::set<const Identifier*>::const_iterator getDirectChildrenBegin() const { return this->directChildren_.begin(); }
[7401]181            /// Returns the end-iterator of the direct-children-list.
[5929]182            inline std::set<const Identifier*>::const_iterator getDirectChildrenEnd() const { return this->directChildren_.end(); }
[1052]183
184
[5929]185            /////////////////////////
186            ///// Config Values /////
187            /////////////////////////
188            virtual void updateConfigValues(bool updateChildren = true) const = 0;
189
[7401]190            /// Returns true if this class has at least one config value.
[5929]191            inline bool hasConfigValues() const { return this->bHasConfigValues_; }
192
193            void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container);
194            ConfigValueContainer* getConfigValueContainer(const std::string& varname);
[1052]195
[5929]196
197            ///////////////////
198            ///// XMLPort /////
199            ///////////////////
[7401]200            /// Returns the map that stores all XMLPort params.
[5781]201            inline const std::map<std::string, XMLPortParamContainer*>& getXMLPortParamMap() const { return this->xmlportParamContainers_; }
[7401]202            /// Returns a const_iterator to the beginning of the map that stores all XMLPort params.
[5781]203            inline std::map<std::string, XMLPortParamContainer*>::const_iterator getXMLPortParamMapBegin() const { return this->xmlportParamContainers_.begin(); }
[7401]204            /// Returns a const_iterator to the end of the map that stores all XMLPort params.
[5781]205            inline std::map<std::string, XMLPortParamContainer*>::const_iterator getXMLPortParamMapEnd() const { return this->xmlportParamContainers_.end(); }
206
[7401]207            /// Returns the map that stores all XMLPort objects.
[5781]208            inline const std::map<std::string, XMLPortObjectContainer*>& getXMLPortObjectMap() const { return this->xmlportObjectContainers_; }
[7401]209            /// Returns a const_iterator to the beginning of the map that stores all XMLPort objects.
[5781]210            inline std::map<std::string, XMLPortObjectContainer*>::const_iterator getXMLPortObjectMapBegin() const { return this->xmlportObjectContainers_.begin(); }
[7401]211            /// Returns a const_iterator to the end of the map that stores all XMLPort objects.
[5781]212            inline std::map<std::string, XMLPortObjectContainer*>::const_iterator getXMLPortObjectMapEnd() const { return this->xmlportObjectContainers_.end(); }
213
214            void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container);
215            XMLPortParamContainer* getXMLPortParamContainer(const std::string& paramname);
216
217            void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container);
218            XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname);
219
220
[1052]221        protected:
[1747]222            Identifier();
223            Identifier(const Identifier& identifier); // don't copy
224            virtual ~Identifier();
225
[5781]226            virtual void createSuperFunctionCaller() const = 0;
[1747]227
[5929]228            void initializeClassHierarchy(std::set<const Identifier*>* parents, bool bRootClass);
229
[7401]230            /// Returns the children of the class the Identifier belongs to.
[5929]231            inline std::set<const Identifier*>& getChildrenIntern() const { return this->children_; }
[7401]232            /// Returns the direct children of the class the Identifier belongs to.
[5929]233            inline std::set<const Identifier*>& getDirectChildrenIntern() const { return this->directChildren_; }
[1052]234
[1747]235            ObjectListBase* objects_;                                      //!< The list of all objects of this class
236
237        private:
[1856]238            void initialize(std::set<const Identifier*>* parents);
239
[1052]240            std::set<const Identifier*> parents_;                          //!< The parents of the class the Identifier belongs to
[5929]241            mutable std::set<const Identifier*> children_;                 //!< The children of the class the Identifier belongs to
[790]242
[1052]243            std::set<const Identifier*> directParents_;                    //!< The direct parents of the class the Identifier belongs to
[5929]244            mutable std::set<const Identifier*> directChildren_;           //!< The direct children of the class the Identifier belongs to
[1052]245
[1856]246            bool bCreatedOneObject_;                                       //!< True if at least one object of the given type was created (used to determine the need of storing the parents)
[1747]247            bool bSetName_;                                                //!< True if the name is set
[5781]248            bool bLoadable_;                                               //!< False = it's not permitted to load the object through XML
[1052]249            std::string name_;                                             //!< The name of the class the Identifier belongs to
[5929]250            Factory* factory_;                                             //!< The Factory, able to create new objects of the given class (if available)
[5781]251            uint32_t networkID_;                                           //!< The network ID to identify a class through the network
[3325]252            const unsigned int classID_;                                   //!< Uniquely identifies a class (might not be the same as the networkID_)
[1052]253
254            bool bHasConfigValues_;                                        //!< True if this class has at least one assigned config value
255            std::map<std::string, ConfigValueContainer*> configValues_;    //!< A map to link the string of configurable variables with their ConfigValueContainer
[5781]256
257            std::map<std::string, XMLPortParamContainer*> xmlportParamContainers_;     //!< All loadable parameters
258            std::map<std::string, XMLPortObjectContainer*> xmlportObjectContainers_;   //!< All attachable objects
[790]259    };
260
[1052]261    _CoreExport std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list);
[790]262
[1052]263
[790]264    // ###############################
265    // ###     ClassIdentifier     ###
266    // ###############################
267    /**
[7401]268        @brief The ClassIdentifier is derived from Identifier and holds all class-specific functions and variables the Identifier cannot have.
269
270        ClassIdentifier is a Singleton, which means that only one ClassIdentifier for a given type T exists.
[5695]271        This makes it possible to store information about a class, sharing them with all
[1052]272        objects of that class without defining static variables in every class.
273
274        To be really sure that not more than exactly one object exists (even with libraries),
[7401]275        ClassIdentifiers are stored in a static map in Identifier.
[790]276    */
277    template <class T>
278    class ClassIdentifier : public Identifier
[1052]279    {
[7401]280        #ifndef DOXYGEN_SHOULD_SKIP_THIS
281          #define SUPER_INTRUSIVE_DECLARATION_INCLUDE
282          #include "Super.h"
283        #endif
[5781]284
[790]285        public:
[5929]286            static ClassIdentifier<T>* getIdentifier();
287            static ClassIdentifier<T>* getIdentifier(const std::string& name);
[1052]288
[3325]289            bool initialiseObject(T* object, const std::string& className, bool bRootClass);
290
[9586]291            void setConfigValues(T* object, Configurable*) const;
292            void setConfigValues(T* object, Identifiable*) const;
293
[9572]294            void addObjectToList(T* object, Listable*);
295            void addObjectToList(T* object, Identifiable*);
296
[1747]297            void updateConfigValues(bool updateChildren = true) const;
[1052]298
[790]299        private:
[2784]300            static void initialiseIdentifier();
[790]301            ClassIdentifier(const ClassIdentifier<T>& identifier) {}    // don't copy
[1747]302            ClassIdentifier()
303            {
[5781]304                SuperFunctionInitialization<0, T>::initialize(this);
[1747]305            }
306            ~ClassIdentifier()
307            {
[5781]308                SuperFunctionDestruction<0, T>::destroy(this);
[1747]309            }
[790]310
[1856]311            static ClassIdentifier<T>* classIdentifier_s;
[790]312    };
313
[1543]314    template <class T>
[2784]315    ClassIdentifier<T>* ClassIdentifier<T>::classIdentifier_s = 0;
[1543]316
[790]317    /**
[1747]318        @brief Returns the only instance of this class.
[1543]319        @return The unique Identifier
320    */
321    template <class T>
[3196]322    inline ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier()
[1543]323    {
[5929]324        // check if the Identifier already exists
325        if (!ClassIdentifier<T>::classIdentifier_s)
[2784]326            ClassIdentifier<T>::initialiseIdentifier();
[1543]327
328        return ClassIdentifier<T>::classIdentifier_s;
329    }
330
331    /**
[1747]332        @brief Does the same as getIdentifier() but sets the name if this wasn't done yet.
333        @param name The name of this Identifier
334        @return The Identifier
[790]335    */
336    template <class T>
[3196]337    inline ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier(const std::string& name)
[790]338    {
[1747]339        ClassIdentifier<T>* identifier = ClassIdentifier<T>::getIdentifier();
340        identifier->setName(name);
341        return identifier;
[790]342    }
343
344    /**
[2784]345        @brief Assigns the static field for the identifier singleton.
346    */
347    template <class T>
348    void ClassIdentifier<T>::initialiseIdentifier()
349    {
350        // Get the name of the class
[8351]351        std::string name = typeid(T).name();
[2784]352
353        // create a new identifier anyway. Will be deleted in Identifier::getIdentifier if not used.
354        ClassIdentifier<T>* proposal = new ClassIdentifier<T>();
355
356        // Get the entry from the map
[9564]357        ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*)IdentifierManager::getIdentifierSingleton(name, proposal);
[2784]358
359        if (ClassIdentifier<T>::classIdentifier_s == proposal)
360        {
[8858]361            orxout(verbose, context::identifier) << "Requested Identifier for " << name << " was not yet existing and got created." << endl;
[2784]362        }
363        else
364        {
[8858]365            orxout(verbose, context::identifier) << "Requested Identifier for " << name << " was already existing and got assigned." << endl;
[2784]366        }
367    }
368
369    /**
[790]370        @brief Adds an object of the given type to the ObjectList.
371        @param object The object to add
[7401]372        @param className The name of the class T
373        @param bRootClass True if this is a root class (i.e. it inherits directly from OrxonoxClass)
[790]374    */
375    template <class T>
[3325]376    bool ClassIdentifier<T>::initialiseObject(T* object, const std::string& className, bool bRootClass)
[790]377    {
[3325]378        if (bRootClass)
[8858]379            orxout(verbose, context::object_list) << "Register Root-Object: " << className << endl;
[3325]380        else
[8858]381            orxout(verbose, context::object_list) << "Register Object: " << className << endl;
[3325]382
383        object->identifier_ = this;
[9564]384        if (IdentifierManager::isCreatingHierarchy())
[3325]385        {
386            if (bRootClass && !object->parents_)
387                object->parents_ = new std::set<const Identifier*>();
388
389            if (object->parents_)
390            {
391                this->initializeClassHierarchy(object->parents_, bRootClass);
392                object->parents_->insert(object->parents_->end(), this);
393            }
394
[9586]395            this->setConfigValues(object, object);
[3325]396            return true;
397        }
398        else
399        {
[9572]400            this->addObjectToList(object, object);
[3325]401
[9566]402            // Add pointer of type T to the map in the Identifiable instance that enables "dynamic_casts"
[3333]403            object->objectPointers_.push_back(std::make_pair(this->getClassID(), static_cast<void*>(object)));
[3325]404            return false;
405        }
[790]406    }
407
408    /**
[9586]409     * @brief Only configures the object if is a @ref Configurable
410     */
411    template <class T>
412    void ClassIdentifier<T>::setConfigValues(T* object, Configurable*) const
413    {
414        object->setConfigValues();
415    }
416
417    template <class T>
418    void ClassIdentifier<T>::setConfigValues(T*, Identifiable*) const
419    {
420        // no action
421    }
422
423    /**
[9572]424     * @brief Only adds the object to the object list if is a @ref Listable
425     */
426    template <class T>
427    void ClassIdentifier<T>::addObjectToList(T* object, Listable*)
428    {
429        orxout(verbose, context::object_list) << "Added object to " << this->getName() << "-list." << endl;
[9593]430        object->metaList_->add(this->objects_, this->objects_->add(object));
[9572]431    }
432
433    template <class T>
434    void ClassIdentifier<T>::addObjectToList(T*, Identifiable*)
435    {
436        // no action
437    }
438
439    /**
[1052]440        @brief Updates the config-values of all existing objects of this class by calling their setConfigValues() function.
[790]441    */
442    template <class T>
[1747]443    void ClassIdentifier<T>::updateConfigValues(bool updateChildren) const
[790]444    {
[1747]445        if (!this->hasConfigValues())
446            return;
[790]447
[1747]448        for (ObjectListIterator<T> it = ObjectList<T>::begin(); it; ++it)
[9586]449            this->setConfigValues(*it, *it);
[1052]450
[1747]451        if (updateChildren)
452            for (std::set<const Identifier*>::const_iterator it = this->getChildrenBegin(); it != this->getChildrenEnd(); ++it)
453                (*it)->updateConfigValues(false);
[1052]454    }
455
456
[790]457    // ###############################
[3325]458    // ###      orxonox_cast       ###
459    // ###############################
460    /**
461    @brief
[9566]462        Casts on object of type Identifiable to any derived type that is
[3325]463        registered in the class hierarchy.
464    @return
465        Returns NULL if the cast is not possible
466    @note
467        In case of NULL return (and using MSVC), a dynamic_cast might still be possible if
468        a class forgot to register its objects.
469        Also note that the function is implemented differently for GCC/MSVC.
470    */
471    template <class T, class U>
[8351]472    ORX_FORCEINLINE T orxonox_cast(U* source)
[3325]473    {
[3332]474#ifdef ORXONOX_COMPILER_MSVC
[3333]475        typedef Loki::TypeTraits<typename Loki::TypeTraits<T>::PointeeType>::NonConstType ClassType;
[3370]476        if (source != NULL)
477            return source->template getDerivedPointer<ClassType>(ClassIdentifier<ClassType>::getIdentifier()->getClassID());
478        else
479            return NULL;
[3332]480#else
481        return dynamic_cast<T>(source);
482#endif
[3325]483    }
[790]484}
485
486#endif /* _Identifier_H__ */
Note: See TracBrowser for help on using the repository browser.