[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 | |
---|
[4840] | 111 | tList<BaseObject>* ClassList::getList(long classID) |
---|
| 112 | { |
---|
| 113 | if(unlikely(ClassList::first == NULL)) |
---|
| 114 | return NULL; |
---|
| 115 | else |
---|
| 116 | { |
---|
| 117 | ClassList* tmpCL = ClassList::first; |
---|
| 118 | while (likely(tmpCL != NULL)) |
---|
| 119 | { |
---|
| 120 | if (unlikely(tmpCL->classID == classID)) |
---|
| 121 | return tmpCL->objectList; |
---|
| 122 | tmpCL = tmpCL->next; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | return NULL; |
---|
| 126 | } |
---|
| 127 | |
---|
[4748] | 128 | /** |
---|
[4754] | 129 | * checks if the BaseObject* object exists. |
---|
[4761] | 130 | * @param name the name of the BaseObject to look for |
---|
| 131 | * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere. |
---|
| 132 | * @return true, if the Object Exists in the specified ClassID, false otherwise |
---|
| 133 | * @todo: speed this up!! |
---|
| 134 | */ |
---|
| 135 | BaseObject* ClassList::getObject(const char* name, long classID) |
---|
| 136 | { |
---|
| 137 | if(unlikely(ClassList::first == NULL) || name == NULL) |
---|
| 138 | return NULL; |
---|
| 139 | else |
---|
| 140 | { |
---|
| 141 | ClassList* tmp = ClassList::first; |
---|
| 142 | while (likely(tmp != NULL)) |
---|
| 143 | { |
---|
[4778] | 144 | if (tmp->classID == classID || classID == CL_NULL) |
---|
[4761] | 145 | { |
---|
| 146 | tIterator<BaseObject>* iterator = tmp->objectList->getIterator(); |
---|
| 147 | BaseObject* enumBO = iterator->nextElement(); |
---|
| 148 | const char* tmpName; |
---|
| 149 | while (enumBO != NULL) |
---|
| 150 | { |
---|
| 151 | tmpName = enumBO->getName(); |
---|
| 152 | if (tmpName && !strcmp(tmpName, name)) |
---|
| 153 | { |
---|
| 154 | delete iterator; |
---|
| 155 | return enumBO; |
---|
| 156 | } |
---|
| 157 | enumBO = iterator->nextElement(); |
---|
| 158 | } |
---|
| 159 | delete iterator; |
---|
| 160 | break; |
---|
| 161 | } |
---|
| 162 | tmp = tmp->next; |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | return NULL; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | * checks if the BaseObject* object exists. |
---|
[4754] | 171 | * @param object the Pointer to a BaseObject to check if it exists |
---|
| 172 | * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere. |
---|
| 173 | * @return true, if the Object Exists in the specified ClassID, false otherwise |
---|
| 174 | * @todo: speed this up!! |
---|
| 175 | */ |
---|
[4874] | 176 | bool ClassList::exists(const BaseObject* object, long classID) |
---|
[4754] | 177 | { |
---|
| 178 | if(unlikely(ClassList::first == NULL)) |
---|
| 179 | return false; |
---|
| 180 | else |
---|
| 181 | { |
---|
| 182 | ClassList* tmp = ClassList::first; |
---|
| 183 | while (likely(tmp != NULL)) |
---|
| 184 | { |
---|
[4778] | 185 | if (tmp->classID == classID || classID == CL_NULL) |
---|
[4754] | 186 | { |
---|
| 187 | tIterator<BaseObject>* iterator = tmp->objectList->getIterator(); |
---|
| 188 | BaseObject* enumBO = iterator->nextElement(); |
---|
[4761] | 189 | while (enumBO != NULL) |
---|
[4754] | 190 | { |
---|
| 191 | if (enumBO == object) |
---|
[4761] | 192 | { |
---|
| 193 | delete iterator; |
---|
[4754] | 194 | return true; |
---|
[4761] | 195 | } |
---|
[4754] | 196 | enumBO = iterator->nextElement(); |
---|
| 197 | } |
---|
| 198 | delete iterator; |
---|
| 199 | break; |
---|
| 200 | } |
---|
| 201 | tmp = tmp->next; |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | return false; |
---|
| 205 | } |
---|
| 206 | |
---|
[4874] | 207 | |
---|
[4754] | 208 | /** |
---|
[4874] | 209 | * prints out a string of all the types this Object matches |
---|
| 210 | * @param object a Pointer to the object to analyze |
---|
| 211 | */ |
---|
| 212 | void ClassList::whatIs(const BaseObject* object) |
---|
| 213 | { |
---|
| 214 | ClassList* tmp = ClassList::first; |
---|
| 215 | while (likely(tmp != NULL)) |
---|
| 216 | { |
---|
| 217 | if (object->isA(tmp->classID)) |
---|
| 218 | { |
---|
| 219 | PRINT(0)("=%s=-", tmp->className); |
---|
| 220 | } |
---|
| 221 | tmp = tmp->next; |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | /** |
---|
[4748] | 226 | * Print out some very nice debug information |
---|
[4871] | 227 | * @param debugLevel the level of verbosity |
---|
| 228 | * @param classID the class that should be displayed (if CL_NULL (default) all classes will be displayed) |
---|
[4748] | 229 | */ |
---|
[4873] | 230 | void ClassList::debug(unsigned int debugLevel, long classID) |
---|
[4747] | 231 | { |
---|
[4872] | 232 | if (debugLevel > 3) |
---|
| 233 | debugLevel = 3; |
---|
[4752] | 234 | PRINT(0)("==========================\n"); |
---|
| 235 | PRINT(0)("= CLASS_LIST (level %d) =\n", debugLevel); |
---|
| 236 | PRINT(0)("==========================\n"); |
---|
[4760] | 237 | PRINT(0)("| knows %d Classes\n|\n", ClassList::classCount); |
---|
[4747] | 238 | ClassList* tmp = ClassList::first; |
---|
[4748] | 239 | char niceString[100]; |
---|
| 240 | unsigned int lenCount = 0; |
---|
| 241 | |
---|
[4747] | 242 | while (likely(tmp != NULL)) |
---|
| 243 | { |
---|
[4871] | 244 | if ((debugLevel >= 1 || tmp->objectList->getSize() > 0 ) && |
---|
| 245 | (classID == CL_NULL || unlikely (classID == tmp->classID))) |
---|
[4752] | 246 | { |
---|
| 247 | lenCount = 1; |
---|
| 248 | while (pow(10,lenCount) <= tmp->objectList->getSize()) |
---|
| 249 | ++lenCount; |
---|
| 250 | for (int i=0; i < 30-strlen(tmp->className) - lenCount; i++) |
---|
| 251 | (niceString[i]) = ' '; |
---|
| 252 | niceString[30-strlen(tmp->className) - lenCount] = '\0'; |
---|
[4747] | 253 | |
---|
[4758] | 254 | PRINT(0)("| CLASS %s:%s %d\n", tmp->className, niceString, tmp->objectList->getSize()); |
---|
[4748] | 255 | |
---|
[4752] | 256 | if (debugLevel >=2 && tmp->objectList->getSize() > 0) |
---|
| 257 | { |
---|
[4758] | 258 | PRINT(0)("| Listing Instances:\n"); |
---|
[4752] | 259 | tIterator<BaseObject>* iterator = tmp->objectList->getIterator(); |
---|
| 260 | BaseObject* enumBO = iterator->nextElement(); |
---|
| 261 | while (enumBO) |
---|
| 262 | { |
---|
[4873] | 263 | PRINT(0)("| (class %s): NAME(%s)->%p ", enumBO->getClassName(), enumBO->getName(), enumBO); |
---|
[4872] | 264 | if (debugLevel == 3) |
---|
[4874] | 265 | ClassList::whatIs(enumBO); |
---|
| 266 | PRINT(0)("\n"); |
---|
[4752] | 267 | enumBO = iterator->nextElement(); |
---|
| 268 | } |
---|
| 269 | delete iterator; |
---|
| 270 | } |
---|
| 271 | } |
---|
[4747] | 272 | tmp = tmp->next; |
---|
| 273 | } |
---|
[4758] | 274 | PRINT(0)("=======================CL=\n"); |
---|
[4747] | 275 | } |
---|