Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

cleanup

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