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