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
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 "new_class_id.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 _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; };
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  NewClassID                    _identity;          //!< The Identity of the Class (ID and Name).
72
73private:
74  static classIDMap*            _classesByID;       //!< A Map of all the classes in existance.
75  static classNameMap*          _classesByName;     //!< A Map of all the classes in existance.
76  static std::list<std::string> _classNames;        //!< A list of all the registered ClassNames.
77};
78
79
80/////////////////////////
81//// TEMPLATISATION /////
82/////////////////////////
83//! Defines a ObjectsList handler for objects of type T.
84/**
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 *
90 * @note The Class must define the compare with const std::string& operator for this to work.
91 */
92template<class T>
93class NewObjectList : public NewObjectListBase
94{
95public:
96  typedef std::list<T*>                  list;
97  typedef typename list::iterator        iterator;
98  typedef typename list::const_iterator  const_iterator;
99
100class Iterator : public NewObjectListBase::IteratorBase
101  {
102  public:
103    Iterator(iterator it) { _it = it; }
104    inline iterator& it() { return _it; }
105    typename NewObjectList::iterator _it;
106  };
107
108public:
109  NewObjectList(const std::string& name, int id = -1);
110  ~NewObjectList();
111
112  T*                      getObject(const std::string& name) const;
113  inline const list&      objects() const { return _objects; };
114
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
125  NewObjectListBase::IteratorBase* registerObject(T* object);
126  void unregisterObject(IteratorBase* iterator);
127
128  virtual void debug() const;
129
130private:
131  //! the copy constructor will be hidden.
132  NewObjectList(const NewObjectList& definer) {};
133
134private:
135  list                _objects;
136};
137
138
139
140
141
142/////////////////////////
143//// IMPLEMENTATION /////
144/////////////////////////
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 */
150template <class T>
151NewObjectList<T>::NewObjectList(const std::string& name, int id)
152    : NewObjectListBase(name, id)
153{}
154
155/**
156 * @brief deletes the NewObjectList.
157 */
158template <class T>
159NewObjectList<T>::~NewObjectList()
160{
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  }
166}
167
168template <class T>
169T* NewObjectList<T>::getObject(const std::string& name) const
170{
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;
176}
177
178template <class T>
179NewObjectListBase::IteratorBase* NewObjectList<T>::registerObject(T* object)
180{
181  this->_objects.push_front(object);
182  return new Iterator(this->_objects.begin());
183}
184
185template <class T>
186void NewObjectList<T>::unregisterObject(IteratorBase* iterator)
187{
188  this->_objects.erase(static_cast<Iterator*>(iterator)->it());
189  //_objects.erase(std::find(_objects.begin(), _objects.end(), object));
190}
191
192
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  {
199    std::cout << (*it)->getName() << std::endl;
200  }
201}
202
203#endif /* _NEW_OBJECT_LIST_H */
Note: See TracBrowser for help on using the repository browser.