Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/lang/new_object_list.h @ 9704

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

orxonox/trunk: sync

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