Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/lang/object_list.cc @ 10055

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File size: 9.7 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 "object_list.h"
19#include <cassert>
20
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 */
27ObjectListBase::ObjectListBase(const std::string& className, int id)
28    : _name(className)
29{
30  if (ObjectListBase::_classesByID == NULL)
31  {
32    ObjectListBase::_classesByID = new IDMap;
33    assert (ObjectListBase::_classesByName == NULL);
34    ObjectListBase::_classesByName = new NameMap;
35  }
36  assert(!ObjectListBase::classNameExists(className) && "Classes should only be included once, and no two classes should have the same name (key value)");
37
38  if (id == -1)
39  {
40    id = ObjectListBase::_classesByID->size();
41    // searching for a free ID
42    while (ObjectListBase::classIDExists(id)) ++id;
43  }
44  assert(!ObjectListBase::classIDExists(id) && "Classes should only be included once, and no two classes should have the same ID (key value)");
45
46  this->_id = id;
47  /// Some Output, that will fall out later
48  //  std::cout << "register new ObjectList " << className << " ID: " << this->_id << std::endl;
49
50  this->_identity = ClassID(this);
51  (*ObjectListBase::_classesByID)[this->_identity.id()] = this;
52  (*ObjectListBase::_classesByName)[this->_identity.name()] = this;
53}
54
55
56/**
57 * Destructor.
58 *
59 * This destructor deletes the ObjectList, and cleans up the ObjectList sorted Maps.
60 */
61ObjectListBase::~ObjectListBase()
62{
63  assert (ObjectListBase::_classesByName != NULL && ObjectListBase::_classesByID != NULL);
64  /*
65  std::cout << "Erasing: " << this->_name << " "<< this->_id  << std::endl;
66  std::cout << "SIZE OF _classByID: " << ObjectListBase::_classesByID->size() << std::endl;
67  std::cout << "SIZE OF _classByName: " << ObjectListBase::_classesByName->size() << std::endl;
68  */
69  ObjectListBase::_classesByID->erase(this->_identity.id());
70  ObjectListBase::_classesByName->erase(this->_identity.name());
71
72  if (ObjectListBase::_classesByID->empty())
73  {
74    delete ObjectListBase::_classesByID;
75    ObjectListBase::_classesByID = NULL;
76    assert(ObjectListBase::_classesByName != NULL);
77    delete ObjectListBase::_classesByName;
78    ObjectListBase::_classesByName = NULL;
79  }
80}
81
82ObjectListBase::IDMap* ObjectListBase::_classesByID = NULL;
83ObjectListBase::NameMap* ObjectListBase::_classesByName = NULL;
84std::list<std::string> ObjectListBase::_classNames;
85
86/**
87 * @returns the Registered Class Count.
88 */
89unsigned int ObjectListBase::classCount()
90{
91  assert (ObjectListBase::_classesByID != NULL);
92  return ObjectListBase::_classesByID->size();
93};
94
95/**
96 * @brief Checks if a Class with name already exists.
97 * @param id The id of the Class to check.
98 * @return true if such a class already exists.
99 */
100bool ObjectListBase::classIDExists(int id)
101{
102  return (ObjectListBase::_classesByID->find(id) != ObjectListBase::_classesByID->end());
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 */
110bool ObjectListBase::classNameExists(const std::string& name)
111{
112  return (ObjectListBase::_classesByName->find(name) != ObjectListBase::_classesByName->end());
113}
114
115/**
116 * @brief searches for a ClassID in the list of all ObjectLists, and returns its Identity
117 * @param id: The Id to search for
118 * @returns the ClassID if found and NullClass' identity if not.
119 */
120const ClassID& ObjectListBase::retrieveIdentity(int id)
121{
122  const ObjectListBase* const base = ObjectListBase::getObjectList(id);
123
124  if (base != NULL)
125    return base->_identity;
126  else
127    return NullClass::staticClassID();
128}
129
130
131/**
132 * @brief searches for a ClassID in the list of all ObjectLists, and returns its Identity
133 * @param name: The Name to search for
134 * @returns the ClassID if found and NullClass' identity if not.
135 */
136const ClassID& ObjectListBase::retrieveIdentity(const std::string& name)
137{
138  const ObjectListBase* const base = ObjectListBase::getObjectList(name);
139
140  if (base != NULL)
141    return base->_identity;
142  else
143    return NullClass::staticClassID();
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 ObjectList if found, NULL otherwise.
151 */
152const ObjectListBase* const ObjectListBase::getObjectList(int classID)
153{
154  assert (ObjectListBase::_classesByID != NULL);
155  ObjectListBase::IDMap::iterator it = ObjectListBase::_classesByID->find(classID);
156  if (it != ObjectListBase::_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 ObjectList if found, NULL otherwise.
166 */
167const ObjectListBase* const ObjectListBase::getObjectList(const std::string& className)
168{
169  assert (ObjectListBase::_classesByName != NULL);
170  ObjectListBase::NameMap::iterator it = ObjectListBase::_classesByName->find(className);
171  if (it != ObjectListBase::_classesByName->end())
172    return (*it).second;
173  else
174    return NULL;
175}
176
177/**
178 * @brief Searches for a ObjectList with the ClassID classID
179 * @param classID the ID to search for.
180 * @return The ObjectList if found, NULL otherwise.
181 */
182const ObjectListBase* const ObjectListBase::getObjectList(const ClassID& classID)
183{
184  return ObjectListBase::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* ObjectListBase::getBaseObject(int classID, const std::string& objectName)
193{
194  const ObjectListBase* const base = ObjectListBase::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* ObjectListBase::getBaseObject(const std::string& className, const std::string& objectName)
208{
209  const ObjectListBase* const base = ObjectListBase::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 ClassID of the List.
220 * @param objectName the Name of the Object to search for
221 */
222BaseObject* ObjectListBase::getBaseObject(const ClassID& classID, const std::string& objectName)
223{
224  const ObjectListBase* const base = ObjectListBase::getObjectList(classID);
225
226  if (base != NULL)
227    return base->getBaseObject(objectName);
228  else
229    return NULL;
230}
231
232
233/**
234 * @returns An alphabetically sorted List of all stored ClassNames.
235 */
236const std::list<std::string>& ObjectListBase::getClassNames()
237{
238  if (ObjectListBase::classCount() != ObjectListBase::_classNames.size())
239  {
240    ObjectListBase::_classNames.clear();
241
242    for (NameMap::const_iterator it = ObjectListBase::_classesByName->begin();
243         it != ObjectListBase::_classesByName->end();
244         ++it)
245      ObjectListBase::_classNames.push_back((*it).second->name());
246  }
247  return ObjectListBase::_classNames;
248}
249
250
251#include "base_object.h"
252
253/**
254 * @brief Prints out some debugging information about a given List.
255 * @param level:
256 *  1: List ObjectListsand how many object.
257 *  2: 1+List ObjectLists entries, and information about Objects.
258 */
259void ObjectListBase::debug(unsigned int level) const
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 (%p)\n", (*it)->getClassCName(), (*it)->getCName(), (*it));
275    }
276  }
277}
278
279
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 */
287void ObjectListBase::debugAll(unsigned int level)
288{
289  printf("Listing all %d ObjectLists \n", ObjectListBase::_classesByID->size());
290
291  for (NameMap::const_iterator it = ObjectListBase::_classesByName->begin();
292       it != ObjectListBase::_classesByName->end();
293       ++it)
294    (*it).second->debug(level);
295}
296
297
298
299/**
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 */
304const std::string& ObjectListBase::IDToString(int classID)
305{
306  const ObjectListBase* const base = ObjectListBase::getObjectList(classID);
307
308  if (base != NULL)
309    return base->name();
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 */
323int ObjectListBase::StringToID(const std::string& className)
324{
325  const ObjectListBase* const base = ObjectListBase::getObjectList(className);
326
327  if (base != NULL)
328    return base->id();
329  else
330    return -1;
331}
Note: See TracBrowser for help on using the repository browser.