Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ClassList objectively implemented. This is not the fastest, but the most modular approach.
Now is the question of redundancy in code writing:
Should the ClassWriter explicitely declare a Class with superclasses, or should on runtime each object decide for itself, as it is done in BaseObject at the moment… questions questions questions…

File size: 5.0 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 <vector>
14#include <string>
15
16
17#define NewObjectListDeclaration(ClassName) \
18   static NewObjectList<ClassName> objectList
19
20#define NewObjectListDefinition(ClassName) \
21   NewObjectList<ClassName> ClassName::objectList(#ClassName)
22
23class NewObjectListBase
24{
25public:
26  class IteratorBase { };
27  struct ClassEntry{
28    inline ClassEntry (NewObjectListBase* objectList, NewObjectListBase::IteratorBase* iterator) : _objectList(objectList), _iterator(iterator) {}
29    NewObjectListBase*                _objectList;
30    NewObjectListBase::IteratorBase*  _iterator;
31  };
32
33public:
34  int id() const { return _id; };
35  const std::string& name() const { return _name; };
36  bool operator==(int id) const { return _id == id; };
37  bool operator==(const std::string& name) const { return _name == name; };
38
39
40  /// Comparing operators.
41  bool compareName(const NewObjectListBase& more) const { return this->_name < more.name(); };
42  bool compareID(const NewObjectListBase& more) const { return this->_id < more.id(); };
43
44  virtual void debug() const = 0;
45
46  static unsigned int                   classCount() { return _idCounter; };
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);
56  virtual ~NewObjectListBase();
57
58private:
59  NewObjectListBase(const NewObjectListBase&);
60
61  static bool classNameExists(const std::string& className);
62
63protected:
64  typedef std::set<NewObjectListBase*>     cSet;    //!< The Generic Set.
65  typedef std::vector<NewObjectListBase*>  cVector; //!< The
66
67  int                           _id;                //!< The ID of the class.
68  std::string                   _name;              //!< The Name of the Class.
69
70  cVector                       _typeOfList;        //!< A List of all classes this class is derived of, and the class itself, ordered by age of addition.
71  cSet                          _typeOfSet;         //!< A Set of all classes this is derived from and the class itself (for isA).
72private:
73
74  static int                    _idCounter;         //!< A counter, that gives all classes a Unique ClassID. Access to this Variable is to be Thread-Safe.
75  static cSet*                  _classes;           //!< A Set of all the classes in existance.
76  static std::list<std::string> _classNames;        //!< A list of all the registered ClassNames.
77};
78
79
80/////////////////////////
81//// TEMPLATISATION /////
82/////////////////////////
83//! Defines a ObjectsList handler for objects of type T.
84/**
85 * @note The Class must define the compare with const std::string& operator for this to work.
86 */
87template<class T>
88class NewObjectList : public NewObjectListBase
89{
90public:
91  typedef std::list<T*>                  list;
92  typedef typename list::iterator        iterator;
93  typedef typename list::const_iterator  const_iterator;
94
95class Iterator : public NewObjectListBase::IteratorBase
96  {
97  public:
98    Iterator(iterator it) { _it = it; }
99    inline iterator& it() { return _it; }
100    typename NewObjectList::iterator _it;
101  };
102
103public:
104  NewObjectList(const std::string& name);
105  ~NewObjectList();
106
107  T*                      getObject(const std::string& name) const;
108  inline const list&      objects() const { return _objects; };
109
110  NewObjectListBase::IteratorBase* registerObject(T* object);
111  void unregisterObject(IteratorBase* iterator);
112
113  virtual void debug() const;
114
115private:
116  //! the copy constructor will be hidden.
117  NewObjectList(const NewObjectList& definer) {};
118
119private:
120  list                _objects;
121};
122
123
124
125
126
127/////////////////////////
128//// IMPLEMENTATION /////
129/////////////////////////
130template <class T>
131NewObjectList<T>::NewObjectList(const std::string& name)
132    : NewObjectListBase(name)
133{}
134
135template <class T>
136NewObjectList<T>::~NewObjectList()
137{
138  // assert(_objects.empty());
139}
140
141template <class T>
142T* NewObjectList<T>::getObject(const std::string& name) const
143{
144  iterator it = std::find(this->_objects.begin(), this->_objects.end(), name);
145  if (it != this->_objects.end())
146    return *it;
147  else
148    return NULL;
149}
150
151template <class T>
152    NewObjectListBase::IteratorBase* NewObjectList<T>::registerObject(T* object)
153{
154  this->_objects.push_front(object);
155  return new Iterator(this->_objects.begin());
156}
157
158template <class T>
159void NewObjectList<T>::unregisterObject(IteratorBase* iterator)
160{
161  this->_objects.erase(static_cast<Iterator*>(iterator)->it());
162  //_objects.erase(std::find(_objects.begin(), _objects.end(), object));
163}
164
165#include <iostream>
166
167template <class T>
168void NewObjectList<T>::debug() const
169{
170  const_iterator it;
171  for (it = this->_objects.begin(); it != this->_objects.end(); ++it)
172  {
173    std::cout << (*it)->name() << std::endl;
174  }
175}
176
177#endif /* _NEW_OBJECT_LIST_H */
Note: See TracBrowser for help on using the repository browser.