Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/core/Identifier.h @ 876

Last change on this file since 876 was 876, checked in by landauf, 16 years ago

std::set instead of std::list for Identifier-lists (parents, children, …) for faster accessing by key ( std::set::find(xyz) )

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