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