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