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
RevLine 
[4838]1/*!
[9716]2 * @file object_list.h
[9659]3 * @brief Definition of a dynamically allocating ClassID
4 *
5 */
[1853]6
[9716]7#ifndef _OBJECT_LIST_H
8#define _OBJECT_LIST_H
[1853]9
[9716]10#include "class_id.h"
[9713]11#include <cassert>
[9675]12#include <map>
[9663]13#include <list>
[9659]14#include <string>
[9681]15#include <iostream>
[1853]16
[9686]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 */
[9715]23#define ObjectListDeclaration(ClassName) \
[9684]24  public: \
[9715]25   static inline const ObjectList<ClassName>& objectList() { return ClassName::_objectList; }; \
26   static inline const ClassID& classID() { return ClassName::_objectList.identity(); }; \
[9684]27  private: \
[9715]28   static ObjectList<ClassName> _objectList
[9660]29
[9724]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 */
[9715]35#define ObjectListDefinitionID(ClassName, ID) \
36   ObjectList<ClassName> ClassName::_objectList(#ClassName, ID)
[9673]37
[9724]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 */
[9715]42#define ObjectListDefinition(ClassName) \
43    ObjectListDefinitionID(ClassName, -1)
[9682]44
[9692]45class BaseObject;
[9715]46//! The superclass that all ObjectLists follow.
[9673]47/**
[9715]48 * @see template<class T> ObjectList<T>
[9673]49 */
[9715]50class ObjectListBase
[9659]51{
52public:
[9709]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;
[9724]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 { };
[9705]60
[9666]61public:
[9705]62  /** @returns The Identity of the Class stored within. */
[9724]63  inline const ClassID&                 identity() const { return _identity; }
[9705]64  /** @returns the ID of the Identity of the ObjectList */
[9709]65  inline int                            id() const { return _id; };
[9705]66  /** @returns The Name of the Class stored in this ObjectList */
[9709]67  inline const std::string&             name() const { return _name; };
[9705]68  /** @param id The id to compare @returns true on match, false otherwise */
[9709]69  inline bool                           operator==(int id) const { return _id == id; };
[9705]70  /** @param id The id to compare @returns true on match, false otherwise */
[9715]71  inline bool                           operator==(const ClassID& id) const { return id == _id; };
[9705]72  /** @param name The name to compare @returns true on match, false otherwise */
[9709]73  inline bool                           operator==(const std::string& name) const { return _name == name; };
[9724]74  /** @param id The id to acquire @param name the Name to acquire @brief stores a Name and an ID in a pointer. */
[9709]75  inline void                           acquireID(const int*& id, const std::string*& name) const { id = &_id; name = &_name; };
[9705]76  /** @brief fills a list of Objects into a BaseObject*-List. @param list the list to fill */
[9709]77  virtual void                          getBaseObjectList(base_list* list) const = 0;
[2036]78
[9718]79  static const ClassID&                 retrieveIdentity(int id);
80  static const ClassID&                 retrieveIdentity(const std::string& name);
[9701]81
[9718]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);
[9693]85
[9709]86  static BaseObject*                    getBaseObject(int classID, const std::string& objectName);
87  static BaseObject*                    getBaseObject(const std::string& className, const std::string& objectName);
[9715]88  static BaseObject*                    getBaseObject(const ClassID& classID, const std::string& objectName);
[9696]89
[9705]90  /** @returns an Object with Name name out of this List @param name the name of the Object. */
[9709]91  virtual BaseObject*                   getBaseObject(const std::string& name) const = 0;
[9696]92
[9705]93  static const std::list<std::string>&  getClassNames();
94
[9709]95
[9705]96  static unsigned int                   classCount();
[9709]97  void                                  debug(unsigned int level) const;
98  static void                           debugAll(unsigned int level);
99
[9705]100  static const std::string&             IDToString(int classID);
101  static int                            StringToID(const std::string& className);
102
103  //! Only uset to unsubscribe a BaseObject.
[9709]104  virtual void                          unregisterObject(IteratorBase* _iterators) = 0;
[9705]105
[9671]106protected:
[9715]107  ObjectListBase(const std::string& className, int id = -1);
108  virtual ~ObjectListBase();
[9702]109
110private:
[9724]111  /** @brief hidden copy constructor. */
112  ObjectListBase(const ObjectListBase&) {};
[9702]113
[9709]114  static bool                         classIDExists(int id);
115  static bool                         classNameExists(const std::string& className);
[9702]116
117
118protected:
[9715]119  typedef std::map<int, ObjectListBase*>         classIDMap;   //!< The Generic Map.
120  typedef std::map<std::string, ObjectListBase*> classNameMap; //!< The Generic Map.
[9660]121
[9705]122private:
[9718]123  int                           _id;                //!< The Unique ID of the Class.
[9701]124  const std::string             _name;              //!< The Name of the Class.
[9718]125  ClassID                       _identity;          //!< The Identity of the Class. (equal to _id and _name)
[9671]126
[9660]127private:
[9675]128  static classIDMap*            _classesByID;       //!< A Map of all the classes in existance.
129  static classNameMap*          _classesByName;     //!< A Map of all the classes in existance.
[9671]130  static std::list<std::string> _classNames;        //!< A list of all the registered ClassNames.
[9659]131};
[1853]132
[9661]133
134/////////////////////////
135//// TEMPLATISATION /////
136/////////////////////////
[9663]137//! Defines a ObjectsList handler for objects of type T.
138/**
[9709]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 *
[9715]143 * Furthermore the linkage over the single Lists is given over the Superclass ObjectListBase.
[9709]144 *
145 *
146 *
147 *
[9673]148 * To define a Class with a ObjectList, you have to:
[9715]149 *  1. Include 'ObjectListDeclaration(T);' in its Declaration (at the beginning)
150 *  2. Include 'ObjectListDefinition(T);' in some Definition file (cc-file)
[9673]151 *  3. In the constructor add 'registerObject(this, objectList);'
152 *
[9663]153 * @note The Class must define the compare with const std::string& operator for this to work.
[9709]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 *
[9724]161 * example: Iterating: Iteration is made easy, and fast as follows:
[9715]162 *   for (ObjectList<PlayerStats>::const_iterator it = PlayerStats::objectList().begin();
[9709]163 *      it != PlayerStats::objectList().end();
164 *     ++it)
165 *   {
166 *    (*it)->dosomething
167 *   }
168 *
[9724]169 * example: Find an Object:
[9709]170 * Playable* playable = Playable::objectList("orxonox-super-rocket-fighter"); // searches an Object By its name.
171 *
[9663]172 */
[9665]173template<class T>
[9715]174class ObjectList : public ObjectListBase
[9659]175{
176public:
[9718]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)
[9663]180
[9692]181
[9718]182  //! An iterator to store Objects in the BaseObject, and remove Objects fast.
[9715]183class Iterator : public ObjectListBase::IteratorBase
[9665]184  {
185  public:
[9718]186    /** @brief creates an Iterator fast. @param it the Iterator. */
187    inline Iterator(iterator it) { _it = it; }
188    /** @returns the Iterator */
[9672]189    inline iterator& it() { return _it; }
[9718]190  private:
191    typename ObjectList::iterator _it;  //!< Stored Iterator
[9665]192  };
193
[9663]194public:
[9715]195  ObjectList(const std::string& name, int id = -1);
196  ~ObjectList();
[3245]197
[9709]198  virtual BaseObject*                getBaseObject(const std::string& name) const;
199  T*                                 getObject(const std::string& name) const;
[9724]200  /** @returns A constant list of Objects of this Class. */
[9709]201  inline const list&                 objects() const { return _objects; };
202  bool                               exists(const T* const object) const;
[9663]203
[9709]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(); };
[9685]212
[9709]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(); };
[9685]221
[9705]222
[9718]223  ObjectListBase::IteratorBase*      registerObject(T* object);
[9709]224  virtual void                       unregisterObject(IteratorBase* iterator);
[9663]225
[9693]226protected:
[9715]227  virtual void                       getBaseObjectList(ObjectListBase::base_list* list) const;
[9693]228
[9702]229
[9660]230private:
231  //! the copy constructor will be hidden.
[9715]232  ObjectList(const ObjectList& definer) {};
[9663]233
[9665]234private:
[9709]235  list                _objects;     //!< The List of stored Objects of Type T.
[9660]236};
[9659]237
[9665]238
239
240
241
242/////////////////////////
243//// IMPLEMENTATION /////
244/////////////////////////
[9681]245/**
[9715]246 * @brief creates a new ObjectList
[9681]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 */
[9660]250template <class T>
[9715]251ObjectList<T>::ObjectList(const std::string& name, int id)
252    : ObjectListBase(name, id)
[9660]253{}
[9659]254
[9681]255/**
[9715]256 * @brief deletes the ObjectList.
[9681]257 */
[9660]258template <class T>
[9715]259ObjectList<T>::~ObjectList()
[9663]260{
[9681]261  if (!_objects.empty())
262  {
[9709]263    // std::cout << "There are " << this->size() << " objects from class " << this->name() << "(id:" << this->id() << ") in existance\n";
[9681]264  }
[9663]265}
[9659]266
[9696]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 */
[9663]272template <class T>
[9715]273BaseObject* ObjectList<T>::getBaseObject(const std::string& name) const
[9696]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>
[9715]286T* ObjectList<T>::getObject(const std::string& name) const
[9663]287{
[9684]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;
[9663]293}
[9659]294
[9696]295/**
[9705]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>
[9718]301bool ObjectList<T>::exists(const T* const object) const
[9705]302{
303  return (std::find(_objects.begin(), _objects.end(), object) != _objects.end());
304}
305
306
307/**
[9702]308 * @brief retrieves a List of BaseObjects
309 * @param list the list to push the ObjectList into.
310 */
311template <class T>
[9715]312void ObjectList<T>::getBaseObjectList(ObjectListBase::base_list* list) const
[9702]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/**
[9715]322 * @brief registers an Object to the ObjectList.
[9724]323 * @param object The Object to register.
[9696]324 * @returns a pointer to the iterator inside of the list.
325 */
[9664]326template <class T>
[9715]327ObjectListBase::IteratorBase* ObjectList<T>::registerObject(T* object)
[9664]328{
[9709]329  this->_objects.push_back(object);
330  return new Iterator(--this->_objects.end());
[9664]331}
332
[9696]333/**
334 * @brief removes an Object from the ClassList.
335 * @param iterator the Position at which to remove the Object.
336 */
[9664]337template <class T>
[9715]338void ObjectList<T>::unregisterObject(IteratorBase* iterator)
[9664]339{
[9672]340  this->_objects.erase(static_cast<Iterator*>(iterator)->it());
[9665]341  //_objects.erase(std::find(_objects.begin(), _objects.end(), object));
[9664]342}
343
[9716]344#endif /* _OBJECT_LIST_H */
Note: See TracBrowser for help on using the repository browser.