Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

it starts to work, but there is still much to do.

@bensch: thanks for you mail, but i can't tell you much at the moment - i'm still changing lots of things and i have no idea if everything will work as intendet, so i'll write you as soon as i have reliable informations :)
</abuse log for pms>

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