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