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