Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/lang/new_object_list.cc @ 9692

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

adapted many classes to the new ClassID System, now comes the hard part… Scripting… then Network… wow this will be so bad :/

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{
30  if (NewObjectListBase::_classesByID == NULL)
31  {
32    NewObjectListBase::_classesByID = new classIDMap;
33    assert (NewObjectListBase::_classesByName == NULL);
34    NewObjectListBase::_classesByName = new classNameMap;
35  }
36  assert(!NewObjectListBase::classNameExists(className) && "Classes should only be included once, and no two classes should have the same name (key value)");
37
38  if (id == -1)
39  {
40    id = NewObjectListBase::_classesByID->size();
41    // searching for a free ID
42    while (NewObjectListBase::classIDExists(id)) ++id;
43  }
44  assert(!NewObjectListBase::classIDExists(id) && "Classes should only be included once, and no two classes should have the same ID (key value)");
45
46  /// Some Output, that will fall out later
47  //std::cout << "register new ObjectList " << className << " ID: " << this->_id << std::endl;
48
49  this->_identity = NewClassID(id, className);
50  (*NewObjectListBase::_classesByID)[this->_identity.id()] = this;
51  (*NewObjectListBase::_classesByName)[this->_identity.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  std::cout << "SIZE OF _classByID: " << NewObjectListBase::_classesByID->size() << std::endl;
66  std::cout << "SIZE OF _classByName: " << NewObjectListBase::_classesByName->size() << std::endl;
67  */
68  NewObjectListBase::_classesByID->erase(this->_identity.id());
69  NewObjectListBase::_classesByName->erase(this->_identity.name());
70
71  if (NewObjectListBase::_classesByID->empty())
72  {
73    delete NewObjectListBase::_classesByID;
74    NewObjectListBase::_classesByID = NULL;
75    assert(NewObjectListBase::_classesByName != NULL);
76    delete NewObjectListBase::_classesByName;
77    NewObjectListBase::_classesByName = NULL;
78  }
79}
80
81NewObjectListBase::classIDMap* NewObjectListBase::_classesByID = NULL;
82NewObjectListBase::classNameMap* NewObjectListBase::_classesByName = NULL;
83
84/**
85 * @returns the Registered Class Count.
86 */
87unsigned int NewObjectListBase::classCount()
88{
89  assert (NewObjectListBase::_classesByID != NULL);
90  return NewObjectListBase::_classesByID->size();
91};
92
93/**
94 * @brief Checks if a Class with name already exists.
95 * @param name The Name of the Class to check.
96 * @return true if such a class already exists.
97 */
98bool NewObjectListBase::classNameExists(const std::string& name)
99{
100  return (NewObjectListBase::_classesByName->find(name) != NewObjectListBase::_classesByName->end());
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 */
108bool NewObjectListBase::classIDExists(int id)
109{
110  return (NewObjectListBase::_classesByID->find(id) != NewObjectListBase::_classesByID->end());
111}
112
113
114/**
115 * @brief Converts an ID into a ClassName String.
116 * @param classID The ID to convert.
117 * @return The ClassName or an empty string if the ID was not found.
118 */
119const std::string& NewObjectListBase::IDToString(int classID)
120{
121  assert (NewObjectListBase::_classesByID != NULL);
122  NewObjectListBase::classIDMap::iterator it = NewObjectListBase::_classesByID->find(classID);
123  if (it != NewObjectListBase::_classesByID->end())
124    return (*it).second->name();
125  else
126  {
127    static std::string empty;
128    return empty;
129  }
130}
131
132
133/**
134 * @brief Converts a String into an ID
135 * @param className the Name of the Class to search for
136 * @return The Classes ID if found, -1 otherwise.
137 */
138int NewObjectListBase::StringToID(const std::string& className)
139{
140  assert (NewObjectListBase::_classesByName != NULL);
141  NewObjectListBase::classNameMap::iterator it = NewObjectListBase::_classesByName->find(className);
142  if (it != NewObjectListBase::_classesByName->end())
143    return (*it).second->id();
144  else
145    return -1;
146}
147
Note: See TracBrowser for help on using the repository browser.