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