[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; |
---|
| 24 | |
---|
[149] | 25 | class Identifier |
---|
[132] | 26 | { |
---|
[149] | 27 | template <class T> |
---|
| 28 | friend class ClassIdentifier; |
---|
| 29 | |
---|
[132] | 30 | public: |
---|
[149] | 31 | // static Identifier* registerClass(IdentifierList* parents); |
---|
| 32 | void addObject(BaseObject* object); |
---|
| 33 | void removeObject(BaseObject* object); |
---|
[132] | 34 | |
---|
[149] | 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 | |
---|
[132] | 56 | private: |
---|
[149] | 57 | bool bCreatedOneObject_; |
---|
[132] | 58 | }; |
---|
| 59 | |
---|
[149] | 60 | template <class T> |
---|
| 61 | class ClassIdentifier : public Identifier |
---|
[132] | 62 | { |
---|
[149] | 63 | // friend class Identifier; |
---|
| 64 | |
---|
[132] | 65 | public: |
---|
[149] | 66 | static Identifier* registerClass(IdentifierList* parents); |
---|
| 67 | static Identifier* getIdentifier(); |
---|
| 68 | static T* create(); |
---|
[132] | 69 | |
---|
[149] | 70 | private: |
---|
| 71 | ClassIdentifier(); |
---|
[132] | 72 | }; |
---|
| 73 | |
---|
[149] | 74 | #define getStringFromClassName(ClassName) \ |
---|
| 75 | #ClassName |
---|
| 76 | |
---|
| 77 | template <class T> |
---|
| 78 | ClassIdentifier<T>::ClassIdentifier() |
---|
[132] | 79 | { |
---|
[149] | 80 | } |
---|
[132] | 81 | |
---|
[149] | 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 | } |
---|
[132] | 91 | |
---|
[149] | 92 | return pointer_; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | template <class T> |
---|
| 96 | Identifier* ClassIdentifier<T>::getIdentifier() |
---|
[132] | 97 | { |
---|
[149] | 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 | { |
---|
[132] | 118 | public: |
---|
[149] | 119 | IdentifierList(); |
---|
| 120 | ~IdentifierList(); |
---|
| 121 | void add(Identifier* identifier); |
---|
| 122 | void remove(Identifier* identifier); |
---|
| 123 | bool isInList(Identifier* identifier); |
---|
[132] | 124 | |
---|
[149] | 125 | IdentifierListElement* first_; |
---|
[132] | 126 | }; |
---|
| 127 | |
---|
[149] | 128 | class IdentifierListElement |
---|
[132] | 129 | { |
---|
| 130 | public: |
---|
[149] | 131 | IdentifierListElement(Identifier* identifier); |
---|
[132] | 132 | |
---|
[149] | 133 | Identifier* identifier_; |
---|
| 134 | IdentifierListElement* next_; |
---|
| 135 | bool bDirect_; |
---|
[132] | 136 | }; |
---|
| 137 | |
---|
[149] | 138 | |
---|
| 139 | // ##### Object List ##### |
---|
| 140 | class ObjectListElement; |
---|
| 141 | |
---|
[132] | 142 | class ObjectList |
---|
| 143 | { |
---|
| 144 | public: |
---|
| 145 | ObjectList(); |
---|
| 146 | ~ObjectList(); |
---|
| 147 | void add(BaseObject* object); |
---|
| 148 | void remove(BaseObject* object); |
---|
| 149 | |
---|
[149] | 150 | ObjectListElement* first_; |
---|
[132] | 151 | }; |
---|
| 152 | |
---|
[149] | 153 | class ObjectListElement |
---|
[132] | 154 | { |
---|
| 155 | public: |
---|
[149] | 156 | ObjectListElement(BaseObject* object); |
---|
[132] | 157 | |
---|
[149] | 158 | BaseObject* object_; |
---|
| 159 | ObjectListElement* next_; |
---|
[132] | 160 | }; |
---|
| 161 | |
---|
| 162 | |
---|
[149] | 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) |
---|
[132] | 169 | |
---|
[149] | 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) |
---|
[132] | 175 | |
---|
| 176 | #define unregisterObject() \ |
---|
[149] | 177 | delete this->parents_; \ |
---|
| 178 | this->identifier_->removeObject(this) |
---|
[132] | 179 | |
---|
[149] | 180 | #define Class(ClassName) \ |
---|
| 181 | ClassIdentifier<ClassName>::getIdentifier() |
---|
[132] | 182 | |
---|
[149] | 183 | #define Factory(ClassName) \ |
---|
| 184 | ClassIdentifier<ClassName>::create() |
---|
| 185 | } |
---|
| 186 | |
---|
[132] | 187 | #endif |
---|