/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ #include "class_list.h" #include "base_object.h" #include "list.h" #include "compiler.h" #include "debug.h" #include #include using namespace std; /** \brief Creates a new ClassList */ ClassList::ClassList(const long& classID, const char* className) { this->next = NULL; this->className = className; this->classID = classID; this->objectList = new tList; ++ClassList::classCount; } /** \brief standard deconstructor */ ClassList::~ClassList () { delete this->objectList; --ClassList::classCount; } //! the first class that is registered ClassList* ClassList::first = NULL; //! the Count of classes unsigned int ClassList::classCount = 0; /** * Adds a new Object to the ClassList (and if necessary a new Class) * @param objectPointer Pointer to the Object at hand * @param classID ID of the Given ObjectType \see ClassID * @param className name of the Class to add */ void ClassList::addToClassList(BaseObject* objectPointer, const long& classID, const char* className) { ClassList* regClass; PRINTF(5)("subscribe a %s\n", className ); if(ClassList::first == NULL) ClassList::first = regClass = new ClassList(classID, className); else { ClassList* tmp = ClassList::first; while (likely(tmp != NULL)) { if (tmp->classID == classID) { regClass = tmp; break; } if (unlikely(tmp->next == NULL)) { tmp->next = regClass = new ClassList(classID, className); break; } tmp = tmp->next; } } regClass->objectList->add(objectPointer); } /** * removes an Object from a the ClassList * @param objectPointer the Object to delete from the List */ void ClassList::removeFromClassList(BaseObject* objectPointer) { ClassList* tmp = ClassList::first; while (likely(tmp != NULL)) { if (objectPointer->isA(tmp->classID)) { tmp->objectList->remove(objectPointer); } tmp = tmp->next; } } /** * checks if the BaseObject* object exists. * @param name the name of the BaseObject to look for * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere. * @return true, if the Object Exists in the specified ClassID, false otherwise * @todo: speed this up!! */ BaseObject* ClassList::getObject(const char* name, long classID) { if(unlikely(ClassList::first == NULL) || name == NULL) return NULL; else { ClassList* tmp = ClassList::first; while (likely(tmp != NULL)) { if (tmp->classID == classID || classID == CL_NULL) { tIterator* iterator = tmp->objectList->getIterator(); BaseObject* enumBO = iterator->nextElement(); const char* tmpName; while (enumBO != NULL) { tmpName = enumBO->getName(); if (tmpName && !strcmp(tmpName, name)) { delete iterator; return enumBO; } enumBO = iterator->nextElement(); } delete iterator; break; } tmp = tmp->next; } } return NULL; } /** * checks if the BaseObject* object exists. * @param object the Pointer to a BaseObject to check if it exists * @param classID if not CL_NULL it will only search through a specific type of Objects. Otherwise it will be searched everywhere. * @return true, if the Object Exists in the specified ClassID, false otherwise * @todo: speed this up!! */ bool ClassList::exists(BaseObject* object, long classID) { if(unlikely(ClassList::first == NULL)) return false; else { ClassList* tmp = ClassList::first; while (likely(tmp != NULL)) { if (tmp->classID == classID || classID == CL_NULL) { tIterator* iterator = tmp->objectList->getIterator(); BaseObject* enumBO = iterator->nextElement(); while (enumBO != NULL) { if (enumBO == object) { delete iterator; return true; } enumBO = iterator->nextElement(); } delete iterator; break; } tmp = tmp->next; } } return false; } /** * Print out some very nice debug information */ void ClassList::debug(unsigned int debugLevel) { if (debugLevel > 2) debugLevel = 2; PRINT(0)("==========================\n"); PRINT(0)("= CLASS_LIST (level %d) =\n", debugLevel); PRINT(0)("==========================\n"); PRINT(0)("| knows %d Classes\n|\n", ClassList::classCount); ClassList* tmp = ClassList::first; char niceString[100]; unsigned int lenCount = 0; while (likely(tmp != NULL)) { if (debugLevel >= 1 || tmp->objectList->getSize() > 0) { lenCount = 1; while (pow(10,lenCount) <= tmp->objectList->getSize()) ++lenCount; for (int i=0; i < 30-strlen(tmp->className) - lenCount; i++) (niceString[i]) = ' '; niceString[30-strlen(tmp->className) - lenCount] = '\0'; PRINT(0)("| CLASS %s:%s %d\n", tmp->className, niceString, tmp->objectList->getSize()); if (debugLevel >=2 && tmp->objectList->getSize() > 0) { PRINT(0)("| Listing Instances:\n"); tIterator* iterator = tmp->objectList->getIterator(); BaseObject* enumBO = iterator->nextElement(); while (enumBO) { PRINT(0)("| (class %s): NAME(%s)->%p\n", enumBO->getClassName(), enumBO->getName(), enumBO); enumBO = iterator->nextElement(); } delete iterator; } } tmp = tmp->next; } PRINT(0)("=======================CL=\n"); }