Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1049 was 1024, checked in by rgrieder, 17 years ago

merged input back into trunk
note: I have created an asylum with obsolete code, please check the files

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