Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/new_class_id: some thoughts on a BaseIterator class, that can travers through ObejectLists without knowing the Polymorph type.
This is all virtual, and since templated virutal functions are not allowed, quite hard to implements…
hpe it will work

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