Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/core/Identifier.h @ 883

Last change on this file since 883 was 871, checked in by landauf, 17 years ago

merged core branch to trunk

File size: 23.8 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28/**
29    @file Identifier.h
30    @brief Definition of the Identifier, ClassIdentifier and SubclassIdentifier classes, implementation of the ClassIdentifier and SubclassIdentifier classes.
31
32    The Identifier contains all needed informations about the class it belongs to:
33     - the name
34     - a list with all objects
35     - parents and childs
36     - the factory (if available)
37     - the networkID that can be synchronised with the server
38     - all configurable variables (if available)
39
40    Every object has a pointer to the Identifier of its class. This allows the use isA(...),
41    isExactlyA(...), isChildOf(...) and isParentOf(...).
42
43    To create the class-hierarchy, the Identifier has some intern functions and variables.
44
45    Every Identifier is in fact a ClassIdentifier, but they are derived from Identifier.
46
47    SubclassIdentifier is a separated class, acting like an Identifier, but has a given class.
48    You can only assign Identifiers of exactly the given class or of a derivative to a SubclassIdentifier.
49*/
50
51#ifndef _Identifier_H__
52#define _Identifier_H__
53
54#include <list>
55#include <map>
56#include <string>
57#include <utility>
58
59#include "CorePrereqs.h"
60
61#include "ObjectList.h"
62#include "Debug.h"
63#include "Iterator.h"
64
65namespace orxonox
66{
67    class BaseFactory; // Forward declaration
68    class BaseObject;  // Forward declaration
69
70    // ###############################
71    // ###       Identifier        ###
72    // ###############################
73    //! The Identifier is used to identify the class of an object and to store informations about the class.
74    /**
75        The Identifier contains all needed informations about the class it belongs to:
76         - the name
77         - a list with all objects
78         - parents and childs
79         - the factory (if available)
80         - the networkID that can be synchronised with the server
81         - all configurable variables (if available)
82
83        Every object has a pointer to the Identifier of its class. This allows the use isA(...),
84        isExactlyA(...), isChildOf(...) and isParentOf(...).
85
86        You can't directly create an Identifier, it's just the base-class for ClassIdentifier.
87    */
88    class _CoreExport Identifier
89    {
90        template <class T>
91        friend class ClassIdentifier;
92
93        template <class T>
94        friend class SubclassIdentifier;
95
96        friend class Factory;
97
98        public:
99            /** @brief Sets the Factory. @param factory The factory to assign */
100            inline void addFactory(BaseFactory* factory) { this->factory_ = factory; }
101
102            BaseObject* fabricate();
103            bool isA(const Identifier* identifier) const;
104            bool isExactlyA(const Identifier* identifier) const;
105            bool isChildOf(const Identifier* identifier) const;
106            bool isDirectChildOf(const Identifier* identifier) const;
107            bool isParentOf(const Identifier* identifier) const;
108            bool isDirectParentOf(const Identifier* identifier) const;
109
110            /** @brief Removes all objects of the corresponding class. */
111            virtual void removeObjects() const = 0;
112
113            /** @brief Returns the name of the class the Identifier belongs to. @return The name */
114            inline const std::string& getName() const { return this->name_; }
115
116            /** @brief Returns the parents of the class the Identifier belongs to. @return The list of all parents */
117            inline const std::list<const Identifier*>& getParents() const { return this->parents_; }
118            /** @brief Returns the begin-iterator of the parents-list. @return The begin-iterator */
119            inline std::list<const Identifier*>::const_iterator getParentsBegin() const { return this->parents_.begin(); }
120            /** @brief Returns the end-iterator of the parents-list. @return The end-iterator */
121            inline std::list<const Identifier*>::const_iterator getParentsEnd() const { return this->parents_.end(); }
122
123            /** @brief Returns the children of the class the Identifier belongs to. @return The list of all children */
124            inline const std::list<const Identifier*>& getChildren() const { return (*this->children_); }
125            /** @brief Returns the begin-iterator of the children-list. @return The begin-iterator */
126            inline std::list<const Identifier*>::const_iterator getChildrenBegin() const { return this->children_->begin(); }
127            /** @brief Returns the end-iterator of the children-list. @return The end-iterator */
128            inline std::list<const Identifier*>::const_iterator getChildrenEnd() const { return this->children_->end(); }
129
130            /** @brief Returns the direct parents of the class the Identifier belongs to. @return The list of all direct parents */
131            inline const std::list<const Identifier*>& getDirectParents() const { return this->directParents_; }
132            /** @brief Returns the begin-iterator of the direct-parents-list. @return The begin-iterator */
133            inline std::list<const Identifier*>::const_iterator getDirectParentsBegin() const { return this->directParents_.begin(); }
134            /** @brief Returns the end-iterator of the direct-parents-list. @return The end-iterator */
135            inline std::list<const Identifier*>::const_iterator getDirectParentsEnd() const { return this->directParents_.end(); }
136
137            /** @brief Returns the direct children the class the Identifier belongs to. @return The list of all direct children */
138            inline const std::list<const Identifier*>& getDirectChildren() const { return (*this->directChildren_); }
139            /** @brief Returns the begin-iterator of the direct-children-list. @return The begin-iterator */
140            inline std::list<const Identifier*>::const_iterator getDirectChildrenBegin() const { return this->directChildren_->begin(); }
141            /** @brief Returns the end-iterator of the direct-children-list. @return The end-iterator */
142            inline std::list<const Identifier*>::const_iterator getDirectChildrenEnd() const { return this->directChildren_->end(); }
143
144            /** @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 */
145            inline static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); }
146
147            /** @brief Returns the network ID to identify a class through the network. @return the network ID */
148            inline const unsigned int getNetworkID() const { return this->classID_; }
149
150            /** @brief Sets the network ID to a new value. @param id The new value */
151            void setNetworkID(unsigned int id);
152
153            ConfigValueContainer* getConfigValueContainer(const std::string& varname);
154            void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container);
155
156            virtual XMLPortParamContainer* getXMLPortParamContainer(const std::string& paramname) = 0;
157            virtual void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container) = 0;
158
159            virtual XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname) = 0;
160            virtual void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container) = 0;
161
162            static bool identifierIsInList(const Identifier* identifier, const std::list<const Identifier*>& list);
163
164        private:
165            Identifier();
166            Identifier(const Identifier& identifier) {} // don't copy
167            virtual ~Identifier();
168            void initialize(std::list<const Identifier*>* parents);
169
170            /** @brief Returns the children of the class the Identifier belongs to. @return The list of all children */
171            inline std::list<const Identifier*>& getChildrenIntern() const { return (*this->children_); }
172            /** @brief Returns the direct children of the class the Identifier belongs to. @return The list of all direct children */
173            inline std::list<const Identifier*>& getDirectChildrenIntern() const { return (*this->directChildren_); }
174
175            /**
176                @brief Increases the hierarchyCreatingCounter_s variable, causing all new objects to store their parents.
177            */
178            inline static void startCreatingHierarchy()
179            {
180                hierarchyCreatingCounter_s++;
181                COUT(4) << "*** Identifier: Increased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl;
182            }
183
184            /**
185                @brief Decreases the hierarchyCreatingCounter_s variable, causing the objects to stop storing their parents.
186            */
187            inline static void stopCreatingHierarchy()
188            {
189                hierarchyCreatingCounter_s--;
190                COUT(4) << "*** Identifier: Decreased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl;
191            }
192
193            std::list<const Identifier*> parents_;                      //!< The parents of the class the Identifier belongs to
194            std::list<const Identifier*>* children_;                    //!< The children of the class the Identifier belongs to
195
196            std::list<const Identifier*> directParents_;                //!< The direct parents of the class the Identifier belongs to
197            std::list<const Identifier*>* directChildren_;              //!< The direct children of the class the Identifier belongs to
198
199            std::string name_;                                          //!< The name of the class the Identifier belongs to
200
201            BaseFactory* factory_;                                      //!< The Factory, able to create new objects of the given class (if available)
202            bool bCreatedOneObject_;                                    //!< True if at least one object of the given type was created (used to determine the need of storing the parents)
203            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)
204            unsigned int classID_;                                      //!< The network ID to identify a class through the network
205            std::map<std::string, ConfigValueContainer*> configValues_; //!< A map to link the string of configurable variables with their ConfigValueContainer
206    };
207
208    std::ostream& operator<<(std::ostream& out, const std::list<const Identifier*>& list);
209
210
211    // ###############################
212    // ###     ClassIdentifier     ###
213    // ###############################
214    //! The ClassIdentifier is derived from Identifier and holds all class-specific functions and variables the Identifier cannot have.
215    /**
216        ClassIdentifier is a Singleton, which means that only one object of a given type T exists.
217        This makes it possible to store informations about a class, sharing them with all
218        objects of that class without defining static variables in every class.
219
220        To be really sure that not more than exactly one object exists (even with libraries),
221        ClassIdentifiers are only created by IdentifierDistributor.
222
223        You can access the ClassIdentifiers created by IdentifierDistributor through the
224        ClassManager singletons.
225    */
226    template <class T>
227    class ClassIdentifier : public Identifier
228    {
229        template <class TT>
230        friend class ClassManager;
231
232        public:
233            ClassIdentifier<T>* registerClass(std::list<const Identifier*>* parents, const std::string& name, bool bRootClass);
234            void addObject(T* object);
235            void removeObjects() const;
236            void setName(const std::string& name);
237            inline const ObjectList<T>* getObjects() const { return this->objects_; }
238
239            XMLPortParamContainer* getXMLPortParamContainer(const std::string& paramname);
240            void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container);
241
242            XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname);
243            void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container);
244
245        private:
246            ClassIdentifier();
247            ClassIdentifier(const ClassIdentifier<T>& identifier) {}    // don't copy
248            ~ClassIdentifier() {}                                       // don't delete
249
250            ObjectList<T>* objects_;    //!< The ObjectList, containing all objects of type T
251            bool bSetName_;             //!< True if the name is set
252            std::map<std::string, XMLPortClassParamContainer<T>*> xmlportParamContainers_;
253            std::map<std::string, XMLPortClassObjectContainer<T, class O>*> xmlportObjectContainers_;
254    };
255
256    /**
257        @brief Constructor: Creates the ObjectList.
258    */
259    template <class T>
260    ClassIdentifier<T>::ClassIdentifier()
261    {
262//        this->objects_ = ObjectList<T>::getList();
263        this->objects_ = new ObjectList<T>();
264        this->bSetName_ = false;
265    }
266
267    /**
268        @brief Registers a class, which means that the name and the parents get stored.
269        @param parents A list, containing the Identifiers of all parents of the class
270        @param name A string, containing exactly the name of the class
271        @param bRootClass True if the class is either an Interface or the BaseObject itself
272        @return The ClassIdentifier itself
273    */
274    template <class T>
275    ClassIdentifier<T>* ClassIdentifier<T>::registerClass(std::list<const Identifier*>* parents, const std::string& name, bool bRootClass)
276    {
277        COUT(5) << "*** ClassIdentifier: Register Class in " << name << "-Singleton." << std::endl;
278
279        // Check if at least one object of the given type was created
280        if (!this->bCreatedOneObject_)
281        {
282            // If no: We have to store the informations and initialize the Identifier
283            this->setName(name);
284
285            COUT(4) << "*** ClassIdentifier: Register Class in " << name << "-Singleton -> Initialize Singleton." << std::endl;
286            if (bRootClass)
287                this->initialize(NULL); // If a class is derived from two interfaces, the second interface might think it's derived from the first because of the order of constructor-calls. Thats why we set parents to zero in that case.
288            else
289                this->initialize(parents);
290        }
291
292        return this;
293    }
294
295    /**
296        @brief Sets the name of the class.
297        @param name The name
298    */
299    template <class T>
300    void ClassIdentifier<T>::setName(const std::string& name)
301    {
302        if (!this->bSetName_)
303        {
304            this->name_ = name;
305            this->bSetName_ = true;
306        }
307    }
308
309    /**
310        @brief Adds an object of the given type to the ObjectList.
311        @param object The object to add
312    */
313    template <class T>
314    void ClassIdentifier<T>::addObject(T* object)
315    {
316        COUT(5) << "*** ClassIdentifier: Added object to " << this->getName() << "-list." << std::endl;
317        object->getMetaList().add(this->objects_, this->objects_->add(object));
318    }
319
320    /**
321        @brief Removes all objects of the corresponding class.
322    */
323    template <class T>
324    void ClassIdentifier<T>::removeObjects() const
325    {
326        for (Iterator<T> it = this->objects_->start(); it;)
327            delete *(it++);
328    }
329
330    template <class T>
331    XMLPortParamContainer* ClassIdentifier<T>::getXMLPortParamContainer(const std::string& paramname)
332    {
333        typename std::map<std::string, XMLPortClassParamContainer<T>*>::const_iterator it = xmlportParamContainers_.find(paramname);
334        if (it != xmlportParamContainers_.end())
335            return (XMLPortParamContainer*)((*it).second);
336        else
337            return 0;
338    }
339
340    template <class T>
341    void ClassIdentifier<T>::addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container)
342    {
343        this->xmlportParamContainers_[paramname] = (XMLPortClassParamContainer<T>*)container;
344    }
345
346    template <class T>
347    XMLPortObjectContainer* ClassIdentifier<T>::getXMLPortObjectContainer(const std::string& sectionname)
348    {
349        typename std::map<std::string, XMLPortClassObjectContainer<T, class O>*>::const_iterator it = xmlportObjectContainers_.find(sectionname);
350        if (it != xmlportObjectContainers_.end())
351            return (XMLPortObjectContainer*)((*it).second);
352        else
353            return 0;
354    }
355
356    template <class T>
357    void ClassIdentifier<T>::addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container)
358    {
359        this->xmlportObjectContainers_[sectionname] = (XMLPortClassObjectContainer<T, class O>*)container;
360    }
361
362
363    // ###############################
364    // ###   SubclassIdentifier    ###
365    // ###############################
366    //! The SubclassIdentifier acts almost like an Identifier, but has some prerequisites.
367    /**
368        You can only assign an Identifier that belongs to a class T (or derived) to a SubclassIdentifier<T>.
369        If you assign something else, the program aborts.
370        Because we know the minimal type, a dynamic_cast is done, which makes it easier to create a new object.
371    */
372    template <class T>
373    class SubclassIdentifier
374    {
375        public:
376            /**
377                @brief Constructor: Automaticaly assigns the Identifier of the given class.
378            */
379            SubclassIdentifier()
380            {
381                this->identifier_ = ClassManager<T>::getIdentifier();
382            }
383
384            /**
385                @brief Copyconstructor: Assigns the given Identifier.
386                @param identifier The Identifier
387            */
388            SubclassIdentifier(Identifier* identifier)
389            {
390                this->identifier_ = identifier;
391            }
392
393            /**
394                @brief Overloading of the = operator: assigns the identifier and checks its type.
395                @param identifier The Identifier to assign
396                @return The SubclassIdentifier itself
397            */
398            SubclassIdentifier<T>& operator=(Identifier* identifier)
399            {
400                if (!identifier->isA(ClassManager<T>::getIdentifier()))
401                {
402                    COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl;
403                    COUT(1) << "Error: Class " << identifier->getName() << " is not a " << ClassManager<T>::getIdentifier()->getName() << "!" << std::endl;
404                    COUT(1) << "Error: SubclassIdentifier<" << ClassManager<T>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden." << std::endl;
405                    COUT(1) << "Aborting..." << std::endl;
406                    abort();
407                }
408                this->identifier_ = identifier;
409                return *this;
410            }
411
412            /**
413                @brief Overloading of the * operator: returns the assigned identifier.
414                @return The assigned identifier
415            */
416            Identifier* operator*()
417            {
418                return this->identifier_;
419            }
420
421            /**
422                @brief Overloading of the -> operator: returns the assigned identifier.
423                @return The assigned identifier
424            */
425            Identifier* operator->() const
426            {
427                return this->identifier_;
428            }
429
430            /**
431                @brief Creates a new object of the type of the assigned Identifier and dynamic_casts it to the minimal type given by T.
432                @return The new object
433            */
434            T* fabricate()
435            {
436                BaseObject* newObject = this->identifier_->fabricate();
437
438                // Check if the creation was successful
439                if (newObject)
440                {
441                    // Do a dynamic_cast, because an object of type T is much better than of type BaseObject
442                    return (T*)(newObject);
443                }
444                else
445                {
446                    // Something went terribly wrong
447                    if (this->identifier_)
448                    {
449                        COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl;
450                        COUT(1) << "Error: Class " << this->identifier_->getName() << " is not a " << ClassManager<T>::getIdentifier()->getName() << "!" << std::endl;
451                        COUT(1) << "Error: Couldn't fabricate a new Object." << std::endl;
452                        COUT(1) << "Aborting..." << std::endl;
453                    }
454                    else
455                    {
456                        COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl;
457                        COUT(1) << "Error: Couldn't fabricate a new Object - Identifier is undefined." << std::endl;
458                        COUT(1) << "Aborting..." << std::endl;
459                    }
460
461                    abort();
462                }
463            }
464
465            /** @brief Returns the assigned identifier. @return The identifier */
466            inline const Identifier* getIdentifier() const
467                { return this->identifier_; }
468
469            /** @brief Returns true, if the assigned identifier is at least of the given type. @param identifier The identifier to compare with */
470            inline bool isA(const Identifier* identifier) const
471                { return this->identifier_->isA(identifier); }
472
473            /** @brief Returns true, if the assigned identifier is exactly of the given type. @param identifier The identifier to compare with */
474            inline bool isExactlyA(const Identifier* identifier) const
475                { return this->identifier_->isExactlyA(identifier); }
476
477            /** @brief Returns true, if the assigned identifier is a child of the given identifier. @param identifier The identifier to compare with */
478            inline bool isChildOf(const Identifier* identifier) const
479                { return this->identifier_->isChildOf(identifier); }
480
481            /** @brief Returns true, if the assigned identifier is a direct child of the given identifier. @param identifier The identifier to compare with */
482            inline bool isDirectChildOf(const Identifier* identifier) const
483                { return this->identifier_->isDirectChildOf(identifier); }
484
485            /** @brief Returns true, if the assigned identifier is a parent of the given identifier. @param identifier The identifier to compare with */
486            inline bool isParentOf(const Identifier* identifier) const
487                { return this->identifier_->isParentOf(identifier); }
488
489            /** @brief Returns true, if the assigned identifier is a direct parent of the given identifier. @param identifier The identifier to compare with */
490            inline bool isDirectParentOf(const Identifier* identifier) const
491                { return this->identifier_->isDirectParentOf(identifier); }
492
493        private:
494            Identifier* identifier_;            //!< The assigned identifier
495    };
496}
497
498#endif /* _Identifier_H__ */
Note: See TracBrowser for help on using the repository browser.