[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 | |
---|
[9716] | 18 | #include "object_list.h" |
---|
[9659] | 19 | #include <cassert> |
---|
[1853] | 20 | |
---|
[9681] | 21 | /** |
---|
| 22 | * @brief Constructor, that creates an ObjectList while checking (development mode) for uniqueness of all Keys (names and ID's) |
---|
| 23 | * @param className The Name of the Class to create an ObjectList for. |
---|
| 24 | * @param id The ID if you like, or -1 otherwise. |
---|
| 25 | * @return a new NewObejctList |
---|
| 26 | */ |
---|
[9715] | 27 | ObjectListBase::ObjectListBase(const std::string& className, int id) |
---|
[9709] | 28 | : _name(className) |
---|
[9659] | 29 | { |
---|
[9715] | 30 | if (ObjectListBase::_classesByID == NULL) |
---|
[9675] | 31 | { |
---|
[9726] | 32 | ObjectListBase::_classesByID = new IDMap; |
---|
[9715] | 33 | assert (ObjectListBase::_classesByName == NULL); |
---|
[9726] | 34 | ObjectListBase::_classesByName = new NameMap; |
---|
[9675] | 35 | } |
---|
[9715] | 36 | assert(!ObjectListBase::classNameExists(className) && "Classes should only be included once, and no two classes should have the same name (key value)"); |
---|
[9671] | 37 | |
---|
[9685] | 38 | if (id == -1) |
---|
[9676] | 39 | { |
---|
[9715] | 40 | id = ObjectListBase::_classesByID->size(); |
---|
[9677] | 41 | // searching for a free ID |
---|
[9715] | 42 | while (ObjectListBase::classIDExists(id)) ++id; |
---|
[9676] | 43 | } |
---|
[9715] | 44 | assert(!ObjectListBase::classIDExists(id) && "Classes should only be included once, and no two classes should have the same ID (key value)"); |
---|
[9671] | 45 | |
---|
[9701] | 46 | _id = id; |
---|
[9677] | 47 | /// Some Output, that will fall out later |
---|
[9725] | 48 | // std::cout << "register new ObjectList " << className << " ID: " << this->_id << std::endl; |
---|
[9677] | 49 | |
---|
[9715] | 50 | this->_identity = ClassID(this); |
---|
| 51 | (*ObjectListBase::_classesByID)[this->_identity.id()] = this; |
---|
| 52 | (*ObjectListBase::_classesByName)[this->_identity.name()] = this; |
---|
[3365] | 53 | } |
---|
[9661] | 54 | |
---|
[9676] | 55 | |
---|
| 56 | /** |
---|
| 57 | * Destructor. |
---|
| 58 | * |
---|
[9715] | 59 | * This destructor deletes the ObjectList, and cleans up the ObjectList sorted Maps. |
---|
[9676] | 60 | */ |
---|
[9715] | 61 | ObjectListBase::~ObjectListBase() |
---|
[9661] | 62 | { |
---|
[9715] | 63 | assert (ObjectListBase::_classesByName != NULL && ObjectListBase::_classesByID != NULL); |
---|
[9681] | 64 | /* |
---|
[9677] | 65 | std::cout << "Erasing: " << this->_name << " "<< this->_id << std::endl; |
---|
[9715] | 66 | std::cout << "SIZE OF _classByID: " << ObjectListBase::_classesByID->size() << std::endl; |
---|
| 67 | std::cout << "SIZE OF _classByName: " << ObjectListBase::_classesByName->size() << std::endl; |
---|
[9681] | 68 | */ |
---|
[9715] | 69 | ObjectListBase::_classesByID->erase(this->_identity.id()); |
---|
| 70 | ObjectListBase::_classesByName->erase(this->_identity.name()); |
---|
[9661] | 71 | |
---|
[9715] | 72 | if (ObjectListBase::_classesByID->empty()) |
---|
[9675] | 73 | { |
---|
[9715] | 74 | delete ObjectListBase::_classesByID; |
---|
| 75 | ObjectListBase::_classesByID = NULL; |
---|
| 76 | assert(ObjectListBase::_classesByName != NULL); |
---|
| 77 | delete ObjectListBase::_classesByName; |
---|
| 78 | ObjectListBase::_classesByName = NULL; |
---|
[9675] | 79 | } |
---|
[9661] | 80 | } |
---|
| 81 | |
---|
[9726] | 82 | ObjectListBase::IDMap* ObjectListBase::_classesByID = NULL; |
---|
| 83 | ObjectListBase::NameMap* ObjectListBase::_classesByName = NULL; |
---|
[9725] | 84 | std::list<std::string> ObjectListBase::_classNames; |
---|
[9661] | 85 | |
---|
| 86 | /** |
---|
[9676] | 87 | * @returns the Registered Class Count. |
---|
| 88 | */ |
---|
[9715] | 89 | unsigned int ObjectListBase::classCount() |
---|
[9676] | 90 | { |
---|
[9715] | 91 | assert (ObjectListBase::_classesByID != NULL); |
---|
| 92 | return ObjectListBase::_classesByID->size(); |
---|
[9676] | 93 | }; |
---|
| 94 | |
---|
| 95 | /** |
---|
[9661] | 96 | * @brief Checks if a Class with name already exists. |
---|
[9724] | 97 | * @param id The id of the Class to check. |
---|
[9661] | 98 | * @return true if such a class already exists. |
---|
| 99 | */ |
---|
[9715] | 100 | bool ObjectListBase::classIDExists(int id) |
---|
[9709] | 101 | { |
---|
[9715] | 102 | return (ObjectListBase::_classesByID->find(id) != ObjectListBase::_classesByID->end()); |
---|
[9709] | 103 | } |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | * @brief Checks if a Class with name already exists. |
---|
| 107 | * @param name The Name of the Class to check. |
---|
| 108 | * @return true if such a class already exists. |
---|
| 109 | */ |
---|
[9715] | 110 | bool ObjectListBase::classNameExists(const std::string& name) |
---|
[9661] | 111 | { |
---|
[9715] | 112 | return (ObjectListBase::_classesByName->find(name) != ObjectListBase::_classesByName->end()); |
---|
[9661] | 113 | } |
---|
[9675] | 114 | |
---|
[9676] | 115 | /** |
---|
[9715] | 116 | * @brief searches for a ClassID in the list of all ObjectLists, and returns its Identity |
---|
[9705] | 117 | * @param id: The Id to search for |
---|
| 118 | * @returns the ClassID if found and NullClass' identity if not. |
---|
| 119 | */ |
---|
[9715] | 120 | const ClassID& ObjectListBase::retrieveIdentity(int id) |
---|
[9705] | 121 | { |
---|
[9715] | 122 | const ObjectListBase* const base = ObjectListBase::getObjectList(id); |
---|
[9705] | 123 | |
---|
| 124 | if (base != NULL) |
---|
| 125 | return base->_identity; |
---|
| 126 | else |
---|
[9757] | 127 | return NullClass::staticClassID(); |
---|
[9705] | 128 | } |
---|
| 129 | |
---|
[9709] | 130 | |
---|
[9705] | 131 | /** |
---|
[9715] | 132 | * @brief searches for a ClassID in the list of all ObjectLists, and returns its Identity |
---|
[9705] | 133 | * @param name: The Name to search for |
---|
| 134 | * @returns the ClassID if found and NullClass' identity if not. |
---|
| 135 | */ |
---|
[9715] | 136 | const ClassID& ObjectListBase::retrieveIdentity(const std::string& name) |
---|
[9705] | 137 | { |
---|
[9715] | 138 | const ObjectListBase* const base = ObjectListBase::getObjectList(name); |
---|
[9705] | 139 | |
---|
| 140 | if (base != NULL) |
---|
| 141 | return base->_identity; |
---|
| 142 | else |
---|
[9757] | 143 | return NullClass::staticClassID(); |
---|
[9705] | 144 | } |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | /** |
---|
[9696] | 148 | * @brief Searches for a ObjectList with the ID classID |
---|
| 149 | * @param classID the ID to search for. |
---|
[9715] | 150 | * @return The ObjectList if found, NULL otherwise. |
---|
[9696] | 151 | */ |
---|
[9715] | 152 | const ObjectListBase* const ObjectListBase::getObjectList(int classID) |
---|
[9696] | 153 | { |
---|
[9715] | 154 | assert (ObjectListBase::_classesByID != NULL); |
---|
[9726] | 155 | ObjectListBase::IDMap::iterator it = ObjectListBase::_classesByID->find(classID); |
---|
[9715] | 156 | if (it != ObjectListBase::_classesByID->end()) |
---|
[9696] | 157 | return (*it).second; |
---|
| 158 | else |
---|
| 159 | return NULL; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * @brief Searches for a ObjectList with the Name className |
---|
| 164 | * @param className the Name to search for. |
---|
[9715] | 165 | * @return The ObjectList if found, NULL otherwise. |
---|
[9696] | 166 | */ |
---|
[9715] | 167 | const ObjectListBase* const ObjectListBase::getObjectList(const std::string& className) |
---|
[9696] | 168 | { |
---|
[9715] | 169 | assert (ObjectListBase::_classesByName != NULL); |
---|
[9726] | 170 | ObjectListBase::NameMap::iterator it = ObjectListBase::_classesByName->find(className); |
---|
[9715] | 171 | if (it != ObjectListBase::_classesByName->end()) |
---|
[9696] | 172 | return (*it).second; |
---|
| 173 | else |
---|
| 174 | return NULL; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /** |
---|
[9715] | 178 | * @brief Searches for a ObjectList with the ClassID classID |
---|
[9696] | 179 | * @param classID the ID to search for. |
---|
[9715] | 180 | * @return The ObjectList if found, NULL otherwise. |
---|
[9696] | 181 | */ |
---|
[9715] | 182 | const ObjectListBase* const ObjectListBase::getObjectList(const ClassID& classID) |
---|
[9696] | 183 | { |
---|
[9715] | 184 | return ObjectListBase::getObjectList(classID.id()); |
---|
[9696] | 185 | } |
---|
| 186 | |
---|
| 187 | /** |
---|
| 188 | * @brief Retrieves the first BaseObject matching the name objectName from the List matching classID. |
---|
| 189 | * @param classID the ID of the List. |
---|
| 190 | * @param objectName the Name of the Object to search for |
---|
| 191 | */ |
---|
[9715] | 192 | BaseObject* ObjectListBase::getBaseObject(int classID, const std::string& objectName) |
---|
[9696] | 193 | { |
---|
[9715] | 194 | const ObjectListBase* const base = ObjectListBase::getObjectList(classID); |
---|
[9696] | 195 | |
---|
| 196 | if (base != NULL) |
---|
| 197 | return base->getBaseObject(objectName); |
---|
| 198 | else |
---|
| 199 | return NULL; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | /** |
---|
| 203 | * @brief Retrieves the first BaseObject matching the name objectName from the List matching className. |
---|
| 204 | * @param className the Name of the List. |
---|
| 205 | * @param objectName the Name of the Object to search for |
---|
| 206 | */ |
---|
[9715] | 207 | BaseObject* ObjectListBase::getBaseObject(const std::string& className, const std::string& objectName) |
---|
[9696] | 208 | { |
---|
[9715] | 209 | const ObjectListBase* const base = ObjectListBase::getObjectList(className); |
---|
[9696] | 210 | |
---|
| 211 | if (base != NULL) |
---|
| 212 | return base->getBaseObject(objectName); |
---|
| 213 | else |
---|
| 214 | return NULL; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | /** |
---|
| 218 | * @brief Retrieves the first BaseObject matching the name objectName from the List matching classID. |
---|
[9715] | 219 | * @param classID The ClassID of the List. |
---|
[9696] | 220 | * @param objectName the Name of the Object to search for |
---|
| 221 | */ |
---|
[9715] | 222 | BaseObject* ObjectListBase::getBaseObject(const ClassID& classID, const std::string& objectName) |
---|
[9696] | 223 | { |
---|
[9715] | 224 | const ObjectListBase* const base = ObjectListBase::getObjectList(classID); |
---|
[9696] | 225 | |
---|
| 226 | if (base != NULL) |
---|
| 227 | return base->getBaseObject(objectName); |
---|
| 228 | else |
---|
| 229 | return NULL; |
---|
| 230 | } |
---|
| 231 | |
---|
[9724] | 232 | |
---|
| 233 | /** |
---|
| 234 | * @returns An alphabetically sorted List of all stored ClassNames. |
---|
| 235 | */ |
---|
| 236 | const std::list<std::string>& ObjectListBase::getClassNames() |
---|
| 237 | { |
---|
| 238 | if (ObjectListBase::classCount() != ObjectListBase::_classNames.size()) |
---|
| 239 | { |
---|
| 240 | ObjectListBase::_classNames.clear(); |
---|
| 241 | |
---|
[9726] | 242 | for (NameMap::const_iterator it = ObjectListBase::_classesByName->begin(); |
---|
| 243 | it != ObjectListBase::_classesByName->end(); |
---|
| 244 | ++it) |
---|
[9724] | 245 | ObjectListBase::_classNames.push_back((*it).second->name()); |
---|
| 246 | } |
---|
| 247 | return ObjectListBase::_classNames; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | |
---|
[9709] | 251 | #include "base_object.h" |
---|
[9696] | 252 | |
---|
| 253 | /** |
---|
[9709] | 254 | * @brief Prints out some debugging information about a given List. |
---|
[9724] | 255 | * @param level: |
---|
| 256 | * 1: List ObjectListsand how many object. |
---|
| 257 | * 2: 1+List ObjectLists entries, and information about Objects. |
---|
[9709] | 258 | */ |
---|
[9715] | 259 | void ObjectListBase::debug(unsigned int level) const |
---|
[9709] | 260 | { |
---|
| 261 | base_list list; |
---|
| 262 | this->getBaseObjectList(&list); |
---|
| 263 | |
---|
| 264 | if (level > 1 || !list.empty()) |
---|
| 265 | printf(" ObjectList of class %s(id:%d) contains %d objects\n", this->name().c_str(), this->id(), list.size()); |
---|
| 266 | |
---|
| 267 | if (level >= 2) |
---|
| 268 | { |
---|
| 269 | printf(" - listing Instances: \n"); |
---|
| 270 | for (base_iterator it = list.begin(); |
---|
| 271 | it != list.end(); |
---|
| 272 | ++it) |
---|
| 273 | { |
---|
| 274 | printf(" + %s::%s\n", (*it)->getClassCName(), (*it)->getCName()); |
---|
| 275 | } |
---|
| 276 | } |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | |
---|
[9724] | 280 | /** |
---|
| 281 | * @brief prints out debug output about all Lists |
---|
| 282 | * @param level: |
---|
| 283 | * 0: list number of ClassList and general info. |
---|
| 284 | * 1: 0+List ObjectLists and how many object. |
---|
| 285 | * 2: 1+List ObjectLists entries, and information about Objects. |
---|
| 286 | */ |
---|
[9715] | 287 | void ObjectListBase::debugAll(unsigned int level) |
---|
[9709] | 288 | { |
---|
[9715] | 289 | printf("Listing all %d ObjectLists \n", ObjectListBase::_classesByID->size()); |
---|
[9709] | 290 | |
---|
[9726] | 291 | for (NameMap::const_iterator it = ObjectListBase::_classesByName->begin(); |
---|
[9715] | 292 | it != ObjectListBase::_classesByName->end(); |
---|
[9709] | 293 | ++it) |
---|
| 294 | (*it).second->debug(level); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | |
---|
| 298 | |
---|
| 299 | /** |
---|
[9675] | 300 | * @brief Converts an ID into a ClassName String. |
---|
| 301 | * @param classID The ID to convert. |
---|
| 302 | * @return The ClassName or an empty string if the ID was not found. |
---|
| 303 | */ |
---|
[9715] | 304 | const std::string& ObjectListBase::IDToString(int classID) |
---|
[9675] | 305 | { |
---|
[9715] | 306 | const ObjectListBase* const base = ObjectListBase::getObjectList(classID); |
---|
[9696] | 307 | |
---|
| 308 | if (base != NULL) |
---|
| 309 | return base->name(); |
---|
[9675] | 310 | else |
---|
| 311 | { |
---|
| 312 | static std::string empty; |
---|
| 313 | return empty; |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | |
---|
| 318 | /** |
---|
| 319 | * @brief Converts a String into an ID |
---|
| 320 | * @param className the Name of the Class to search for |
---|
| 321 | * @return The Classes ID if found, -1 otherwise. |
---|
| 322 | */ |
---|
[9715] | 323 | int ObjectListBase::StringToID(const std::string& className) |
---|
[9675] | 324 | { |
---|
[9715] | 325 | const ObjectListBase* const base = ObjectListBase::getObjectList(className); |
---|
[9696] | 326 | |
---|
| 327 | if (base != NULL) |
---|
| 328 | return base->id(); |
---|
[9675] | 329 | else |
---|
| 330 | return -1; |
---|
| 331 | } |
---|