Changeset 149 for code/branches/objecthierarchie/src/ClassHierarchy.h
- Timestamp:
- Nov 2, 2007, 1:25:00 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/objecthierarchie/src/ClassHierarchy.h
r132 r149 6 6 // DONE: 7 7 // - klassenhierarchie aufbauen 8 // - in listen einfügen 9 // - factory 8 10 // - klassen-identifier 11 // - isA u.ä. vergleiche 9 12 10 13 // TODO: 11 14 // - durch listen iterieren 12 // - isA usw vergleiche13 // - new überladen + objekt in liste eintragen14 15 // - searchtree für classname-strings 15 16 16 //namespace orxonox 17 //{ 18 class ClassName; 19 class ClassList; 17 18 namespace orxonox 19 { 20 // ##### Identifier ##### 21 class IdentifierList; 20 22 class ObjectList; 21 23 class BaseObject; 22 24 25 class Identifier 26 { 27 template <class T> 28 friend class ClassIdentifier; 29 30 public: 31 // static Identifier* registerClass(IdentifierList* parents); 32 void addObject(BaseObject* object); 33 void removeObject(BaseObject* object); 34 35 bool isA(Identifier* identifier); 36 bool isDirectA(Identifier* identifier); 37 bool isChildOf(Identifier* identifier); 38 bool isDirectChildOf(Identifier* identifier); 39 bool isParentOf(Identifier* identifier); 40 bool isDirectParentOf(Identifier* identifier); 41 42 protected: 43 Identifier(); 44 void initialize(IdentifierList* identifier); 45 46 static Identifier* pointer_; 47 48 IdentifierList* directParents_; 49 IdentifierList* allParents_; 50 IdentifierList* directChildren_; 51 IdentifierList* allChildren_; 52 53 ObjectList* objects_; 54 std::string name_; 55 56 private: 57 bool bCreatedOneObject_; 58 }; 59 23 60 template <class T> 24 class ClassNameSingleton 61 class ClassIdentifier : public Identifier 62 { 63 // friend class Identifier; 64 65 public: 66 static Identifier* registerClass(IdentifierList* parents); 67 static Identifier* getIdentifier(); 68 static T* create(); 69 70 private: 71 ClassIdentifier(); 72 }; 73 74 #define getStringFromClassName(ClassName) \ 75 #ClassName 76 77 template <class T> 78 ClassIdentifier<T>::ClassIdentifier() 79 { 80 } 81 82 template <class T> 83 Identifier* ClassIdentifier<T>::registerClass(IdentifierList* parents) 84 { 85 if (!pointer_) 86 { 87 pointer_ = new ClassIdentifier(); 88 pointer_->name_ = getStringFromClassName(T); 89 pointer_->initialize(parents); 90 } 91 92 return pointer_; 93 } 94 95 template <class T> 96 Identifier* ClassIdentifier<T>::getIdentifier() 97 { 98 if (!pointer_) 99 { 100 T* temp = new T(); 101 delete temp; 102 } 103 104 return pointer_; 105 } 106 107 template <class T> 108 T* ClassIdentifier<T>::create() 109 { 110 return new T(); 111 } 112 113 // ##### Identifier List ##### 114 class IdentifierListElement; 115 116 class IdentifierList 25 117 { 26 118 public: 27 static ClassName* getClassName(BaseObject* object, bool bIsRootClass); 28 static ClassName* getNewClassName(); 29 BaseObject* create(); 119 IdentifierList(); 120 ~IdentifierList(); 121 void add(Identifier* identifier); 122 void remove(Identifier* identifier); 123 bool isInList(Identifier* identifier); 30 124 31 private: 32 ClassNameSingleton(); 33 ~ClassNameSingleton(); 34 static ClassNameSingleton *pointer; 35 static ClassName *className; 125 IdentifierListElement* first_; 36 126 }; 37 127 38 class ClassName128 class IdentifierListElement 39 129 { 40 130 public: 41 ClassName(const std::string& name, ClassNameSingleton<class T>* factory); 42 ~ClassName(); 131 IdentifierListElement(Identifier* identifier); 43 132 44 std::string name; 45 ClassName *parentClass; 46 ClassList *childClasses; 47 ObjectList *objects; 48 ClassNameSingleton<class T> *factory; 133 Identifier* identifier_; 134 IdentifierListElement* next_; 135 bool bDirect_; 49 136 }; 50 137 51 class ClassListItem52 {53 public:54 ClassListItem(ClassName* className);55 ~ClassListItem();56 138 57 ClassListItem *next; 58 ClassName *className; 59 }; 60 61 class ClassList 62 { 63 public: 64 ClassList(); 65 ~ClassList(); 66 void add(ClassName* className); 67 68 ClassListItem *first; 69 }; 70 71 class ObjectListItem 72 { 73 public: 74 ObjectListItem(BaseObject* object); 75 ~ObjectListItem(); 76 77 ObjectListItem *next; 78 BaseObject *object; 79 }; 139 // ##### Object List ##### 140 class ObjectListElement; 80 141 81 142 class ObjectList … … 87 148 void remove(BaseObject* object); 88 149 89 ObjectList Item *first;150 ObjectListElement* first_; 90 151 }; 91 152 92 class ClassNameTree153 class ObjectListElement 93 154 { 94 155 public: 95 static ClassNameTree* getSingleton(); 96 BaseObject* create(ClassName* className); 97 BaseObject* create(std::string& name); 98 ClassName* getClassName(std::string& name); 99 ClassName* getClassName(std::string& name, ClassName* root); 100 ClassName* getRootClass() { return this->rootClass; } 101 void setRootClass(ClassName* className) { this->rootClass = className; } 156 ObjectListElement(BaseObject* object); 102 157 103 private: 104 ClassNameTree(); 105 ~ClassNameTree(); 106 static ClassNameTree *pointer; 107 ClassName* rootClass; 158 BaseObject* object_; 159 ObjectListElement* next_; 108 160 }; 109 161 110 162 111 #define className(ClassName) \ 112 ClassNameSingleton<ClassName>::getNewClassName() 163 // ##### Macros ##### 164 #define registerRootObject(ClassName) \ 165 this->parents_ = new IdentifierList(); \ 166 this->identifier_ = ClassIdentifier<ClassName>::registerClass(this->parents_); \ 167 this->parents_->add(this->identifier_); \ 168 this->identifier_->addObject(this) 113 169 114 #define registerObject(ClassName, bIsRootClass) \ 115 this->className = ClassNameSingleton<ClassName>::getClassName(this, bIsRootClass) 170 #define registerObject(ClassName) \ 171 this->identifier_->removeObject(this); \ 172 this->identifier_ = ClassIdentifier<ClassName>::registerClass(this->parents_); \ 173 this->parents_->add(this->identifier_); \ 174 this->identifier_->addObject(this) 116 175 117 176 #define unregisterObject() \ 118 this->className->objects->remove(this) 177 delete this->parents_; \ 178 this->identifier_->removeObject(this) 119 179 120 #define factory(ClassName) \ 121 ClassNameTree::getSingleton()->create(ClassName) 122 //} 180 #define Class(ClassName) \ 181 ClassIdentifier<ClassName>::getIdentifier() 182 183 #define Factory(ClassName) \ 184 ClassIdentifier<ClassName>::create() 185 } 123 186 124 187 #endif
Note: See TracChangeset
for help on using the changeset viewer.