Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/lang/new_object_list.h @ 9702

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

new BaseObjectList retrieval function, thus removing the awefully strange and confusing virutal base_iterator

File size: 8.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 "new_class_id.h"
11#include <map>
12#include <list>
13#include <string>
14#include <iostream>
15
16/**
17 * @brief Use this macro to easily declare a Class to store its own ObjectListDeclaration
18 * @param ClassName the Name of the Class.
19 * @note: Using this inside of a Class means, that you loose the member: _objectList, while defining
20 * two new functions objectList() and classID().
21 */
22#define NewObjectListDeclaration(ClassName) \
23  public: \
24   static inline const NewObjectList<ClassName>& objectList() { return ClassName::_objectList; }; \
25   static inline const NewClassID& classID() { return ClassName::_objectList.identity(); }; \
26  private: \
27   static NewObjectList<ClassName> _objectList
28
29#define NewObjectListDefinitionID(ClassName, ID) \
30   NewObjectList<ClassName> ClassName::_objectList(#ClassName, ID)
31
32
33#define NewObjectListDefinition(ClassName) \
34    NewObjectListDefinitionID(ClassName, -1)
35
36class BaseObject;
37//! The superclass that all NewObjectLists follow.
38/**
39 * @see template<class T> NewObjectList<T>
40 */
41class NewObjectListBase
42{
43public:
44  //! A fast iterator Base-Class, for iterator-casting and storing.
45  /**
46   * @note This Iterator is explicitely used only for storage purposes
47   */
48  class IteratorBase { };
49
50public:
51  inline const NewClassID& identity() const { return _identity; }
52  inline int id() const { return _id; };
53  inline const std::string& name() const { return _name; };
54  bool operator==(int id) const { return _id == id; };
55  bool operator==(const NewClassID& id) const { return id == _id; };
56  bool operator==(const std::string& name) const { return _name == name; };
57
58  void acquireID(const int*& id, const std::string*& name) const { id = &_id; name = &_name; };
59
60  virtual void debug() const = 0;
61
62  static unsigned int                   classCount();
63  static const std::string&             IDToString(int classID);
64  static int                            StringToID(const std::string& className);
65
66  static const std::list<std::string>&  getClassNames();
67
68  virtual void unregisterObject(IteratorBase* _iterators) = 0;
69
70public:
71  typedef std::list<BaseObject*>            base_list;
72  typedef std::list<BaseObject*>::iterator  base_iterator;
73
74  virtual void getBaseObjectList(base_list* list) const = 0;
75
76  static const NewObjectListBase* const getObjectList(int classID);
77  static const NewObjectListBase* const getObjectList(const std::string& className);
78  static const NewObjectListBase* const getObjectList(const NewClassID& classID);
79
80  static BaseObject* getBaseObject(int classID, const std::string& objectName);
81  static BaseObject* getBaseObject(const std::string& className, const std::string& objectName);
82  static BaseObject* getBaseObject(const NewClassID& classID, const std::string& objectName);
83
84  virtual BaseObject* getBaseObject(const std::string& name) const = 0;
85
86protected:
87  NewObjectListBase(const std::string& className, int id = -1);
88  virtual ~NewObjectListBase();
89
90private:
91  NewObjectListBase(const NewObjectListBase&);
92
93  static bool classIDExists(int id);
94  static bool classNameExists(const std::string& className);
95
96
97protected:
98  typedef std::map<int, NewObjectListBase*>         classIDMap;   //!< The Generic Map.
99  typedef std::map<std::string, NewObjectListBase*> classNameMap; //!< The Generic Map.
100
101  int                           _id;
102  const std::string             _name;              //!< The Name of the Class.
103  NewClassID                    _identity;          //!< The Identity of the Class. (equal to _id and _name)
104
105private:
106  static classIDMap*            _classesByID;       //!< A Map of all the classes in existance.
107  static classNameMap*          _classesByName;     //!< A Map of all the classes in existance.
108  static std::list<std::string> _classNames;        //!< A list of all the registered ClassNames.
109};
110
111
112/////////////////////////
113//// TEMPLATISATION /////
114/////////////////////////
115//! Defines a ObjectsList handler for objects of type T.
116/**
117 * To define a Class with a ObjectList, you have to:
118 *  1. Include 'NewObjectListDeclaration(T);' in its Declaration (at the beginning)
119 *  2. Include 'NewObjectListDefinition(T);' in some Definition file (cc-file)
120 *  3. In the constructor add 'registerObject(this, objectList);'
121 *
122 * @note The Class must define the compare with const std::string& operator for this to work.
123 */
124template<class T>
125class NewObjectList : public NewObjectListBase
126{
127public:
128  typedef std::list<T*>                  list;
129  typedef typename list::iterator        iterator;
130  typedef typename list::const_iterator  const_iterator;
131
132
133class Iterator : public NewObjectListBase::IteratorBase
134  {
135  public:
136    Iterator(iterator it) { _it = it; }
137    inline iterator& it() { return _it; }
138    typename NewObjectList::iterator _it;
139  };
140
141public:
142  NewObjectList(const std::string& name, int id = -1);
143  ~NewObjectList();
144
145  virtual BaseObject*     getBaseObject(const std::string& name) const;
146  T*                      getObject(const std::string& name) const;
147  inline const list&      objects() const { return _objects; };
148
149  inline iterator begin() { return _objects.begin(); };
150  inline const_iterator begin() const { return _objects.begin(); };
151  inline iterator  end() { return _objects.end(); };
152  inline const_iterator  end() const { return _objects.end(); };
153
154  inline bool empty() const { return _objects.empty(); };
155  inline int size() const { return _objects.size(); };
156  inline T* front() const { return _objects.front(); };
157  inline T* back() const { return _objects.back(); };
158
159  NewObjectListBase::IteratorBase* registerObject(T* object);
160  void unregisterObject(IteratorBase* iterator);
161
162  virtual void debug() const;
163
164protected:
165  virtual void getBaseObjectList(NewObjectListBase::base_list* list) const;
166
167
168private:
169  //! the copy constructor will be hidden.
170  NewObjectList(const NewObjectList& definer) {};
171
172private:
173  list                _objects;
174};
175
176
177
178
179
180/////////////////////////
181//// IMPLEMENTATION /////
182/////////////////////////
183/**
184 * @brief creates a new NewObjectList
185 * @param name The name of the Class.
186 * @param id The ID of the class if desired, or -1 if an id should be assigned automatically.
187 */
188template <class T>
189NewObjectList<T>::NewObjectList(const std::string& name, int id)
190    : NewObjectListBase(name, id)
191{}
192
193/**
194 * @brief deletes the NewObjectList.
195 */
196template <class T>
197NewObjectList<T>::~NewObjectList()
198{
199  if (!_objects.empty())
200  {
201    std::cout << "There are still Object in the ObjectList of " << this->name() << "(id:" << this->id() << ")\n";
202    this->debug();
203  }
204}
205
206/**
207 * @brief Retrieves a BaseObject matching the Name name in this List.
208 * @param name the Name of the Object.
209 * @returns a BaseObject pointing to the object if found, NULL otherwise.
210 */
211template <class T>
212BaseObject* NewObjectList<T>::getBaseObject(const std::string& name) const
213{
214  return this->getObject(name);
215}
216
217
218
219/**
220 * @brief Retrieves an Object of type T matching the Name name in this List.
221 * @param name the Name of the Object.
222 * @returns an Object of type T pointing to the object if found, NULL otherwise.
223 */
224template <class T>
225T* NewObjectList<T>::getObject(const std::string& name) const
226{
227  const_iterator it;
228  for (it = this->_objects.begin(); it != this->_objects.end(); ++it)
229    if ((*it)->getName() == name)
230      return (*it);
231  return NULL;
232}
233
234/**
235 * @brief retrieves a List of BaseObjects
236 * @param list the list to push the ObjectList into.
237 */
238template <class T>
239void NewObjectList<T>::getBaseObjectList(NewObjectListBase::base_list* list) const
240{
241  assert (list != NULL);
242  const_iterator it;
243  for (it = this->_objects.begin(); it != this->_objects.end(); ++it)
244    list->push_back(*it);
245}
246
247
248/**
249 * @brief registers an Object to the NewObjectList.
250 * @param T object the Object to register.
251 * @returns a pointer to the iterator inside of the list.
252 */
253template <class T>
254NewObjectListBase::IteratorBase* NewObjectList<T>::registerObject(T* object)
255{
256  this->_objects.push_front(object);
257  return new Iterator(this->_objects.begin());
258}
259
260/**
261 * @brief removes an Object from the ClassList.
262 * @param iterator the Position at which to remove the Object.
263 */
264template <class T>
265void NewObjectList<T>::unregisterObject(IteratorBase* iterator)
266{
267  this->_objects.erase(static_cast<Iterator*>(iterator)->it());
268  //_objects.erase(std::find(_objects.begin(), _objects.end(), object));
269}
270
271/**
272 * @brief print out some debug information
273 * @note this function will most probably vanish from here and be completely moved to the base class.
274 */
275template <class T>
276void NewObjectList<T>::debug() const
277{
278  const_iterator it;
279  for (it = this->_objects.begin(); it != this->_objects.end(); ++it)
280  {
281    std::cout << (*it)->getName() << std::endl;
282  }
283}
284
285#endif /* _NEW_OBJECT_LIST_H */
Note: See TracBrowser for help on using the repository browser.