Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

many many documentations and one new functiopm

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