Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 221 was 221, checked in by landauf, 16 years ago
  • made ObjectList double-linked to allow forward- and backward-iterating. its now a LI(F/L)O list.
  • added an iterator to iterate through object-lists. you can iterate forwards and backwards.

iterating forwards is easy: you get "0 1 2 … last"
iterating backwards is a bit tricky: you still get "0" first, but then "last … 2 1".
thats caused by the structure of the for-loop: you get the first element before the iterator knows if you'll increase or decrease it

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