Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

no idea if this has any chance to work… template-syntax is crap

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