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
RevLine 
[4838]1/*!
[9662]2 * @file new_object_list.h
[9659]3 * @brief Definition of a dynamically allocating ClassID
4 *
5 */
[1853]6
[9662]7#ifndef _NEW_OBJECT_LIST_H
8#define _NEW_OBJECT_LIST_H
[1853]9
[9659]10#include "type_info.h"
11#include <set>
[9663]12#include <list>
[9659]13#include <string>
[1853]14
[3543]15
[9663]16#define NewObjectListDeclaration(ClassName) \
17   static NewObjectList<ClassName> objectList
[9660]18
[9663]19#define NewObjectListDefinition(ClassName) \
20   NewObjectList<ClassName> ClassName::objectList(#ClassName)
21
[9661]22class NewObjectListBase
[9659]23{
24public:
[9666]25  class IteratorBase { };
26
27public:
[9659]28  int id() const { return _id; };
29  const std::string& name() const { return _name; };
[9663]30  bool operator==(int id) const { return _id == id; };
31  bool operator==(const std::string& name) const { return _name == name; };
[2036]32
[9660]33
[9663]34  /// Comparing operators.
[9661]35  bool compareName(const NewObjectListBase& more) { return this->_name < more.name(); };
36  bool compareID(const NewObjectListBase& more) { return this->_id < more.id(); };
37
[9663]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
[9660]44protected:
[9665]45
[9661]46  NewObjectListBase(const std::string& className);
47  ~NewObjectListBase();
[9663]48
[9659]49private:
[9661]50  NewObjectListBase(const NewObjectListBase&);
[1853]51
[9663]52  static bool classNameExists(const std::string& className);
[9660]53
[9659]54private:
[9660]55  int                           _id;
56  std::string                   _name;
57
58private:
[9661]59  typedef std::set<NewObjectListBase*>  cList;
60
[9660]61  static int                           _idCounter;      //!< A counter, that gives all classes a Unique ClassID. Access to this Variable is to be Thread-Safe.
[9661]62  static cList*                        _classes;        //!< A Set of all the classes in existance.
[9663]63  static std::list<std::string>        _classNames;     //!< A list of all the registered ClassNames.
[9659]64};
[1853]65
[9661]66
67/////////////////////////
68//// TEMPLATISATION /////
69/////////////////////////
[9663]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 */
[9665]74template<class T>
75class NewObjectList : public NewObjectListBase
[9659]76{
77public:
[9663]78  typedef std::list<T*>               list;
79  typedef typename list::iterator     iterator;
80
[9666]81class Iterator : public NewObjectListBase::IteratorBase
[9665]82  {
83  public:
84    typename NewObjectList::iterator it;
85  };
86
[9663]87public:
[9661]88  NewObjectList(const std::string& name);
89  ~NewObjectList();
[3245]90
[9663]91  T*                      getObject(const std::string& name) const;
92  inline const list&      objects() const { return _objects; };
93
[9664]94  void registerObject(T* object);
[9665]95  void unregisterObject(const IteratorBase& iterator);
[9663]96
[9660]97private:
98  //! the copy constructor will be hidden.
[9661]99  NewObjectList(const NewObjectList& definer) {};
[9663]100
[9665]101private:
[9663]102  list                _objects;
[9660]103};
[9659]104
[9665]105
106
107
108
109/////////////////////////
110//// IMPLEMENTATION /////
111/////////////////////////
[9660]112template <class T>
[9661]113NewObjectList<T>::NewObjectList(const std::string& name)
114    : NewObjectListBase(name)
[9660]115{}
[9659]116
[9660]117template <class T>
[9661]118NewObjectList<T>::~NewObjectList()
[9663]119{
120  assert(_objects.empty());
121}
[9659]122
[9663]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}
[9659]132
[9664]133template <class T>
134void NewObjectList<T>::registerObject(T* object)
135{
136  this->_objects.push_back(object);
137}
138
139template <class T>
[9665]140void NewObjectList<T>::unregisterObject(const IteratorBase& iterator)
[9664]141{
[9665]142  this->_objects.erase(static_cast<Iterator>(iterator));
143  //_objects.erase(std::find(_objects.begin(), _objects.end(), object));
[9664]144}
145
146
[9662]147#endif /* _NEW_OBJECT_LIST_H */
Note: See TracBrowser for help on using the repository browser.