[4744] | 1 | /* |
---|
[1853] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
[9659] | 4 | Copyright (C) 2006 orx |
---|
[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[9659] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3955] | 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
[1853] | 17 | |
---|
[9662] | 18 | #include "new_object_list.h" |
---|
[9659] | 19 | #include <cassert> |
---|
[1853] | 20 | |
---|
| 21 | |
---|
[9681] | 22 | /** |
---|
| 23 | * @brief Constructor, that creates an ObjectList while checking (development mode) for uniqueness of all Keys (names and ID's) |
---|
| 24 | * @param className The Name of the Class to create an ObjectList for. |
---|
| 25 | * @param id The ID if you like, or -1 otherwise. |
---|
| 26 | * @return a new NewObejctList |
---|
| 27 | */ |
---|
[9676] | 28 | NewObjectListBase::NewObjectListBase(const std::string& className, int id) |
---|
| 29 | : _name(className) |
---|
[9659] | 30 | { |
---|
[9675] | 31 | if (NewObjectListBase::_classesByID == NULL) |
---|
| 32 | { |
---|
| 33 | NewObjectListBase::_classesByID = new classIDMap; |
---|
| 34 | assert (NewObjectListBase::_classesByName == NULL); |
---|
| 35 | NewObjectListBase::_classesByName = new classNameMap; |
---|
| 36 | } |
---|
[9676] | 37 | assert(!NewObjectListBase::classNameExists(className) && "Classes should only be included once, and no two classes should have the same name (key value)"); |
---|
[9671] | 38 | |
---|
[9676] | 39 | if (id != -1) |
---|
| 40 | { |
---|
[9677] | 41 | this->_id = id; |
---|
[9676] | 42 | } |
---|
| 43 | else |
---|
| 44 | { |
---|
| 45 | this->_id = NewObjectListBase::_classesByID->size(); |
---|
[9677] | 46 | // searching for a free ID |
---|
| 47 | while (NewObjectListBase::classIDExists(_id)) ++id; |
---|
[9676] | 48 | } |
---|
[9677] | 49 | assert(!NewObjectListBase::classIDExists(id) && "Classes should only be included once, and no two classes should have the same ID (key value)"); |
---|
[9671] | 50 | |
---|
[9677] | 51 | /// Some Output, that will fall out later |
---|
[9681] | 52 | //std::cout << "register new ObjectList " << className << " ID: " << this->_id << std::endl; |
---|
[9677] | 53 | |
---|
[9675] | 54 | (*NewObjectListBase::_classesByID)[this->_id] = this; |
---|
| 55 | (*NewObjectListBase::_classesByName)[this->_name] = this; |
---|
[3365] | 56 | } |
---|
[9661] | 57 | |
---|
[9676] | 58 | |
---|
| 59 | /** |
---|
| 60 | * Destructor. |
---|
| 61 | * |
---|
| 62 | * This destructor deletes the NewObjectList, and cleans up the NewObjectList sorted Maps. |
---|
| 63 | */ |
---|
[9661] | 64 | NewObjectListBase::~NewObjectListBase() |
---|
| 65 | { |
---|
[9677] | 66 | assert (NewObjectListBase::_classesByName != NULL && NewObjectListBase::_classesByID != NULL); |
---|
[9681] | 67 | /* |
---|
[9677] | 68 | std::cout << "Erasing: " << this->_name << " "<< this->_id << std::endl; |
---|
| 69 | std::cout << "SIZE OF _classByID: " << NewObjectListBase::_classesByID->size() << std::endl; |
---|
| 70 | std::cout << "SIZE OF _classByName: " << NewObjectListBase::_classesByName->size() << std::endl; |
---|
[9681] | 71 | */ |
---|
[9675] | 72 | NewObjectListBase::_classesByName->erase(this->_name); |
---|
| 73 | NewObjectListBase::_classesByID->erase(this->_id); |
---|
[9661] | 74 | |
---|
[9675] | 75 | if (NewObjectListBase::_classesByID->empty()) |
---|
| 76 | { |
---|
| 77 | delete NewObjectListBase::_classesByID; |
---|
| 78 | NewObjectListBase::_classesByID = NULL; |
---|
| 79 | assert(NewObjectListBase::_classesByName != NULL); |
---|
| 80 | delete NewObjectListBase::_classesByName; |
---|
| 81 | NewObjectListBase::_classesByName = NULL; |
---|
| 82 | } |
---|
[9661] | 83 | } |
---|
| 84 | |
---|
[9675] | 85 | NewObjectListBase::classIDMap* NewObjectListBase::_classesByID = NULL; |
---|
| 86 | NewObjectListBase::classNameMap* NewObjectListBase::_classesByName = NULL; |
---|
[9661] | 87 | |
---|
| 88 | /** |
---|
[9676] | 89 | * @returns the Registered Class Count. |
---|
| 90 | */ |
---|
| 91 | unsigned int NewObjectListBase::classCount() |
---|
| 92 | { |
---|
| 93 | assert (NewObjectListBase::_classesByID != NULL); |
---|
| 94 | return NewObjectListBase::_classesByID->size(); |
---|
| 95 | }; |
---|
| 96 | |
---|
| 97 | /** |
---|
[9661] | 98 | * @brief Checks if a Class with name already exists. |
---|
| 99 | * @param name The Name of the Class to check. |
---|
| 100 | * @return true if such a class already exists. |
---|
| 101 | */ |
---|
[9663] | 102 | bool NewObjectListBase::classNameExists(const std::string& name) |
---|
[9661] | 103 | { |
---|
[9675] | 104 | return (NewObjectListBase::_classesByName->find(name) != NewObjectListBase::_classesByName->end()); |
---|
[9661] | 105 | } |
---|
[9675] | 106 | |
---|
[9676] | 107 | /** |
---|
| 108 | * @brief Checks if a Class with name already exists. |
---|
| 109 | * @param name The Name of the Class to check. |
---|
| 110 | * @return true if such a class already exists. |
---|
| 111 | */ |
---|
| 112 | bool NewObjectListBase::classIDExists(int id) |
---|
| 113 | { |
---|
| 114 | return (NewObjectListBase::_classesByID->find(id) != NewObjectListBase::_classesByID->end()); |
---|
| 115 | } |
---|
[9675] | 116 | |
---|
[9676] | 117 | |
---|
[9675] | 118 | /** |
---|
| 119 | * @brief Converts an ID into a ClassName String. |
---|
| 120 | * @param classID The ID to convert. |
---|
| 121 | * @return The ClassName or an empty string if the ID was not found. |
---|
| 122 | */ |
---|
| 123 | const std::string& NewObjectListBase::IDToString(int classID) |
---|
| 124 | { |
---|
| 125 | assert (NewObjectListBase::_classesByID != NULL); |
---|
| 126 | NewObjectListBase::classIDMap::iterator it = NewObjectListBase::_classesByID->find(classID); |
---|
| 127 | if (it != NewObjectListBase::_classesByID->end()) |
---|
| 128 | return (*it).second->name(); |
---|
| 129 | else |
---|
| 130 | { |
---|
| 131 | static std::string empty; |
---|
| 132 | return empty; |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | /** |
---|
| 138 | * @brief Converts a String into an ID |
---|
| 139 | * @param className the Name of the Class to search for |
---|
| 140 | * @return The Classes ID if found, -1 otherwise. |
---|
| 141 | */ |
---|
| 142 | int NewObjectListBase::StringToID(const std::string& className) |
---|
| 143 | { |
---|
| 144 | assert (NewObjectListBase::_classesByName != NULL); |
---|
| 145 | NewObjectListBase::classNameMap::iterator it = NewObjectListBase::_classesByName->find(className); |
---|
| 146 | if (it != NewObjectListBase::_classesByName->end()) |
---|
| 147 | return (*it).second->id(); |
---|
| 148 | else |
---|
| 149 | return -1; |
---|
| 150 | } |
---|
| 151 | |
---|