Changeset 149
- Timestamp:
- Nov 2, 2007, 1:25:00 AM (17 years ago)
- Location:
- code/branches/objecthierarchie/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/objecthierarchie/src/BaseObject.cc
r132 r149 1 1 #include "BaseObject.h" 2 2 3 //namespace orxonox4 //{3 namespace orxonox 4 { 5 5 BaseObject::BaseObject() 6 6 { 7 // registerObject(BaseObject, true);7 registerRootObject(BaseObject); 8 8 } 9 9 10 10 BaseObject::~BaseObject() 11 11 { 12 //unregisterObject();12 unregisterObject(); 13 13 } 14 /* 15 void* BaseObject::operator new (size_t size) 16 { 17 void *pMem = new char[size]; 18 memset(pMem,0,size); 19 20 return pMem; 21 } 22 */ 23 //} 14 } -
code/branches/objecthierarchie/src/BaseObject.h
r132 r149 4 4 #include "ClassHierarchy.h" 5 5 6 //namespace orxonox7 //{6 namespace orxonox 7 { 8 8 class BaseObject 9 9 { … … 12 12 ~BaseObject(); 13 13 14 bool isA(ClassName* className); 15 bool isChildOf(ClassName* className); 16 bool isParentOf(ClassName* className); 17 static bool getParentOf(ClassName* className); 14 inline bool isA(Identifier* identifier) 15 { this->identifier_->isA(identifier); } 16 inline bool isDirectA(Identifier* identifier) 17 { this->identifier_->isDirectA(identifier); } 18 inline bool isChildOf(Identifier* identifier) 19 { this->identifier_->isChildOf(identifier); } 20 inline bool isDirectChildOf(Identifier* identifier) 21 { this->identifier_->isDirectChildOf(identifier); } 22 inline bool isParentOf(Identifier* identifier) 23 { this->identifier_->isParentOf(identifier); } 24 inline bool isDirectParentOf(Identifier* identifier) 25 { this->identifier_->isDirectParentOf(identifier); } 18 26 19 // static void operator new (size_t size);27 Identifier* identifier_; 20 28 21 ClassName *className; 29 protected: 30 IdentifierList* parents_; // INTERN! Don't touch this! 22 31 23 32 private: 24 33 25 34 }; 26 //}35 } 27 36 28 37 #endif -
code/branches/objecthierarchie/src/ClassHierarchy.cc
r132 r149 1 1 #include "ClassHierarchy.h" 2 #include "BaseObject.h" 3 4 //namespace orxonox 5 //{ 6 // ##### ClassName ##### 7 ClassName::ClassName(const std::string& name, ClassNameSingleton<class T>* factory) 8 { 9 this->parentClass = NULL; 10 this->childClasses = new ClassList(); 11 this->objects = new ObjectList(); 12 this->name = name; 13 this->factory = factory; 14 } 15 16 ClassName::~ClassName() 17 { 18 delete this->childClasses; 19 delete this->objects; 20 } 21 22 23 // ##### ClassListItem ##### 24 ClassListItem::ClassListItem(ClassName* className) 25 { 26 this->next = NULL; 27 this->className = className; 28 } 29 30 ClassListItem::~ClassListItem() 31 { 32 delete this->className; 33 delete this->next; 34 } 35 36 37 // ##### ClassList ##### 38 ClassList::ClassList() 39 { 40 this->first = NULL; 41 } 42 43 ClassList::~ClassList() 44 { 45 delete this->first; 46 } 47 48 void ClassList::add(ClassName* className) 49 { 50 if (!this->first) 51 { 52 this->first = new ClassListItem(className); 53 return; 54 } 55 56 ClassListItem* iterator = this->first; 57 while (iterator != NULL) 58 { 59 if (iterator->next == NULL) 60 { 61 iterator->next = new ClassListItem(className); 2 3 namespace orxonox 4 { 5 // ############################### 6 // ### Identifier ### 7 // ############################### 8 Identifier* Identifier::pointer_ = NULL; 9 /* 10 Identifier* Identifier::registerClass(IdentifierList* parents) 11 { 12 if (!pointer_) 13 { 14 pointer_ = new Identifier(); 15 pointer_->initialize(parents); 16 } 17 18 return pointer_; 19 } 20 */ 21 Identifier::Identifier() 22 { 23 this->bCreatedOneObject_ = false; 24 this->directParents_ = new IdentifierList(); 25 this->allParents_ = new IdentifierList(); 26 this->directChildren_ = new IdentifierList(); 27 this->allChildren_ = new IdentifierList(); 28 this->objects_ = new ObjectList(); 29 } 30 31 void Identifier::initialize(IdentifierList* parents) 32 { 33 if (parents) 34 { 35 this->bCreatedOneObject_ = true; 36 37 IdentifierListElement* temp1; 38 IdentifierListElement* temp2; 39 IdentifierListElement* temp3; 40 41 temp1 = parents->first_; 42 while (temp1) 43 { 44 temp2 = temp1->identifier_->directParents_->first_; 45 while (temp2) 46 { 47 temp3 = parents->first_; 48 while(temp3) 49 { 50 if (temp3->identifier_ == temp2->identifier_) 51 temp3->bDirect_ = false; 52 53 temp3 = temp3->next_; 54 } 55 56 temp2 = temp2->next_; 57 } 58 temp1 = temp1->next_; 59 } 60 61 temp1 = parents->first_; 62 while (temp1) 63 { 64 if (temp1->bDirect_) 65 { 66 this->directParents_->add(temp1->identifier_); 67 temp1->identifier_->directChildren_->add(this->pointer_); 68 } 69 70 this->allParents_->add(temp1->identifier_); 71 temp1->identifier_->allChildren_->add(this->pointer_); 72 73 temp1 = temp1->next_; 74 } 75 } 76 } 77 78 void Identifier::addObject(BaseObject* object) 79 { 80 this->objects_->add(object); 81 } 82 83 void Identifier::removeObject(BaseObject* object) 84 { 85 this->objects_->remove(object); 86 } 87 88 bool Identifier::isA(Identifier* identifier) 89 { 90 return (identifier == this->pointer_ || this->allParents_->isInList(identifier)); 91 } 92 93 bool Identifier::isDirectA(Identifier* identifier) 94 { 95 return (identifier == this->pointer_); 96 } 97 98 bool Identifier::isChildOf(Identifier* identifier) 99 { 100 return this->allParents_->isInList(identifier); 101 } 102 103 bool Identifier::isDirectChildOf(Identifier* identifier) 104 { 105 return this->directParents_->isInList(identifier); 106 } 107 108 bool Identifier::isParentOf(Identifier* identifier) 109 { 110 return this->allChildren_->isInList(identifier); 111 } 112 113 bool Identifier::isDirectParentOf(Identifier* identifier) 114 { 115 return this->directChildren_->isInList(identifier); 116 } 117 118 119 // ############################### 120 // ### IdentifierList ### 121 // ############################### 122 IdentifierList::IdentifierList() 123 { 124 this->first_ = NULL; 125 } 126 127 IdentifierList::~IdentifierList() 128 { 129 IdentifierListElement* temp; 130 while (this->first_) 131 { 132 temp = this->first_->next_; 133 delete this->first_; 134 this->first_ = temp; 135 } 136 } 137 138 void IdentifierList::add(Identifier* identifier) 139 { 140 IdentifierListElement* temp = this->first_; 141 this->first_ = new IdentifierListElement(identifier); 142 this->first_->next_ = temp; 143 } 144 145 void IdentifierList::remove(Identifier* identifier) 146 { 147 if (!identifier) 148 return; 149 150 if (this->first_->identifier_ == identifier) 151 { 152 IdentifierListElement* temp = this->first_->next_; 153 delete this->first_; 154 this->first_ = temp; 155 156 return; 157 } 158 159 IdentifierListElement* temp = this->first_; 160 while (temp->next_) 161 { 162 if (temp->next_->identifier_ == identifier) 163 { 164 IdentifierListElement* temp2 = temp->next_->next_; 165 delete temp->next_; 166 temp->next_ = temp2; 167 62 168 return; 63 169 } 64 170 65 iterator = iterator->next; 66 } 67 } 68 69 // ##### ObjectListItem ##### 70 ObjectListItem::ObjectListItem(BaseObject* object) 71 { 72 this->next = NULL; 73 this->object = object; 74 } 75 76 ObjectListItem::~ObjectListItem() 77 { 78 delete this->object; 79 delete this->next; 80 } 81 82 83 // ##### ObjectList ##### 171 temp = temp->next_; 172 } 173 } 174 175 bool IdentifierList::isInList(Identifier* identifier) 176 { 177 IdentifierListElement* temp = this->first_; 178 while (temp) 179 { 180 if (temp->identifier_ == identifier) 181 return true; 182 183 temp = temp->next_; 184 } 185 186 return false; 187 } 188 189 190 // ############################### 191 // ### IdentifierListElement ### 192 // ############################### 193 IdentifierListElement::IdentifierListElement(Identifier* identifier) 194 { 195 this->identifier_ = identifier; 196 this->next_ = NULL; 197 this->bDirect_ = true; 198 } 199 200 201 // ############################### 202 // ### ObjectList ### 203 // ############################### 84 204 ObjectList::ObjectList() 85 205 { 86 this->first = NULL;206 this->first_ = NULL; 87 207 } 88 208 89 209 ObjectList::~ObjectList() 90 210 { 91 delete this->first; 211 ObjectListElement* temp; 212 while (this->first_) 213 { 214 temp = this->first_->next_; 215 delete this->first_; 216 this->first_ = temp; 217 } 92 218 } 93 219 94 220 void ObjectList::add(BaseObject* object) 95 221 { 96 if (!this->first) 97 { 98 this->first = new ObjectListItem(object); 99 return; 100 } 101 102 ObjectListItem* iterator = this->first; 103 while (iterator != NULL) 104 { 105 if (iterator->next == NULL) 106 { 107 iterator->next = new ObjectListItem(object); 222 ObjectListElement* temp = this->first_; 223 this->first_ = new ObjectListElement(object); 224 this->first_->next_ = temp; 225 } 226 227 void ObjectList::remove(BaseObject* object) 228 { 229 if (!object) 230 return; 231 232 if (this->first_->object_ == object) 233 { 234 ObjectListElement* temp = this->first_->next_; 235 delete this->first_; 236 this->first_ = temp; 237 238 return; 239 } 240 241 ObjectListElement* temp = this->first_; 242 while (temp->next_) 243 { 244 if (temp->next_->object_ == object) 245 { 246 ObjectListElement* temp2 = temp->next_->next_; 247 delete temp->next_; 248 temp->next_ = temp2; 249 108 250 return; 109 251 } 110 252 111 iterator = iterator->next; 112 } 113 } 114 115 void ObjectList::remove(BaseObject* object) 116 { 117 if (!this->first || !object) 118 return; 119 120 if (this->first->object == object) 121 { 122 ObjectListItem* temp = this->first->next; 123 delete this->first; 124 this->first = temp; 125 126 return; 127 } 128 129 ObjectListItem* iterator = this->first; 130 while (iterator->next != NULL) 131 { 132 if (iterator->next->object == object) 133 { 134 ObjectListItem* temp = iterator->next->next; 135 delete iterator->next; 136 iterator->next = temp; 137 138 return; 139 } 140 141 iterator = iterator->next; 142 } 143 } 144 145 // ##### ClassNameTree ##### 146 ClassNameTree* ClassNameTree::pointer = NULL; 147 148 ClassNameTree::ClassNameTree() 149 { 150 } 151 152 ClassNameTree::~ClassNameTree() 153 { 154 this->pointer = NULL; 155 } 156 157 ClassNameTree* ClassNameTree::getSingleton() 158 { 159 if (!pointer) 160 { 161 pointer = new ClassNameTree(); 162 } 163 164 return pointer; 165 } 166 /* 167 BaseObject* ClassNameTree::create(ClassName* className) 168 { 169 return className->factory->create(); 170 } 171 172 BaseObject* ClassNameTree::create(std::string& name) 173 { 174 return this->getClassName(name)->factory->create(); 175 } 176 */ 177 ClassName* ClassNameTree::getClassName(std::string& name) 178 { 179 return getClassName(name, this->rootClass); 180 } 181 182 ClassName* ClassNameTree::getClassName(std::string& name, ClassName* root) 183 { 184 if (root->name == name) 185 return root; 186 187 ClassListItem* temp = root->childClasses->first; 188 while (temp != NULL) 189 { 190 ClassName* temp2 = this->getClassName(name, temp->className); 191 if (temp2) 192 return temp2; 193 194 temp = temp->next; 195 } 196 197 return NULL; 198 } 199 200 201 // ##### ClassNameSingleton ##### 202 #define getClassNameString(ClassName) \ 203 #ClassName 204 205 template <class T> 206 ClassNameSingleton<T>* ClassNameSingleton<T>::pointer = NULL; 207 208 template <class T> 209 ClassName* ClassNameSingleton<T>::className = NULL; 210 211 template <class T> 212 ClassName* ClassNameSingleton<T>::getClassName(BaseObject* object, bool bIsRootClass) 213 { 214 if (!pointer || !className) 215 { 216 pointer = new ClassNameSingleton<T>(); 217 className = new ClassName(getClassNameString(T), pointer); 218 219 if (bIsRootClass) 220 { 221 className->parentClass = NULL; 222 ClassNameTree::getSingleton()->setRootClass(className); 223 } 224 else 225 { 226 className->parentClass = object->className; 227 object->className->childClasses->add(className); 228 } 229 } 230 231 return className; 232 } 233 234 template <class T> 235 ClassName* ClassNameSingleton<T>::getNewClassName() 236 { 237 if (!pointer || !className) 238 { 239 T* temp = new T(); 240 delete temp; 241 } 242 243 return className; 244 } 245 246 template <class T> 247 BaseObject* ClassNameSingleton<T>::create() 248 { 249 return new T(); 250 } 251 252 template <class T> 253 ClassNameSingleton<T>::~ClassNameSingleton() 254 { 255 this->pointer = NULL; 256 delete this->className; 257 this->className = NULL; 258 } 259 //} 253 temp = temp->next_; 254 } 255 } 256 257 258 // ############################### 259 // ### ObjectListElement ### 260 // ############################### 261 ObjectListElement::ObjectListElement(BaseObject* object) 262 { 263 this->object_ = object; 264 this->next_ = NULL; 265 } 266 } -
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.