Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

more renamings

File size: 8.9 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  printf("ObjectList, Registered %s::%d\n", className.c_str(), id);
34  if (ObjectListBase::_classesByID == NULL)
35  {
36    ObjectListBase::_classesByID = new classIDMap;
37    assert (ObjectListBase::_classesByName == NULL);
38    ObjectListBase::_classesByName = new classNameMap;
39  }
40  assert(!ObjectListBase::classNameExists(className) && "Classes should only be included once, and no two classes should have the same name (key value)");
41
42  if (id == -1)
43  {
44    id = ObjectListBase::_classesByID->size();
45    // searching for a free ID
46    while (ObjectListBase::classIDExists(id)) ++id;
47  }
48  assert(!ObjectListBase::classIDExists(id) && "Classes should only be included once, and no two classes should have the same ID (key value)");
49
50  _id = id;
51  /// Some Output, that will fall out later
52  std::cout << "register new ObjectList " << className << " ID: " << this->_id << std::endl;
53
54  this->_identity = ClassID(this);
55  (*ObjectListBase::_classesByID)[this->_identity.id()] = this;
56  (*ObjectListBase::_classesByName)[this->_identity.name()] = this;
57}
58
59
60/**
61 * Destructor.
62 *
63 * This destructor deletes the ObjectList, and cleans up the ObjectList sorted Maps.
64 */
65ObjectListBase::~ObjectListBase()
66{
67  assert (ObjectListBase::_classesByName != NULL && ObjectListBase::_classesByID != NULL);
68  /*
69  std::cout << "Erasing: " << this->_name << " "<< this->_id  << std::endl;
70  std::cout << "SIZE OF _classByID: " << ObjectListBase::_classesByID->size() << std::endl;
71  std::cout << "SIZE OF _classByName: " << ObjectListBase::_classesByName->size() << std::endl;
72  */
73  ObjectListBase::_classesByID->erase(this->_identity.id());
74  ObjectListBase::_classesByName->erase(this->_identity.name());
75
76  if (ObjectListBase::_classesByID->empty())
77  {
78    delete ObjectListBase::_classesByID;
79    ObjectListBase::_classesByID = NULL;
80    assert(ObjectListBase::_classesByName != NULL);
81    delete ObjectListBase::_classesByName;
82    ObjectListBase::_classesByName = NULL;
83  }
84}
85
86ObjectListBase::classIDMap* ObjectListBase::_classesByID = NULL;
87ObjectListBase::classNameMap* ObjectListBase::_classesByName = NULL;
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 name The Name 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#include "base_object.h"
236
237/**
238 * @brief Prints out some debugging information about a given List.
239 */
240void ObjectListBase::debug(unsigned int level) const
241{
242  base_list list;
243  this->getBaseObjectList(&list);
244
245  if (level > 1 || !list.empty())
246    printf(" ObjectList of class %s(id:%d) contains %d objects\n", this->name().c_str(), this->id(), list.size());
247
248  if (level >= 2)
249  {
250    printf("  - listing Instances: \n");
251    for (base_iterator it = list.begin();
252         it != list.end();
253         ++it)
254    {
255      printf("   + %s::%s\n", (*it)->getClassCName(), (*it)->getCName());
256    }
257  }
258}
259
260
261void ObjectListBase::debugAll(unsigned int level)
262{
263  printf("Listing all %d ObjectLists \n", ObjectListBase::_classesByID->size());
264
265  for (classNameMap::const_iterator it = ObjectListBase::_classesByName->begin();
266       it != ObjectListBase::_classesByName->end();
267       ++it)
268  {
269    (*it).second->debug(level);
270
271  }
272
273}
274
275
276
277/**
278 * @brief Converts an ID into a ClassName String.
279 * @param classID The ID to convert.
280 * @return The ClassName or an empty string if the ID was not found.
281 */
282const std::string& ObjectListBase::IDToString(int classID)
283{
284  const ObjectListBase* const base = ObjectListBase::getObjectList(classID);
285
286  if (base != NULL)
287    return base->name();
288  else
289  {
290    static std::string empty;
291    return empty;
292  }
293}
294
295
296/**
297 * @brief Converts a String into an ID
298 * @param className the Name of the Class to search for
299 * @return The Classes ID if found, -1 otherwise.
300 */
301int ObjectListBase::StringToID(const std::string& className)
302{
303  const ObjectListBase* const base = ObjectListBase::getObjectList(className);
304
305  if (base != NULL)
306    return base->id();
307  else
308    return -1;
309}
310
Note: See TracBrowser for help on using the repository browser.