[197] | 1 | #ifndef _Identifier_H__ |
---|
| 2 | #define _Identifier_H__ |
---|
| 3 | |
---|
[219] | 4 | #include <iostream> |
---|
| 5 | |
---|
[197] | 6 | #include "IdentifierList.h" |
---|
| 7 | #include "ObjectList.h" |
---|
[219] | 8 | //#include "OrxonoxClass.h" |
---|
[218] | 9 | #include "Factory.h" |
---|
[197] | 10 | |
---|
[219] | 11 | // DONE AND TESTED: |
---|
| 12 | // - build class hierarchy |
---|
| 13 | // - isA, isChildOf, ... |
---|
| 14 | // - insert into class-lists |
---|
| 15 | // - ClassIdentifier |
---|
| 16 | // - BaseIdentifier |
---|
| 17 | // - Factory |
---|
| 18 | |
---|
| 19 | // IN WORK: |
---|
| 20 | |
---|
| 21 | // TO DO: |
---|
| 22 | // - iterate through lists |
---|
| 23 | |
---|
[197] | 24 | namespace orxonox |
---|
| 25 | { |
---|
[219] | 26 | class BaseObject; |
---|
| 27 | |
---|
[197] | 28 | // ##### Identifier ##### |
---|
| 29 | class Identifier |
---|
| 30 | { |
---|
| 31 | template <class T> |
---|
| 32 | friend class ClassIdentifier; |
---|
| 33 | |
---|
| 34 | template <class T> |
---|
| 35 | friend class BaseIdentifier; |
---|
| 36 | |
---|
[221] | 37 | template <class T> |
---|
| 38 | friend class Iterator; |
---|
| 39 | |
---|
[197] | 40 | public: |
---|
| 41 | void addObject(OrxonoxClass* object); |
---|
| 42 | void removeObject(OrxonoxClass* object); |
---|
| 43 | |
---|
[219] | 44 | virtual BaseObject* fabricate() {}; |
---|
[218] | 45 | |
---|
[197] | 46 | bool isA(Identifier* identifier); |
---|
| 47 | bool isDirectlyA(Identifier* identifier); |
---|
| 48 | bool isChildOf(Identifier* identifier); |
---|
| 49 | bool isDirectChildOf(Identifier* identifier); |
---|
| 50 | bool isParentOf(Identifier* identifier); |
---|
| 51 | bool isDirectParentOf(Identifier* identifier); |
---|
| 52 | |
---|
| 53 | std::string getName() { return this->name_; } |
---|
[219] | 54 | IdentifierList* getDirectParents() { return &(this->directParents_); } |
---|
| 55 | IdentifierList* getAllParents() { return &(this->allParents_); } |
---|
| 56 | IdentifierList* getDirectChildren() { return &(this->directChildren_); } |
---|
| 57 | IdentifierList* getAllChildren() { return &(this->allChildren_); } |
---|
[197] | 58 | |
---|
[219] | 59 | static bool isCreatingHierarchy() { return (hierarchyCreatingCounter_s > 0); } |
---|
| 60 | |
---|
[197] | 61 | private: |
---|
| 62 | Identifier(); |
---|
| 63 | Identifier(const Identifier& identifier) {} |
---|
| 64 | virtual ~Identifier(); |
---|
| 65 | void initialize(IdentifierList* parents); |
---|
| 66 | |
---|
[219] | 67 | static void startCreatingHierarchy() { hierarchyCreatingCounter_s++; std::cout << "*** Increased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << "\n"; } |
---|
| 68 | static void stopCreatingHierarchy() { hierarchyCreatingCounter_s--; std::cout << "*** Decreased Hierarchy-Creating-Counter to " << hierarchyCreatingCounter_s << "\n"; } |
---|
[197] | 69 | |
---|
[219] | 70 | IdentifierList directParents_; |
---|
| 71 | IdentifierList allParents_; |
---|
| 72 | IdentifierList directChildren_; |
---|
| 73 | IdentifierList allChildren_; |
---|
| 74 | |
---|
| 75 | ObjectList objects_; |
---|
[197] | 76 | std::string name_; |
---|
| 77 | |
---|
| 78 | bool bIsAbstractClass_; |
---|
| 79 | bool bCreatedOneObject_; |
---|
[219] | 80 | |
---|
| 81 | static int hierarchyCreatingCounter_s; |
---|
[197] | 82 | }; |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | // ##### ClassIdentifier ##### |
---|
| 86 | template <class T> |
---|
| 87 | class ClassIdentifier : public Identifier |
---|
| 88 | { |
---|
| 89 | public: |
---|
| 90 | static ClassIdentifier<T>* registerClass(IdentifierList* parents, std::string name, bool bRootClass, bool bIsAbstractClass); |
---|
| 91 | static ClassIdentifier<T>* getIdentifier(); |
---|
[219] | 92 | BaseObject* fabricate(); |
---|
[218] | 93 | T* fabricateClass(); |
---|
[197] | 94 | |
---|
| 95 | private: |
---|
| 96 | ClassIdentifier(); |
---|
| 97 | ClassIdentifier(const ClassIdentifier<T>& identifier) {} |
---|
| 98 | ~ClassIdentifier(); |
---|
| 99 | |
---|
[219] | 100 | static ClassIdentifier<T>* pointer_s; |
---|
[197] | 101 | |
---|
| 102 | }; |
---|
| 103 | |
---|
| 104 | template <class T> |
---|
[219] | 105 | ClassIdentifier<T>* ClassIdentifier<T>::pointer_s = NULL; |
---|
[197] | 106 | |
---|
| 107 | template <class T> |
---|
| 108 | ClassIdentifier<T>::ClassIdentifier() |
---|
| 109 | { |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | template <class T> |
---|
| 113 | ClassIdentifier<T>::~ClassIdentifier() |
---|
| 114 | { |
---|
[219] | 115 | this->pointer_s = NULL; |
---|
[197] | 116 | } |
---|
| 117 | |
---|
| 118 | template <class T> |
---|
[219] | 119 | BaseObject* ClassIdentifier<T>::fabricate() |
---|
[218] | 120 | { |
---|
[219] | 121 | return dynamic_cast<BaseObject*>(this->fabricateClass()); |
---|
[218] | 122 | } |
---|
| 123 | |
---|
| 124 | template <class T> |
---|
| 125 | T* ClassIdentifier<T>::fabricateClass() |
---|
| 126 | { |
---|
[219] | 127 | if (!this->bIsAbstractClass_) |
---|
| 128 | { |
---|
| 129 | return new T; |
---|
| 130 | } |
---|
| 131 | else |
---|
| 132 | { |
---|
| 133 | std::cout << "Error: Couldn't create a new Object - Class " << this->name_ << " is abstract!\n"; |
---|
| 134 | std::cout << "Aborting...\n"; |
---|
| 135 | abort(); |
---|
| 136 | } |
---|
[218] | 137 | } |
---|
| 138 | |
---|
| 139 | template <class T> |
---|
[197] | 140 | ClassIdentifier<T>* ClassIdentifier<T>::registerClass(IdentifierList* parents, std::string name, bool bRootClass, bool bIsAbstractClass) |
---|
| 141 | { |
---|
| 142 | std::cout << "*** Register Class in " << name << "-Singleton.\n"; |
---|
[219] | 143 | if (!pointer_s) |
---|
[197] | 144 | { |
---|
| 145 | std::cout << "*** Register Class in " << name << "-Singleton -> Create Singleton.\n"; |
---|
| 146 | if (parents || bRootClass) |
---|
| 147 | { |
---|
[219] | 148 | pointer_s = new ClassIdentifier(); |
---|
| 149 | pointer_s->name_ = name; |
---|
| 150 | pointer_s->bIsAbstractClass_ = bIsAbstractClass; |
---|
[218] | 151 | |
---|
[219] | 152 | ClassFactory::add(name, pointer_s); |
---|
[218] | 153 | |
---|
[220] | 154 | if (!bRootClass) |
---|
| 155 | pointer_s->initialize(parents); |
---|
| 156 | else |
---|
| 157 | pointer_s->initialize(NULL); |
---|
[197] | 158 | } |
---|
| 159 | else |
---|
| 160 | { |
---|
[219] | 161 | pointer_s = getIdentifier(); |
---|
[197] | 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
[219] | 165 | return pointer_s; |
---|
[197] | 166 | } |
---|
| 167 | |
---|
| 168 | template <class T> |
---|
| 169 | ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier() |
---|
| 170 | { |
---|
| 171 | // std::cout << "*** Get Identifier.\n"; |
---|
[219] | 172 | if (!pointer_s) |
---|
[197] | 173 | { |
---|
| 174 | std::cout << "*** Get Identifier -> Create Class\n"; |
---|
[219] | 175 | Identifier::startCreatingHierarchy(); |
---|
[197] | 176 | T* temp = new T(); |
---|
[221] | 177 | delete temp; |
---|
[219] | 178 | Identifier::stopCreatingHierarchy(); |
---|
[197] | 179 | } |
---|
| 180 | |
---|
[219] | 181 | return pointer_s; |
---|
[197] | 182 | } |
---|
| 183 | |
---|
| 184 | // ##### BaseIdentifier ##### |
---|
| 185 | template <class B> |
---|
[218] | 186 | class BaseIdentifier |
---|
[197] | 187 | { |
---|
| 188 | public: |
---|
| 189 | BaseIdentifier(); |
---|
| 190 | |
---|
[221] | 191 | BaseIdentifier<B>& operator=(Identifier* identifier) |
---|
[197] | 192 | { |
---|
| 193 | if (!identifier->isA(ClassIdentifier<B>::getIdentifier())) |
---|
| 194 | { |
---|
| 195 | std::cout << "Error: Class " << identifier->getName() << " is not a " << ClassIdentifier<B>::getIdentifier()->getName() << "!\n"; |
---|
| 196 | std::cout << "Error: BaseIdentifier<" << ClassIdentifier<B>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden.\n"; |
---|
| 197 | std::cout << "Aborting...\n"; |
---|
| 198 | abort(); |
---|
| 199 | } |
---|
| 200 | this->identifier_ = identifier; |
---|
| 201 | return *this; |
---|
| 202 | } |
---|
[218] | 203 | |
---|
[221] | 204 | Identifier* operator*() |
---|
[218] | 205 | { |
---|
| 206 | return this->identifier_; |
---|
| 207 | } |
---|
| 208 | |
---|
[221] | 209 | Identifier* operator->() const |
---|
[218] | 210 | { |
---|
| 211 | return this->identifier_; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | B* fabricate() |
---|
| 215 | { |
---|
[219] | 216 | BaseObject* newObject = this->identifier_->fabricate(); |
---|
[218] | 217 | if (newObject) |
---|
| 218 | { |
---|
| 219 | return dynamic_cast<B*>(newObject); |
---|
| 220 | } |
---|
| 221 | else |
---|
| 222 | { |
---|
| 223 | if (this->identifier_) |
---|
| 224 | { |
---|
| 225 | std::cout << "Error: Class " << this->identifier_->getName() << " is not a " << ClassIdentifier<B>::getIdentifier()->getName() << "!\n"; |
---|
| 226 | std::cout << "Error: Couldn't fabricate a new Object.\n"; |
---|
| 227 | std::cout << "Aborting...\n"; |
---|
| 228 | } |
---|
| 229 | else |
---|
| 230 | { |
---|
| 231 | std::cout << "Error: Couldn't fabricate a new Object - Identifier is undefined.\n"; |
---|
| 232 | std::cout << "Aborting...\n"; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | abort(); |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
[197] | 239 | inline Identifier* getIdentifier() |
---|
| 240 | { return this->identifier_; } |
---|
| 241 | inline bool isA(Identifier* identifier) |
---|
| 242 | { return this->identifier_->isA(identifier); } |
---|
| 243 | inline bool isDirectlyA(Identifier* identifier) |
---|
| 244 | { return this->identifier_->isDirectlyA(identifier); } |
---|
| 245 | inline bool isChildOf(Identifier* identifier) |
---|
| 246 | { return this->identifier_->isChildOf(identifier); } |
---|
| 247 | inline bool isDirectChildOf(Identifier* identifier) |
---|
| 248 | { return this->identifier_->isDirectChildOf(identifier); } |
---|
| 249 | inline bool isParentOf(Identifier* identifier) |
---|
| 250 | { return this->identifier_->isParentOf(identifier); } |
---|
| 251 | inline bool isDirectParentOf(Identifier* identifier) |
---|
| 252 | { return this->identifier_->isDirectParentOf(identifier); } |
---|
| 253 | |
---|
| 254 | private: |
---|
| 255 | Identifier* identifier_; |
---|
| 256 | }; |
---|
| 257 | |
---|
| 258 | template <class B> |
---|
| 259 | BaseIdentifier<B>::BaseIdentifier() |
---|
| 260 | { |
---|
| 261 | this->identifier_ = ClassIdentifier<B>::getIdentifier(); |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | #endif |
---|