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
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
[9686]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 */
[9663]22#define NewObjectListDeclaration(ClassName) \
[9684]23  public: \
[9686]24   static inline const NewObjectList<ClassName>& objectList() { return ClassName::_objectList; }; \
[9691]25   static inline const NewClassID& classID() { return ClassName::_objectList.identity(); }; \
[9684]26  private: \
27   static NewObjectList<ClassName> _objectList
[9660]28
[9677]29#define NewObjectListDefinitionID(ClassName, ID) \
[9684]30   NewObjectList<ClassName> ClassName::_objectList(#ClassName, ID)
[9673]31
[9677]32
[9682]33#define NewObjectListDefinition(ClassName) \
34    NewObjectListDefinitionID(ClassName, -1)
35
[9692]36class BaseObject;
[9673]37//! The superclass that all NewObjectLists follow.
38/**
39 * @see template<class T> NewObjectList<T>
40 */
[9661]41class NewObjectListBase
[9659]42{
43public:
[9692]44  //! A fast iterator Base-Class, for iterator-casting and storing.
45  /**
46   * @note This Iterator is explicitely used only for storage purposes
47   */
[9666]48  class IteratorBase { };
49
50public:
[9701]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; };
[2036]57
[9701]58  void acquireID(const int*& id, const std::string*& name) const { id = &_id; name = &_name; };
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
[9693]70public:
[9702]71  typedef std::list<BaseObject*>            base_list;
72  typedef std::list<BaseObject*>::iterator  base_iterator;
[9693]73
[9702]74  virtual void getBaseObjectList(base_list* list) const = 0;
[9693]75
[9696]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);
[9693]79
[9698]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);
[9696]83
84  virtual BaseObject* getBaseObject(const std::string& name) const = 0;
85
[9671]86protected:
[9702]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:
[9692]98  typedef std::map<int, NewObjectListBase*>         classIDMap;   //!< The Generic Map.
99  typedef std::map<std::string, NewObjectListBase*> classNameMap; //!< The Generic Map.
[9660]100
[9701]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)
[9671]104
[9660]105private:
[9675]106  static classIDMap*            _classesByID;       //!< A Map of all the classes in existance.
107  static classNameMap*          _classesByName;     //!< A Map of all the classes in existance.
[9671]108  static std::list<std::string> _classNames;        //!< A list of all the registered ClassNames.
[9659]109};
[1853]110
[9661]111
112/////////////////////////
113//// TEMPLATISATION /////
114/////////////////////////
[9663]115//! Defines a ObjectsList handler for objects of type T.
116/**
[9673]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 *
[9663]122 * @note The Class must define the compare with const std::string& operator for this to work.
123 */
[9665]124template<class T>
125class NewObjectList : public NewObjectListBase
[9659]126{
127public:
[9671]128  typedef std::list<T*>                  list;
129  typedef typename list::iterator        iterator;
130  typedef typename list::const_iterator  const_iterator;
[9663]131
[9692]132
[9666]133class Iterator : public NewObjectListBase::IteratorBase
[9665]134  {
135  public:
[9672]136    Iterator(iterator it) { _it = it; }
137    inline iterator& it() { return _it; }
138    typename NewObjectList::iterator _it;
[9665]139  };
140
[9663]141public:
[9676]142  NewObjectList(const std::string& name, int id = -1);
[9661]143  ~NewObjectList();
[3245]144
[9696]145  virtual BaseObject*     getBaseObject(const std::string& name) const;
[9663]146  T*                      getObject(const std::string& name) const;
147  inline const list&      objects() const { return _objects; };
148
[9685]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
[9672]159  NewObjectListBase::IteratorBase* registerObject(T* object);
160  void unregisterObject(IteratorBase* iterator);
[9663]161
[9671]162  virtual void debug() const;
163
[9693]164protected:
[9702]165  virtual void getBaseObjectList(NewObjectListBase::base_list* list) const;
[9693]166
[9702]167
[9660]168private:
169  //! the copy constructor will be hidden.
[9661]170  NewObjectList(const NewObjectList& definer) {};
[9663]171
[9665]172private:
[9663]173  list                _objects;
[9660]174};
[9659]175
[9665]176
177
178
179
180/////////////////////////
181//// IMPLEMENTATION /////
182/////////////////////////
[9681]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 */
[9660]188template <class T>
[9676]189NewObjectList<T>::NewObjectList(const std::string& name, int id)
190    : NewObjectListBase(name, id)
[9660]191{}
[9659]192
[9681]193/**
194 * @brief deletes the NewObjectList.
195 */
[9660]196template <class T>
[9661]197NewObjectList<T>::~NewObjectList()
[9663]198{
[9681]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  }
[9663]204}
[9659]205
[9696]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 */
[9663]211template <class T>
[9696]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>
[9663]225T* NewObjectList<T>::getObject(const std::string& name) const
226{
[9684]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;
[9663]232}
[9659]233
[9696]234/**
[9702]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/**
[9696]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 */
[9664]253template <class T>
[9681]254NewObjectListBase::IteratorBase* NewObjectList<T>::registerObject(T* object)
[9664]255{
[9667]256  this->_objects.push_front(object);
257  return new Iterator(this->_objects.begin());
[9664]258}
259
[9696]260/**
261 * @brief removes an Object from the ClassList.
262 * @param iterator the Position at which to remove the Object.
263 */
[9664]264template <class T>
[9672]265void NewObjectList<T>::unregisterObject(IteratorBase* iterator)
[9664]266{
[9672]267  this->_objects.erase(static_cast<Iterator*>(iterator)->it());
[9665]268  //_objects.erase(std::find(_objects.begin(), _objects.end(), object));
[9664]269}
270
[9696]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 */
[9671]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  {
[9674]281    std::cout << (*it)->getName() << std::endl;
[9671]282  }
283}
284
[9662]285#endif /* _NEW_OBJECT_LIST_H */
Note: See TracBrowser for help on using the repository browser.