Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: going towards the implementation

File size: 3.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 "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    typename NewObjectList::iterator it;
85  };
86
87public:
88  NewObjectList(const std::string& name);
89  ~NewObjectList();
90
91  T*                      getObject(const std::string& name) const;
92  inline const list&      objects() const { return _objects; };
93
94  void registerObject(T* object);
95  void unregisterObject(const IteratorBase& iterator);
96
97private:
98  //! the copy constructor will be hidden.
99  NewObjectList(const NewObjectList& definer) {};
100
101private:
102  list                _objects;
103};
104
105
106
107
108
109/////////////////////////
110//// IMPLEMENTATION /////
111/////////////////////////
112template <class T>
113NewObjectList<T>::NewObjectList(const std::string& name)
114    : NewObjectListBase(name)
115{}
116
117template <class T>
118NewObjectList<T>::~NewObjectList()
119{
120  assert(_objects.empty());
121}
122
123template <class T>
124T* NewObjectList<T>::getObject(const std::string& name) const
125{
126  iterator it = std::find(this->_objects.begin(), this->_objects.end(), name);
127  if (it != this->_objects.end())
128    return *it;
129  else
130    return NULL;
131}
132
133template <class T>
134void NewObjectList<T>::registerObject(T* object)
135{
136  this->_objects.push_back(object);
137}
138
139template <class T>
140void NewObjectList<T>::unregisterObject(const IteratorBase& iterator)
141{
142  this->_objects.erase(static_cast<Iterator>(iterator));
143  //_objects.erase(std::find(_objects.begin(), _objects.end(), object));
144}
145
146
147#endif /* _NEW_OBJECT_LIST_H */
Note: See TracBrowser for help on using the repository browser.