Changeset 5791 in orxonox.OLD for trunk/src/lib/lang
- Timestamp:
- Nov 27, 2005, 3:32:42 AM (19 years ago)
- Location:
- trunk/src/lib/lang
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/lang/base_object.cc
r5671 r5791 71 71 * @param className the class name 72 72 */ 73 void BaseObject::setClassID( longclassID, const char* className)73 void BaseObject::setClassID(ClassID classID, const char* className) 74 74 { 75 this->classID |= classID;75 this->classID |= (long)classID; 76 76 this->className = className; 77 77 … … 142 142 * @returns true on match, false otherwise. 143 143 */ 144 bool BaseObject::operator 144 bool BaseObject::operator==(const char* objectName) 145 145 { 146 146 if (likely(this->objectName != NULL && objectName != NULL)) -
trunk/src/lib/lang/base_object.h
r5626 r5791 39 39 void whatIs() const; 40 40 41 // 42 bool operator== (const char* objectName); 41 bool operator==(const char* objectName); 43 42 44 43 protected: 45 void setClassID( longclassID, const char* className);44 void setClassID(ClassID classID, const char* className); 46 45 47 46 private: -
trunk/src/lib/lang/class_list.cc
r5783 r5791 19 19 #include "base_object.h" 20 20 21 #include "list.h"22 21 #include "compiler.h" 23 22 #include "debug.h" 24 23 #include <string.h> 25 24 #include <math.h> 25 #include <algorithm> 26 26 #include "shell_command.h" 27 27 … … 37 37 ClassList::ClassList(const long& classID, const char* className) 38 38 { 39 this->next = NULL;40 39 this->className = className; 41 40 this->classID = classID; 42 43 ++ClassList::classCount;44 41 } 45 42 … … 49 46 ClassList::~ClassList () 50 47 { 51 // ClassList::classList.clear()); 52 --ClassList::classCount; 53 } 54 55 //! the first class that is registered 56 ClassList* ClassList::first = NULL; 57 58 //! the Count of classes 59 unsigned int ClassList::classCount = 0; 48 // ClassList::classList->clear()); 49 } 50 51 //! a List of all known Classes. 52 std::list<ClassList>* ClassList::classList = new std::list<ClassList>(); 60 53 61 54 //! a List of all strings of all classes, that have registered so far. 62 std::list<const char*> ClassList::class List;55 std::list<const char*> ClassList::classNames; 63 56 64 57 /** … … 67 60 * @param classID ID of the Given ObjectType \see ClassID 68 61 * @param className name of the Class to add 69 */ 70 void ClassList::addToClassList(BaseObject* objectPointer, const long& classID, const char* className) 71 { 72 ClassList* regClass; 73 PRINTF(5)("subscribe a %s\n", className ); 74 75 if(ClassList::first == NULL) 76 ClassList::first = regClass = new ClassList(classID, className); 62 * 63 * !! FIRST YOU HAVE TO CALL THIS FUNCTION ONCE 64 * !! Before unsing the ClassList, as it creates the ClassLits 65 */ 66 void ClassList::addToClassList(BaseObject* objectPointer, ClassID classID, const char* className) 67 { 68 if (unlikely(classList == NULL)) 69 ClassList::classList = new list<ClassList>(); 70 71 PRINTF(5)("subscribe a '%s'\n", className ); 72 73 ClassList* regClass = ClassList::getClassList(classID); 74 if (regClass != NULL) 75 regClass->objectList.push_back(objectPointer); 77 76 else 78 77 { 79 ClassList* tmp = ClassList::first; 80 while (likely(tmp != NULL)) 81 { 82 if (tmp->classID == classID) 83 { 84 regClass = tmp; 85 break; 86 } 87 88 if (unlikely(tmp->next == NULL)) 89 { 90 tmp->next = regClass = new ClassList(classID, className); 91 break; 92 } 93 tmp = tmp->next; 94 } 95 } 96 regClass->objectList.push_back(objectPointer); 78 ClassList::classList->push_back(ClassList(classID, className)); 79 ClassList::classList->back().objectList.push_back(objectPointer); 80 } 97 81 } 98 82 … … 103 87 void ClassList::removeFromClassList(BaseObject* objectPointer) 104 88 { 105 ClassList* tmp = ClassList::first; 106 while (likely(tmp != NULL)) 107 { 108 if (objectPointer->isA(tmp->classID)) 109 { 110 tmp->objectList.remove(objectPointer); 111 } 112 tmp = tmp->next; 113 } 89 list<ClassList>::iterator cl; 90 for(cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++) 91 if (objectPointer->isA((*cl).classID)) 92 (*cl).objectList.remove(objectPointer); 114 93 } 115 94 … … 121 100 * befor it changes anything. 122 101 */ 123 const std::list<const char*>* ClassList::getClassList() 124 { 125 if (ClassList::classList.size() != ClassList::classCount) 126 ClassList::classList.clear(); 127 128 if(likely(ClassList::first != NULL)) 129 { 130 ClassList* tmpCL = ClassList::first; 131 while (likely(tmpCL != NULL)) 132 { 133 ClassList::classList.push_back(tmpCL->className); 134 tmpCL = tmpCL->next; 135 } 136 } 137 return &ClassList::classList; 102 const std::list<const char*>* ClassList::getClassNames() 103 { 104 if (ClassList::classNames.size() != ClassList::classList->size()) 105 { 106 ClassList::classNames.clear(); 107 108 list<ClassList>::const_iterator cl; 109 for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++) 110 ClassList::classNames.push_back((*cl).className); 111 } 112 113 return &ClassList::classNames; 138 114 } 139 115 … … 143 119 * @return the List accessed by classID, or NULL if not found 144 120 */ 145 std::list<BaseObject*>* ClassList::getList(long classID) 146 { 147 if(unlikely(ClassList::first == NULL)) 148 return NULL; 149 else 150 { 151 ClassList* tmpCL = ClassList::first; 152 while (likely(tmpCL != NULL)) 153 { 154 if (unlikely(tmpCL->classID == classID)) 155 return &tmpCL->objectList; 156 tmpCL = tmpCL->next; 157 } 158 } 159 return NULL; 121 std::list<BaseObject*>* ClassList::getList(ClassID classID) 122 { 123 ClassList* fl; 124 return ((fl = ClassList::getClassList(classID)) != NULL)? 125 &(fl->objectList) : NULL; 126 127 /* 128 std::list<ClassList>::iterator classIT = find (classList->begin(), classList->end(), classID); 129 return (likely(classIT != classList->end()))? &(*classIT).objectList : NULL;*/ 130 131 /* for (classIT = ClassList::classList->begin(); classIT != ClassList::classList->end(); classIT++) 132 { 133 if ((*classIT) == classID ) 134 return &(*classIT).objectList; 135 } 136 return NULL;*/ 160 137 } 161 138 … … 167 144 std::list<BaseObject*>* ClassList::getList(const char* className) 168 145 { 169 if(unlikely(ClassList::first == NULL)) 146 ClassList* fl; 147 return ((fl = ClassList::getClassList(className)) != NULL)? 148 &(fl->objectList) : NULL; 149 150 /* 151 std::list<ClassList>::iterator classIT = find (classList->begin(), classList->end(), className); 152 return (likely(classIT != classList->end()))? &(*classIT).objectList : NULL;*/ 153 154 155 /* for (classIT = ClassList::classList->begin(); classIT != ClassList::classList->end(); classIT++) 156 { 157 if ((*classIT) == className ) 158 return &(*classIT).objectList; 159 } 160 return NULL;*/ 161 } 162 163 164 ClassList* ClassList::getClassList(ClassID classID) 165 { 166 std::list<ClassList>::iterator classIT = find (ClassList::classList->begin(), ClassList::classList->end(), classID); 167 return (likely(classIT != classList->end()))? &(*classIT) : NULL; 168 } 169 170 171 ClassList* ClassList::getClassList(const char* className) 172 { 173 if (className == NULL) 170 174 return NULL; 171 else 172 { 173 ClassList* tmpCL = ClassList::first; 174 while (likely(tmpCL != NULL)) 175 { 176 if (unlikely(!strcmp(tmpCL->className, className))) 177 return &tmpCL->objectList; 178 tmpCL = tmpCL->next; 179 } 180 } 181 return NULL; 182 } 175 std::list<ClassList>::iterator classIT = find (classList->begin(), classList->end(), className); 176 return (likely(classIT != classList->end()))? &(*classIT) : NULL; 177 } 178 183 179 184 180 /** 185 181 * checks if the BaseObject* object exists. 186 * @param name the name of the BaseObject to look for182 * @param objectName the name of the BaseObject to look for 187 183 * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere. 188 184 * @return true, if the Object Exists in the specified ClassID, false otherwise 189 185 * @todo: speed this up!! 190 186 */ 191 BaseObject* ClassList::getObject(const char* name, long classID) 192 { 193 if(unlikely(ClassList::first == NULL) || name == NULL) 194 return NULL; 187 BaseObject* ClassList::getObject(const char* objectName, ClassID classID) 188 { 189 if (classID != CL_NULL) 190 { 191 ClassList* cl = ClassList::getClassList(classID); 192 if (cl != NULL) 193 { 194 std::list<BaseObject*>::iterator bo; 195 for (bo = cl->objectList.begin(); bo != cl->objectList.end(); bo++) 196 if ((*bo)->getName() != NULL && !strcmp((*bo)->getName(), objectName)) 197 return (*bo); 198 } 199 } 195 200 else 196 201 { 197 ClassList* tmp = ClassList::first; 198 while (likely(tmp != NULL)) 199 { 200 if (tmp->classID == classID || classID == CL_NULL) 201 { 202 list<BaseObject*>::iterator bo; 203 for (bo = tmp->objectList.begin(); bo != tmp->objectList.end(); bo++) 204 if ((*bo)->getName() && !strcmp((*bo)->getName(), name)) 205 return (*bo); 206 break; 207 } 208 tmp = tmp->next; 202 list<ClassList>::iterator cl; 203 for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++) 204 { 205 std::list<BaseObject*>::iterator bo; 206 for (bo = (*cl).objectList.begin(); bo != (*cl).objectList.end(); bo++) 207 if ((*bo)->getName() != NULL && !strcmp((*bo)->getName(), objectName)) 208 return (*bo); 209 209 } 210 210 } … … 220 220 * @todo: speed this up!! 221 221 */ 222 bool ClassList::exists(const BaseObject* object, long classID) 223 { 224 if(unlikely(ClassList::first == NULL) || object == NULL) 225 return false; 222 bool ClassList::exists(const BaseObject* object, ClassID classID) 223 { 224 if (classID != CL_NULL) 225 { 226 ClassList* cl = ClassList::getClassList(classID); 227 if (cl != NULL) 228 { 229 std::list<BaseObject*>::const_iterator bo = find (cl->objectList.begin(), cl->objectList.end(), object); 230 return (bo != cl->objectList.end()); 231 } 232 } 226 233 else 227 234 { 228 ClassList* tmp = ClassList::first; 229 while (likely(tmp != NULL)) 230 { 231 if (tmp->classID == classID || classID == CL_NULL) 232 { 233 list<BaseObject*>::iterator bo; 234 for (bo = tmp->objectList.begin(); bo != tmp->objectList.end(); bo++) 235 if ((*bo) == object) 236 return true; 237 if (likely(tmp->classID == classID)) 238 break; 239 } 240 tmp = tmp->next; 235 list<ClassList>::iterator cl; 236 for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++) 237 { 238 std::list<BaseObject*>::const_iterator bo = find ((*cl).objectList.begin(), (*cl).objectList.end(), object); 239 if (bo != (*cl).objectList.end()) 240 return true; 241 241 } 242 242 } … … 250 250 void ClassList::whatIs(const BaseObject* object) 251 251 { 252 ClassList* tmp = ClassList::first; 253 while (likely(tmp != NULL)) 254 { 255 if (object->isA(tmp->classID)) 256 { 257 PRINT(0)("=%s=-", tmp->className); 258 } 259 tmp = tmp->next; 252 list<ClassList>::iterator cl; 253 for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++) 254 if (object->isA((*cl).classID)) 255 { 256 PRINT(0)("=%s=-", (*cl).className); 260 257 } 261 258 } … … 266 263 * @return a String containing the name of the Class, NULL if the Class was not found 267 264 */ 268 const char* ClassList::IDToString(long classID) 269 { 270 if(likely(ClassList::first != NULL)) 271 { 272 ClassList* tmpCL = ClassList::first; 273 while (likely(tmpCL != NULL)) 274 { 275 if (tmpCL->classID == classID) 276 return tmpCL->className; 277 tmpCL = tmpCL->next; 278 } 279 } 280 return NULL; 265 const char* ClassList::IDToString(ClassID classID) 266 { 267 ClassList* cl = ClassList::getClassList(classID); 268 return (cl != NULL) ? cl->className : NULL; 281 269 } 282 270 … … 288 276 long ClassList::StringToID(const char* className) 289 277 { 290 if (className == NULL) 291 return CL_NULL; 292 if(likely(ClassList::first != NULL)) 293 { 294 ClassList* tmpCL = ClassList::first; 295 while (likely(tmpCL != NULL)) 296 { 297 if (!strcasecmp(tmpCL->className, className)) 298 return tmpCL->classID; 299 tmpCL = tmpCL->next; 300 } 301 } 302 return CL_NULL; 278 ClassList* cl = ClassList::getClassList(className); 279 return (cl != NULL) ? cl->classID : CL_NULL; 280 } 281 282 /** 283 * checks if this ClassList is named className 284 * @param className the Name to check this ClassList's ClassName against 285 * @returns true on match, false otherwise 286 */ 287 bool ClassList::operator==(const char* className) 288 { 289 if (likely( className != NULL && this->className != NULL)) 290 return (!strcmp(this->className, className)); 291 else 292 return false; 303 293 } 304 294 … … 317 307 PRINT(0)("= CLASS_LIST (level %d) =\n", debugLevel); 318 308 PRINT(0)("==========================\n"); 319 PRINT(0)("| knows %d Classes\n|\n", ClassList::classCount); 320 ClassList* tmp = ClassList::first; 309 PRINT(0)("| knows %d Classes\n|\n", ClassList::classList->size()); 321 310 char niceString[100]; 322 311 unsigned int lenCount = 0; 323 312 324 while (likely(tmp != NULL)) 325 { 326 if ((debugLevel >= 1 || tmp->objectList.size() > 0 ) && 327 (classID == CL_NULL || unlikely (classID == tmp->classID))) 313 list<ClassList>::iterator cl; 314 for (cl = ClassList::classList->begin(); cl != ClassList::classList->end(); cl++) 315 { 316 if ((debugLevel >= 1 || (*cl).objectList.size() > 0 ) && 317 (classID == CL_NULL || unlikely (classID == (*cl).classID))) 328 318 { 329 319 lenCount = 1; 330 while (pow(10, lenCount) <= tmp->objectList.size())320 while (pow(10, lenCount) <= (*cl).objectList.size()) 331 321 ++lenCount; 332 for (int i=0; i < 30-strlen( tmp->className) - lenCount; i++)322 for (int i=0; i < 30-strlen((*cl).className) - lenCount; i++) 333 323 (niceString[i]) = ' '; 334 niceString[30-strlen( tmp->className) - lenCount] = '\0';335 336 PRINT(0)("| CLASS %s:%s %d\n", tmp->className, niceString, tmp->objectList.size());337 338 if (debugLevel >=2 && tmp->objectList.size() > 0)324 niceString[30-strlen((*cl).className) - lenCount] = '\0'; 325 326 PRINT(0)("| CLASS %s:%s %d\n", (*cl).className, niceString, (*cl).objectList.size()); 327 328 if (debugLevel >=2 && (*cl).objectList.size() > 0) 339 329 { 340 330 PRINT(0)("| Listing Instances:\n"); 341 331 list<BaseObject*>::const_iterator bo; 342 for (bo = tmp->objectList.begin(); bo != tmp->objectList.end(); bo++)332 for (bo = (*cl).objectList.begin(); bo != (*cl).objectList.end(); bo++) 343 333 { 344 334 PRINT(0)("| (class %s): NAME(%s)->%p ", (*bo)->getClassName(), (*bo)->getName(), (*bo)); … … 349 339 } 350 340 } 351 tmp = tmp->next;352 341 } 353 342 PRINT(0)("=======================CL=\n"); -
trunk/src/lib/lang/class_list.h
r5779 r5791 10 10 #include "class_id.h" 11 11 #include <list> 12 #ifndef NULL 13 #define NULL 0 //!< NULL 14 #endif 12 15 13 16 // FORWARD DECLARATION … … 34 37 35 38 // STATIC FUNCTIONS 36 static void addToClassList(BaseObject* objectPointer, const long&classID, const char* className);37 static void removeFromClassList(BaseObject* objectPointer);39 static void addToClassList(BaseObject* objectPointer, ClassID classID, const char* className); 40 static void removeFromClassList(BaseObject* objectPointer); 38 41 39 static std::list<BaseObject*>* getList(long classID = CL_NULL);40 static std::list<BaseObject*>* getList(const char* className);41 static const std::list<const char*>* getClassList();42 static BaseObject* getObject(const char* name, longclassID = CL_NULL);43 static bool exists(const BaseObject* object, longclassID = CL_NULL);42 static std::list<BaseObject*>* getList(ClassID classID = CL_NULL);// { return (ClassList* fl = ClassList::getClassList(classID) != NULL)? &(fl->objectList) : NULL; }; 43 static std::list<BaseObject*>* getList(const char* className); // { return (ClassList* fl = ClassList::getClassList(className) != NULL)? &(fl->objectList) : NULL; }; 44 static const std::list<const char*>* getClassNames(); 45 static BaseObject* getObject(const char* name, ClassID classID = CL_NULL); 46 static bool exists(const BaseObject* object, ClassID classID = CL_NULL); 44 47 45 static void whatIs(const BaseObject* object);48 static void whatIs(const BaseObject* object); 46 49 47 static const char* IDToString(longclassID = CL_NULL);48 static long StringToID(const char* className);49 static void debug(unsigned int debugLevel = 0, long classID = CL_NULL);50 static void debugS(const char* className = 0x0, unsigned int debugLevel = 0);50 static const char* IDToString(ClassID classID = CL_NULL); 51 static long StringToID(const char* className); 52 static void debug(unsigned int debugLevel = 0, long classID = CL_NULL); 53 static void debugS(const char* className = 0x0, unsigned int debugLevel = 0); 51 54 52 55 bool operator==(ClassID classID) { return (this->classID == classID); }; 56 bool operator==(const char* className); 53 57 54 58 private: 55 std::list<BaseObject*> objectList; //!< A list of Objects belonging to this Class 59 static ClassList* getClassList(ClassID classID); 60 static ClassList* getClassList(const char* className); 61 62 private: 56 63 57 64 long classID; //!< ClassID stored in this ClassList \see ClassID 58 65 const char* className; //!< Name of the Class Stored here 59 66 60 ClassList* next; //!< Pointer to the next class in the List67 std::list<BaseObject*> objectList; //!< A list of Objects belonging to this Class 61 68 62 69 // STATIC MEMBERS 63 static ClassList* first; //!< The first Class in the List 64 static std::list<const char*> classList; //!< a List of all Names of all classes, that have registered so far. 65 static unsigned int classCount; //!< The Count of classes that have been registered (should match the lower description) 70 static std::list<ClassList>* classList; //!< The first Class in the List 71 static std::list<const char*> classNames; //!< a List of all Names of all classes, that have registered so far. 66 72 }; 67 73
Note: See TracChangeset
for help on using the changeset viewer.