Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 957 was 957, checked in by landauf, 16 years ago
  • added set and tset functions to the ConfigValueContainer to (temporary) set a config-value to a new value
  • ConfigValueContainer uses now the functions of MultiTypeMath to convert and assign values
  • added some errorhandling to the CommandExecutor in case there are not enough parameters when executing the command
  • added updateConfigValues function to Identifier
  • added addTime and removeTime functions to the Timer
  • some changes in Executor to allow adding description and default-values when using the ConsoleCommand macro
File size: 32.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#include "util/String.h"
65
66namespace orxonox
67{
68    class BaseFactory; // Forward declaration
69    class BaseObject;  // Forward declaration
70
71    // ###############################
72    // ###       Identifier        ###
73    // ###############################
74    //! The Identifier is used to identify the class of an object and to store informations about the class.
75    /**
76        The Identifier contains all needed informations about the class it belongs to:
77         - the name
78         - a list with all objects
79         - parents and childs
80         - the factory (if available)
81         - the networkID that can be synchronised with the server
82         - all configurable variables (if available)
83
84        Every object has a pointer to the Identifier of its class. This allows the use isA(...),
85        isExactlyA(...), isChildOf(...) and isParentOf(...).
86
87        You can't directly create an Identifier, it's just the base-class for ClassIdentifier.
88    */
89    class _CoreExport Identifier
90    {
91        template <class T>
92        friend class ClassIdentifier;
93
94        template <class T>
95        friend class SubclassIdentifier;
96
97        friend class Factory;
98
99        public:
100            /** @brief Sets the Factory. @param factory The factory to assign */
101            inline void addFactory(BaseFactory* factory) { this->factory_ = factory; }
102
103            BaseObject* fabricate();
104            bool isA(const Identifier* identifier) const;
105            bool isExactlyA(const Identifier* identifier) const;
106            bool isChildOf(const Identifier* identifier) const;
107            bool isDirectChildOf(const Identifier* identifier) const;
108            bool isParentOf(const Identifier* identifier) const;
109            bool isDirectParentOf(const Identifier* identifier) const;
110
111            virtual const ObjectList<BaseObject>* getObjectList() const = 0;
112
113            virtual void updateConfigValues() const = 0;
114
115            /** @brief Removes all objects of the corresponding class. */
116            virtual void removeObjects() const = 0;
117
118            /** @brief Returns the name of the class the Identifier belongs to. @return The name */
119            inline const std::string& getName() const { return this->name_; }
120
121
122            /** @brief Returns the parents of the class the Identifier belongs to. @return The list of all parents */
123            inline const std::set<const Identifier*>& getParents() const { return this->parents_; }
124            /** @brief Returns the begin-iterator of the parents-list. @return The begin-iterator */
125            inline std::set<const Identifier*>::const_iterator getParentsBegin() const { return this->parents_.begin(); }
126            /** @brief Returns the end-iterator of the parents-list. @return The end-iterator */
127            inline std::set<const Identifier*>::const_iterator getParentsEnd() const { return this->parents_.end(); }
128
129            /** @brief Returns the children of the class the Identifier belongs to. @return The list of all children */
130            inline const std::set<const Identifier*>& getChildren() const { return (*this->children_); }
131            /** @brief Returns the begin-iterator of the children-list. @return The begin-iterator */
132            inline std::set<const Identifier*>::const_iterator getChildrenBegin() const { return this->children_->begin(); }
133            /** @brief Returns the end-iterator of the children-list. @return The end-iterator */
134            inline std::set<const Identifier*>::const_iterator getChildrenEnd() const { return this->children_->end(); }
135
136            /** @brief Returns the direct parents of the class the Identifier belongs to. @return The list of all direct parents */
137            inline const std::set<const Identifier*>& getDirectParents() const { return this->directParents_; }
138            /** @brief Returns the begin-iterator of the direct-parents-list. @return The begin-iterator */
139            inline std::set<const Identifier*>::const_iterator getDirectParentsBegin() const { return this->directParents_.begin(); }
140            /** @brief Returns the end-iterator of the direct-parents-list. @return The end-iterator */
141            inline std::set<const Identifier*>::const_iterator getDirectParentsEnd() const { return this->directParents_.end(); }
142
143            /** @brief Returns the direct children the class the Identifier belongs to. @return The list of all direct children */
144            inline const std::set<const Identifier*>& getDirectChildren() const { return (*this->directChildren_); }
145            /** @brief Returns the begin-iterator of the direct-children-list. @return The begin-iterator */
146            inline std::set<const Identifier*>::const_iterator getDirectChildrenBegin() const { return this->directChildren_->begin(); }
147            /** @brief Returns the end-iterator of the direct-children-list. @return The end-iterator */
148            inline std::set<const Identifier*>::const_iterator getDirectChildrenEnd() const { return this->directChildren_->end(); }
149
150
151            /** @brief Returns the map that stores all Identifiers. @return The map */
152            static inline const std::map<std::string, Identifier*>& getIdentifierMap() { return Identifier::getIdentifierMapIntern(); }
153            /** @brief Returns a const_iterator to the beginning of the map that stores all Identifiers. @return The const_iterator */
154            static inline std::map<std::string, Identifier*>::const_iterator getIdentifierMapBegin() { return Identifier::getIdentifierMap().begin(); }
155            /** @brief Returns a const_iterator to the end of the map that stores all Identifiers. @return The const_iterator */
156            static inline std::map<std::string, Identifier*>::const_iterator getIdentifierMapEnd() { return Identifier::getIdentifierMap().end(); }
157
158            /** @brief Returns the map that stores all Identifiers with their names in lowercase. @return The map */
159            static inline const std::map<std::string, Identifier*>& getLowercaseIdentifierMap() { return Identifier::getLowercaseIdentifierMapIntern(); }
160            /** @brief Returns a const_iterator to the beginning of the map that stores all Identifiers with their names in lowercase. @return The const_iterator */
161            static inline std::map<std::string, Identifier*>::const_iterator getLowercaseIdentifierMapBegin() { return Identifier::getLowercaseIdentifierMap().begin(); }
162            /** @brief Returns a const_iterator to the end of the map that stores all Identifiers with their names in lowercase. @return The const_iterator */
163            static inline std::map<std::string, Identifier*>::const_iterator getLowercaseIdentifierMapEnd() { return Identifier::getLowercaseIdentifierMap().end(); }
164
165
166            /** @brief Returns the map that stores all config values. @return The const_iterator */
167            inline const std::map<std::string, ConfigValueContainer*>& getConfigValueMap() const { return this->configValues_; }
168            /** @brief Returns a const_iterator to the beginning of the map that stores all config values. @return The const_iterator */
169            inline std::map<std::string, ConfigValueContainer*>::const_iterator getConfigValueMapBegin() const { return this->configValues_.begin(); }
170            /** @brief Returns a const_iterator to the end of the map that stores all config values. @return The const_iterator */
171            inline std::map<std::string, ConfigValueContainer*>::const_iterator getConfigValueMapEnd() const { return this->configValues_.end(); }
172
173            /** @brief Returns the map that stores all config values with their names in lowercase. @return The const_iterator */
174            inline const std::map<std::string, ConfigValueContainer*>& getLowercaseConfigValueMap() const { return this->configValues_LC_; }
175            /** @brief Returns a const_iterator to the beginning of the map that stores all config values with their names in lowercase. @return The const_iterator */
176            inline std::map<std::string, ConfigValueContainer*>::const_iterator getLowercaseConfigValueMapBegin() const { return this->configValues_LC_.begin(); }
177            /** @brief Returns a const_iterator to the end of the map that stores all config values with their names in lowercase. @return The const_iterator */
178            inline std::map<std::string, ConfigValueContainer*>::const_iterator getLowercaseConfigValueMapEnd() const { return this->configValues_LC_.end(); }
179
180
181            /** @brief Returns the map that stores all console commands. @return The const_iterator */
182            inline const std::map<std::string, ExecutorStatic*>& getConsoleCommandMap() const { return this->consoleCommands_; }
183            /** @brief Returns a const_iterator to the beginning of the map that stores all console commands. @return The const_iterator */
184            inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandMapBegin() const { return this->consoleCommands_.begin(); }
185            /** @brief Returns a const_iterator to the end of the map that stores all console commands. @return The const_iterator */
186            inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandMapEnd() const { return this->consoleCommands_.end(); }
187
188            /** @brief Returns the map that stores all console commands with their names in lowercase. @return The const_iterator */
189            inline const std::map<std::string, ExecutorStatic*>& getLowercaseConsoleCommandMap() const { return this->consoleCommands_LC_; }
190            /** @brief Returns a const_iterator to the beginning of the map that stores all console commands with their names in lowercase. @return The const_iterator */
191            inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandMapBegin() const { return this->consoleCommands_LC_.begin(); }
192            /** @brief Returns a const_iterator to the end of the map that stores all console commands with their names in lowercase. @return The const_iterator */
193            inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandMapEnd() const { return this->consoleCommands_LC_.end(); }
194
195
196            /** @brief Returns true if this class has at least one config value. @return True if this class has at least one config value */
197            inline bool hasConfigValues() const { return this->bHasConfigValues_; }
198            /** @brief Returns true if this class has at least one console command. @return True if this class has at least one console command */
199            inline bool hasConsoleCommands() const { return this->bHasConsoleCommands_; }
200
201            /** @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 */
202            inline static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); }
203
204            /** @brief Returns the network ID to identify a class through the network. @return the network ID */
205            inline const unsigned int getNetworkID() const { return this->classID_; }
206
207            /** @brief Sets the network ID to a new value. @param id The new value */
208            void setNetworkID(unsigned int id);
209
210            void addConfigValueContainer(const std::string& varname, ConfigValueContainer* container);
211            ConfigValueContainer* getConfigValueContainer(const std::string& varname);
212            ConfigValueContainer* getLowercaseConfigValueContainer(const std::string& varname);
213
214            virtual void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container) = 0;
215            virtual XMLPortParamContainer* getXMLPortParamContainer(const std::string& paramname) = 0;
216
217            virtual void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container) = 0;
218            virtual XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname) = 0;
219
220            ExecutorStatic& addConsoleCommand(ExecutorStatic* executor, bool bCreateShortcut);
221            ExecutorStatic* getConsoleCommand(const std::string& name) const;
222            ExecutorStatic* getLowercaseConsoleCommand(const std::string& name) const;
223
224        protected:
225            /** @brief Returns the map that stores all Identifiers. @return The map */
226            static std::map<std::string, Identifier*>& getIdentifierMapIntern();
227            /** @brief Returns the map that stores all Identifiers with their names in lowercase. @return The map */
228            static std::map<std::string, Identifier*>& getLowercaseIdentifierMapIntern();
229
230        private:
231            Identifier();
232            Identifier(const Identifier& identifier) {} // don't copy
233            virtual ~Identifier();
234            void initialize(std::set<const Identifier*>* parents);
235
236            /** @brief Returns the children of the class the Identifier belongs to. @return The list of all children */
237            inline std::set<const Identifier*>& getChildrenIntern() const { return (*this->children_); }
238            /** @brief Returns the direct children of the class the Identifier belongs to. @return The list of all direct children */
239            inline std::set<const Identifier*>& getDirectChildrenIntern() const { return (*this->directChildren_); }
240
241            /**
242                @brief Increases the hierarchyCreatingCounter_s variable, causing all new objects to store their parents.
243            */
244            inline static void startCreatingHierarchy()
245            {
246                hierarchyCreatingCounter_s++;
247                COUT(4) << "*** Identifier: Increased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl;
248            }
249
250            /**
251                @brief Decreases the hierarchyCreatingCounter_s variable, causing the objects to stop storing their parents.
252            */
253            inline static void stopCreatingHierarchy()
254            {
255                hierarchyCreatingCounter_s--;
256                COUT(4) << "*** Identifier: Decreased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << std::endl;
257            }
258
259            std::set<const Identifier*> parents_;                          //!< The parents of the class the Identifier belongs to
260            std::set<const Identifier*>* children_;                        //!< The children of the class the Identifier belongs to
261
262            std::set<const Identifier*> directParents_;                    //!< The direct parents of the class the Identifier belongs to
263            std::set<const Identifier*>* directChildren_;                  //!< The direct children of the class the Identifier belongs to
264
265            std::string name_;                                             //!< The name of the class the Identifier belongs to
266
267            BaseFactory* factory_;                                         //!< The Factory, able to create new objects of the given class (if available)
268            bool bCreatedOneObject_;                                       //!< True if at least one object of the given type was created (used to determine the need of storing the parents)
269            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)
270            unsigned int classID_;                                         //!< The network ID to identify a class through the network
271
272            bool bHasConfigValues_;                                        //!< True if this class has at least one assigned config value
273            std::map<std::string, ConfigValueContainer*> configValues_;    //!< A map to link the string of configurable variables with their ConfigValueContainer
274            std::map<std::string, ConfigValueContainer*> configValues_LC_; //!< A map to link the string of configurable variables with their ConfigValueContainer
275
276            bool bHasConsoleCommands_;                                     //!< True if this class has at least one assigned console command
277            std::map<std::string, ExecutorStatic*> consoleCommands_;       //!< All console commands of this class
278            std::map<std::string, ExecutorStatic*> consoleCommands_LC_;    //!< All console commands of this class with their names in lowercase
279    };
280
281    std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list);
282
283
284    // ###############################
285    // ###     ClassIdentifier     ###
286    // ###############################
287    //! The ClassIdentifier is derived from Identifier and holds all class-specific functions and variables the Identifier cannot have.
288    /**
289        ClassIdentifier is a Singleton, which means that only one object of a given type T exists.
290        This makes it possible to store informations about a class, sharing them with all
291        objects of that class without defining static variables in every class.
292
293        To be really sure that not more than exactly one object exists (even with libraries),
294        ClassIdentifiers are only created by IdentifierDistributor.
295
296        You can access the ClassIdentifiers created by IdentifierDistributor through the
297        ClassManager singletons.
298    */
299    template <class T>
300    class ClassIdentifier : public Identifier
301    {
302        template <class TT>
303        friend class ClassManager;
304
305        public:
306            ClassIdentifier<T>* registerClass(std::set<const Identifier*>* parents, const std::string& name, bool bRootClass);
307            void addObject(T* object);
308            void removeObjects() const;
309            void setName(const std::string& name);
310            /** @brief Returns the list of all existing objects of this class. @return The list */
311            inline const ObjectList<T>* getObjects() const { return this->objects_; }
312            /** @brief Returns a list of all existing objects of this class. @return The list */
313            inline const ObjectList<BaseObject>* getObjectList() const { return (ObjectList<BaseObject>*)this->objects_; }
314
315            void updateConfigValues() const;
316
317            XMLPortParamContainer* getXMLPortParamContainer(const std::string& paramname);
318            void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container);
319
320            XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname);
321            void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container);
322
323        private:
324            ClassIdentifier();
325            ClassIdentifier(const ClassIdentifier<T>& identifier) {}    // don't copy
326            ~ClassIdentifier() {}                                       // don't delete
327
328            ObjectList<T>* objects_;                                                                    //!< The ObjectList, containing all objects of type T
329            bool bSetName_;                                                                             //!< True if the name is set
330            std::map<std::string, XMLPortClassParamContainer<T>*> xmlportParamContainers_;              //!< All loadable parameters
331            std::map<std::string, XMLPortClassObjectContainer<T, class O>*> xmlportObjectContainers_;   //!< All attachable objects
332    };
333
334    /**
335        @brief Constructor: Creates the ObjectList.
336    */
337    template <class T>
338    ClassIdentifier<T>::ClassIdentifier()
339    {
340//        this->objects_ = ObjectList<T>::getList();
341        this->objects_ = new ObjectList<T>();
342        this->bSetName_ = false;
343    }
344
345    /**
346        @brief Registers a class, which means that the name and the parents get stored.
347        @param parents A list, containing the Identifiers of all parents of the class
348        @param name A string, containing exactly the name of the class
349        @param bRootClass True if the class is either an Interface or the BaseObject itself
350        @return The ClassIdentifier itself
351    */
352    template <class T>
353    ClassIdentifier<T>* ClassIdentifier<T>::registerClass(std::set<const Identifier*>* parents, const std::string& name, bool bRootClass)
354    {
355        this->setName(name);
356
357        // Check if at least one object of the given type was created
358        if (!this->bCreatedOneObject_ && Identifier::isCreatingHierarchy())
359        {
360            // If no: We have to store the informations and initialize the Identifier
361            COUT(4) << "*** ClassIdentifier: Register Class in " << name << "-Singleton -> Initialize Singleton." << std::endl;
362            if (bRootClass)
363                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.
364            else
365                this->initialize(parents);
366        }
367
368        return this;
369    }
370
371    /**
372        @brief Sets the name of the class.
373        @param name The name
374    */
375    template <class T>
376    void ClassIdentifier<T>::setName(const std::string& name)
377    {
378        if (!this->bSetName_)
379        {
380            this->name_ = name;
381            this->bSetName_ = true;
382            Identifier::getIdentifierMapIntern()[name] = this;
383            Identifier::getLowercaseIdentifierMapIntern()[getLowercase(name)] = this;
384        }
385    }
386
387    /**
388        @brief Adds an object of the given type to the ObjectList.
389        @param object The object to add
390    */
391    template <class T>
392    void ClassIdentifier<T>::addObject(T* object)
393    {
394        COUT(5) << "*** ClassIdentifier: Added object to " << this->getName() << "-list." << std::endl;
395        object->getMetaList().add(this->objects_, this->objects_->add(object));
396    }
397
398    /**
399        @brief Removes all objects of the corresponding class.
400    */
401    template <class T>
402    void ClassIdentifier<T>::removeObjects() const
403    {
404        for (Iterator<T> it = this->objects_->start(); it;)
405            delete *(it++);
406    }
407
408    /**
409        @brief Updates the config-values of all existing objects of this class by calling their setConfigValues() function.
410    */
411    template <class T>
412    void ClassIdentifier<T>::updateConfigValues() const
413    {
414        for (Iterator<T> it = this->objects_->start(); it; ++it)
415            ((T*)*it)->setConfigValues();
416    }
417
418    /**
419        @brief Returns a XMLPortParamContainer that loads a parameter of this class.
420        @param paramname The name of the parameter
421        @return The container
422    */
423    template <class T>
424    XMLPortParamContainer* ClassIdentifier<T>::getXMLPortParamContainer(const std::string& paramname)
425    {
426        typename std::map<std::string, XMLPortClassParamContainer<T>*>::const_iterator it = xmlportParamContainers_.find(paramname);
427        if (it != xmlportParamContainers_.end())
428            return (XMLPortParamContainer*)((*it).second);
429        else
430            return 0;
431    }
432
433    /**
434        @brief Adds a new XMLPortParamContainer that loads a parameter of this class.
435        @param paramname The name of the parameter
436        @param container The container
437    */
438    template <class T>
439    void ClassIdentifier<T>::addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container)
440    {
441        this->xmlportParamContainers_[paramname] = (XMLPortClassParamContainer<T>*)container;
442    }
443
444    /**
445        @brief Returns a XMLPortObjectContainer that attaches an object to this class.
446        @param sectionname The name of the section that contains the attachable objects
447        @return The container
448    */
449    template <class T>
450    XMLPortObjectContainer* ClassIdentifier<T>::getXMLPortObjectContainer(const std::string& sectionname)
451    {
452        typename std::map<std::string, XMLPortClassObjectContainer<T, class O>*>::const_iterator it = xmlportObjectContainers_.find(sectionname);
453        if (it != xmlportObjectContainers_.end())
454            return (XMLPortObjectContainer*)((*it).second);
455        else
456            return 0;
457    }
458
459    /**
460        @brief Adds a new XMLPortObjectContainer that attaches an object to this class.
461        @param sectionname The name of the section that contains the attachable objects
462        @param container The container
463    */
464    template <class T>
465    void ClassIdentifier<T>::addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container)
466    {
467        this->xmlportObjectContainers_[sectionname] = (XMLPortClassObjectContainer<T, class O>*)container;
468    }
469
470
471    // ###############################
472    // ###   SubclassIdentifier    ###
473    // ###############################
474    //! The SubclassIdentifier acts almost like an Identifier, but has some prerequisites.
475    /**
476        You can only assign an Identifier that belongs to a class T (or derived) to a SubclassIdentifier<T>.
477        If you assign something else, the program aborts.
478        Because we know the minimal type, a dynamic_cast is done, which makes it easier to create a new object.
479    */
480    template <class T>
481    class SubclassIdentifier
482    {
483        public:
484            /**
485                @brief Constructor: Automaticaly assigns the Identifier of the given class.
486            */
487            SubclassIdentifier()
488            {
489                this->identifier_ = ClassManager<T>::getIdentifier();
490            }
491
492            /**
493                @brief Copyconstructor: Assigns the given Identifier.
494                @param identifier The Identifier
495            */
496            SubclassIdentifier(Identifier* identifier)
497            {
498                this->identifier_ = identifier;
499            }
500
501            /**
502                @brief Overloading of the = operator: assigns the identifier and checks its type.
503                @param identifier The Identifier to assign
504                @return The SubclassIdentifier itself
505            */
506            SubclassIdentifier<T>& operator=(Identifier* identifier)
507            {
508                if (!identifier->isA(ClassManager<T>::getIdentifier()))
509                {
510                    COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl;
511                    COUT(1) << "Error: Class " << identifier->getName() << " is not a " << ClassManager<T>::getIdentifier()->getName() << "!" << std::endl;
512                    COUT(1) << "Error: SubclassIdentifier<" << ClassManager<T>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden." << std::endl;
513                    COUT(1) << "Aborting..." << std::endl;
514                    abort();
515                }
516                this->identifier_ = identifier;
517                return *this;
518            }
519
520            /**
521                @brief Overloading of the * operator: returns the assigned identifier.
522                @return The assigned identifier
523            */
524            Identifier* operator*()
525            {
526                return this->identifier_;
527            }
528
529            /**
530                @brief Overloading of the -> operator: returns the assigned identifier.
531                @return The assigned identifier
532            */
533            Identifier* operator->() const
534            {
535                return this->identifier_;
536            }
537
538            /**
539                @brief Creates a new object of the type of the assigned Identifier and dynamic_casts it to the minimal type given by T.
540                @return The new object
541            */
542            T* fabricate()
543            {
544                BaseObject* newObject = this->identifier_->fabricate();
545
546                // Check if the creation was successful
547                if (newObject)
548                {
549                    // Do a dynamic_cast, because an object of type T is much better than of type BaseObject
550                    return (T*)(newObject);
551                }
552                else
553                {
554                    // Something went terribly wrong
555                    if (this->identifier_)
556                    {
557                        COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl;
558                        COUT(1) << "Error: Class " << this->identifier_->getName() << " is not a " << ClassManager<T>::getIdentifier()->getName() << "!" << std::endl;
559                        COUT(1) << "Error: Couldn't fabricate a new Object." << std::endl;
560                        COUT(1) << "Aborting..." << std::endl;
561                    }
562                    else
563                    {
564                        COUT(1) << "An error occurred in SubclassIdentifier (Identifier.h):" << std::endl;
565                        COUT(1) << "Error: Couldn't fabricate a new Object - Identifier is undefined." << std::endl;
566                        COUT(1) << "Aborting..." << std::endl;
567                    }
568
569                    abort();
570                }
571            }
572
573            /** @brief Returns the assigned identifier. @return The identifier */
574            inline const Identifier* getIdentifier() const
575                { return this->identifier_; }
576
577            /** @brief Returns true, if the assigned identifier is at least of the given type. @param identifier The identifier to compare with */
578            inline bool isA(const Identifier* identifier) const
579                { return this->identifier_->isA(identifier); }
580
581            /** @brief Returns true, if the assigned identifier is exactly of the given type. @param identifier The identifier to compare with */
582            inline bool isExactlyA(const Identifier* identifier) const
583                { return this->identifier_->isExactlyA(identifier); }
584
585            /** @brief Returns true, if the assigned identifier is a child of the given identifier. @param identifier The identifier to compare with */
586            inline bool isChildOf(const Identifier* identifier) const
587                { return this->identifier_->isChildOf(identifier); }
588
589            /** @brief Returns true, if the assigned identifier is a direct child of the given identifier. @param identifier The identifier to compare with */
590            inline bool isDirectChildOf(const Identifier* identifier) const
591                { return this->identifier_->isDirectChildOf(identifier); }
592
593            /** @brief Returns true, if the assigned identifier is a parent of the given identifier. @param identifier The identifier to compare with */
594            inline bool isParentOf(const Identifier* identifier) const
595                { return this->identifier_->isParentOf(identifier); }
596
597            /** @brief Returns true, if the assigned identifier is a direct parent of the given identifier. @param identifier The identifier to compare with */
598            inline bool isDirectParentOf(const Identifier* identifier) const
599                { return this->identifier_->isDirectParentOf(identifier); }
600
601        private:
602            Identifier* identifier_;            //!< The assigned identifier
603    };
604}
605
606#endif /* _Identifier_H__ */
Note: See TracBrowser for help on using the repository browser.