Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/lang/object_list.cc @ 9725

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

orxonox/new_class_id: less debug

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