Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

new_class_id: adapted Network

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