Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: here is the speedup test… more or less:
20 iterations of generating and deleting 10000 objects

old version:
real 0m7.485s
user 0m7.344s
sys 0m0.012s

new version:
real 0m0.627s
user 0m0.568s
sys 0m0.032s

for that it is way more dynamic, the speedup was worth it :)

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