Changeset 149 for code/branches/objecthierarchie/src/ClassHierarchy.cc
- Timestamp:
- Nov 2, 2007, 1:25:00 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.