[4743] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
[4750] | 12 | main-programmer: Benjamin Grauer |
---|
[4743] | 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
| 17 | |
---|
| 18 | #include "class_list.h" |
---|
[4747] | 19 | #include "base_object.h" |
---|
[4743] | 20 | |
---|
[4752] | 21 | #include "list.h" |
---|
[4747] | 22 | #include "compiler.h" |
---|
| 23 | #include "debug.h" |
---|
[4748] | 24 | #include <string.h> |
---|
| 25 | #include <math.h> |
---|
[4747] | 26 | |
---|
[4743] | 27 | using namespace std; |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | /** |
---|
[4836] | 31 | * Creates a new ClassList |
---|
[4743] | 32 | */ |
---|
[4747] | 33 | ClassList::ClassList(const long& classID, const char* className) |
---|
[4743] | 34 | { |
---|
[4747] | 35 | this->next = NULL; |
---|
| 36 | this->className = className; |
---|
| 37 | this->classID = classID; |
---|
[4752] | 38 | this->objectList = new tList<BaseObject>; |
---|
[4748] | 39 | |
---|
| 40 | ++ClassList::classCount; |
---|
[4743] | 41 | } |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | /** |
---|
[4836] | 45 | * standard deconstructor |
---|
[4743] | 46 | */ |
---|
| 47 | ClassList::~ClassList () |
---|
| 48 | { |
---|
[4782] | 49 | delete this->objectList; |
---|
[5103] | 50 | if(ClassList::classList != NULL) |
---|
| 51 | { |
---|
| 52 | delete ClassList::classList; |
---|
| 53 | ClassList::classList = NULL; |
---|
| 54 | } |
---|
[4748] | 55 | --ClassList::classCount; |
---|
[4743] | 56 | } |
---|
[4747] | 57 | |
---|
[4748] | 58 | //! the first class that is registered |
---|
[4747] | 59 | ClassList* ClassList::first = NULL; |
---|
[4748] | 60 | |
---|
| 61 | //! the Count of classes |
---|
[4747] | 62 | unsigned int ClassList::classCount = 0; |
---|
| 63 | |
---|
[5103] | 64 | //! a List of all strings of all classes, that have registered so far. |
---|
| 65 | tList<const char>* ClassList::classList = NULL; |
---|
| 66 | |
---|
[4748] | 67 | /** |
---|
| 68 | * Adds a new Object to the ClassList (and if necessary a new Class) |
---|
| 69 | * @param objectPointer Pointer to the Object at hand |
---|
| 70 | * @param classID ID of the Given ObjectType \see ClassID |
---|
| 71 | * @param className name of the Class to add |
---|
| 72 | */ |
---|
[4747] | 73 | void ClassList::addToClassList(BaseObject* objectPointer, const long& classID, const char* className) |
---|
| 74 | { |
---|
| 75 | ClassList* regClass; |
---|
[4782] | 76 | PRINTF(5)("subscribe a %s\n", className ); |
---|
[4747] | 77 | |
---|
| 78 | if(ClassList::first == NULL) |
---|
| 79 | ClassList::first = regClass = new ClassList(classID, className); |
---|
| 80 | else |
---|
| 81 | { |
---|
| 82 | ClassList* tmp = ClassList::first; |
---|
| 83 | while (likely(tmp != NULL)) |
---|
| 84 | { |
---|
| 85 | if (tmp->classID == classID) |
---|
| 86 | { |
---|
| 87 | regClass = tmp; |
---|
| 88 | break; |
---|
| 89 | } |
---|
| 90 | |
---|
[4782] | 91 | if (unlikely(tmp->next == NULL)) |
---|
| 92 | { |
---|
[4747] | 93 | tmp->next = regClass = new ClassList(classID, className); |
---|
[4782] | 94 | break; |
---|
| 95 | } |
---|
[4747] | 96 | tmp = tmp->next; |
---|
| 97 | } |
---|
| 98 | } |
---|
[4752] | 99 | regClass->objectList->add(objectPointer); |
---|
[4747] | 100 | } |
---|
| 101 | |
---|
[4748] | 102 | /** |
---|
| 103 | * removes an Object from a the ClassList |
---|
| 104 | * @param objectPointer the Object to delete from the List |
---|
| 105 | */ |
---|
[4747] | 106 | void ClassList::removeFromClassList(BaseObject* objectPointer) |
---|
| 107 | { |
---|
| 108 | ClassList* tmp = ClassList::first; |
---|
| 109 | while (likely(tmp != NULL)) |
---|
| 110 | { |
---|
| 111 | if (objectPointer->isA(tmp->classID)) |
---|
| 112 | { |
---|
[4752] | 113 | tmp->objectList->remove(objectPointer); |
---|
[4747] | 114 | } |
---|
| 115 | tmp = tmp->next; |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
[5102] | 119 | /** |
---|
[5103] | 120 | * grabs the names of all Classes, and injects it into a List of const chars |
---|
| 121 | * @return the generated List |
---|
| 122 | * |
---|
| 123 | * This function first looks, if the List has been changed (by the ListSize) |
---|
| 124 | * befor it changes anything. |
---|
| 125 | */ |
---|
| 126 | const tList<const char>* ClassList::getClassList() |
---|
| 127 | { |
---|
| 128 | if (unlikely(ClassList::classList != NULL && ClassList::classList->getSize() != ClassList::classCount)) |
---|
| 129 | { |
---|
| 130 | delete ClassList::classList; |
---|
| 131 | ClassList::classList = NULL; |
---|
| 132 | } |
---|
| 133 | if (unlikely(ClassList::classList == NULL)) |
---|
| 134 | ClassList::classList = new tList<const char>; |
---|
| 135 | |
---|
| 136 | if(likely(ClassList::first != NULL)) |
---|
| 137 | { |
---|
| 138 | ClassList* tmpCL = ClassList::first; |
---|
| 139 | while (likely(tmpCL != NULL)) |
---|
| 140 | { |
---|
| 141 | ClassList::classList->add(tmpCL->className); |
---|
| 142 | tmpCL = tmpCL->next; |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | return ClassList::classList; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | /** |
---|
[5102] | 149 | * searches for classID and returns the list of Entities |
---|
| 150 | * @param classID the ID of the class to get the list from |
---|
| 151 | * @return the List accessed by classID, or NULL if not found |
---|
| 152 | */ |
---|
[4840] | 153 | tList<BaseObject>* ClassList::getList(long classID) |
---|
| 154 | { |
---|
| 155 | if(unlikely(ClassList::first == NULL)) |
---|
| 156 | return NULL; |
---|
| 157 | else |
---|
| 158 | { |
---|
| 159 | ClassList* tmpCL = ClassList::first; |
---|
| 160 | while (likely(tmpCL != NULL)) |
---|
| 161 | { |
---|
| 162 | if (unlikely(tmpCL->classID == classID)) |
---|
| 163 | return tmpCL->objectList; |
---|
| 164 | tmpCL = tmpCL->next; |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | return NULL; |
---|
| 168 | } |
---|
| 169 | |
---|
[4748] | 170 | /** |
---|
[5102] | 171 | * searches for className and returns the list of Entities |
---|
| 172 | * @param className the name of the class to get the list from |
---|
| 173 | * @return the List accessed by classID, or NULL if not found |
---|
| 174 | */tList<BaseObject>* ClassList::getList(const char* className) |
---|
| 175 | { |
---|
| 176 | if(unlikely(ClassList::first == NULL)) |
---|
| 177 | return NULL; |
---|
| 178 | else |
---|
| 179 | { |
---|
| 180 | ClassList* tmpCL = ClassList::first; |
---|
| 181 | while (likely(tmpCL != NULL)) |
---|
| 182 | { |
---|
| 183 | if (unlikely(!strcmp(tmpCL->className, className))) |
---|
| 184 | return tmpCL->objectList; |
---|
| 185 | tmpCL = tmpCL->next; |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | return NULL; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /** |
---|
[4754] | 192 | * checks if the BaseObject* object exists. |
---|
[4761] | 193 | * @param name the name of the BaseObject to look for |
---|
| 194 | * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere. |
---|
| 195 | * @return true, if the Object Exists in the specified ClassID, false otherwise |
---|
| 196 | * @todo: speed this up!! |
---|
| 197 | */ |
---|
| 198 | BaseObject* ClassList::getObject(const char* name, long classID) |
---|
| 199 | { |
---|
| 200 | if(unlikely(ClassList::first == NULL) || name == NULL) |
---|
| 201 | return NULL; |
---|
| 202 | else |
---|
| 203 | { |
---|
| 204 | ClassList* tmp = ClassList::first; |
---|
| 205 | while (likely(tmp != NULL)) |
---|
| 206 | { |
---|
[4778] | 207 | if (tmp->classID == classID || classID == CL_NULL) |
---|
[4761] | 208 | { |
---|
| 209 | tIterator<BaseObject>* iterator = tmp->objectList->getIterator(); |
---|
| 210 | BaseObject* enumBO = iterator->nextElement(); |
---|
| 211 | const char* tmpName; |
---|
| 212 | while (enumBO != NULL) |
---|
| 213 | { |
---|
| 214 | tmpName = enumBO->getName(); |
---|
| 215 | if (tmpName && !strcmp(tmpName, name)) |
---|
| 216 | { |
---|
| 217 | delete iterator; |
---|
| 218 | return enumBO; |
---|
| 219 | } |
---|
| 220 | enumBO = iterator->nextElement(); |
---|
| 221 | } |
---|
| 222 | delete iterator; |
---|
| 223 | break; |
---|
| 224 | } |
---|
| 225 | tmp = tmp->next; |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | return NULL; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | /** |
---|
| 233 | * checks if the BaseObject* object exists. |
---|
[4754] | 234 | * @param object the Pointer to a BaseObject to check if it exists |
---|
| 235 | * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere. |
---|
| 236 | * @return true, if the Object Exists in the specified ClassID, false otherwise |
---|
| 237 | * @todo: speed this up!! |
---|
| 238 | */ |
---|
[4874] | 239 | bool ClassList::exists(const BaseObject* object, long classID) |
---|
[4754] | 240 | { |
---|
| 241 | if(unlikely(ClassList::first == NULL)) |
---|
| 242 | return false; |
---|
| 243 | else |
---|
| 244 | { |
---|
| 245 | ClassList* tmp = ClassList::first; |
---|
| 246 | while (likely(tmp != NULL)) |
---|
| 247 | { |
---|
[4778] | 248 | if (tmp->classID == classID || classID == CL_NULL) |
---|
[4754] | 249 | { |
---|
| 250 | tIterator<BaseObject>* iterator = tmp->objectList->getIterator(); |
---|
| 251 | BaseObject* enumBO = iterator->nextElement(); |
---|
[4761] | 252 | while (enumBO != NULL) |
---|
[4754] | 253 | { |
---|
| 254 | if (enumBO == object) |
---|
[4761] | 255 | { |
---|
| 256 | delete iterator; |
---|
[4754] | 257 | return true; |
---|
[4761] | 258 | } |
---|
[4754] | 259 | enumBO = iterator->nextElement(); |
---|
| 260 | } |
---|
| 261 | delete iterator; |
---|
| 262 | break; |
---|
| 263 | } |
---|
| 264 | tmp = tmp->next; |
---|
| 265 | } |
---|
| 266 | } |
---|
| 267 | return false; |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | /** |
---|
[4874] | 271 | * prints out a string of all the types this Object matches |
---|
| 272 | * @param object a Pointer to the object to analyze |
---|
| 273 | */ |
---|
| 274 | void ClassList::whatIs(const BaseObject* object) |
---|
| 275 | { |
---|
| 276 | ClassList* tmp = ClassList::first; |
---|
| 277 | while (likely(tmp != NULL)) |
---|
| 278 | { |
---|
| 279 | if (object->isA(tmp->classID)) |
---|
| 280 | { |
---|
| 281 | PRINT(0)("=%s=-", tmp->className); |
---|
| 282 | } |
---|
| 283 | tmp = tmp->next; |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | /** |
---|
[4748] | 288 | * Print out some very nice debug information |
---|
[4871] | 289 | * @param debugLevel the level of verbosity |
---|
| 290 | * @param classID the class that should be displayed (if CL_NULL (default) all classes will be displayed) |
---|
[4748] | 291 | */ |
---|
[4873] | 292 | void ClassList::debug(unsigned int debugLevel, long classID) |
---|
[4747] | 293 | { |
---|
[4872] | 294 | if (debugLevel > 3) |
---|
| 295 | debugLevel = 3; |
---|
[4752] | 296 | PRINT(0)("==========================\n"); |
---|
| 297 | PRINT(0)("= CLASS_LIST (level %d) =\n", debugLevel); |
---|
| 298 | PRINT(0)("==========================\n"); |
---|
[4760] | 299 | PRINT(0)("| knows %d Classes\n|\n", ClassList::classCount); |
---|
[4747] | 300 | ClassList* tmp = ClassList::first; |
---|
[4748] | 301 | char niceString[100]; |
---|
| 302 | unsigned int lenCount = 0; |
---|
| 303 | |
---|
[4747] | 304 | while (likely(tmp != NULL)) |
---|
| 305 | { |
---|
[4871] | 306 | if ((debugLevel >= 1 || tmp->objectList->getSize() > 0 ) && |
---|
| 307 | (classID == CL_NULL || unlikely (classID == tmp->classID))) |
---|
[4752] | 308 | { |
---|
| 309 | lenCount = 1; |
---|
| 310 | while (pow(10,lenCount) <= tmp->objectList->getSize()) |
---|
| 311 | ++lenCount; |
---|
| 312 | for (int i=0; i < 30-strlen(tmp->className) - lenCount; i++) |
---|
| 313 | (niceString[i]) = ' '; |
---|
| 314 | niceString[30-strlen(tmp->className) - lenCount] = '\0'; |
---|
[4747] | 315 | |
---|
[4758] | 316 | PRINT(0)("| CLASS %s:%s %d\n", tmp->className, niceString, tmp->objectList->getSize()); |
---|
[4748] | 317 | |
---|
[4752] | 318 | if (debugLevel >=2 && tmp->objectList->getSize() > 0) |
---|
| 319 | { |
---|
[4758] | 320 | PRINT(0)("| Listing Instances:\n"); |
---|
[4752] | 321 | tIterator<BaseObject>* iterator = tmp->objectList->getIterator(); |
---|
| 322 | BaseObject* enumBO = iterator->nextElement(); |
---|
| 323 | while (enumBO) |
---|
| 324 | { |
---|
[4873] | 325 | PRINT(0)("| (class %s): NAME(%s)->%p ", enumBO->getClassName(), enumBO->getName(), enumBO); |
---|
[4872] | 326 | if (debugLevel == 3) |
---|
[4874] | 327 | ClassList::whatIs(enumBO); |
---|
| 328 | PRINT(0)("\n"); |
---|
[4752] | 329 | enumBO = iterator->nextElement(); |
---|
| 330 | } |
---|
| 331 | delete iterator; |
---|
| 332 | } |
---|
| 333 | } |
---|
[4747] | 334 | tmp = tmp->next; |
---|
| 335 | } |
---|
[4758] | 336 | PRINT(0)("=======================CL=\n"); |
---|
[4747] | 337 | } |
---|