Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchie/src/Identifier.h @ 246

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

added "MetaObjectList", containing the list-element of the ObjectList of all classes an object is registered in. this allowes much faster deleting of objects.

File size: 8.0 KB
RevLine 
[197]1#ifndef _Identifier_H__
2#define _Identifier_H__
3
[219]4#include <iostream>
5
[197]6#include "IdentifierList.h"
7#include "ObjectList.h"
[218]8#include "Factory.h"
[197]9
[246]10#define HIERARCHY_VERBOSE false
[219]11
12
[197]13namespace orxonox
14{
[219]15    class BaseObject;
16
[224]17    // ###############################
18    // ###       Identifier        ###
19    // ###############################
[197]20    class Identifier
21    {
22        template <class T>
23        friend class ClassIdentifier;
24
25        template <class T>
[242]26        friend class SubclassIdentifier;
[197]27
[244]28        template <class T>
29        friend class ClassFactory;
30
[197]31        public:
[244]32            inline void addFactory(BaseFactory* factory) { this->factory_ = factory; }
33            BaseObject* fabricate();
[218]34
[239]35            bool isA(const Identifier* identifier) const;
36            bool isDirectlyA(const Identifier* identifier) const;
37            bool isChildOf(const Identifier* identifier) const;
38            bool isParentOf(const Identifier* identifier) const;
[197]39
[244]40            inline const std::string& getName() const { return this->name_; }
41            inline const IdentifierList& getParents() const { return this->parents_; }
42            inline IdentifierList& getChildren() const { return *this->children_; }
[197]43
[244]44            inline static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); }
[219]45
[197]46        private:
47            Identifier();
48            Identifier(const Identifier& identifier) {}
49            virtual ~Identifier();
[239]50            void initialize(const IdentifierList* parents);
[197]51
[244]52            inline static void startCreatingHierarchy()
[231]53            {
54                hierarchyCreatingCounter_s++;
55#if HIERARCHY_VERBOSE
56                std::cout << "*** Increased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << "\n";
57#endif
58            }
[197]59
[244]60            inline static void stopCreatingHierarchy()
[231]61            {
62                hierarchyCreatingCounter_s--;
63#if HIERARCHY_VERBOSE
64                std::cout << "*** Decreased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << "\n";
65#endif
66            }
67
[243]68            IdentifierList parents_;
69            IdentifierList* children_;
[219]70
[197]71            std::string name_;
72
[244]73            BaseFactory* factory_;
[197]74            bool bCreatedOneObject_;
[219]75            static int hierarchyCreatingCounter_s;
[197]76    };
77
78
[224]79    // ###############################
80    // ###     ClassIdentifier     ###
81    // ###############################
[197]82    template <class T>
83    class ClassIdentifier : public Identifier
84    {
[224]85        template <class U>
86        friend class Iterator;
87
[197]88        public:
[244]89            static ClassIdentifier<T>* registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass);
[197]90            static ClassIdentifier<T>* getIdentifier();
[224]91            static void addObject(T* object);
[197]92
93        private:
94            ClassIdentifier();
95            ClassIdentifier(const ClassIdentifier<T>& identifier) {}
96            ~ClassIdentifier();
97
[219]98            static ClassIdentifier<T>* pointer_s;
[239]99            ObjectList<T>* objects_;
[197]100    };
101
102    template <class T>
[219]103    ClassIdentifier<T>* ClassIdentifier<T>::pointer_s = NULL;
[197]104
105    template <class T>
106    ClassIdentifier<T>::ClassIdentifier()
107    {
[239]108        this->objects_ = new ObjectList<T>;
[197]109    }
110
111    template <class T>
112    ClassIdentifier<T>::~ClassIdentifier()
113    {
[239]114        delete this->objects_;
[219]115        this->pointer_s = NULL;
[197]116    }
117
118    template <class T>
[244]119    ClassIdentifier<T>* ClassIdentifier<T>::registerClass(const IdentifierList* parents, const std::string& name, bool bRootClass)
[218]120    {
[231]121#if HIERARCHY_VERBOSE
[197]122        std::cout << "*** Register Class in " << name << "-Singleton.\n";
[231]123#endif
[219]124        if (!pointer_s)
[197]125        {
[231]126#if HIERARCHY_VERBOSE
[197]127            std::cout << "*** Register Class in " << name << "-Singleton -> Create Singleton.\n";
[231]128#endif
[244]129            pointer_s = new ClassIdentifier();
130        }
[218]131
[244]132        if (!pointer_s->bCreatedOneObject_)
133        {
134#if HIERARCHY_VERBOSE
135            std::cout << "*** Register Class in " << name << "-Singleton -> Initialize Singleton.\n";
136#endif
137            pointer_s->name_ = name;
138            Factory::add(name, pointer_s);
[218]139
[244]140            if (bRootClass)
141                pointer_s->initialize(NULL);
[197]142            else
[244]143                pointer_s->initialize(parents);
[197]144        }
145
[219]146        return pointer_s;
[197]147    }
148
149    template <class T>
150    ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier()
151    {
[219]152        if (!pointer_s)
[197]153        {
[231]154#if HIERARCHY_VERBOSE
[244]155            std::cout << "*** Create Singleton.\n";
[231]156#endif
[244]157            pointer_s = new ClassIdentifier();
[197]158        }
159
[219]160        return pointer_s;
[197]161    }
162
[224]163    template <class T>
164    void ClassIdentifier<T>::addObject(T* object)
165    {
[231]166#if HIERARCHY_VERBOSE
[224]167        std::cout << "*** Added object to " << ClassIdentifier<T>::getIdentifier()->getName() << "-list.\n";
[231]168#endif
[246]169        object->getMetaList()->add(ClassIdentifier<T>::getIdentifier()->objects_, ClassIdentifier<T>::getIdentifier()->objects_->add(object));
[224]170    }
171
172
173    // ###############################
[242]174    // ###   SubclassIdentifier    ###
[224]175    // ###############################
[197]176    template <class B>
[242]177    class SubclassIdentifier
[197]178    {
179        public:
[242]180            SubclassIdentifier();
[197]181
[242]182            SubclassIdentifier<B>& operator=(Identifier* identifier)
[197]183            {
184                if (!identifier->isA(ClassIdentifier<B>::getIdentifier()))
185                {
186                    std::cout << "Error: Class " << identifier->getName() << " is not a " << ClassIdentifier<B>::getIdentifier()->getName() << "!\n";
[242]187                    std::cout << "Error: SubclassIdentifier<" << ClassIdentifier<B>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden.\n";
[197]188                    std::cout << "Aborting...\n";
189                    abort();
190                }
191                this->identifier_ = identifier;
192                return *this;
193            }
[218]194
[221]195            Identifier* operator*()
[218]196            {
197                return this->identifier_;
198            }
199
[221]200            Identifier* operator->() const
[218]201            {
202                return this->identifier_;
203            }
204
205            B* fabricate()
206            {
[219]207                BaseObject* newObject = this->identifier_->fabricate();
[218]208                if (newObject)
209                {
210                    return dynamic_cast<B*>(newObject);
211                }
212                else
213                {
214                    if (this->identifier_)
215                    {
216                        std::cout << "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<B>::getIdentifier()->getName() << "!\n";
217                        std::cout << "Error: Couldn't fabricate a new Object.\n";
218                        std::cout << "Aborting...\n";
219                    }
220                    else
221                    {
222                        std::cout << "Error: Couldn't fabricate a new Object - Identifier is undefined.\n";
223                        std::cout << "Aborting...\n";
224                    }
225
226                    abort();
227                }
228            }
229
[239]230            inline const Identifier* getIdentifier() const
[197]231                { return this->identifier_; }
[239]232            inline bool isA(const Identifier* identifier) const
[197]233                { return this->identifier_->isA(identifier); }
[239]234            inline bool isDirectlyA(const Identifier* identifier) const
[197]235                { return this->identifier_->isDirectlyA(identifier); }
[239]236            inline bool isChildOf(const Identifier* identifier) const
[197]237                { return this->identifier_->isChildOf(identifier); }
[239]238            inline bool isParentOf(const Identifier* identifier) const
[197]239                { return this->identifier_->isParentOf(identifier); }
240
241        private:
242            Identifier* identifier_;
243    };
244
245    template <class B>
[242]246    SubclassIdentifier<B>::SubclassIdentifier()
[197]247    {
248        this->identifier_ = ClassIdentifier<B>::getIdentifier();
249    }
250}
251
252#endif
Note: See TracBrowser for help on using the repository browser.