Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

adapted many classes to the new ClassID System, now comes the hard part… Scripting… then Network… wow this will be so bad :/

File size: 5.7 KB
RevLine 
[4838]1/*!
[9662]2 * @file new_object_list.h
[9659]3 * @brief Definition of a dynamically allocating ClassID
4 *
5 */
[1853]6
[9662]7#ifndef _NEW_OBJECT_LIST_H
8#define _NEW_OBJECT_LIST_H
[1853]9
[9685]10#include "new_class_id.h"
[9675]11#include <map>
[9663]12#include <list>
[9659]13#include <string>
[9681]14#include <iostream>
[1853]15
[3543]16
[9663]17#define NewObjectListDeclaration(ClassName) \
[9684]18  public: \
19   static const NewObjectList<ClassName>& objectList() { return ClassName::_objectList; }; \
20  private: \
21   static NewObjectList<ClassName> _objectList
[9660]22
[9677]23#define NewObjectListDefinitionID(ClassName, ID) \
[9684]24   NewObjectList<ClassName> ClassName::_objectList(#ClassName, ID)
[9673]25
[9677]26
[9682]27#define NewObjectListDefinition(ClassName) \
28    NewObjectListDefinitionID(ClassName, -1)
29
30
[9673]31//! The superclass that all NewObjectLists follow.
32/**
33 * @see template<class T> NewObjectList<T>
34 */
[9661]35class NewObjectListBase
[9659]36{
37public:
[9673]38  //! An iterator Base-Class, for iterator-casting and storing.
[9666]39  class IteratorBase { };
40
41public:
[9685]42  inline int id() const { return _identity.id(); };
43  inline const std::string& name() const { return _identity.name(); };
44  bool operator==(int id) const { return _identity == id; };
45  bool operator==(const std::string& name) const { return _identity == name; };
[2036]46
[9671]47  virtual void debug() const = 0;
48
[9676]49  static unsigned int                   classCount();
[9663]50  static const std::string&             IDToString(int classID);
51  static int                            StringToID(const std::string& className);
52
53  static const std::list<std::string>&  getClassNames();
54
[9672]55  virtual void unregisterObject(IteratorBase* _iterators) = 0;
[9671]56
[9660]57protected:
[9676]58  NewObjectListBase(const std::string& className, int id = -1);
[9671]59  virtual ~NewObjectListBase();
[9663]60
[9659]61private:
[9661]62  NewObjectListBase(const NewObjectListBase&);
[1853]63
[9676]64  static bool classIDExists(int id);
[9663]65  static bool classNameExists(const std::string& className);
[9660]66
[9671]67protected:
[9675]68  typedef std::map<int, NewObjectListBase*> classIDMap;    //!< The Generic Map.
69  typedef std::map<std::string, NewObjectListBase*> classNameMap;//!< The Generic Map.
[9660]70
[9685]71  NewClassID                    _identity;          //!< The Identity of the Class (ID and Name).
[9671]72
[9660]73private:
[9675]74  static classIDMap*            _classesByID;       //!< A Map of all the classes in existance.
75  static classNameMap*          _classesByName;     //!< A Map of all the classes in existance.
[9671]76  static std::list<std::string> _classNames;        //!< A list of all the registered ClassNames.
[9659]77};
[1853]78
[9661]79
80/////////////////////////
81//// TEMPLATISATION /////
82/////////////////////////
[9663]83//! Defines a ObjectsList handler for objects of type T.
84/**
[9673]85 * To define a Class with a ObjectList, you have to:
86 *  1. Include 'NewObjectListDeclaration(T);' in its Declaration (at the beginning)
87 *  2. Include 'NewObjectListDefinition(T);' in some Definition file (cc-file)
88 *  3. In the constructor add 'registerObject(this, objectList);'
89 *
[9663]90 * @note The Class must define the compare with const std::string& operator for this to work.
91 */
[9665]92template<class T>
93class NewObjectList : public NewObjectListBase
[9659]94{
95public:
[9671]96  typedef std::list<T*>                  list;
97  typedef typename list::iterator        iterator;
98  typedef typename list::const_iterator  const_iterator;
[9663]99
[9666]100class Iterator : public NewObjectListBase::IteratorBase
[9665]101  {
102  public:
[9672]103    Iterator(iterator it) { _it = it; }
104    inline iterator& it() { return _it; }
105    typename NewObjectList::iterator _it;
[9665]106  };
107
[9663]108public:
[9676]109  NewObjectList(const std::string& name, int id = -1);
[9661]110  ~NewObjectList();
[3245]111
[9663]112  T*                      getObject(const std::string& name) const;
113  inline const list&      objects() const { return _objects; };
114
[9685]115  inline iterator begin() { return _objects.begin(); };
116  inline const_iterator begin() const { return _objects.begin(); };
117  inline iterator  end() { return _objects.end(); };
118  inline const_iterator  end() const { return _objects.end(); };
119
120  inline bool empty() const { return _objects.empty(); };
121  inline int size() const { return _objects.size(); };
122  inline T* front() const { return _objects.front(); };
123  inline T* back() const { return _objects.back(); };
124
[9672]125  NewObjectListBase::IteratorBase* registerObject(T* object);
126  void unregisterObject(IteratorBase* iterator);
[9663]127
[9671]128  virtual void debug() const;
129
[9660]130private:
131  //! the copy constructor will be hidden.
[9661]132  NewObjectList(const NewObjectList& definer) {};
[9663]133
[9665]134private:
[9663]135  list                _objects;
[9660]136};
[9659]137
[9665]138
139
140
141
142/////////////////////////
143//// IMPLEMENTATION /////
144/////////////////////////
[9681]145/**
146 * @brief creates a new NewObjectList
147 * @param name The name of the Class.
148 * @param id The ID of the class if desired, or -1 if an id should be assigned automatically.
149 */
[9660]150template <class T>
[9676]151NewObjectList<T>::NewObjectList(const std::string& name, int id)
152    : NewObjectListBase(name, id)
[9660]153{}
[9659]154
[9681]155/**
156 * @brief deletes the NewObjectList.
157 */
[9660]158template <class T>
[9661]159NewObjectList<T>::~NewObjectList()
[9663]160{
[9681]161  if (!_objects.empty())
162  {
163    std::cout << "There are still Object in the ObjectList of " << this->name() << "(id:" << this->id() << ")\n";
164    this->debug();
165  }
[9663]166}
[9659]167
[9663]168template <class T>
169T* NewObjectList<T>::getObject(const std::string& name) const
170{
[9684]171  const_iterator it;
172  for (it = this->_objects.begin(); it != this->_objects.end(); ++it)
173    if ((*it)->getName() == name)
174      return (*it);
175  return NULL;
[9663]176}
[9659]177
[9664]178template <class T>
[9681]179NewObjectListBase::IteratorBase* NewObjectList<T>::registerObject(T* object)
[9664]180{
[9667]181  this->_objects.push_front(object);
182  return new Iterator(this->_objects.begin());
[9664]183}
184
185template <class T>
[9672]186void NewObjectList<T>::unregisterObject(IteratorBase* iterator)
[9664]187{
[9672]188  this->_objects.erase(static_cast<Iterator*>(iterator)->it());
[9665]189  //_objects.erase(std::find(_objects.begin(), _objects.end(), object));
[9664]190}
191
192
[9671]193template <class T>
194void NewObjectList<T>::debug() const
195{
196  const_iterator it;
197  for (it = this->_objects.begin(); it != this->_objects.end(); ++it)
198  {
[9674]199    std::cout << (*it)->getName() << std::endl;
[9671]200  }
201}
202
[9662]203#endif /* _NEW_OBJECT_LIST_H */
Note: See TracBrowser for help on using the repository browser.