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