[132] | 1 | #ifndef _ClassHierarchy_H__ |
---|
| 2 | #define _ClassHierarchy_H__ |
---|
| 3 | |
---|
| 4 | #include <string> |
---|
| 5 | |
---|
| 6 | // DONE: |
---|
| 7 | // - klassenhierarchie aufbauen |
---|
[149] | 8 | // - in listen einfügen |
---|
| 9 | // - factory |
---|
[132] | 10 | // - klassen-identifier |
---|
[149] | 11 | // - isA u.ä. vergleiche |
---|
[132] | 12 | |
---|
| 13 | // TODO: |
---|
| 14 | // - durch listen iterieren |
---|
| 15 | // - searchtree für classname-strings |
---|
| 16 | |
---|
[149] | 17 | |
---|
| 18 | namespace orxonox |
---|
| 19 | { |
---|
| 20 | // ##### Identifier ##### |
---|
| 21 | class IdentifierList; |
---|
[132] | 22 | class ObjectList; |
---|
| 23 | class BaseObject; |
---|
[150] | 24 | template <class T> |
---|
| 25 | class ClassIdentifier; |
---|
[132] | 26 | |
---|
[149] | 27 | class Identifier |
---|
[132] | 28 | { |
---|
[149] | 29 | template <class T> |
---|
| 30 | friend class ClassIdentifier; |
---|
| 31 | |
---|
[132] | 32 | public: |
---|
[149] | 33 | void addObject(BaseObject* object); |
---|
| 34 | void removeObject(BaseObject* object); |
---|
[132] | 35 | |
---|
[149] | 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 | |
---|
[150] | 43 | private: |
---|
[149] | 44 | Identifier(); |
---|
[150] | 45 | void initialize(IdentifierList* parents); |
---|
[149] | 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_; |
---|
[132] | 56 | }; |
---|
| 57 | |
---|
[149] | 58 | template <class T> |
---|
| 59 | class ClassIdentifier : public Identifier |
---|
[132] | 60 | { |
---|
| 61 | public: |
---|
[150] | 62 | static ClassIdentifier<T>* registerClass(IdentifierList* parents); |
---|
| 63 | static ClassIdentifier<T>* getIdentifier(); |
---|
[149] | 64 | static T* create(); |
---|
[132] | 65 | |
---|
[149] | 66 | private: |
---|
| 67 | ClassIdentifier(); |
---|
[150] | 68 | |
---|
| 69 | static ClassIdentifier<T>* pointer_; |
---|
| 70 | |
---|
[132] | 71 | }; |
---|
| 72 | |
---|
[149] | 73 | #define getStringFromClassName(ClassName) \ |
---|
| 74 | #ClassName |
---|
| 75 | |
---|
| 76 | template <class T> |
---|
[150] | 77 | ClassIdentifier<T>* ClassIdentifier<T>::pointer_ = NULL; |
---|
| 78 | |
---|
| 79 | template <class T> |
---|
[149] | 80 | ClassIdentifier<T>::ClassIdentifier() |
---|
[132] | 81 | { |
---|
[149] | 82 | } |
---|
[132] | 83 | |
---|
[149] | 84 | template <class T> |
---|
[150] | 85 | ClassIdentifier<T>* ClassIdentifier<T>::registerClass(IdentifierList* parents) |
---|
[149] | 86 | { |
---|
| 87 | if (!pointer_) |
---|
| 88 | { |
---|
| 89 | pointer_ = new ClassIdentifier(); |
---|
| 90 | pointer_->name_ = getStringFromClassName(T); |
---|
| 91 | pointer_->initialize(parents); |
---|
| 92 | } |
---|
[132] | 93 | |
---|
[149] | 94 | return pointer_; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | template <class T> |
---|
[150] | 98 | ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier() |
---|
[132] | 99 | { |
---|
[149] | 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 | { |
---|
[132] | 120 | public: |
---|
[149] | 121 | IdentifierList(); |
---|
| 122 | ~IdentifierList(); |
---|
| 123 | void add(Identifier* identifier); |
---|
| 124 | void remove(Identifier* identifier); |
---|
| 125 | bool isInList(Identifier* identifier); |
---|
[132] | 126 | |
---|
[149] | 127 | IdentifierListElement* first_; |
---|
[132] | 128 | }; |
---|
| 129 | |
---|
[149] | 130 | class IdentifierListElement |
---|
[132] | 131 | { |
---|
| 132 | public: |
---|
[149] | 133 | IdentifierListElement(Identifier* identifier); |
---|
[132] | 134 | |
---|
[149] | 135 | Identifier* identifier_; |
---|
| 136 | IdentifierListElement* next_; |
---|
| 137 | bool bDirect_; |
---|
[132] | 138 | }; |
---|
| 139 | |
---|
[149] | 140 | |
---|
| 141 | // ##### Object List ##### |
---|
| 142 | class ObjectListElement; |
---|
| 143 | |
---|
[132] | 144 | class ObjectList |
---|
| 145 | { |
---|
| 146 | public: |
---|
| 147 | ObjectList(); |
---|
| 148 | ~ObjectList(); |
---|
| 149 | void add(BaseObject* object); |
---|
| 150 | void remove(BaseObject* object); |
---|
| 151 | |
---|
[149] | 152 | ObjectListElement* first_; |
---|
[132] | 153 | }; |
---|
| 154 | |
---|
[149] | 155 | class ObjectListElement |
---|
[132] | 156 | { |
---|
| 157 | public: |
---|
[149] | 158 | ObjectListElement(BaseObject* object); |
---|
[132] | 159 | |
---|
[149] | 160 | BaseObject* object_; |
---|
| 161 | ObjectListElement* next_; |
---|
[132] | 162 | }; |
---|
| 163 | |
---|
| 164 | |
---|
[149] | 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) |
---|
[132] | 171 | |
---|
[149] | 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) |
---|
[132] | 177 | |
---|
| 178 | #define unregisterObject() \ |
---|
[149] | 179 | delete this->parents_; \ |
---|
| 180 | this->identifier_->removeObject(this) |
---|
[132] | 181 | |
---|
[149] | 182 | #define Class(ClassName) \ |
---|
| 183 | ClassIdentifier<ClassName>::getIdentifier() |
---|
[132] | 184 | |
---|
[149] | 185 | #define Factory(ClassName) \ |
---|
| 186 | ClassIdentifier<ClassName>::create() |
---|
| 187 | } |
---|
| 188 | |
---|
[132] | 189 | #endif |
---|