1 | /*! |
---|
2 | * @file object_list.h |
---|
3 | * @brief Definition of a dynamically allocating ClassID |
---|
4 | * |
---|
5 | */ |
---|
6 | |
---|
7 | #ifndef _OBJECT_LIST_H |
---|
8 | #define _OBJECT_LIST_H |
---|
9 | |
---|
10 | #include "class_id.h" |
---|
11 | #include <map> |
---|
12 | #include <list> |
---|
13 | #include <string> |
---|
14 | |
---|
15 | #include <cassert> |
---|
16 | #include <iostream> |
---|
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 ObjectListDeclaration(ClassName) \ |
---|
25 | public: \ |
---|
26 | static inline const ObjectList<ClassName>& objectList() { return ClassName::_objectList; }; \ |
---|
27 | static inline const ClassID& staticClassID() { return ClassName::_objectList.identity(); }; \ |
---|
28 | private: \ |
---|
29 | static ObjectList<ClassName> _objectList |
---|
30 | |
---|
31 | /** |
---|
32 | * @brief Use this macro to easily define a Class to store its own ObjectListDefinition |
---|
33 | * @param ClassName: the Name of the Class. |
---|
34 | * @param ID: optional set a Fixed ID. |
---|
35 | */ |
---|
36 | #define ObjectListDefinitionID(ClassName, ID) \ |
---|
37 | ObjectList<ClassName> ClassName::_objectList(#ClassName, ID) |
---|
38 | |
---|
39 | /** |
---|
40 | * @brief Use this macro to easily define a Class to store its own ObjectListDefinition |
---|
41 | * @param ClassName: the Name of the Class. |
---|
42 | */ |
---|
43 | #define ObjectListDefinition(ClassName) \ |
---|
44 | ObjectListDefinitionID(ClassName, -1) |
---|
45 | |
---|
46 | class BaseObject; |
---|
47 | //! The superclass that all ObjectLists follow. |
---|
48 | /** |
---|
49 | * @see template<class T> ObjectList<T> |
---|
50 | */ |
---|
51 | class ObjectListBase |
---|
52 | { |
---|
53 | public: |
---|
54 | /** @brief A Typedefinition for the Base-List that can be retrieved with getBaseObjectList(base_list*) */ |
---|
55 | typedef std::list<BaseObject*> base_list; |
---|
56 | /** @brief An iterator for the base_list */ |
---|
57 | typedef base_list::iterator base_iterator; |
---|
58 | //! A fast iterator Base-Class, for iterator-casting and storing. |
---|
59 | /** @note This Iterator is explicitely used only for storage purposes in the BaseObject */ |
---|
60 | class IteratorBase { }; |
---|
61 | |
---|
62 | public: |
---|
63 | /** @returns The Identity of the Class stored within. */ |
---|
64 | inline const ClassID& identity() const { return _identity; } |
---|
65 | /** @returns the ID of the Identity of the ObjectList */ |
---|
66 | inline int id() const { return _id; }; |
---|
67 | /** @returns The Name of the Class stored in this ObjectList */ |
---|
68 | inline const std::string& name() const { return _name; }; |
---|
69 | /** @param id The id to compare @returns true on match, false otherwise */ |
---|
70 | inline bool operator==(int id) const { return _id == id; }; |
---|
71 | /** @param id The id to compare @returns true on match, false otherwise */ |
---|
72 | inline bool operator==(const ClassID& id) const { return id == _id; }; |
---|
73 | /** @param name The name to compare @returns true on match, false otherwise */ |
---|
74 | inline bool operator==(const std::string& name) const { return _name == name; }; |
---|
75 | /** @param id The id to acquire @param name the Name to acquire @brief stores a Name and an ID in a pointer. */ |
---|
76 | inline void acquireID(const int*& id, const std::string*& name) const { id = &_id; name = &_name; }; |
---|
77 | /** @brief fills a list of Objects into a BaseObject*-List. @param list the list to fill */ |
---|
78 | virtual void getBaseObjectList(base_list* list) const = 0; |
---|
79 | |
---|
80 | static const ClassID& retrieveIdentity(int id); |
---|
81 | static const ClassID& retrieveIdentity(const std::string& name); |
---|
82 | |
---|
83 | static const ObjectListBase* const getObjectList(int classID); |
---|
84 | static const ObjectListBase* const getObjectList(const std::string& className); |
---|
85 | static const ObjectListBase* const getObjectList(const ClassID& classID); |
---|
86 | |
---|
87 | static BaseObject* getBaseObject(int classID, const std::string& objectName); |
---|
88 | static BaseObject* getBaseObject(const std::string& className, const std::string& objectName); |
---|
89 | static BaseObject* getBaseObject(const ClassID& classID, const std::string& objectName); |
---|
90 | |
---|
91 | /** @returns an Object with Name name out of this List @param name the name of the Object. */ |
---|
92 | virtual BaseObject* getBaseObject(const std::string& name) const = 0; |
---|
93 | |
---|
94 | static const std::list<std::string>& getClassNames(); |
---|
95 | |
---|
96 | |
---|
97 | static unsigned int classCount(); |
---|
98 | void debug(unsigned int level) const; |
---|
99 | static void debugAll(unsigned int level); |
---|
100 | |
---|
101 | static const std::string& IDToString(int classID); |
---|
102 | static int StringToID(const std::string& className); |
---|
103 | |
---|
104 | //! Only used to unsubscribe a BaseObject. (just try to make a valid iterator, stupid :)) |
---|
105 | virtual void unregisterObject(IteratorBase* _iterators) = 0; |
---|
106 | //! Only for debug purposes |
---|
107 | virtual bool checkIteratorInList(IteratorBase* _iterator) const = 0; |
---|
108 | virtual bool checkObjectInList(BaseObject* obj) const = 0; |
---|
109 | |
---|
110 | protected: |
---|
111 | ObjectListBase(const std::string& className, int id = -1); |
---|
112 | virtual ~ObjectListBase(); |
---|
113 | |
---|
114 | private: |
---|
115 | /** @brief hidden copy constructor. */ |
---|
116 | ObjectListBase(const ObjectListBase&) {}; |
---|
117 | |
---|
118 | static bool classIDExists(int id); |
---|
119 | static bool classNameExists(const std::string& className); |
---|
120 | |
---|
121 | |
---|
122 | protected: |
---|
123 | typedef std::map<int, ObjectListBase*> IDMap; //!< The Generic Map. |
---|
124 | typedef std::map<std::string, ObjectListBase*> NameMap; //!< The Generic Map. |
---|
125 | |
---|
126 | private: |
---|
127 | int _id; //!< The Unique ID of the Class. |
---|
128 | const std::string _name; //!< The Name of the Class. |
---|
129 | ClassID _identity; //!< The Identity of the Class. (equal to _id and _name) |
---|
130 | |
---|
131 | private: |
---|
132 | static IDMap* _classesByID; //!< A Map of all the classes in existance. |
---|
133 | static NameMap* _classesByName; //!< A Map of all the classes in existance. |
---|
134 | static std::list<std::string> _classNames; //!< A list of all the registered ClassNames. |
---|
135 | }; |
---|
136 | |
---|
137 | |
---|
138 | ///////////////////////// |
---|
139 | //// TEMPLATISATION ///// |
---|
140 | ///////////////////////// |
---|
141 | //! Defines a ObjectList handler for objects of type T. |
---|
142 | /** |
---|
143 | * The ObjectList is a generic way to store every object created from a Class 'T' |
---|
144 | * in a List. The list can be retrieved, and used constantly over iterators, |
---|
145 | * as with normal std::list. |
---|
146 | * |
---|
147 | * Furthermore the linkage over the single Lists is given over the Superclass ObjectListBase. |
---|
148 | * |
---|
149 | * The approach of ObjectList is an intrusive one, meaning, that each Class that wants to |
---|
150 | * support it must implement a Declaration and a Definition for the ObjectList, as mentioned |
---|
151 | * in the next statement: |
---|
152 | * |
---|
153 | * To define a Class with a ObjectList, you have to: |
---|
154 | * 1. Include 'ObjectListDeclaration(T);' in its Declaration (at the beginning) |
---|
155 | * 2. Include 'ObjectListDefinition(T);' in some Definition file (cc-file) |
---|
156 | * 3. In the constructor add 'registerObject(this, objectList);' |
---|
157 | * |
---|
158 | * @note The Class must define the compare with const std::string& operator for this to work. |
---|
159 | * The operator==(const std::string& name) should compare the object's name with name. |
---|
160 | * |
---|
161 | * |
---|
162 | * |
---|
163 | * Limitations: |
---|
164 | * ObjectList cannot be used with other Factory style Classes, if the class is also a BaseObject, |
---|
165 | * and not loaded before the ObjectList. |
---|
166 | * |
---|
167 | * example: Iterating: Iteration is made easy, and fast as follows: |
---|
168 | * for (ObjectList<PlayerStats>::const_iterator it = PlayerStats::objectList().begin(); |
---|
169 | * it != PlayerStats::objectList().end(); |
---|
170 | * ++it) |
---|
171 | * { |
---|
172 | * (*it)->dosomething |
---|
173 | * } |
---|
174 | * |
---|
175 | * example: Find an Object: |
---|
176 | * Playable* playable = Playable::objectList("orxonox-super-rocket-fighter"); // searches an Object By its name. |
---|
177 | * |
---|
178 | */ |
---|
179 | template<class T> |
---|
180 | class ObjectList : public ObjectListBase |
---|
181 | { |
---|
182 | public: |
---|
183 | typedef std::list<T*> list; //!< The list of Type T* (used for Objects in this ObjectList) |
---|
184 | typedef typename list::iterator iterator; //!< The iterator for the List of type T (use with ObjectList<Type>::iterator) |
---|
185 | typedef typename list::const_iterator const_iterator; //!< A constant iterator for the List of type T (use with ObjectList<Type>::const_iterator) |
---|
186 | |
---|
187 | |
---|
188 | //! An iterator to store Objects in the BaseObject, and remove Objects fast. |
---|
189 | class Iterator : public ObjectListBase::IteratorBase |
---|
190 | { |
---|
191 | public: |
---|
192 | /** @brief creates an Iterator fast. @param it the Iterator. */ |
---|
193 | inline Iterator(iterator it) { _it = it; } |
---|
194 | /** @returns the Iterator */ |
---|
195 | inline iterator& it() { return _it; } |
---|
196 | private: |
---|
197 | typename ObjectList::iterator _it; //!< Stored Iterator |
---|
198 | }; |
---|
199 | |
---|
200 | public: |
---|
201 | ObjectList(const std::string& name, int id = -1); |
---|
202 | ~ObjectList(); |
---|
203 | |
---|
204 | virtual BaseObject* getBaseObject(const std::string& name) const; |
---|
205 | T* getObject(const std::string& name) const; |
---|
206 | /** @returns A constant list of Objects of this Class. */ |
---|
207 | inline const list& objects() const { return _objects; }; |
---|
208 | bool exists(const T* const object) const; |
---|
209 | |
---|
210 | /** @returns an Iterator to the beginning of the List. */ |
---|
211 | inline iterator begin() { return _objects.begin(); }; |
---|
212 | /** @returns a constant Iterator to the beginning of the List. */ |
---|
213 | inline const_iterator begin() const { return _objects.begin(); }; |
---|
214 | /** @returns an Iterator to the end of the List. */ |
---|
215 | inline iterator end() { return _objects.end(); }; |
---|
216 | /** @returns a constant Iterator to the beginning of the List. */ |
---|
217 | inline const_iterator end() const { return _objects.end(); }; |
---|
218 | |
---|
219 | /** @returns true if the List is empty. */ |
---|
220 | inline bool empty() const { return _objects.empty(); }; |
---|
221 | /** @returns the size of the List */ |
---|
222 | inline int size() const { return _objects.size(); }; |
---|
223 | /** @returns the frontmost Element of the list (normaly the last added Object). */ |
---|
224 | inline T* front() const { return _objects.front(); }; |
---|
225 | /** @returns the last added Object */ |
---|
226 | inline T* back() const { return _objects.back(); }; |
---|
227 | |
---|
228 | |
---|
229 | ObjectListBase::IteratorBase* registerObject(T* object); |
---|
230 | virtual void unregisterObject(IteratorBase* iterator); |
---|
231 | bool checkIteratorInList(IteratorBase* iterator) const; |
---|
232 | bool checkObjectInList(BaseObject* obj) const; |
---|
233 | protected: |
---|
234 | virtual void getBaseObjectList(ObjectListBase::base_list* list) const; |
---|
235 | |
---|
236 | |
---|
237 | private: |
---|
238 | //! the copy constructor will be hidden. |
---|
239 | ObjectList(const ObjectList& definer) {}; |
---|
240 | |
---|
241 | private: |
---|
242 | list _objects; //!< The List of stored Objects of Type T. |
---|
243 | }; |
---|
244 | |
---|
245 | |
---|
246 | |
---|
247 | |
---|
248 | |
---|
249 | ///////////////////////// |
---|
250 | //// IMPLEMENTATION ///// |
---|
251 | ///////////////////////// |
---|
252 | /** |
---|
253 | * @brief creates a new ObjectList |
---|
254 | * @param name The name of the Class. |
---|
255 | * @param id The ID of the class if desired, or -1 if an id should be assigned automatically. |
---|
256 | */ |
---|
257 | template <class T> |
---|
258 | ObjectList<T>::ObjectList(const std::string& name, int id) |
---|
259 | : ObjectListBase(name, id) |
---|
260 | {} |
---|
261 | |
---|
262 | /** |
---|
263 | * @brief deletes the ObjectList. |
---|
264 | */ |
---|
265 | template <class T> |
---|
266 | ObjectList<T>::~ObjectList() |
---|
267 | { |
---|
268 | if (!_objects.empty()) |
---|
269 | { |
---|
270 | // std::cout << "There are " << this->size() << " objects from class " << this->name() << "(id:" << this->id() << ") in existance\n"; |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | /** |
---|
275 | * @brief Retrieves a BaseObject matching the Name name in this List. |
---|
276 | * @param name the Name of the Object. |
---|
277 | * @returns a BaseObject pointing to the object if found, NULL otherwise. |
---|
278 | */ |
---|
279 | template <class T> |
---|
280 | BaseObject* ObjectList<T>::getBaseObject(const std::string& name) const |
---|
281 | { |
---|
282 | return this->getObject(name); |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | |
---|
287 | /** |
---|
288 | * @brief Retrieves an Object of type T matching the Name name in this List. |
---|
289 | * @param name the Name of the Object. |
---|
290 | * @returns an Object of type T pointing to the object if found, NULL otherwise. |
---|
291 | */ |
---|
292 | template <class T> |
---|
293 | T* ObjectList<T>::getObject(const std::string& name) const |
---|
294 | { |
---|
295 | const_iterator it; |
---|
296 | for (it = this->_objects.begin(); it != this->_objects.end(); ++it) |
---|
297 | if ((*it)->getName() == name) |
---|
298 | return (*it); |
---|
299 | return NULL; |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | * @brief checks if Object object exists in this ClassList. |
---|
304 | * @param object the Object to check for |
---|
305 | * @returns True if the object is found within the List, false otherwise |
---|
306 | */ |
---|
307 | template <class T> |
---|
308 | bool ObjectList<T>::exists(const T* const object) const |
---|
309 | { |
---|
310 | return (std::find(_objects.begin(), _objects.end(), object) != _objects.end()); |
---|
311 | } |
---|
312 | |
---|
313 | |
---|
314 | /** |
---|
315 | * @brief retrieves a List of BaseObjects |
---|
316 | * @param list the list to push the ObjectList into. |
---|
317 | */ |
---|
318 | template <class T> |
---|
319 | void ObjectList<T>::getBaseObjectList(ObjectListBase::base_list* list) const |
---|
320 | { |
---|
321 | assert (list != NULL); |
---|
322 | const_iterator it; |
---|
323 | for (it = this->_objects.begin(); it != this->_objects.end(); ++it) |
---|
324 | list->push_back(*it); |
---|
325 | } |
---|
326 | |
---|
327 | |
---|
328 | /** |
---|
329 | * @brief registers an Object to the ObjectList. |
---|
330 | * @param object The Object to register. |
---|
331 | * @returns a pointer to the iterator inside of the list. |
---|
332 | */ |
---|
333 | template <class T> |
---|
334 | ObjectListBase::IteratorBase* ObjectList<T>::registerObject(T* object) |
---|
335 | { |
---|
336 | this->_objects.push_back(object); |
---|
337 | return new Iterator(--this->_objects.end()); |
---|
338 | } |
---|
339 | |
---|
340 | /** |
---|
341 | * @brief removes an Object from the ClassList. |
---|
342 | * @param iterator the Position at which to remove the Object. |
---|
343 | */ |
---|
344 | template <class T> |
---|
345 | void ObjectList<T>::unregisterObject(IteratorBase* iterator) |
---|
346 | { |
---|
347 | this->_objects.erase(static_cast<Iterator*>(iterator)->it()); |
---|
348 | //_objects.erase(std::find(_objects.begin(), _objects.end(), object)); |
---|
349 | } |
---|
350 | |
---|
351 | template <class T> |
---|
352 | bool ObjectList<T>::checkIteratorInList(IteratorBase* iterator) const |
---|
353 | { |
---|
354 | const_iterator it; |
---|
355 | for (it = this->_objects.begin(); it != this->_objects.end(); ++it) |
---|
356 | if (static_cast<Iterator*>(iterator)->it() == it) |
---|
357 | return true; |
---|
358 | printf("ObjectList:: checkIteratorInList:: ITERATOR NOT IN THE LIST '%s' (UN-SYNC SHOULD NOT HAPPEN!!)\n", this->name().c_str()); |
---|
359 | return false; |
---|
360 | } |
---|
361 | |
---|
362 | template <class T> |
---|
363 | bool ObjectList<T>::checkObjectInList(BaseObject* obj) const |
---|
364 | { |
---|
365 | T* object = dynamic_cast<T*>(obj); |
---|
366 | if (object != NULL) |
---|
367 | { |
---|
368 | printf("EXTREME BUG: Object does not exist anymore!! (or a bug in ObjectList)\n"); |
---|
369 | return false;; |
---|
370 | } |
---|
371 | |
---|
372 | const_iterator it; |
---|
373 | for (it = this->_objects.begin(); it != this->_objects.end(); ++it) |
---|
374 | { |
---|
375 | if (*it == object) |
---|
376 | return true; |
---|
377 | } |
---|
378 | printf("ObjectList:: checkObjectInList:: OBJECT NOT IN THE LIST '%s' (UN-SYNC SHOULD NOT HAPPEN!!)\n", this->name().c_str()); |
---|
379 | return false; |
---|
380 | } |
---|
381 | |
---|
382 | |
---|
383 | #endif /* _OBJECT_LIST_H */ |
---|