Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

cleanup

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