[132] | 1 | #ifndef _ClassHierarchy_H__ |
---|
| 2 | #define _ClassHierarchy_H__ |
---|
| 3 | |
---|
| 4 | #include <string> |
---|
[162] | 5 | #include <iostream> |
---|
[132] | 6 | |
---|
| 7 | // DONE: |
---|
| 8 | // - klassenhierarchie aufbauen |
---|
[149] | 9 | // - in listen einfügen |
---|
| 10 | // - factory |
---|
[132] | 11 | // - klassen-identifier |
---|
[149] | 12 | // - isA u.ä. vergleiche |
---|
[132] | 13 | |
---|
| 14 | // TODO: |
---|
| 15 | // - durch listen iterieren |
---|
| 16 | // - searchtree für classname-strings |
---|
| 17 | |
---|
[149] | 18 | |
---|
| 19 | namespace orxonox |
---|
| 20 | { |
---|
[162] | 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 | |
---|
[149] | 36 | // ##### Identifier ##### |
---|
| 37 | class IdentifierList; |
---|
[132] | 38 | class ObjectList; |
---|
[162] | 39 | class OrxonoxClass; |
---|
[150] | 40 | template <class T> |
---|
| 41 | class ClassIdentifier; |
---|
[132] | 42 | |
---|
[149] | 43 | class Identifier |
---|
[132] | 44 | { |
---|
[149] | 45 | template <class T> |
---|
| 46 | friend class ClassIdentifier; |
---|
| 47 | |
---|
[132] | 48 | public: |
---|
[162] | 49 | void addObject(OrxonoxClass* object); |
---|
| 50 | void removeObject(OrxonoxClass* object); |
---|
[132] | 51 | |
---|
[149] | 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 | |
---|
[150] | 59 | private: |
---|
[149] | 60 | Identifier(); |
---|
[150] | 61 | void initialize(IdentifierList* parents); |
---|
[149] | 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_; |
---|
[132] | 72 | }; |
---|
| 73 | |
---|
[149] | 74 | template <class T> |
---|
| 75 | class ClassIdentifier : public Identifier |
---|
[132] | 76 | { |
---|
| 77 | public: |
---|
[162] | 78 | static ClassIdentifier<T>* registerClass(IdentifierList* parents, std::string name, bool bRootClass); |
---|
[150] | 79 | static ClassIdentifier<T>* getIdentifier(); |
---|
[149] | 80 | static T* create(); |
---|
[132] | 81 | |
---|
[149] | 82 | private: |
---|
| 83 | ClassIdentifier(); |
---|
[150] | 84 | |
---|
| 85 | static ClassIdentifier<T>* pointer_; |
---|
| 86 | |
---|
[132] | 87 | }; |
---|
| 88 | |
---|
[149] | 89 | template <class T> |
---|
[150] | 90 | ClassIdentifier<T>* ClassIdentifier<T>::pointer_ = NULL; |
---|
| 91 | |
---|
| 92 | template <class T> |
---|
[149] | 93 | ClassIdentifier<T>::ClassIdentifier() |
---|
[132] | 94 | { |
---|
[149] | 95 | } |
---|
[132] | 96 | |
---|
[149] | 97 | template <class T> |
---|
[162] | 98 | ClassIdentifier<T>* ClassIdentifier<T>::registerClass(IdentifierList* parents, std::string name, bool bRootClass) |
---|
[149] | 99 | { |
---|
[162] | 100 | std::cout << "*** Register Class in " << name << "-Singleton.\n"; |
---|
[149] | 101 | if (!pointer_) |
---|
| 102 | { |
---|
[162] | 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 | } |
---|
[149] | 114 | } |
---|
[132] | 115 | |
---|
[149] | 116 | return pointer_; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | template <class T> |
---|
[150] | 120 | ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier() |
---|
[132] | 121 | { |
---|
[162] | 122 | std::cout << "*** Get Identifier.\n"; |
---|
[149] | 123 | if (!pointer_) |
---|
| 124 | { |
---|
[162] | 125 | std::cout << "*** Get Identifier -> Create Class\n"; |
---|
| 126 | ClassHierarchy::getSingleton()->createHierarchy(true); |
---|
[149] | 127 | T* temp = new T(); |
---|
[162] | 128 | ClassHierarchy::getSingleton()->createHierarchy(false); |
---|
[149] | 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 | { |
---|
[132] | 146 | public: |
---|
[149] | 147 | IdentifierList(); |
---|
| 148 | ~IdentifierList(); |
---|
| 149 | void add(Identifier* identifier); |
---|
| 150 | void remove(Identifier* identifier); |
---|
| 151 | bool isInList(Identifier* identifier); |
---|
[132] | 152 | |
---|
[149] | 153 | IdentifierListElement* first_; |
---|
[132] | 154 | }; |
---|
| 155 | |
---|
[149] | 156 | class IdentifierListElement |
---|
[132] | 157 | { |
---|
| 158 | public: |
---|
[149] | 159 | IdentifierListElement(Identifier* identifier); |
---|
[132] | 160 | |
---|
[149] | 161 | Identifier* identifier_; |
---|
| 162 | IdentifierListElement* next_; |
---|
| 163 | bool bDirect_; |
---|
[132] | 164 | }; |
---|
| 165 | |
---|
[149] | 166 | |
---|
| 167 | // ##### Object List ##### |
---|
| 168 | class ObjectListElement; |
---|
| 169 | |
---|
[132] | 170 | class ObjectList |
---|
| 171 | { |
---|
| 172 | public: |
---|
| 173 | ObjectList(); |
---|
| 174 | ~ObjectList(); |
---|
[162] | 175 | void add(OrxonoxClass* object); |
---|
| 176 | void remove(OrxonoxClass* object); |
---|
[132] | 177 | |
---|
[149] | 178 | ObjectListElement* first_; |
---|
[132] | 179 | }; |
---|
| 180 | |
---|
[149] | 181 | class ObjectListElement |
---|
[132] | 182 | { |
---|
| 183 | public: |
---|
[162] | 184 | ObjectListElement(OrxonoxClass* object); |
---|
[132] | 185 | |
---|
[162] | 186 | OrxonoxClass* object_; |
---|
[149] | 187 | ObjectListElement* next_; |
---|
[132] | 188 | }; |
---|
| 189 | |
---|
[149] | 190 | // ##### Macros ##### |
---|
| 191 | #define registerRootObject(ClassName) \ |
---|
[162] | 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) |
---|
[132] | 201 | |
---|
[149] | 202 | #define registerObject(ClassName) \ |
---|
[162] | 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) |
---|
[132] | 209 | |
---|
| 210 | #define unregisterObject() \ |
---|
[162] | 211 | this->getIdentifier()->removeObject(this) |
---|
[132] | 212 | |
---|
[149] | 213 | #define Class(ClassName) \ |
---|
| 214 | ClassIdentifier<ClassName>::getIdentifier() |
---|
[132] | 215 | |
---|
[149] | 216 | #define Factory(ClassName) \ |
---|
| 217 | ClassIdentifier<ClassName>::create() |
---|
| 218 | } |
---|
| 219 | |
---|
[132] | 220 | #endif |
---|