Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchie/src/ClassHierarchy.h @ 172

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

some features work perfectly,
some features are fucked up,
some features arent yet implemented.

x3n→hair→brightness++;

theres still a lot to do, but i can see the light on the end of the tunnel.
templates, makros, operator overloading, function pointers… the beauty of the beast. i'm in love and still dying piece by piece with every line of code i'm writing.
</film noir>

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