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