Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/lang/object_list.h @ 9717

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

more renamings

File size: 11.7 KB
Line 
1/*!
2 * @file object_list.h
3 * @brief Definition of a dynamically allocating ClassID
4 *
5 */
6
7#ifndef _OBJECT_LIST_H
8#define _OBJECT_LIST_H
9
10#include "class_id.h"
11#include <cassert>
12#include <map>
13#include <list>
14#include <string>
15#include <iostream>
16
17/**
18 * @brief Use this macro to easily declare a Class to store its own ObjectListDeclaration
19 * @param ClassName the Name of the Class.
20 * @note: Using this inside of a Class means, that you loose the member: _objectList, while defining
21 * two new functions objectList() and classID().
22 */
23#define ObjectListDeclaration(ClassName) \
24  public: \
25   static inline const ObjectList<ClassName>& objectList() { return ClassName::_objectList; }; \
26   static inline const ClassID& classID() { return ClassName::_objectList.identity(); }; \
27  private: \
28   static ObjectList<ClassName> _objectList
29
30#define ObjectListDefinitionID(ClassName, ID) \
31   ObjectList<ClassName> ClassName::_objectList(#ClassName, ID)
32
33
34#define ObjectListDefinition(ClassName) \
35    ObjectListDefinitionID(ClassName, -1)
36
37class BaseObject;
38//! The superclass that all ObjectLists follow.
39/**
40 * @see template<class T> ObjectList<T>
41 */
42class ObjectListBase
43{
44public:
45  //! A fast iterator Base-Class, for iterator-casting and storing.
46  /**
47   * @note This Iterator is explicitely used only for storage purposes in the BaseObject
48   */
49  class IteratorBase { };
50
51  /** @brief A Typedefinition for the Base-List that can be retrieved with getBaseObjectList(base_list*) */
52  typedef std::list<BaseObject*>        base_list;
53  /** @brief An iterator for the base_list */
54  typedef base_list::iterator           base_iterator;
55
56public:
57  /** @returns The Identity of the Class stored within. */
58  inline const ClassID&              identity() const { return _identity; }
59  /** @returns the ID of the Identity of the ObjectList */
60  inline int                            id() const { return _id; };
61  /** @returns The Name of the Class stored in this ObjectList */
62  inline const std::string&             name() const { return _name; };
63  /** @param id The id to compare @returns true on match, false otherwise */
64  inline bool                           operator==(int id) const { return _id == id; };
65  /** @param id The id to compare @returns true on match, false otherwise */
66  inline bool                           operator==(const ClassID& id) const { return id == _id; };
67  /** @param name The name to compare @returns true on match, false otherwise */
68  inline bool                           operator==(const std::string& name) const { return _name == name; };
69  /** @param id The id to check @returns true on match, false otherwise */
70  inline void                           acquireID(const int*& id, const std::string*& name) const { id = &_id; name = &_name; };
71  /** @brief fills a list of Objects into a BaseObject*-List. @param list the list to fill */
72  virtual void                          getBaseObjectList(base_list* list) const = 0;
73
74  static const ClassID&              retrieveIdentity(int id);
75  static const ClassID&              retrieveIdentity(const std::string& name);
76
77  static const ObjectListBase* const getObjectList(int classID);
78  static const ObjectListBase* const getObjectList(const std::string& className);
79  static const ObjectListBase* const getObjectList(const ClassID& classID);
80
81  static BaseObject*                    getBaseObject(int classID, const std::string& objectName);
82  static BaseObject*                    getBaseObject(const std::string& className, const std::string& objectName);
83  static BaseObject*                    getBaseObject(const ClassID& classID, const std::string& objectName);
84
85  /** @returns an Object with Name name out of this List @param name the name of the Object. */
86  virtual BaseObject*                   getBaseObject(const std::string& name) const = 0;
87
88  static const std::list<std::string>&  getClassNames();
89
90
91  static unsigned int                   classCount();
92  void                                  debug(unsigned int level) const;
93  static void                           debugAll(unsigned int level);
94
95  static const std::string&             IDToString(int classID);
96  static int                            StringToID(const std::string& className);
97
98  //! Only uset to unsubscribe a BaseObject.
99  virtual void                          unregisterObject(IteratorBase* _iterators) = 0;
100
101protected:
102  ObjectListBase(const std::string& className, int id = -1);
103  virtual ~ObjectListBase();
104
105private:
106  ObjectListBase(const ObjectListBase&);
107
108  static bool                         classIDExists(int id);
109  static bool                         classNameExists(const std::string& className);
110
111
112protected:
113  typedef std::map<int, ObjectListBase*>         classIDMap;   //!< The Generic Map.
114  typedef std::map<std::string, ObjectListBase*> classNameMap; //!< The Generic Map.
115
116private:
117  int                           _id;
118  const std::string             _name;              //!< The Name of the Class.
119  ClassID                    _identity;          //!< The Identity of the Class. (equal to _id and _name)
120
121private:
122  static classIDMap*            _classesByID;       //!< A Map of all the classes in existance.
123  static classNameMap*          _classesByName;     //!< A Map of all the classes in existance.
124  static std::list<std::string> _classNames;        //!< A list of all the registered ClassNames.
125};
126
127
128/////////////////////////
129//// TEMPLATISATION /////
130/////////////////////////
131//! Defines a ObjectsList handler for objects of type T.
132/**
133 * The ObjectList is a generic way to store every object created from a Class 'T'
134 *  into a List. The list can be retrieved, and used constantly over iterators,
135 *  as with normal std::list.
136 *
137 * Furthermore the linkage over the single Lists is given over the Superclass ObjectListBase.
138 *
139 *
140 *
141 *
142 * To define a Class with a ObjectList, you have to:
143 *  1. Include 'ObjectListDeclaration(T);' in its Declaration (at the beginning)
144 *  2. Include 'ObjectListDefinition(T);' in some Definition file (cc-file)
145 *  3. In the constructor add 'registerObject(this, objectList);'
146 *
147 * @note The Class must define the compare with const std::string& operator for this to work.
148 *
149 *
150 *
151 * Limitations:
152 *  ObjectList cannot be used with other Factory style Classes, if the class is also a BaseObject,
153 *   and not loaded before the ObjectList.
154 *
155 * @example Iterating: Iteration is made easy, and fast as follows:
156 *   for (ObjectList<PlayerStats>::const_iterator it = PlayerStats::objectList().begin();
157 *      it != PlayerStats::objectList().end();
158 *     ++it)
159 *   {
160 *    (*it)->dosomething
161 *   }
162 *
163 * @example Find an Object:
164 * Playable* playable = Playable::objectList("orxonox-super-rocket-fighter"); // searches an Object By its name.
165 *
166 */
167template<class T>
168class ObjectList : public ObjectListBase
169{
170public:
171  typedef std::list<T*>                  list;
172  typedef typename list::iterator        iterator;
173  typedef typename list::const_iterator  const_iterator;
174
175
176class Iterator : public ObjectListBase::IteratorBase
177  {
178  public:
179    Iterator(iterator it) { _it = it; }
180    inline iterator& it() { return _it; }
181    typename ObjectList::iterator _it;
182  };
183
184public:
185  ObjectList(const std::string& name, int id = -1);
186  ~ObjectList();
187
188  virtual BaseObject*                getBaseObject(const std::string& name) const;
189  T*                                 getObject(const std::string& name) const;
190  inline const list&                 objects() const { return _objects; };
191  bool                               exists(const T* const object) const;
192
193  /** @returns an Iterator to the beginning of the List. */
194  inline iterator                    begin() { return _objects.begin(); };
195  /** @returns a constant Iterator to the beginning of the List. */
196  inline const_iterator              begin() const { return _objects.begin(); };
197  /** @returns an Iterator to the end of the List. */
198  inline iterator                    end() { return _objects.end(); };
199  /** @returns a constant Iterator to the beginning of the List. */
200  inline const_iterator              end() const { return _objects.end(); };
201
202  /** @returns true if the List is empty. */
203  inline bool                        empty() const { return _objects.empty(); };
204  /** @returns the size of the List */
205  inline int                         size() const { return _objects.size(); };
206  /** @returns the frontmost Element of the list (normaly the last added Object). */
207  inline T*                          front() const { return _objects.front(); };
208  /** @returns the last added Object */
209  inline T*                          back() const { return _objects.back(); };
210
211
212  ObjectListBase::IteratorBase*   registerObject(T* object);
213  virtual void                       unregisterObject(IteratorBase* iterator);
214
215protected:
216  virtual void                       getBaseObjectList(ObjectListBase::base_list* list) const;
217
218
219private:
220  //! the copy constructor will be hidden.
221  ObjectList(const ObjectList& definer) {};
222
223private:
224  list                _objects;     //!< The List of stored Objects of Type T.
225};
226
227
228
229
230
231/////////////////////////
232//// IMPLEMENTATION /////
233/////////////////////////
234/**
235 * @brief creates a new ObjectList
236 * @param name The name of the Class.
237 * @param id The ID of the class if desired, or -1 if an id should be assigned automatically.
238 */
239template <class T>
240ObjectList<T>::ObjectList(const std::string& name, int id)
241    : ObjectListBase(name, id)
242{}
243
244/**
245 * @brief deletes the ObjectList.
246 */
247template <class T>
248ObjectList<T>::~ObjectList()
249{
250  if (!_objects.empty())
251  {
252    // std::cout << "There are " << this->size() << " objects from class " << this->name() << "(id:" << this->id() << ") in existance\n";
253  }
254}
255
256/**
257 * @brief Retrieves a BaseObject matching the Name name in this List.
258 * @param name the Name of the Object.
259 * @returns a BaseObject pointing to the object if found, NULL otherwise.
260 */
261template <class T>
262BaseObject* ObjectList<T>::getBaseObject(const std::string& name) const
263{
264  return this->getObject(name);
265}
266
267
268
269/**
270 * @brief Retrieves an Object of type T matching the Name name in this List.
271 * @param name the Name of the Object.
272 * @returns an Object of type T pointing to the object if found, NULL otherwise.
273 */
274template <class T>
275T* ObjectList<T>::getObject(const std::string& name) const
276{
277  const_iterator it;
278  for (it = this->_objects.begin(); it != this->_objects.end(); ++it)
279    if ((*it)->getName() == name)
280      return (*it);
281  return NULL;
282}
283
284/**
285 * @brief checks if Object object exists in this ClassList.
286 * @param object the Object to check for
287 * @returns True if the object is found within the List, false otherwise
288 */
289template <class T>
290    bool ObjectList<T>::exists(const T* const object) const
291{
292  return (std::find(_objects.begin(), _objects.end(), object) != _objects.end());
293}
294
295
296/**
297 * @brief retrieves a List of BaseObjects
298 * @param list the list to push the ObjectList into.
299 */
300template <class T>
301void ObjectList<T>::getBaseObjectList(ObjectListBase::base_list* list) const
302{
303  assert (list != NULL);
304  const_iterator it;
305  for (it = this->_objects.begin(); it != this->_objects.end(); ++it)
306    list->push_back(*it);
307}
308
309
310/**
311 * @brief registers an Object to the ObjectList.
312 * @param T object the Object to register.
313 * @returns a pointer to the iterator inside of the list.
314 */
315template <class T>
316ObjectListBase::IteratorBase* ObjectList<T>::registerObject(T* object)
317{
318  this->_objects.push_back(object);
319  return new Iterator(--this->_objects.end());
320}
321
322/**
323 * @brief removes an Object from the ClassList.
324 * @param iterator the Position at which to remove the Object.
325 */
326template <class T>
327void ObjectList<T>::unregisterObject(IteratorBase* iterator)
328{
329  this->_objects.erase(static_cast<Iterator*>(iterator)->it());
330  //_objects.erase(std::find(_objects.begin(), _objects.end(), object));
331}
332
333#endif /* _OBJECT_LIST_H */
Note: See TracBrowser for help on using the repository browser.