[132] | 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); |
---|
| 62 | return; |
---|
| 63 | } |
---|
| 64 | |
---|
| 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 ##### |
---|
| 84 | ObjectList::ObjectList() |
---|
| 85 | { |
---|
| 86 | this->first = NULL; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | ObjectList::~ObjectList() |
---|
| 90 | { |
---|
| 91 | delete this->first; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void ObjectList::add(BaseObject* object) |
---|
| 95 | { |
---|
| 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); |
---|
| 108 | return; |
---|
| 109 | } |
---|
| 110 | |
---|
| 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 | //} |
---|