Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/lang/new_object_list.h @ 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.8 KB
Line 
1/*!
2 * @file new_object_list.h
3 * @brief Definition of a dynamically allocating ClassID
4 *
5 */
6
7#ifndef _NEW_OBJECT_LIST_H
8#define _NEW_OBJECT_LIST_H
9
10#include "type_info.h"
11#include <map>
12#include <list>
13#include <string>
14
15
16#define NewObjectListDeclaration(ClassName) \
17   static NewObjectList<ClassName> objectList
18
19#define NewObjectListDefinition(ClassName) \
20   NewObjectList<ClassName> ClassName::objectList(#ClassName)
21
22#define NewObjectListDefinitionID(ClassName, ID) \
23   NewObjectList<ClassName> ClassName::objectList(#ClassName, ID)
24
25
26//! The superclass that all NewObjectLists follow.
27/**
28 * @see template<class T> NewObjectList<T>
29 */
30class NewObjectListBase
31{
32public:
33  //! An iterator Base-Class, for iterator-casting and storing.
34  class IteratorBase { };
35
36public:
37  inline int id() const { return _id; };
38  inline const std::string& name() const { return _name; };
39  bool operator==(int id) const { return _id == id; };
40  bool operator==(const std::string& name) const { return _name == name; };
41
42  virtual void debug() const = 0;
43
44  static unsigned int                   classCount();
45  static const std::string&             IDToString(int classID);
46  static int                            StringToID(const std::string& className);
47
48  static const std::list<std::string>&  getClassNames();
49
50  virtual void unregisterObject(IteratorBase* _iterators) = 0;
51
52protected:
53  NewObjectListBase(const std::string& className, int id = -1);
54  virtual ~NewObjectListBase();
55
56private:
57  NewObjectListBase(const NewObjectListBase&);
58
59  static bool classIDExists(int id);
60  static bool classNameExists(const std::string& className);
61
62protected:
63  typedef std::map<int, NewObjectListBase*> classIDMap;    //!< The Generic Map.
64  typedef std::map<std::string, NewObjectListBase*> classNameMap;//!< The Generic Map.
65
66  int                           _id;                //!< The ID of the class.
67  std::string                   _name;              //!< The Name of the Class.
68
69private:
70  static classIDMap*            _classesByID;       //!< A Map of all the classes in existance.
71  static classNameMap*          _classesByName;     //!< A Map of all the classes in existance.
72  static std::list<std::string> _classNames;        //!< A list of all the registered ClassNames.
73};
74
75
76/////////////////////////
77//// TEMPLATISATION /////
78/////////////////////////
79//! Defines a ObjectsList handler for objects of type T.
80/**
81 * To define a Class with a ObjectList, you have to:
82 *  1. Include 'NewObjectListDeclaration(T);' in its Declaration (at the beginning)
83 *  2. Include 'NewObjectListDefinition(T);' in some Definition file (cc-file)
84 *  3. In the constructor add 'registerObject(this, objectList);'
85 *
86 * @note The Class must define the compare with const std::string& operator for this to work.
87 */
88template<class T>
89class NewObjectList : public NewObjectListBase
90{
91public:
92  typedef std::list<T*>                  list;
93  typedef typename list::iterator        iterator;
94  typedef typename list::const_iterator  const_iterator;
95
96class Iterator : public NewObjectListBase::IteratorBase
97  {
98  public:
99    Iterator(iterator it) { _it = it; }
100    inline iterator& it() { return _it; }
101    typename NewObjectList::iterator _it;
102  };
103
104public:
105  NewObjectList(const std::string& name, int id = -1);
106  ~NewObjectList();
107
108  T*                      getObject(const std::string& name) const;
109  inline const list&      objects() const { return _objects; };
110
111  NewObjectListBase::IteratorBase* registerObject(T* object);
112  void unregisterObject(IteratorBase* iterator);
113
114  virtual void debug() const;
115
116private:
117  //! the copy constructor will be hidden.
118  NewObjectList(const NewObjectList& definer) {};
119
120private:
121  list                _objects;
122};
123
124
125
126
127
128/////////////////////////
129//// IMPLEMENTATION /////
130/////////////////////////
131template <class T>
132NewObjectList<T>::NewObjectList(const std::string& name, int id)
133    : NewObjectListBase(name, id)
134{}
135
136template <class T>
137NewObjectList<T>::~NewObjectList()
138{
139  // assert(_objects.empty());
140}
141
142template <class T>
143T* NewObjectList<T>::getObject(const std::string& name) const
144{
145  iterator it = std::find(this->_objects.begin(), this->_objects.end(), name);
146  if (it != this->_objects.end())
147    return *it;
148  else
149    return NULL;
150}
151
152template <class T>
153    NewObjectListBase::IteratorBase* NewObjectList<T>::registerObject(T* object)
154{
155  this->_objects.push_front(object);
156  return new Iterator(this->_objects.begin());
157}
158
159template <class T>
160void NewObjectList<T>::unregisterObject(IteratorBase* iterator)
161{
162  this->_objects.erase(static_cast<Iterator*>(iterator)->it());
163  //_objects.erase(std::find(_objects.begin(), _objects.end(), object));
164}
165
166#include <iostream>
167
168template <class T>
169void NewObjectList<T>::debug() const
170{
171  const_iterator it;
172  for (it = this->_objects.begin(); it != this->_objects.end(); ++it)
173  {
174    std::cout << (*it)->getName() << std::endl;
175  }
176}
177
178#endif /* _NEW_OBJECT_LIST_H */
Note: See TracBrowser for help on using the repository browser.