Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/lang/new_object_list.cc @ 9705

Last change on this file since 9705 was 9705, checked in by bensch, 18 years ago

adapted many more classes

File size: 8.2 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2006 orx
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.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "new_object_list.h"
19#include <cassert>
20
21
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 */
28NewObjectListBase::NewObjectListBase(const std::string& className, int id)
29  : _name(className)
30{
31  if (NewObjectListBase::_classesByID == NULL)
32  {
33    NewObjectListBase::_classesByID = new classIDMap;
34    assert (NewObjectListBase::_classesByName == NULL);
35    NewObjectListBase::_classesByName = new classNameMap;
36  }
37  assert(!NewObjectListBase::classNameExists(className) && "Classes should only be included once, and no two classes should have the same name (key value)");
38
39  if (id == -1)
40  {
41    id = NewObjectListBase::_classesByID->size();
42    // searching for a free ID
43    while (NewObjectListBase::classIDExists(id)) ++id;
44  }
45  assert(!NewObjectListBase::classIDExists(id) && "Classes should only be included once, and no two classes should have the same ID (key value)");
46
47  _id = id;
48  /// Some Output, that will fall out later
49  //std::cout << "register new ObjectList " << className << " ID: " << this->_id << std::endl;
50
51  this->_identity = NewClassID(this);
52  (*NewObjectListBase::_classesByID)[this->_identity.id()] = this;
53  (*NewObjectListBase::_classesByName)[this->_identity.name()] = this;
54}
55
56
57/**
58 * Destructor.
59 *
60 * This destructor deletes the NewObjectList, and cleans up the NewObjectList sorted Maps.
61 */
62NewObjectListBase::~NewObjectListBase()
63{
64  assert (NewObjectListBase::_classesByName != NULL && NewObjectListBase::_classesByID != NULL);
65  /*
66  std::cout << "Erasing: " << this->_name << " "<< this->_id  << std::endl;
67  std::cout << "SIZE OF _classByID: " << NewObjectListBase::_classesByID->size() << std::endl;
68  std::cout << "SIZE OF _classByName: " << NewObjectListBase::_classesByName->size() << std::endl;
69  */
70  NewObjectListBase::_classesByID->erase(this->_identity.id());
71  NewObjectListBase::_classesByName->erase(this->_identity.name());
72
73  if (NewObjectListBase::_classesByID->empty())
74  {
75    delete NewObjectListBase::_classesByID;
76    NewObjectListBase::_classesByID = NULL;
77    assert(NewObjectListBase::_classesByName != NULL);
78    delete NewObjectListBase::_classesByName;
79    NewObjectListBase::_classesByName = NULL;
80  }
81}
82
83NewObjectListBase::classIDMap* NewObjectListBase::_classesByID = NULL;
84NewObjectListBase::classNameMap* NewObjectListBase::_classesByName = NULL;
85
86/**
87 * @returns the Registered Class Count.
88 */
89unsigned int NewObjectListBase::classCount()
90{
91  assert (NewObjectListBase::_classesByID != NULL);
92  return NewObjectListBase::_classesByID->size();
93};
94
95/**
96 * @brief Checks if a Class with name already exists.
97 * @param name The Name of the Class to check.
98 * @return true if such a class already exists.
99 */
100bool NewObjectListBase::classNameExists(const std::string& name)
101{
102  return (NewObjectListBase::_classesByName->find(name) != NewObjectListBase::_classesByName->end());
103}
104
105/**
106 * @brief searches for a NewClassID in the list of all NewObjectLists, and returns its Identity
107 * @param id: The Id to search for
108 * @returns the ClassID if found and NullClass' identity if not.
109 */
110const NewClassID& NewObejctListBase::retrieveIdentity(int id)
111{
112  const NewObjectListBase* const base = NewObjectListBase::getObjectList(id);
113
114  if (base != NULL)
115    return base->_identity;
116  else
117    return NullClass::classID();
118}
119
120/**
121 * @brief searches for a NewClassID in the list of all NewObjectLists, and returns its Identity
122 * @param name: The Name to search for
123 * @returns the ClassID if found and NullClass' identity if not.
124 */
125const NewClassID& NewObjectListBase::retrieveIdentity(const std::string& name)
126{
127  const NewObjectListBase* const base = NewObjectListBase::getObjectList(name);
128
129  if (base != NULL)
130    return base->_identity;
131  else
132    return NullObject::classID();
133}
134
135
136/**
137 * @brief Checks if a Class with name already exists.
138 * @param name The Name of the Class to check.
139 * @return true if such a class already exists.
140 */
141bool NewObjectListBase::classIDExists(int id)
142{
143  return (NewObjectListBase::_classesByID->find(id) != NewObjectListBase::_classesByID->end());
144}
145
146
147/**
148 * @brief Searches for a ObjectList with the ID classID
149 * @param classID the ID to search for.
150 * @return The NewObjectList if found, NULL otherwise.
151 */
152const NewObjectListBase* const NewObjectListBase::getObjectList(int classID)
153{
154  assert (NewObjectListBase::_classesByID != NULL);
155  NewObjectListBase::classIDMap::iterator it = NewObjectListBase::_classesByID->find(classID);
156  if (it != NewObjectListBase::_classesByID->end())
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.
165 * @return The NewObjectList if found, NULL otherwise.
166 */
167const NewObjectListBase* const NewObjectListBase::getObjectList(const std::string& className)
168{
169  assert (NewObjectListBase::_classesByName != NULL);
170  NewObjectListBase::classNameMap::iterator it = NewObjectListBase::_classesByName->find(className);
171  if (it != NewObjectListBase::_classesByName->end())
172    return (*it).second;
173  else
174    return NULL;
175}
176
177/**
178 * @brief Searches for a ObjectList with the NewClassID classID
179 * @param classID the ID to search for.
180 * @return The NewObjectList if found, NULL otherwise.
181 */
182const NewObjectListBase* const NewObjectListBase::getObjectList(const NewClassID& classID)
183{
184  return NewObjectListBase::getObjectList(classID.id());
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 */
192BaseObject* NewObjectListBase::getBaseObject(int classID, const std::string& objectName)
193{
194  const NewObjectListBase* const base = NewObjectListBase::getObjectList(classID);
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 */
207BaseObject* NewObjectListBase::getBaseObject(const std::string& className, const std::string& objectName)
208{
209  const NewObjectListBase* const base = NewObjectListBase::getObjectList(className);
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.
219 * @param classID The NewClassID of the List.
220 * @param objectName the Name of the Object to search for
221 */
222BaseObject* NewObjectListBase::getBaseObject(const NewClassID& classID, const std::string& objectName)
223{
224  const NewObjectListBase* const base = NewObjectListBase::getObjectList(classID);
225
226  if (base != NULL)
227    return base->getBaseObject(objectName);
228  else
229    return NULL;
230}
231
232
233/**
234 * @brief Converts an ID into a ClassName String.
235 * @param classID The ID to convert.
236 * @return The ClassName or an empty string if the ID was not found.
237 */
238const std::string& NewObjectListBase::IDToString(int classID)
239{
240  const NewObjectListBase* const base = NewObjectListBase::getObjectList(classID);
241
242  if (base != NULL)
243    return base->name();
244  else
245  {
246    static std::string empty;
247    return empty;
248  }
249}
250
251
252/**
253 * @brief Converts a String into an ID
254 * @param className the Name of the Class to search for
255 * @return The Classes ID if found, -1 otherwise.
256 */
257int NewObjectListBase::StringToID(const std::string& className)
258{
259  const NewObjectListBase* const base = NewObjectListBase::getObjectList(className);
260
261  if (base != NULL)
262    return base->id();
263  else
264    return -1;
265}
266
Note: See TracBrowser for help on using the repository browser.