Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: Better ID handling, now it is possible, to use pre-defined ID's, as it was with the old appreach

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