Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10114 was 10114, checked in by patrick, 17 years ago

merged network back to trunk

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