Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: iterator-insertion works

File size: 3.8 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 <set>
12#include <list>
13#include <string>
14
15
16#define NewObjectListDeclaration(ClassName) \
17   static NewObjectList<ClassName> objectList
18
19#define NewObjectListDefinition(ClassName) \
20   NewObjectList<ClassName> ClassName::objectList(#ClassName)
21
22class NewObjectListBase
23{
24public:
25  class IteratorBase { };
26
27public:
28  int id() const { return _id; };
29  const std::string& name() const { return _name; };
30  bool operator==(int id) const { return _id == id; };
31  bool operator==(const std::string& name) const { return _name == name; };
32
33
34  /// Comparing operators.
35  bool compareName(const NewObjectListBase& more) { return this->_name < more.name(); };
36  bool compareID(const NewObjectListBase& more) { return this->_id < more.id(); };
37
38  static unsigned int                   classCount() { return _idCounter; };
39  static const std::string&             IDToString(int classID);
40  static int                            StringToID(const std::string& className);
41
42  static const std::list<std::string>&  getClassNames();
43
44protected:
45
46  NewObjectListBase(const std::string& className);
47  ~NewObjectListBase();
48
49private:
50  NewObjectListBase(const NewObjectListBase&);
51
52  static bool classNameExists(const std::string& className);
53
54private:
55  int                           _id;
56  std::string                   _name;
57
58private:
59  typedef std::set<NewObjectListBase*>  cList;
60
61  static int                           _idCounter;      //!< A counter, that gives all classes a Unique ClassID. Access to this Variable is to be Thread-Safe.
62  static cList*                        _classes;        //!< A Set of all the classes in existance.
63  static std::list<std::string>        _classNames;     //!< A list of all the registered ClassNames.
64};
65
66
67/////////////////////////
68//// TEMPLATISATION /////
69/////////////////////////
70//! Defines a ObjectsList handler for objects of type T.
71/**
72 * @note The Class must define the compare with const std::string& operator for this to work.
73 */
74template<class T>
75class NewObjectList : public NewObjectListBase
76{
77public:
78  typedef std::list<T*>               list;
79  typedef typename list::iterator     iterator;
80
81class Iterator : public NewObjectListBase::IteratorBase
82  {
83  public:
84    Iterator(iterator it) { it = it; }
85    typename NewObjectList::iterator it;
86  };
87
88public:
89  NewObjectList(const std::string& name);
90  ~NewObjectList();
91
92  T*                      getObject(const std::string& name) const;
93  inline const list&      objects() const { return _objects; };
94
95  NewObjectListBase::IteratorBase* registerObject(T* object);
96  void unregisterObject(const IteratorBase& iterator);
97
98private:
99  //! the copy constructor will be hidden.
100  NewObjectList(const NewObjectList& definer) {};
101
102private:
103  list                _objects;
104};
105
106
107
108
109
110/////////////////////////
111//// IMPLEMENTATION /////
112/////////////////////////
113template <class T>
114NewObjectList<T>::NewObjectList(const std::string& name)
115    : NewObjectListBase(name)
116{}
117
118template <class T>
119NewObjectList<T>::~NewObjectList()
120{
121 // assert(_objects.empty());
122}
123
124template <class T>
125T* NewObjectList<T>::getObject(const std::string& name) const
126{
127  iterator it = std::find(this->_objects.begin(), this->_objects.end(), name);
128  if (it != this->_objects.end())
129    return *it;
130  else
131    return NULL;
132}
133
134template <class T>
135NewObjectListBase::IteratorBase* NewObjectList<T>::registerObject(T* object)
136{
137  this->_objects.push_front(object);
138  return new Iterator(this->_objects.begin());
139}
140
141template <class T>
142void NewObjectList<T>::unregisterObject(const IteratorBase& iterator)
143{
144  this->_objects.erase(static_cast<Iterator>(iterator));
145  //_objects.erase(std::find(_objects.begin(), _objects.end(), object));
146}
147
148
149#endif /* _NEW_OBJECT_LIST_H */
Note: See TracBrowser for help on using the repository browser.