Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/new_class_id: slowly but surely reimplementing to the new groove… way to go

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