[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; |
---|
[4748] | 50 | --ClassList::classCount; |
---|
[4743] | 51 | } |
---|
[4747] | 52 | |
---|
[4748] | 53 | //! the first class that is registered |
---|
[4747] | 54 | ClassList* ClassList::first = NULL; |
---|
[4748] | 55 | |
---|
| 56 | //! the Count of classes |
---|
[4747] | 57 | unsigned int ClassList::classCount = 0; |
---|
| 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 |
---|
| 64 | */ |
---|
[4747] | 65 | void ClassList::addToClassList(BaseObject* objectPointer, const long& classID, const char* className) |
---|
| 66 | { |
---|
| 67 | ClassList* regClass; |
---|
[4782] | 68 | PRINTF(5)("subscribe a %s\n", className ); |
---|
[4747] | 69 | |
---|
| 70 | if(ClassList::first == NULL) |
---|
| 71 | ClassList::first = regClass = new ClassList(classID, className); |
---|
| 72 | else |
---|
| 73 | { |
---|
| 74 | ClassList* tmp = ClassList::first; |
---|
| 75 | while (likely(tmp != NULL)) |
---|
| 76 | { |
---|
| 77 | if (tmp->classID == classID) |
---|
| 78 | { |
---|
| 79 | regClass = tmp; |
---|
| 80 | break; |
---|
| 81 | } |
---|
| 82 | |
---|
[4782] | 83 | if (unlikely(tmp->next == NULL)) |
---|
| 84 | { |
---|
[4747] | 85 | tmp->next = regClass = new ClassList(classID, className); |
---|
[4782] | 86 | break; |
---|
| 87 | } |
---|
[4747] | 88 | tmp = tmp->next; |
---|
| 89 | } |
---|
| 90 | } |
---|
[4752] | 91 | regClass->objectList->add(objectPointer); |
---|
[4747] | 92 | } |
---|
| 93 | |
---|
[4748] | 94 | /** |
---|
| 95 | * removes an Object from a the ClassList |
---|
| 96 | * @param objectPointer the Object to delete from the List |
---|
| 97 | */ |
---|
[4747] | 98 | void ClassList::removeFromClassList(BaseObject* objectPointer) |
---|
| 99 | { |
---|
| 100 | ClassList* tmp = ClassList::first; |
---|
| 101 | while (likely(tmp != NULL)) |
---|
| 102 | { |
---|
| 103 | if (objectPointer->isA(tmp->classID)) |
---|
| 104 | { |
---|
[4752] | 105 | tmp->objectList->remove(objectPointer); |
---|
[4747] | 106 | } |
---|
| 107 | tmp = tmp->next; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
[5102] | 111 | /** |
---|
| 112 | * searches for classID and returns the list of Entities |
---|
| 113 | * @param classID the ID of the class to get the list from |
---|
| 114 | * @return the List accessed by classID, or NULL if not found |
---|
| 115 | */ |
---|
[4840] | 116 | tList<BaseObject>* ClassList::getList(long classID) |
---|
| 117 | { |
---|
| 118 | if(unlikely(ClassList::first == NULL)) |
---|
| 119 | return NULL; |
---|
| 120 | else |
---|
| 121 | { |
---|
| 122 | ClassList* tmpCL = ClassList::first; |
---|
| 123 | while (likely(tmpCL != NULL)) |
---|
| 124 | { |
---|
| 125 | if (unlikely(tmpCL->classID == classID)) |
---|
| 126 | return tmpCL->objectList; |
---|
| 127 | tmpCL = tmpCL->next; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | return NULL; |
---|
| 131 | } |
---|
| 132 | |
---|
[4748] | 133 | /** |
---|
[5102] | 134 | * searches for className and returns the list of Entities |
---|
| 135 | * @param className the name of the class to get the list from |
---|
| 136 | * @return the List accessed by classID, or NULL if not found |
---|
| 137 | */tList<BaseObject>* ClassList::getList(const char* className) |
---|
| 138 | { |
---|
| 139 | if(unlikely(ClassList::first == NULL)) |
---|
| 140 | return NULL; |
---|
| 141 | else |
---|
| 142 | { |
---|
| 143 | ClassList* tmpCL = ClassList::first; |
---|
| 144 | while (likely(tmpCL != NULL)) |
---|
| 145 | { |
---|
| 146 | if (unlikely(!strcmp(tmpCL->className, className))) |
---|
| 147 | return tmpCL->objectList; |
---|
| 148 | tmpCL = tmpCL->next; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | return NULL; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
[4754] | 155 | * checks if the BaseObject* object exists. |
---|
[4761] | 156 | * @param name the name of the BaseObject to look for |
---|
| 157 | * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere. |
---|
| 158 | * @return true, if the Object Exists in the specified ClassID, false otherwise |
---|
| 159 | * @todo: speed this up!! |
---|
| 160 | */ |
---|
| 161 | BaseObject* ClassList::getObject(const char* name, long classID) |
---|
| 162 | { |
---|
| 163 | if(unlikely(ClassList::first == NULL) || name == NULL) |
---|
| 164 | return NULL; |
---|
| 165 | else |
---|
| 166 | { |
---|
| 167 | ClassList* tmp = ClassList::first; |
---|
| 168 | while (likely(tmp != NULL)) |
---|
| 169 | { |
---|
[4778] | 170 | if (tmp->classID == classID || classID == CL_NULL) |
---|
[4761] | 171 | { |
---|
| 172 | tIterator<BaseObject>* iterator = tmp->objectList->getIterator(); |
---|
| 173 | BaseObject* enumBO = iterator->nextElement(); |
---|
| 174 | const char* tmpName; |
---|
| 175 | while (enumBO != NULL) |
---|
| 176 | { |
---|
| 177 | tmpName = enumBO->getName(); |
---|
| 178 | if (tmpName && !strcmp(tmpName, name)) |
---|
| 179 | { |
---|
| 180 | delete iterator; |
---|
| 181 | return enumBO; |
---|
| 182 | } |
---|
| 183 | enumBO = iterator->nextElement(); |
---|
| 184 | } |
---|
| 185 | delete iterator; |
---|
| 186 | break; |
---|
| 187 | } |
---|
| 188 | tmp = tmp->next; |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | return NULL; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | /** |
---|
| 196 | * checks if the BaseObject* object exists. |
---|
[4754] | 197 | * @param object the Pointer to a BaseObject to check if it exists |
---|
| 198 | * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere. |
---|
| 199 | * @return true, if the Object Exists in the specified ClassID, false otherwise |
---|
| 200 | * @todo: speed this up!! |
---|
| 201 | */ |
---|
[4874] | 202 | bool ClassList::exists(const BaseObject* object, long classID) |
---|
[4754] | 203 | { |
---|
| 204 | if(unlikely(ClassList::first == NULL)) |
---|
| 205 | return false; |
---|
| 206 | else |
---|
| 207 | { |
---|
| 208 | ClassList* tmp = ClassList::first; |
---|
| 209 | while (likely(tmp != NULL)) |
---|
| 210 | { |
---|
[4778] | 211 | if (tmp->classID == classID || classID == CL_NULL) |
---|
[4754] | 212 | { |
---|
| 213 | tIterator<BaseObject>* iterator = tmp->objectList->getIterator(); |
---|
| 214 | BaseObject* enumBO = iterator->nextElement(); |
---|
[4761] | 215 | while (enumBO != NULL) |
---|
[4754] | 216 | { |
---|
| 217 | if (enumBO == object) |
---|
[4761] | 218 | { |
---|
| 219 | delete iterator; |
---|
[4754] | 220 | return true; |
---|
[4761] | 221 | } |
---|
[4754] | 222 | enumBO = iterator->nextElement(); |
---|
| 223 | } |
---|
| 224 | delete iterator; |
---|
| 225 | break; |
---|
| 226 | } |
---|
| 227 | tmp = tmp->next; |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | return false; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | /** |
---|
[4874] | 234 | * prints out a string of all the types this Object matches |
---|
| 235 | * @param object a Pointer to the object to analyze |
---|
| 236 | */ |
---|
| 237 | void ClassList::whatIs(const BaseObject* object) |
---|
| 238 | { |
---|
| 239 | ClassList* tmp = ClassList::first; |
---|
| 240 | while (likely(tmp != NULL)) |
---|
| 241 | { |
---|
| 242 | if (object->isA(tmp->classID)) |
---|
| 243 | { |
---|
| 244 | PRINT(0)("=%s=-", tmp->className); |
---|
| 245 | } |
---|
| 246 | tmp = tmp->next; |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | /** |
---|
[4748] | 251 | * Print out some very nice debug information |
---|
[4871] | 252 | * @param debugLevel the level of verbosity |
---|
| 253 | * @param classID the class that should be displayed (if CL_NULL (default) all classes will be displayed) |
---|
[4748] | 254 | */ |
---|
[4873] | 255 | void ClassList::debug(unsigned int debugLevel, long classID) |
---|
[4747] | 256 | { |
---|
[4872] | 257 | if (debugLevel > 3) |
---|
| 258 | debugLevel = 3; |
---|
[4752] | 259 | PRINT(0)("==========================\n"); |
---|
| 260 | PRINT(0)("= CLASS_LIST (level %d) =\n", debugLevel); |
---|
| 261 | PRINT(0)("==========================\n"); |
---|
[4760] | 262 | PRINT(0)("| knows %d Classes\n|\n", ClassList::classCount); |
---|
[4747] | 263 | ClassList* tmp = ClassList::first; |
---|
[4748] | 264 | char niceString[100]; |
---|
| 265 | unsigned int lenCount = 0; |
---|
| 266 | |
---|
[4747] | 267 | while (likely(tmp != NULL)) |
---|
| 268 | { |
---|
[4871] | 269 | if ((debugLevel >= 1 || tmp->objectList->getSize() > 0 ) && |
---|
| 270 | (classID == CL_NULL || unlikely (classID == tmp->classID))) |
---|
[4752] | 271 | { |
---|
| 272 | lenCount = 1; |
---|
| 273 | while (pow(10,lenCount) <= tmp->objectList->getSize()) |
---|
| 274 | ++lenCount; |
---|
| 275 | for (int i=0; i < 30-strlen(tmp->className) - lenCount; i++) |
---|
| 276 | (niceString[i]) = ' '; |
---|
| 277 | niceString[30-strlen(tmp->className) - lenCount] = '\0'; |
---|
[4747] | 278 | |
---|
[4758] | 279 | PRINT(0)("| CLASS %s:%s %d\n", tmp->className, niceString, tmp->objectList->getSize()); |
---|
[4748] | 280 | |
---|
[4752] | 281 | if (debugLevel >=2 && tmp->objectList->getSize() > 0) |
---|
| 282 | { |
---|
[4758] | 283 | PRINT(0)("| Listing Instances:\n"); |
---|
[4752] | 284 | tIterator<BaseObject>* iterator = tmp->objectList->getIterator(); |
---|
| 285 | BaseObject* enumBO = iterator->nextElement(); |
---|
| 286 | while (enumBO) |
---|
| 287 | { |
---|
[4873] | 288 | PRINT(0)("| (class %s): NAME(%s)->%p ", enumBO->getClassName(), enumBO->getName(), enumBO); |
---|
[4872] | 289 | if (debugLevel == 3) |
---|
[4874] | 290 | ClassList::whatIs(enumBO); |
---|
| 291 | PRINT(0)("\n"); |
---|
[4752] | 292 | enumBO = iterator->nextElement(); |
---|
| 293 | } |
---|
| 294 | delete iterator; |
---|
| 295 | } |
---|
| 296 | } |
---|
[4747] | 297 | tmp = tmp->next; |
---|
| 298 | } |
---|
[4758] | 299 | PRINT(0)("=======================CL=\n"); |
---|
[4747] | 300 | } |
---|