[270] | 1 | #ifndef _Identifier_H__ |
---|
| 2 | #define _Identifier_H__ |
---|
| 3 | |
---|
| 4 | #include "ClassHierarchy.h" |
---|
| 5 | #include "IdentifierList.h" |
---|
| 6 | #include "ObjectList.h" |
---|
| 7 | #include "OrxonoxClass.h" |
---|
| 8 | |
---|
| 9 | namespace orxonox |
---|
| 10 | { |
---|
| 11 | // ##### Identifier ##### |
---|
| 12 | class Identifier |
---|
| 13 | { |
---|
| 14 | template <class T> |
---|
| 15 | friend class ClassIdentifier; |
---|
| 16 | |
---|
| 17 | template <class T> |
---|
| 18 | friend class BaseIdentifier; |
---|
| 19 | |
---|
| 20 | public: |
---|
| 21 | void addObject(OrxonoxClass* object); |
---|
| 22 | void removeObject(OrxonoxClass* object); |
---|
| 23 | |
---|
| 24 | bool isA(Identifier* identifier); |
---|
| 25 | bool isDirectlyA(Identifier* identifier); |
---|
| 26 | bool isChildOf(Identifier* identifier); |
---|
| 27 | bool isDirectChildOf(Identifier* identifier); |
---|
| 28 | bool isParentOf(Identifier* identifier); |
---|
| 29 | bool isDirectParentOf(Identifier* identifier); |
---|
| 30 | |
---|
| 31 | std::string getName() { return this->name_; } |
---|
| 32 | IdentifierList* getDirectParents() { return this->directParents_; } |
---|
| 33 | IdentifierList* getAllParents() { return this->allParents_; } |
---|
| 34 | IdentifierList* getDirectChildren() { return this->directChildren_; } |
---|
| 35 | IdentifierList* getAllChildren() { return this->allChildren_; } |
---|
| 36 | |
---|
| 37 | private: |
---|
| 38 | Identifier(); |
---|
| 39 | Identifier(const Identifier& identifier) {} |
---|
| 40 | virtual ~Identifier(); |
---|
| 41 | void initialize(IdentifierList* parents); |
---|
| 42 | |
---|
| 43 | IdentifierList* directParents_; |
---|
| 44 | IdentifierList* allParents_; |
---|
| 45 | IdentifierList* directChildren_; |
---|
| 46 | IdentifierList* allChildren_; |
---|
| 47 | |
---|
| 48 | ObjectList* objects_; |
---|
| 49 | std::string name_; |
---|
| 50 | |
---|
| 51 | bool bIsAbstractClass_; |
---|
| 52 | bool bCreatedOneObject_; |
---|
| 53 | }; |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | // ##### ClassIdentifier ##### |
---|
| 57 | template <class T> |
---|
| 58 | class ClassIdentifier : public Identifier |
---|
| 59 | { |
---|
| 60 | public: |
---|
| 61 | static ClassIdentifier<T>* registerClass(IdentifierList* parents, std::string name, bool bRootClass, bool bIsAbstractClass); |
---|
| 62 | static ClassIdentifier<T>* getIdentifier(); |
---|
| 63 | |
---|
| 64 | private: |
---|
| 65 | ClassIdentifier(); |
---|
| 66 | ClassIdentifier(const ClassIdentifier<T>& identifier) {} |
---|
| 67 | ~ClassIdentifier(); |
---|
| 68 | |
---|
| 69 | static ClassIdentifier<T>* pointer_; |
---|
| 70 | |
---|
| 71 | }; |
---|
| 72 | |
---|
| 73 | template <class T> |
---|
| 74 | ClassIdentifier<T>* ClassIdentifier<T>::pointer_ = NULL; |
---|
| 75 | |
---|
| 76 | template <class T> |
---|
| 77 | ClassIdentifier<T>::ClassIdentifier() |
---|
| 78 | { |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | template <class T> |
---|
| 82 | ClassIdentifier<T>::~ClassIdentifier() |
---|
| 83 | { |
---|
| 84 | this->pointer_ = NULL; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | template <class T> |
---|
| 88 | ClassIdentifier<T>* ClassIdentifier<T>::registerClass(IdentifierList* parents, std::string name, bool bRootClass, bool bIsAbstractClass) |
---|
| 89 | { |
---|
| 90 | std::cout << "*** Register Class in " << name << "-Singleton.\n"; |
---|
| 91 | if (!pointer_) |
---|
| 92 | { |
---|
| 93 | std::cout << "*** Register Class in " << name << "-Singleton -> Create Singleton.\n"; |
---|
| 94 | if (parents || bRootClass) |
---|
| 95 | { |
---|
| 96 | pointer_ = new ClassIdentifier(); |
---|
| 97 | pointer_->name_ = name; |
---|
| 98 | pointer_->bIsAbstractClass_ = bIsAbstractClass; |
---|
| 99 | pointer_->initialize(parents); |
---|
| 100 | } |
---|
| 101 | else |
---|
| 102 | { |
---|
| 103 | pointer_ = getIdentifier(); |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | return pointer_; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | template <class T> |
---|
| 111 | ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier() |
---|
| 112 | { |
---|
| 113 | // std::cout << "*** Get Identifier.\n"; |
---|
| 114 | if (!pointer_) |
---|
| 115 | { |
---|
| 116 | std::cout << "*** Get Identifier -> Create Class\n"; |
---|
| 117 | ClassHierarchy::getSingleton()->startCreatingHierarchy(); |
---|
| 118 | T* temp = new T(); |
---|
| 119 | ClassHierarchy::getSingleton()->stopCreatingHierarchy(); |
---|
| 120 | delete temp; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | return pointer_; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | // ##### BaseIdentifier ##### |
---|
| 128 | template <class B> |
---|
| 129 | class BaseIdentifier// : public Identifier |
---|
| 130 | { |
---|
| 131 | public: |
---|
| 132 | BaseIdentifier(); |
---|
| 133 | |
---|
| 134 | //template <class T> |
---|
| 135 | BaseIdentifier<B>& operator= (/*Class*/Identifier/*<T>*/* identifier) |
---|
| 136 | { |
---|
| 137 | if (!identifier->isA(ClassIdentifier<B>::getIdentifier())) |
---|
| 138 | { |
---|
| 139 | std::cout << "Error: Class " << identifier->getName() << " is not a " << ClassIdentifier<B>::getIdentifier()->getName() << "!\n"; |
---|
| 140 | std::cout << "Error: BaseIdentifier<" << ClassIdentifier<B>::getIdentifier()->getName() << "> = Class(" << identifier->getName() << ") is forbidden.\n"; |
---|
| 141 | std::cout << "Aborting...\n"; |
---|
| 142 | abort(); |
---|
| 143 | } |
---|
| 144 | this->identifier_ = identifier; |
---|
| 145 | return *this; |
---|
| 146 | } |
---|
| 147 | inline Identifier* getIdentifier() |
---|
| 148 | { return this->identifier_; } |
---|
| 149 | inline bool isA(Identifier* identifier) |
---|
| 150 | { return this->identifier_->isA(identifier); } |
---|
| 151 | inline bool isDirectlyA(Identifier* identifier) |
---|
| 152 | { return this->identifier_->isDirectlyA(identifier); } |
---|
| 153 | inline bool isChildOf(Identifier* identifier) |
---|
| 154 | { return this->identifier_->isChildOf(identifier); } |
---|
| 155 | inline bool isDirectChildOf(Identifier* identifier) |
---|
| 156 | { return this->identifier_->isDirectChildOf(identifier); } |
---|
| 157 | inline bool isParentOf(Identifier* identifier) |
---|
| 158 | { return this->identifier_->isParentOf(identifier); } |
---|
| 159 | inline bool isDirectParentOf(Identifier* identifier) |
---|
| 160 | { return this->identifier_->isDirectParentOf(identifier); } |
---|
| 161 | |
---|
| 162 | private: |
---|
| 163 | Identifier* identifier_; |
---|
| 164 | }; |
---|
| 165 | |
---|
| 166 | template <class B> |
---|
| 167 | BaseIdentifier<B>::BaseIdentifier() |
---|
| 168 | { |
---|
| 169 | this->identifier_ = ClassIdentifier<B>::getIdentifier(); |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | #endif |
---|