1 | /*! |
---|
2 | * @file new_object_list.h |
---|
3 | * @brief Definition of a dynamically allocating ClassID |
---|
4 | * |
---|
5 | */ |
---|
6 | |
---|
7 | #ifndef _NEW_OBJECT_LIST_H |
---|
8 | #define _NEW_OBJECT_LIST_H |
---|
9 | |
---|
10 | #include "new_class_id.h" |
---|
11 | #include <map> |
---|
12 | #include <list> |
---|
13 | #include <string> |
---|
14 | #include <iostream> |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | /** |
---|
19 | * @brief Use this macro to easily declare a Class to store its own ObjectListDeclaration |
---|
20 | * @param ClassName the Name of the Class. |
---|
21 | * @note: Using this inside of a Class means, that you loose the member: _objectList, while defining |
---|
22 | * two new functions objectList() and classID(). |
---|
23 | */ |
---|
24 | #define NewObjectListDeclaration(ClassName) \ |
---|
25 | public: \ |
---|
26 | static inline const NewObjectList<ClassName>& objectList() { return ClassName::_objectList; }; \ |
---|
27 | static inline const NewClassID& classID() { return ClassName::_objectList.identity(); }; \ |
---|
28 | private: \ |
---|
29 | static NewObjectList<ClassName> _objectList |
---|
30 | |
---|
31 | #define NewObjectListDefinitionID(ClassName, ID) \ |
---|
32 | NewObjectList<ClassName> ClassName::_objectList(#ClassName, ID) |
---|
33 | |
---|
34 | |
---|
35 | #define NewObjectListDefinition(ClassName) \ |
---|
36 | NewObjectListDefinitionID(ClassName, -1) |
---|
37 | |
---|
38 | class BaseObject; |
---|
39 | //! The superclass that all NewObjectLists follow. |
---|
40 | /** |
---|
41 | * @see template<class T> NewObjectList<T> |
---|
42 | */ |
---|
43 | class NewObjectListBase |
---|
44 | { |
---|
45 | public: |
---|
46 | //! A fast iterator Base-Class, for iterator-casting and storing. |
---|
47 | /** |
---|
48 | * @note This Iterator is explicitely used only for storage purposes |
---|
49 | */ |
---|
50 | class IteratorBase { }; |
---|
51 | |
---|
52 | public: |
---|
53 | inline const NewClassID& identity() const { return _identity; }; |
---|
54 | inline int id() const { return _identity.id(); }; |
---|
55 | inline const std::string& name() const { return _identity.name(); }; |
---|
56 | bool operator==(int id) const { return _identity == id; }; |
---|
57 | bool operator==(const NewClassID& id) const { return _identity == id; }; |
---|
58 | bool operator==(const std::string& name) const { return _identity == name; }; |
---|
59 | |
---|
60 | virtual void debug() const = 0; |
---|
61 | |
---|
62 | static unsigned int classCount(); |
---|
63 | static const std::string& IDToString(int classID); |
---|
64 | static int StringToID(const std::string& className); |
---|
65 | |
---|
66 | static const std::list<std::string>& getClassNames(); |
---|
67 | |
---|
68 | virtual void unregisterObject(IteratorBase* _iterators) = 0; |
---|
69 | |
---|
70 | protected: |
---|
71 | NewObjectListBase(const std::string& className, int id = -1); |
---|
72 | virtual ~NewObjectListBase(); |
---|
73 | |
---|
74 | private: |
---|
75 | NewObjectListBase(const NewObjectListBase&); |
---|
76 | |
---|
77 | static bool classIDExists(int id); |
---|
78 | static bool classNameExists(const std::string& className); |
---|
79 | |
---|
80 | public: |
---|
81 | class base_iterator |
---|
82 | { |
---|
83 | public: |
---|
84 | /// @NOTE Do not use this class. For internal handling. |
---|
85 | class __base_iterator_base |
---|
86 | { |
---|
87 | public: |
---|
88 | virtual void operator++(int) = 0; |
---|
89 | virtual void operator++() = 0; |
---|
90 | virtual void operator--(int) = 0; |
---|
91 | virtual void operator--() = 0; |
---|
92 | virtual BaseObject* operator*() = 0; |
---|
93 | virtual __base_iterator_base& operator=(const base_iterator& iterator) = 0; |
---|
94 | virtual bool operator==(const __base_iterator_base& iterator) const = 0; |
---|
95 | virtual bool operator!=(const __base_iterator_base& iterator) const = 0; |
---|
96 | virtual __base_iterator_base* clone() const; |
---|
97 | }; |
---|
98 | |
---|
99 | public: |
---|
100 | base_iterator(__base_iterator_base& it) { _it = it.clone(); }; |
---|
101 | ~base_iterator() { delete _it; } |
---|
102 | void operator++() { (*_it)++; }; |
---|
103 | void operator++(int) { ++(*_it); }; |
---|
104 | void operator--() { (*_it)--; }; |
---|
105 | void operator--(int) { --(*_it); }; |
---|
106 | BaseObject* operator*() { return *(*_it); }; |
---|
107 | base_iterator& operator=(const base_iterator& iterator); |
---|
108 | bool operator==(const base_iterator& iter) const { return *this->_it == *iter._it; }; |
---|
109 | bool operator!=(const base_iterator& iter) const { return *this->_it != *iter._it; }; |
---|
110 | |
---|
111 | private: |
---|
112 | __base_iterator_base* _it; |
---|
113 | }; |
---|
114 | |
---|
115 | virtual base_iterator base_begin() const = 0; |
---|
116 | virtual base_iterator base_end() const = 0; |
---|
117 | |
---|
118 | |
---|
119 | protected: |
---|
120 | typedef std::map<int, NewObjectListBase*> classIDMap; //!< The Generic Map. |
---|
121 | typedef std::map<std::string, NewObjectListBase*> classNameMap; //!< The Generic Map. |
---|
122 | |
---|
123 | NewClassID _identity; //!< The Identity of the Class (ID and Name). |
---|
124 | |
---|
125 | private: |
---|
126 | static classIDMap* _classesByID; //!< A Map of all the classes in existance. |
---|
127 | static classNameMap* _classesByName; //!< A Map of all the classes in existance. |
---|
128 | static std::list<std::string> _classNames; //!< A list of all the registered ClassNames. |
---|
129 | }; |
---|
130 | |
---|
131 | |
---|
132 | ///////////////////////// |
---|
133 | //// TEMPLATISATION ///// |
---|
134 | ///////////////////////// |
---|
135 | //! Defines a ObjectsList handler for objects of type T. |
---|
136 | /** |
---|
137 | * To define a Class with a ObjectList, you have to: |
---|
138 | * 1. Include 'NewObjectListDeclaration(T);' in its Declaration (at the beginning) |
---|
139 | * 2. Include 'NewObjectListDefinition(T);' in some Definition file (cc-file) |
---|
140 | * 3. In the constructor add 'registerObject(this, objectList);' |
---|
141 | * |
---|
142 | * @note The Class must define the compare with const std::string& operator for this to work. |
---|
143 | */ |
---|
144 | template<class T> |
---|
145 | class NewObjectList : public NewObjectListBase |
---|
146 | { |
---|
147 | public: |
---|
148 | typedef std::list<T*> list; |
---|
149 | typedef typename list::iterator iterator; |
---|
150 | typedef typename list::const_iterator const_iterator; |
---|
151 | |
---|
152 | virtual base_iterator base_begin() const { return base_iterator(__base_iterator(this->_objects.begin())); }; |
---|
153 | virtual base_iterator base_end() const { return base_iterator(__base_iterator(this->_objects.end())); }; |
---|
154 | |
---|
155 | |
---|
156 | class Iterator : public NewObjectListBase::IteratorBase |
---|
157 | { |
---|
158 | public: |
---|
159 | Iterator(iterator it) { _it = it; } |
---|
160 | inline iterator& it() { return _it; } |
---|
161 | typename NewObjectList::iterator _it; |
---|
162 | }; |
---|
163 | |
---|
164 | public: |
---|
165 | NewObjectList(const std::string& name, int id = -1); |
---|
166 | ~NewObjectList(); |
---|
167 | |
---|
168 | T* getObject(const std::string& name) const; |
---|
169 | inline const list& objects() const { return _objects; }; |
---|
170 | |
---|
171 | inline iterator begin() { return _objects.begin(); }; |
---|
172 | inline const_iterator begin() const { return _objects.begin(); }; |
---|
173 | inline iterator end() { return _objects.end(); }; |
---|
174 | inline const_iterator end() const { return _objects.end(); }; |
---|
175 | |
---|
176 | inline bool empty() const { return _objects.empty(); }; |
---|
177 | inline int size() const { return _objects.size(); }; |
---|
178 | inline T* front() const { return _objects.front(); }; |
---|
179 | inline T* back() const { return _objects.back(); }; |
---|
180 | |
---|
181 | NewObjectListBase::IteratorBase* registerObject(T* object); |
---|
182 | void unregisterObject(IteratorBase* iterator); |
---|
183 | |
---|
184 | virtual void debug() const; |
---|
185 | |
---|
186 | protected: |
---|
187 | class __base_iterator : public base_iterator::__base_iterator_base |
---|
188 | { |
---|
189 | public: |
---|
190 | __base_iterator(const const_iterator& iter); |
---|
191 | void operator++(int) { ++_iter; }; |
---|
192 | void operator++() { _iter++; }; |
---|
193 | void operator--(int) { --_iter; }; |
---|
194 | void operator--() { _iter--; }; |
---|
195 | BaseObject* operator*() { return *iter; }; |
---|
196 | base_iterator::__base_iterator_base& operator=(const base_iterator& iterator) { _iter = iterator._iter; return *this; }; |
---|
197 | bool operator==(const __base_iterator_base& iterator) const { return _iter == iterator._iter; }; |
---|
198 | bool operator!=(const __base_iterator_base& iterator) const { return _iter != iterator._iter; }; |
---|
199 | private: |
---|
200 | iterator _iter; |
---|
201 | }; |
---|
202 | |
---|
203 | private: |
---|
204 | //! the copy constructor will be hidden. |
---|
205 | NewObjectList(const NewObjectList& definer) {}; |
---|
206 | |
---|
207 | private: |
---|
208 | list _objects; |
---|
209 | }; |
---|
210 | |
---|
211 | |
---|
212 | |
---|
213 | |
---|
214 | |
---|
215 | ///////////////////////// |
---|
216 | //// IMPLEMENTATION ///// |
---|
217 | ///////////////////////// |
---|
218 | /** |
---|
219 | * @brief creates a new NewObjectList |
---|
220 | * @param name The name of the Class. |
---|
221 | * @param id The ID of the class if desired, or -1 if an id should be assigned automatically. |
---|
222 | */ |
---|
223 | template <class T> |
---|
224 | NewObjectList<T>::NewObjectList(const std::string& name, int id) |
---|
225 | : NewObjectListBase(name, id) |
---|
226 | {} |
---|
227 | |
---|
228 | /** |
---|
229 | * @brief deletes the NewObjectList. |
---|
230 | */ |
---|
231 | template <class T> |
---|
232 | NewObjectList<T>::~NewObjectList() |
---|
233 | { |
---|
234 | if (!_objects.empty()) |
---|
235 | { |
---|
236 | std::cout << "There are still Object in the ObjectList of " << this->name() << "(id:" << this->id() << ")\n"; |
---|
237 | this->debug(); |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | template <class T> |
---|
242 | T* NewObjectList<T>::getObject(const std::string& name) const |
---|
243 | { |
---|
244 | const_iterator it; |
---|
245 | for (it = this->_objects.begin(); it != this->_objects.end(); ++it) |
---|
246 | if ((*it)->getName() == name) |
---|
247 | return (*it); |
---|
248 | return NULL; |
---|
249 | } |
---|
250 | |
---|
251 | template <class T> |
---|
252 | NewObjectListBase::IteratorBase* NewObjectList<T>::registerObject(T* object) |
---|
253 | { |
---|
254 | this->_objects.push_front(object); |
---|
255 | return new Iterator(this->_objects.begin()); |
---|
256 | } |
---|
257 | |
---|
258 | template <class T> |
---|
259 | void NewObjectList<T>::unregisterObject(IteratorBase* iterator) |
---|
260 | { |
---|
261 | this->_objects.erase(static_cast<Iterator*>(iterator)->it()); |
---|
262 | //_objects.erase(std::find(_objects.begin(), _objects.end(), object)); |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | template <class T> |
---|
267 | void NewObjectList<T>::debug() const |
---|
268 | { |
---|
269 | const_iterator it; |
---|
270 | for (it = this->_objects.begin(); it != this->_objects.end(); ++it) |
---|
271 | { |
---|
272 | std::cout << (*it)->getName() << std::endl; |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | #endif /* _NEW_OBJECT_LIST_H */ |
---|