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