Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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