Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/lang/new_object_list.cc @ 9677

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

orxonox/trunk: much better ID nutrition

File size: 4.4 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
22NewObjectListBase::NewObjectListBase(const std::string& className, int id)
23    : _name(className)
24{
25
26
27  if (NewObjectListBase::_classesByID == NULL)
28  {
29    NewObjectListBase::_classesByID = new classIDMap;
30    assert (NewObjectListBase::_classesByName == NULL);
31    NewObjectListBase::_classesByName = new classNameMap;
32  }
33  assert(!NewObjectListBase::classNameExists(className) && "Classes should only be included once, and no two classes should have the same name (key value)");
34
35  if (id != -1)
36  {
37    this->_id = id;
38  }
39  else
40  {
41    this->_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  /// Some Output, that will fall out later
48  std::cout << "register new ObjectList " << className << " ID: " << this->_id << std::endl;
49
50  (*NewObjectListBase::_classesByID)[this->_id] = this;
51  (*NewObjectListBase::_classesByName)[this->_name] = this;
52}
53
54
55/**
56 * Destructor.
57 *
58 * This destructor deletes the NewObjectList, and cleans up the NewObjectList sorted Maps.
59 */
60NewObjectListBase::~NewObjectListBase()
61{
62  assert (NewObjectListBase::_classesByName != NULL && NewObjectListBase::_classesByID != NULL);
63
64  std::cout << "Erasing: " << this->_name << " "<< this->_id  << std::endl;
65
66  std::cout << "SIZE OF _classByID: " << NewObjectListBase::_classesByID->size() << std::endl;
67  std::cout << "SIZE OF _classByName: " << NewObjectListBase::_classesByName->size() << std::endl;
68
69  NewObjectListBase::_classesByName->erase(this->_name);
70  NewObjectListBase::_classesByID->erase(this->_id);
71
72  if (NewObjectListBase::_classesByID->empty())
73  {
74    delete NewObjectListBase::_classesByID;
75    NewObjectListBase::_classesByID = NULL;
76    assert(NewObjectListBase::_classesByName != NULL);
77    delete NewObjectListBase::_classesByName;
78    NewObjectListBase::_classesByName = NULL;
79  }
80}
81
82NewObjectListBase::classIDMap* NewObjectListBase::_classesByID = NULL;
83NewObjectListBase::classNameMap* NewObjectListBase::_classesByName = NULL;
84
85/**
86 * @returns the Registered Class Count.
87 */
88unsigned int NewObjectListBase::classCount()
89{
90  assert (NewObjectListBase::_classesByID != NULL);
91  return NewObjectListBase::_classesByID->size();
92};
93
94/**
95 * @brief Checks if a Class with name already exists.
96 * @param name The Name of the Class to check.
97 * @return true if such a class already exists.
98 */
99bool NewObjectListBase::classNameExists(const std::string& name)
100{
101  return (NewObjectListBase::_classesByName->find(name) != NewObjectListBase::_classesByName->end());
102}
103
104/**
105 * @brief Checks if a Class with name already exists.
106 * @param name The Name of the Class to check.
107 * @return true if such a class already exists.
108 */
109bool NewObjectListBase::classIDExists(int id)
110{
111  return (NewObjectListBase::_classesByID->find(id) != NewObjectListBase::_classesByID->end());
112}
113
114
115/**
116 * @brief Converts an ID into a ClassName String.
117 * @param classID The ID to convert.
118 * @return The ClassName or an empty string if the ID was not found.
119 */
120const std::string& NewObjectListBase::IDToString(int classID)
121{
122  assert (NewObjectListBase::_classesByID != NULL);
123  NewObjectListBase::classIDMap::iterator it = NewObjectListBase::_classesByID->find(classID);
124  if (it != NewObjectListBase::_classesByID->end())
125    return (*it).second->name();
126  else
127  {
128    static std::string empty;
129    return empty;
130  }
131}
132
133
134/**
135 * @brief Converts a String into an ID
136 * @param className the Name of the Class to search for
137 * @return The Classes ID if found, -1 otherwise.
138 */
139int NewObjectListBase::StringToID(const std::string& className)
140{
141  assert (NewObjectListBase::_classesByName != NULL);
142  NewObjectListBase::classNameMap::iterator it = NewObjectListBase::_classesByName->find(className);
143  if (it != NewObjectListBase::_classesByName->end())
144    return (*it).second->id();
145  else
146    return -1;
147}
148
Note: See TracBrowser for help on using the repository browser.