[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 | |
---|
[9673] | 23 | |
---|
| 24 | //! The superclass that all NewObjectLists follow. |
---|
| 25 | /** |
---|
| 26 | * @see template<class T> NewObjectList<T> |
---|
| 27 | */ |
---|
[9661] | 28 | class NewObjectListBase |
---|
[9659] | 29 | { |
---|
| 30 | public: |
---|
[9673] | 31 | //! An iterator Base-Class, for iterator-casting and storing. |
---|
[9666] | 32 | class IteratorBase { }; |
---|
| 33 | |
---|
| 34 | public: |
---|
[9659] | 35 | int id() const { return _id; }; |
---|
| 36 | const std::string& name() const { return _name; }; |
---|
[9663] | 37 | bool operator==(int id) const { return _id == id; }; |
---|
| 38 | bool operator==(const std::string& name) const { return _name == name; }; |
---|
[2036] | 39 | |
---|
[9660] | 40 | |
---|
[9663] | 41 | /// Comparing operators. |
---|
[9671] | 42 | bool compareName(const NewObjectListBase& more) const { return this->_name < more.name(); }; |
---|
| 43 | bool compareID(const NewObjectListBase& more) const { return this->_id < more.id(); }; |
---|
[9661] | 44 | |
---|
[9671] | 45 | virtual void debug() const = 0; |
---|
| 46 | |
---|
[9663] | 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 | |
---|
[9672] | 53 | virtual void unregisterObject(IteratorBase* _iterators) = 0; |
---|
[9671] | 54 | |
---|
[9660] | 55 | protected: |
---|
[9661] | 56 | NewObjectListBase(const std::string& className); |
---|
[9671] | 57 | virtual ~NewObjectListBase(); |
---|
[9663] | 58 | |
---|
[9659] | 59 | private: |
---|
[9661] | 60 | NewObjectListBase(const NewObjectListBase&); |
---|
[1853] | 61 | |
---|
[9663] | 62 | static bool classNameExists(const std::string& className); |
---|
[9660] | 63 | |
---|
[9671] | 64 | protected: |
---|
| 65 | typedef std::set<NewObjectListBase*> cSet; //!< The Generic Set. |
---|
| 66 | typedef std::vector<NewObjectListBase*> cVector; //!< The |
---|
[9660] | 67 | |
---|
[9671] | 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). |
---|
[9660] | 73 | private: |
---|
[9661] | 74 | |
---|
[9671] | 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. |
---|
[9659] | 78 | }; |
---|
[1853] | 79 | |
---|
[9661] | 80 | |
---|
| 81 | ///////////////////////// |
---|
| 82 | //// TEMPLATISATION ///// |
---|
| 83 | ///////////////////////// |
---|
[9663] | 84 | //! Defines a ObjectsList handler for objects of type T. |
---|
| 85 | /** |
---|
[9673] | 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 | * |
---|
[9663] | 91 | * @note The Class must define the compare with const std::string& operator for this to work. |
---|
| 92 | */ |
---|
[9665] | 93 | template<class T> |
---|
| 94 | class NewObjectList : public NewObjectListBase |
---|
[9659] | 95 | { |
---|
| 96 | public: |
---|
[9671] | 97 | typedef std::list<T*> list; |
---|
| 98 | typedef typename list::iterator iterator; |
---|
| 99 | typedef typename list::const_iterator const_iterator; |
---|
[9663] | 100 | |
---|
[9666] | 101 | class Iterator : public NewObjectListBase::IteratorBase |
---|
[9665] | 102 | { |
---|
| 103 | public: |
---|
[9672] | 104 | Iterator(iterator it) { _it = it; } |
---|
| 105 | inline iterator& it() { return _it; } |
---|
| 106 | typename NewObjectList::iterator _it; |
---|
[9665] | 107 | }; |
---|
| 108 | |
---|
[9663] | 109 | public: |
---|
[9661] | 110 | NewObjectList(const std::string& name); |
---|
| 111 | ~NewObjectList(); |
---|
[3245] | 112 | |
---|
[9663] | 113 | T* getObject(const std::string& name) const; |
---|
| 114 | inline const list& objects() const { return _objects; }; |
---|
| 115 | |
---|
[9672] | 116 | NewObjectListBase::IteratorBase* registerObject(T* object); |
---|
| 117 | void unregisterObject(IteratorBase* iterator); |
---|
[9663] | 118 | |
---|
[9671] | 119 | virtual void debug() const; |
---|
| 120 | |
---|
[9660] | 121 | private: |
---|
| 122 | //! the copy constructor will be hidden. |
---|
[9661] | 123 | NewObjectList(const NewObjectList& definer) {}; |
---|
[9663] | 124 | |
---|
[9665] | 125 | private: |
---|
[9663] | 126 | list _objects; |
---|
[9660] | 127 | }; |
---|
[9659] | 128 | |
---|
[9665] | 129 | |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | ///////////////////////// |
---|
| 134 | //// IMPLEMENTATION ///// |
---|
| 135 | ///////////////////////// |
---|
[9660] | 136 | template <class T> |
---|
[9661] | 137 | NewObjectList<T>::NewObjectList(const std::string& name) |
---|
| 138 | : NewObjectListBase(name) |
---|
[9660] | 139 | {} |
---|
[9659] | 140 | |
---|
[9660] | 141 | template <class T> |
---|
[9661] | 142 | NewObjectList<T>::~NewObjectList() |
---|
[9663] | 143 | { |
---|
[9671] | 144 | // assert(_objects.empty()); |
---|
[9663] | 145 | } |
---|
[9659] | 146 | |
---|
[9663] | 147 | template <class T> |
---|
| 148 | T* 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 | } |
---|
[9659] | 156 | |
---|
[9664] | 157 | template <class T> |
---|
[9672] | 158 | NewObjectListBase::IteratorBase* NewObjectList<T>::registerObject(T* object) |
---|
[9664] | 159 | { |
---|
[9667] | 160 | this->_objects.push_front(object); |
---|
| 161 | return new Iterator(this->_objects.begin()); |
---|
[9664] | 162 | } |
---|
| 163 | |
---|
| 164 | template <class T> |
---|
[9672] | 165 | void NewObjectList<T>::unregisterObject(IteratorBase* iterator) |
---|
[9664] | 166 | { |
---|
[9672] | 167 | this->_objects.erase(static_cast<Iterator*>(iterator)->it()); |
---|
[9665] | 168 | //_objects.erase(std::find(_objects.begin(), _objects.end(), object)); |
---|
[9664] | 169 | } |
---|
| 170 | |
---|
[9671] | 171 | #include <iostream> |
---|
[9664] | 172 | |
---|
[9671] | 173 | template <class T> |
---|
| 174 | void NewObjectList<T>::debug() const |
---|
| 175 | { |
---|
| 176 | const_iterator it; |
---|
| 177 | for (it = this->_objects.begin(); it != this->_objects.end(); ++it) |
---|
| 178 | { |
---|
[9674] | 179 | std::cout << (*it)->getName() << std::endl; |
---|
[9671] | 180 | } |
---|
| 181 | } |
---|
| 182 | |
---|
[9662] | 183 | #endif /* _NEW_OBJECT_LIST_H */ |
---|