Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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