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