/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2006 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 "new_object_list.h" #include /** * @brief Constructor, that creates an ObjectList while checking (development mode) for uniqueness of all Keys (names and ID's) * @param className The Name of the Class to create an ObjectList for. * @param id The ID if you like, or -1 otherwise. * @return a new NewObejctList */ NewObjectListBase::NewObjectListBase(const std::string& className, int id) : _name(className) { if (NewObjectListBase::_classesByID == NULL) { NewObjectListBase::_classesByID = new classIDMap; assert (NewObjectListBase::_classesByName == NULL); NewObjectListBase::_classesByName = new classNameMap; } assert(!NewObjectListBase::classNameExists(className) && "Classes should only be included once, and no two classes should have the same name (key value)"); if (id == -1) { id = NewObjectListBase::_classesByID->size(); // searching for a free ID while (NewObjectListBase::classIDExists(id)) ++id; } assert(!NewObjectListBase::classIDExists(id) && "Classes should only be included once, and no two classes should have the same ID (key value)"); _id = id; /// Some Output, that will fall out later //std::cout << "register new ObjectList " << className << " ID: " << this->_id << std::endl; this->_identity = NewClassID(this); (*NewObjectListBase::_classesByID)[this->_identity.id()] = this; (*NewObjectListBase::_classesByName)[this->_identity.name()] = this; } /** * Destructor. * * This destructor deletes the NewObjectList, and cleans up the NewObjectList sorted Maps. */ NewObjectListBase::~NewObjectListBase() { assert (NewObjectListBase::_classesByName != NULL && NewObjectListBase::_classesByID != NULL); /* std::cout << "Erasing: " << this->_name << " "<< this->_id << std::endl; std::cout << "SIZE OF _classByID: " << NewObjectListBase::_classesByID->size() << std::endl; std::cout << "SIZE OF _classByName: " << NewObjectListBase::_classesByName->size() << std::endl; */ NewObjectListBase::_classesByID->erase(this->_identity.id()); NewObjectListBase::_classesByName->erase(this->_identity.name()); if (NewObjectListBase::_classesByID->empty()) { delete NewObjectListBase::_classesByID; NewObjectListBase::_classesByID = NULL; assert(NewObjectListBase::_classesByName != NULL); delete NewObjectListBase::_classesByName; NewObjectListBase::_classesByName = NULL; } } NewObjectListBase::classIDMap* NewObjectListBase::_classesByID = NULL; NewObjectListBase::classNameMap* NewObjectListBase::_classesByName = NULL; /** * @returns the Registered Class Count. */ unsigned int NewObjectListBase::classCount() { assert (NewObjectListBase::_classesByID != NULL); return NewObjectListBase::_classesByID->size(); }; /** * @brief Checks if a Class with name already exists. * @param name The Name of the Class to check. * @return true if such a class already exists. */ bool NewObjectListBase::classNameExists(const std::string& name) { return (NewObjectListBase::_classesByName->find(name) != NewObjectListBase::_classesByName->end()); } /** * @brief searches for a NewClassID in the list of all NewObjectLists, and returns its Identity * @param id: The Id to search for * @returns the ClassID if found and NullClass' identity if not. */ const NewClassID& NewObejctListBase::retrieveIdentity(int id) { const NewObjectListBase* const base = NewObjectListBase::getObjectList(id); if (base != NULL) return base->_identity; else return NullClass::classID(); } /** * @brief searches for a NewClassID in the list of all NewObjectLists, and returns its Identity * @param name: The Name to search for * @returns the ClassID if found and NullClass' identity if not. */ const NewClassID& NewObjectListBase::retrieveIdentity(const std::string& name) { const NewObjectListBase* const base = NewObjectListBase::getObjectList(name); if (base != NULL) return base->_identity; else return NullObject::classID(); } /** * @brief Checks if a Class with name already exists. * @param name The Name of the Class to check. * @return true if such a class already exists. */ bool NewObjectListBase::classIDExists(int id) { return (NewObjectListBase::_classesByID->find(id) != NewObjectListBase::_classesByID->end()); } /** * @brief Searches for a ObjectList with the ID classID * @param classID the ID to search for. * @return The NewObjectList if found, NULL otherwise. */ const NewObjectListBase* const NewObjectListBase::getObjectList(int classID) { assert (NewObjectListBase::_classesByID != NULL); NewObjectListBase::classIDMap::iterator it = NewObjectListBase::_classesByID->find(classID); if (it != NewObjectListBase::_classesByID->end()) return (*it).second; else return NULL; } /** * @brief Searches for a ObjectList with the Name className * @param className the Name to search for. * @return The NewObjectList if found, NULL otherwise. */ const NewObjectListBase* const NewObjectListBase::getObjectList(const std::string& className) { assert (NewObjectListBase::_classesByName != NULL); NewObjectListBase::classNameMap::iterator it = NewObjectListBase::_classesByName->find(className); if (it != NewObjectListBase::_classesByName->end()) return (*it).second; else return NULL; } /** * @brief Searches for a ObjectList with the NewClassID classID * @param classID the ID to search for. * @return The NewObjectList if found, NULL otherwise. */ const NewObjectListBase* const NewObjectListBase::getObjectList(const NewClassID& classID) { return NewObjectListBase::getObjectList(classID.id()); } /** * @brief Retrieves the first BaseObject matching the name objectName from the List matching classID. * @param classID the ID of the List. * @param objectName the Name of the Object to search for */ BaseObject* NewObjectListBase::getBaseObject(int classID, const std::string& objectName) { const NewObjectListBase* const base = NewObjectListBase::getObjectList(classID); if (base != NULL) return base->getBaseObject(objectName); else return NULL; } /** * @brief Retrieves the first BaseObject matching the name objectName from the List matching className. * @param className the Name of the List. * @param objectName the Name of the Object to search for */ BaseObject* NewObjectListBase::getBaseObject(const std::string& className, const std::string& objectName) { const NewObjectListBase* const base = NewObjectListBase::getObjectList(className); if (base != NULL) return base->getBaseObject(objectName); else return NULL; } /** * @brief Retrieves the first BaseObject matching the name objectName from the List matching classID. * @param classID The NewClassID of the List. * @param objectName the Name of the Object to search for */ BaseObject* NewObjectListBase::getBaseObject(const NewClassID& classID, const std::string& objectName) { const NewObjectListBase* const base = NewObjectListBase::getObjectList(classID); if (base != NULL) return base->getBaseObject(objectName); else return NULL; } /** * @brief Converts an ID into a ClassName String. * @param classID The ID to convert. * @return The ClassName or an empty string if the ID was not found. */ const std::string& NewObjectListBase::IDToString(int classID) { const NewObjectListBase* const base = NewObjectListBase::getObjectList(classID); if (base != NULL) return base->name(); else { static std::string empty; return empty; } } /** * @brief Converts a String into an ID * @param className the Name of the Class to search for * @return The Classes ID if found, -1 otherwise. */ int NewObjectListBase::StringToID(const std::string& className) { const NewObjectListBase* const base = NewObjectListBase::getObjectList(className); if (base != NULL) return base->id(); else return -1; }