Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: iterator definitions

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