Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

more thoughts on Resources, and soon going to adapt… i think i've got a clue :)

File size: 12.2 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;                //!< The Unique ID of the Class.
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;             //!< The list of Type T* (used for Objects in this ObjectList)
172  typedef typename list::iterator        iterator;         //!< The iterator for the List of type T (use with ObjectList<Type>::iterator)
173  typedef typename list::const_iterator  const_iterator;   //!< A constant iterator for the List of type T (use with ObjectList<Type>::const_iterator)
174
175
176  //! An iterator to store Objects in the BaseObject, and remove Objects fast.
177class Iterator : public ObjectListBase::IteratorBase
178  {
179  public:
180    /** @brief creates an Iterator fast. @param it the Iterator. */
181    inline Iterator(iterator it) { _it = it; }
182    /** @returns the Iterator */
183    inline iterator& it() { return _it; }
184  private:
185    typename ObjectList::iterator _it;  //!< Stored Iterator
186  };
187
188public:
189  ObjectList(const std::string& name, int id = -1);
190  ~ObjectList();
191
192  virtual BaseObject*                getBaseObject(const std::string& name) const;
193  T*                                 getObject(const std::string& name) const;
194  inline const list&                 objects() const { return _objects; };
195  bool                               exists(const T* const object) const;
196
197  /** @returns an Iterator to the beginning of the List. */
198  inline iterator                    begin() { return _objects.begin(); };
199  /** @returns a constant Iterator to the beginning of the List. */
200  inline const_iterator              begin() const { return _objects.begin(); };
201  /** @returns an Iterator to the end of the List. */
202  inline iterator                    end() { return _objects.end(); };
203  /** @returns a constant Iterator to the beginning of the List. */
204  inline const_iterator              end() const { return _objects.end(); };
205
206  /** @returns true if the List is empty. */
207  inline bool                        empty() const { return _objects.empty(); };
208  /** @returns the size of the List */
209  inline int                         size() const { return _objects.size(); };
210  /** @returns the frontmost Element of the list (normaly the last added Object). */
211  inline T*                          front() const { return _objects.front(); };
212  /** @returns the last added Object */
213  inline T*                          back() const { return _objects.back(); };
214
215
216  ObjectListBase::IteratorBase*      registerObject(T* object);
217  virtual void                       unregisterObject(IteratorBase* iterator);
218
219protected:
220  virtual void                       getBaseObjectList(ObjectListBase::base_list* list) const;
221
222
223private:
224  //! the copy constructor will be hidden.
225  ObjectList(const ObjectList& definer) {};
226
227private:
228  list                _objects;     //!< The List of stored Objects of Type T.
229};
230
231
232
233
234
235/////////////////////////
236//// IMPLEMENTATION /////
237/////////////////////////
238/**
239 * @brief creates a new ObjectList
240 * @param name The name of the Class.
241 * @param id The ID of the class if desired, or -1 if an id should be assigned automatically.
242 */
243template <class T>
244ObjectList<T>::ObjectList(const std::string& name, int id)
245    : ObjectListBase(name, id)
246{}
247
248/**
249 * @brief deletes the ObjectList.
250 */
251template <class T>
252ObjectList<T>::~ObjectList()
253{
254  if (!_objects.empty())
255  {
256    // std::cout << "There are " << this->size() << " objects from class " << this->name() << "(id:" << this->id() << ") in existance\n";
257  }
258}
259
260/**
261 * @brief Retrieves a BaseObject matching the Name name in this List.
262 * @param name the Name of the Object.
263 * @returns a BaseObject pointing to the object if found, NULL otherwise.
264 */
265template <class T>
266BaseObject* ObjectList<T>::getBaseObject(const std::string& name) const
267{
268  return this->getObject(name);
269}
270
271
272
273/**
274 * @brief Retrieves an Object of type T matching the Name name in this List.
275 * @param name the Name of the Object.
276 * @returns an Object of type T pointing to the object if found, NULL otherwise.
277 */
278template <class T>
279T* ObjectList<T>::getObject(const std::string& name) const
280{
281  const_iterator it;
282  for (it = this->_objects.begin(); it != this->_objects.end(); ++it)
283    if ((*it)->getName() == name)
284      return (*it);
285  return NULL;
286}
287
288/**
289 * @brief checks if Object object exists in this ClassList.
290 * @param object the Object to check for
291 * @returns True if the object is found within the List, false otherwise
292 */
293template <class T>
294bool ObjectList<T>::exists(const T* const object) const
295{
296  return (std::find(_objects.begin(), _objects.end(), object) != _objects.end());
297}
298
299
300/**
301 * @brief retrieves a List of BaseObjects
302 * @param list the list to push the ObjectList into.
303 */
304template <class T>
305void ObjectList<T>::getBaseObjectList(ObjectListBase::base_list* list) const
306{
307  assert (list != NULL);
308  const_iterator it;
309  for (it = this->_objects.begin(); it != this->_objects.end(); ++it)
310    list->push_back(*it);
311}
312
313
314/**
315 * @brief registers an Object to the ObjectList.
316 * @param T object the Object to register.
317 * @returns a pointer to the iterator inside of the list.
318 */
319template <class T>
320ObjectListBase::IteratorBase* ObjectList<T>::registerObject(T* object)
321{
322  this->_objects.push_back(object);
323  return new Iterator(--this->_objects.end());
324}
325
326/**
327 * @brief removes an Object from the ClassList.
328 * @param iterator the Position at which to remove the Object.
329 */
330template <class T>
331void ObjectList<T>::unregisterObject(IteratorBase* iterator)
332{
333  this->_objects.erase(static_cast<Iterator*>(iterator)->it());
334  //_objects.erase(std::find(_objects.begin(), _objects.end(), object));
335}
336
337#endif /* _OBJECT_LIST_H */
Note: See TracBrowser for help on using the repository browser.