Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

new_class_id: syncing the IteratorClass

File size: 8.6 KB
RevLine 
[4838]1/*!
[9662]2 * @file new_object_list.h
[9659]3 * @brief Definition of a dynamically allocating ClassID
4 *
5 */
[1853]6
[9662]7#ifndef _NEW_OBJECT_LIST_H
8#define _NEW_OBJECT_LIST_H
[1853]9
[9685]10#include "new_class_id.h"
[9675]11#include <map>
[9663]12#include <list>
[9659]13#include <string>
[9681]14#include <iostream>
[1853]15
[3543]16
[9686]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 */
[9663]24#define NewObjectListDeclaration(ClassName) \
[9684]25  public: \
[9686]26   static inline const NewObjectList<ClassName>& objectList() { return ClassName::_objectList; }; \
[9691]27   static inline const NewClassID& classID() { return ClassName::_objectList.identity(); }; \
[9684]28  private: \
29   static NewObjectList<ClassName> _objectList
[9660]30
[9677]31#define NewObjectListDefinitionID(ClassName, ID) \
[9684]32   NewObjectList<ClassName> ClassName::_objectList(#ClassName, ID)
[9673]33
[9677]34
[9682]35#define NewObjectListDefinition(ClassName) \
36    NewObjectListDefinitionID(ClassName, -1)
37
[9692]38class BaseObject;
[9673]39//! The superclass that all NewObjectLists follow.
40/**
41 * @see template<class T> NewObjectList<T>
42 */
[9661]43class NewObjectListBase
[9659]44{
45public:
[9692]46  //! A fast iterator Base-Class, for iterator-casting and storing.
47  /**
48   * @note This Iterator is explicitely used only for storage purposes
49   */
[9666]50  class IteratorBase { };
51
52public:
[9686]53  inline const NewClassID& identity() const { return _identity; };
[9685]54  inline int id() const { return _identity.id(); };
55  inline const std::string& name() const { return _identity.name(); };
56  bool operator==(int id) const { return _identity == id; };
[9686]57  bool operator==(const NewClassID& id) const { return _identity == id; };
[9685]58  bool operator==(const std::string& name) const { return _identity == name; };
[2036]59
[9671]60  virtual void debug() const = 0;
61
[9676]62  static unsigned int                   classCount();
[9663]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
[9672]68  virtual void unregisterObject(IteratorBase* _iterators) = 0;
[9671]69
[9660]70protected:
[9676]71  NewObjectListBase(const std::string& className, int id = -1);
[9671]72  virtual ~NewObjectListBase();
[9663]73
[9659]74private:
[9661]75  NewObjectListBase(const NewObjectListBase&);
[1853]76
[9676]77  static bool classIDExists(int id);
[9663]78  static bool classNameExists(const std::string& className);
[9660]79
[9693]80public:
81  class base_iterator
82  {
83  public:
84    /// @NOTE Do not use this class. For internal handling.
85    class __base_iterator_base
86    {
87    public:
88      virtual void operator++(int) = 0;
89      virtual void operator++() = 0;
90      virtual void operator--(int) = 0;
91      virtual void operator--() = 0;
92      virtual BaseObject* operator*() = 0;
93      virtual __base_iterator_base& operator=(const base_iterator& iterator) = 0;
94      virtual bool operator==(const __base_iterator_base& iterator) const = 0;
95      virtual bool operator!=(const __base_iterator_base& iterator) const = 0;
96      virtual __base_iterator_base* clone() const;
97    };
98
99  public:
100    base_iterator(__base_iterator_base& it) { _it = it.clone(); };
101    ~base_iterator() { delete _it; }
102    void operator++() { (*_it)++; };
103    void operator++(int) { ++(*_it); };
104    void operator--() { (*_it)--; };
105    void operator--(int) { --(*_it); };
106    BaseObject* operator*() { return *(*_it); };
107    base_iterator& operator=(const base_iterator& iterator);
108    bool operator==(const base_iterator& iter) const { return *this->_it == *iter._it; };
109    bool operator!=(const base_iterator& iter) const { return *this->_it != *iter._it; };
110
111  private:
112    __base_iterator_base*    _it;
113  };
114
115  virtual base_iterator base_begin() const = 0;
116  virtual base_iterator base_end() const = 0;
117
118
[9671]119protected:
[9692]120  typedef std::map<int, NewObjectListBase*>         classIDMap;   //!< The Generic Map.
121  typedef std::map<std::string, NewObjectListBase*> classNameMap; //!< The Generic Map.
[9660]122
[9685]123  NewClassID                    _identity;          //!< The Identity of the Class (ID and Name).
[9671]124
[9660]125private:
[9675]126  static classIDMap*            _classesByID;       //!< A Map of all the classes in existance.
127  static classNameMap*          _classesByName;     //!< A Map of all the classes in existance.
[9671]128  static std::list<std::string> _classNames;        //!< A list of all the registered ClassNames.
[9659]129};
[1853]130
[9661]131
132/////////////////////////
133//// TEMPLATISATION /////
134/////////////////////////
[9663]135//! Defines a ObjectsList handler for objects of type T.
136/**
[9673]137 * To define a Class with a ObjectList, you have to:
138 *  1. Include 'NewObjectListDeclaration(T);' in its Declaration (at the beginning)
139 *  2. Include 'NewObjectListDefinition(T);' in some Definition file (cc-file)
140 *  3. In the constructor add 'registerObject(this, objectList);'
141 *
[9663]142 * @note The Class must define the compare with const std::string& operator for this to work.
143 */
[9665]144template<class T>
145class NewObjectList : public NewObjectListBase
[9659]146{
147public:
[9671]148  typedef std::list<T*>                  list;
149  typedef typename list::iterator        iterator;
150  typedef typename list::const_iterator  const_iterator;
[9663]151
[9693]152  virtual base_iterator base_begin() const { return base_iterator(__base_iterator(this->_objects.begin())); };
153  virtual base_iterator base_end() const { return base_iterator(__base_iterator(this->_objects.end())); };
[9692]154
155
[9666]156class Iterator : public NewObjectListBase::IteratorBase
[9665]157  {
158  public:
[9672]159    Iterator(iterator it) { _it = it; }
160    inline iterator& it() { return _it; }
161    typename NewObjectList::iterator _it;
[9665]162  };
163
[9663]164public:
[9676]165  NewObjectList(const std::string& name, int id = -1);
[9661]166  ~NewObjectList();
[3245]167
[9663]168  T*                      getObject(const std::string& name) const;
169  inline const list&      objects() const { return _objects; };
170
[9685]171  inline iterator begin() { return _objects.begin(); };
172  inline const_iterator begin() const { return _objects.begin(); };
173  inline iterator  end() { return _objects.end(); };
174  inline const_iterator  end() const { return _objects.end(); };
175
176  inline bool empty() const { return _objects.empty(); };
177  inline int size() const { return _objects.size(); };
178  inline T* front() const { return _objects.front(); };
179  inline T* back() const { return _objects.back(); };
180
[9672]181  NewObjectListBase::IteratorBase* registerObject(T* object);
182  void unregisterObject(IteratorBase* iterator);
[9663]183
[9671]184  virtual void debug() const;
185
[9693]186protected:
187class __base_iterator : public base_iterator::__base_iterator_base
188  {
189  public:
190    __base_iterator(const const_iterator& iter);
191    void operator++(int) { ++_iter; };
192    void operator++()  { _iter++; };
193    void operator--(int) { --_iter; };
194    void operator--()  { _iter--; };
195    BaseObject* operator*() { return *iter; };
196    base_iterator::__base_iterator_base& operator=(const base_iterator& iterator) { _iter = iterator._iter; return *this; };
197    bool operator==(const __base_iterator_base& iterator) const { return _iter == iterator._iter; };
198    bool operator!=(const __base_iterator_base& iterator) const { return _iter != iterator._iter; };
199  private:
200    iterator _iter;
201  };
202
[9660]203private:
204  //! the copy constructor will be hidden.
[9661]205  NewObjectList(const NewObjectList& definer) {};
[9663]206
[9665]207private:
[9663]208  list                _objects;
[9660]209};
[9659]210
[9665]211
212
213
214
215/////////////////////////
216//// IMPLEMENTATION /////
217/////////////////////////
[9681]218/**
219 * @brief creates a new NewObjectList
220 * @param name The name of the Class.
221 * @param id The ID of the class if desired, or -1 if an id should be assigned automatically.
222 */
[9660]223template <class T>
[9676]224NewObjectList<T>::NewObjectList(const std::string& name, int id)
225    : NewObjectListBase(name, id)
[9660]226{}
[9659]227
[9681]228/**
229 * @brief deletes the NewObjectList.
230 */
[9660]231template <class T>
[9661]232NewObjectList<T>::~NewObjectList()
[9663]233{
[9681]234  if (!_objects.empty())
235  {
236    std::cout << "There are still Object in the ObjectList of " << this->name() << "(id:" << this->id() << ")\n";
237    this->debug();
238  }
[9663]239}
[9659]240
[9663]241template <class T>
242T* NewObjectList<T>::getObject(const std::string& name) const
243{
[9684]244  const_iterator it;
245  for (it = this->_objects.begin(); it != this->_objects.end(); ++it)
246    if ((*it)->getName() == name)
247      return (*it);
248  return NULL;
[9663]249}
[9659]250
[9664]251template <class T>
[9681]252NewObjectListBase::IteratorBase* NewObjectList<T>::registerObject(T* object)
[9664]253{
[9667]254  this->_objects.push_front(object);
255  return new Iterator(this->_objects.begin());
[9664]256}
257
258template <class T>
[9672]259void NewObjectList<T>::unregisterObject(IteratorBase* iterator)
[9664]260{
[9672]261  this->_objects.erase(static_cast<Iterator*>(iterator)->it());
[9665]262  //_objects.erase(std::find(_objects.begin(), _objects.end(), object));
[9664]263}
264
265
[9671]266template <class T>
267void NewObjectList<T>::debug() const
268{
269  const_iterator it;
270  for (it = this->_objects.begin(); it != this->_objects.end(); ++it)
271  {
[9674]272    std::cout << (*it)->getName() << std::endl;
[9671]273  }
274}
275
[9662]276#endif /* _NEW_OBJECT_LIST_H */
Note: See TracBrowser for help on using the repository browser.