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