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