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 | * @brief Use this macro to easily declare a Class to store its own ObjectListDeclaration |
---|
18 | * @param ClassName the Name of the Class. |
---|
19 | * @note: Using this inside of a Class means, that you loose the member: _objectList, while defining |
---|
20 | * two new functions objectList() and classID(). |
---|
21 | */ |
---|
22 | #define NewObjectListDeclaration(ClassName) \ |
---|
23 | public: \ |
---|
24 | static inline const NewObjectList<ClassName>& objectList() { return ClassName::_objectList; }; \ |
---|
25 | static inline const NewClassID& classID() { return ClassName::_objectList.identity(); }; \ |
---|
26 | private: \ |
---|
27 | static NewObjectList<ClassName> _objectList |
---|
28 | |
---|
29 | #define NewObjectListDefinitionID(ClassName, ID) \ |
---|
30 | NewObjectList<ClassName> ClassName::_objectList(#ClassName, ID) |
---|
31 | |
---|
32 | |
---|
33 | #define NewObjectListDefinition(ClassName) \ |
---|
34 | NewObjectListDefinitionID(ClassName, -1) |
---|
35 | |
---|
36 | class BaseObject; |
---|
37 | //! The superclass that all NewObjectLists follow. |
---|
38 | /** |
---|
39 | * @see template<class T> NewObjectList<T> |
---|
40 | */ |
---|
41 | class NewObjectListBase |
---|
42 | { |
---|
43 | public: |
---|
44 | //! A fast iterator Base-Class, for iterator-casting and storing. |
---|
45 | /** |
---|
46 | * @note This Iterator is explicitely used only for storage purposes in the BaseObject |
---|
47 | */ |
---|
48 | class IteratorBase { }; |
---|
49 | |
---|
50 | /** @brief A Typedefinition for the Base-List that can be retrieved with getBaseObjectList(base_list*) */ |
---|
51 | typedef std::list<BaseObject*> base_list; |
---|
52 | /** @brief An iterator for the base_list */ |
---|
53 | typedef base_list::iterator base_iterator; |
---|
54 | |
---|
55 | public: |
---|
56 | /** @returns The Identity of the Class stored within. */ |
---|
57 | inline const NewClassID& identity() const { return _identity; } |
---|
58 | /** @returns the ID of the Identity of the ObjectList */ |
---|
59 | inline int id() const { return _id; }; |
---|
60 | /** @returns The Name of the Class stored in this ObjectList */ |
---|
61 | inline const std::string& name() const { return _name; }; |
---|
62 | /** @param id The id to compare @returns true on match, false otherwise */ |
---|
63 | inline bool operator==(int id) const { return _id == id; }; |
---|
64 | /** @param id The id to compare @returns true on match, false otherwise */ |
---|
65 | inline bool operator==(const NewClassID& id) const { return id == _id; }; |
---|
66 | /** @param name The name to compare @returns true on match, false otherwise */ |
---|
67 | inline bool operator==(const std::string& name) const { return _name == name; }; |
---|
68 | /** @param id The id to check @returns true on match, false otherwise */ |
---|
69 | inline void acquireID(const int*& id, const std::string*& name) const { id = &_id; name = &_name; }; |
---|
70 | /** @brief fills a list of Objects into a BaseObject*-List. @param list the list to fill */ |
---|
71 | virtual void getBaseObjectList(base_list* list) const = 0; |
---|
72 | |
---|
73 | static const NewClassID& retrieveIdentity(int id); |
---|
74 | static const NewClassID& retrieveIdentity(const std::string& name); |
---|
75 | |
---|
76 | static const NewObjectListBase* const getObjectList(int classID); |
---|
77 | static const NewObjectListBase* const getObjectList(const std::string& className); |
---|
78 | static const NewObjectListBase* const getObjectList(const NewClassID& classID); |
---|
79 | |
---|
80 | static BaseObject* getBaseObject(int classID, const std::string& objectName); |
---|
81 | static BaseObject* getBaseObject(const std::string& className, const std::string& objectName); |
---|
82 | static BaseObject* getBaseObject(const NewClassID& classID, const std::string& objectName); |
---|
83 | |
---|
84 | /** @returns an Object with Name name out of this List @param name the name of the Object. */ |
---|
85 | virtual BaseObject* getBaseObject(const std::string& name) const = 0; |
---|
86 | |
---|
87 | static const std::list<std::string>& getClassNames(); |
---|
88 | |
---|
89 | |
---|
90 | static unsigned int classCount(); |
---|
91 | void debug(unsigned int level) const; |
---|
92 | static void debugAll(unsigned int level); |
---|
93 | |
---|
94 | static const std::string& IDToString(int classID); |
---|
95 | static int StringToID(const std::string& className); |
---|
96 | |
---|
97 | //! Only uset to unsubscribe a BaseObject. |
---|
98 | virtual void unregisterObject(IteratorBase* _iterators) = 0; |
---|
99 | |
---|
100 | protected: |
---|
101 | NewObjectListBase(const std::string& className, int id = -1); |
---|
102 | virtual ~NewObjectListBase(); |
---|
103 | |
---|
104 | private: |
---|
105 | NewObjectListBase(const NewObjectListBase&); |
---|
106 | |
---|
107 | static bool classIDExists(int id); |
---|
108 | static bool classNameExists(const std::string& className); |
---|
109 | |
---|
110 | |
---|
111 | protected: |
---|
112 | typedef std::map<int, NewObjectListBase*> classIDMap; //!< The Generic Map. |
---|
113 | typedef std::map<std::string, NewObjectListBase*> classNameMap; //!< The Generic Map. |
---|
114 | |
---|
115 | private: |
---|
116 | int _id; |
---|
117 | const std::string _name; //!< The Name of the Class. |
---|
118 | NewClassID _identity; //!< The Identity of the Class. (equal to _id and _name) |
---|
119 | |
---|
120 | private: |
---|
121 | static classIDMap* _classesByID; //!< A Map of all the classes in existance. |
---|
122 | static classNameMap* _classesByName; //!< A Map of all the classes in existance. |
---|
123 | static std::list<std::string> _classNames; //!< A list of all the registered ClassNames. |
---|
124 | }; |
---|
125 | |
---|
126 | |
---|
127 | ///////////////////////// |
---|
128 | //// TEMPLATISATION ///// |
---|
129 | ///////////////////////// |
---|
130 | //! Defines a ObjectsList handler for objects of type T. |
---|
131 | /** |
---|
132 | * The ObjectList is a generic way to store every object created from a Class 'T' |
---|
133 | * into a List. The list can be retrieved, and used constantly over iterators, |
---|
134 | * as with normal std::list. |
---|
135 | * |
---|
136 | * Furthermore the linkage over the single Lists is given over the Superclass NewObjectListBase. |
---|
137 | * |
---|
138 | * |
---|
139 | * |
---|
140 | * |
---|
141 | * To define a Class with a ObjectList, you have to: |
---|
142 | * 1. Include 'NewObjectListDeclaration(T);' in its Declaration (at the beginning) |
---|
143 | * 2. Include 'NewObjectListDefinition(T);' in some Definition file (cc-file) |
---|
144 | * 3. In the constructor add 'registerObject(this, objectList);' |
---|
145 | * |
---|
146 | * @note The Class must define the compare with const std::string& operator for this to work. |
---|
147 | * |
---|
148 | * |
---|
149 | * |
---|
150 | * Limitations: |
---|
151 | * ObjectList cannot be used with other Factory style Classes, if the class is also a BaseObject, |
---|
152 | * and not loaded before the ObjectList. |
---|
153 | * |
---|
154 | * @example Iterating: Iteration is made easy, and fast as follows: |
---|
155 | * for (NewObjectList<PlayerStats>::const_iterator it = PlayerStats::objectList().begin(); |
---|
156 | * it != PlayerStats::objectList().end(); |
---|
157 | * ++it) |
---|
158 | * { |
---|
159 | * (*it)->dosomething |
---|
160 | * } |
---|
161 | * |
---|
162 | * @example Find an Object: |
---|
163 | * Playable* playable = Playable::objectList("orxonox-super-rocket-fighter"); // searches an Object By its name. |
---|
164 | * |
---|
165 | */ |
---|
166 | template<class T> |
---|
167 | class NewObjectList : public NewObjectListBase |
---|
168 | { |
---|
169 | public: |
---|
170 | typedef std::list<T*> list; |
---|
171 | typedef typename list::iterator iterator; |
---|
172 | typedef typename list::const_iterator const_iterator; |
---|
173 | |
---|
174 | |
---|
175 | class Iterator : public NewObjectListBase::IteratorBase |
---|
176 | { |
---|
177 | public: |
---|
178 | Iterator(iterator it) { _it = it; } |
---|
179 | inline iterator& it() { return _it; } |
---|
180 | typename NewObjectList::iterator _it; |
---|
181 | }; |
---|
182 | |
---|
183 | public: |
---|
184 | NewObjectList(const std::string& name, int id = -1); |
---|
185 | ~NewObjectList(); |
---|
186 | |
---|
187 | virtual BaseObject* getBaseObject(const std::string& name) const; |
---|
188 | T* getObject(const std::string& name) const; |
---|
189 | inline const list& objects() const { return _objects; }; |
---|
190 | bool exists(const T* const object) const; |
---|
191 | |
---|
192 | /** @returns an Iterator to the beginning of the List. */ |
---|
193 | inline iterator begin() { return _objects.begin(); }; |
---|
194 | /** @returns a constant Iterator to the beginning of the List. */ |
---|
195 | inline const_iterator begin() const { return _objects.begin(); }; |
---|
196 | /** @returns an Iterator to the end of the List. */ |
---|
197 | inline iterator end() { return _objects.end(); }; |
---|
198 | /** @returns a constant Iterator to the beginning of the List. */ |
---|
199 | inline const_iterator end() const { return _objects.end(); }; |
---|
200 | |
---|
201 | /** @returns true if the List is empty. */ |
---|
202 | inline bool empty() const { return _objects.empty(); }; |
---|
203 | /** @returns the size of the List */ |
---|
204 | inline int size() const { return _objects.size(); }; |
---|
205 | /** @returns the frontmost Element of the list (normaly the last added Object). */ |
---|
206 | inline T* front() const { return _objects.front(); }; |
---|
207 | /** @returns the last added Object */ |
---|
208 | inline T* back() const { return _objects.back(); }; |
---|
209 | |
---|
210 | |
---|
211 | NewObjectListBase::IteratorBase* registerObject(T* object); |
---|
212 | virtual void unregisterObject(IteratorBase* iterator); |
---|
213 | |
---|
214 | protected: |
---|
215 | virtual void getBaseObjectList(NewObjectListBase::base_list* list) const; |
---|
216 | |
---|
217 | |
---|
218 | private: |
---|
219 | //! the copy constructor will be hidden. |
---|
220 | NewObjectList(const NewObjectList& definer) {}; |
---|
221 | |
---|
222 | private: |
---|
223 | list _objects; //!< The List of stored Objects of Type T. |
---|
224 | }; |
---|
225 | |
---|
226 | |
---|
227 | |
---|
228 | |
---|
229 | |
---|
230 | ///////////////////////// |
---|
231 | //// IMPLEMENTATION ///// |
---|
232 | ///////////////////////// |
---|
233 | /** |
---|
234 | * @brief creates a new NewObjectList |
---|
235 | * @param name The name of the Class. |
---|
236 | * @param id The ID of the class if desired, or -1 if an id should be assigned automatically. |
---|
237 | */ |
---|
238 | template <class T> |
---|
239 | NewObjectList<T>::NewObjectList(const std::string& name, int id) |
---|
240 | : NewObjectListBase(name, id) |
---|
241 | {} |
---|
242 | |
---|
243 | /** |
---|
244 | * @brief deletes the NewObjectList. |
---|
245 | */ |
---|
246 | template <class T> |
---|
247 | NewObjectList<T>::~NewObjectList() |
---|
248 | { |
---|
249 | if (!_objects.empty()) |
---|
250 | { |
---|
251 | // std::cout << "There are " << this->size() << " objects from class " << this->name() << "(id:" << this->id() << ") in existance\n"; |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * @brief Retrieves a BaseObject matching the Name name in this List. |
---|
257 | * @param name the Name of the Object. |
---|
258 | * @returns a BaseObject pointing to the object if found, NULL otherwise. |
---|
259 | */ |
---|
260 | template <class T> |
---|
261 | BaseObject* NewObjectList<T>::getBaseObject(const std::string& name) const |
---|
262 | { |
---|
263 | return this->getObject(name); |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | |
---|
268 | /** |
---|
269 | * @brief Retrieves an Object of type T matching the Name name in this List. |
---|
270 | * @param name the Name of the Object. |
---|
271 | * @returns an Object of type T pointing to the object if found, NULL otherwise. |
---|
272 | */ |
---|
273 | template <class T> |
---|
274 | T* NewObjectList<T>::getObject(const std::string& name) const |
---|
275 | { |
---|
276 | const_iterator it; |
---|
277 | for (it = this->_objects.begin(); it != this->_objects.end(); ++it) |
---|
278 | if ((*it)->getName() == name) |
---|
279 | return (*it); |
---|
280 | return NULL; |
---|
281 | } |
---|
282 | |
---|
283 | /** |
---|
284 | * @brief checks if Object object exists in this ClassList. |
---|
285 | * @param object the Object to check for |
---|
286 | * @returns True if the object is found within the List, false otherwise |
---|
287 | */ |
---|
288 | template <class T> |
---|
289 | bool NewObjectList<T>::exists(const T* const object) const |
---|
290 | { |
---|
291 | return (std::find(_objects.begin(), _objects.end(), object) != _objects.end()); |
---|
292 | } |
---|
293 | |
---|
294 | |
---|
295 | /** |
---|
296 | * @brief retrieves a List of BaseObjects |
---|
297 | * @param list the list to push the ObjectList into. |
---|
298 | */ |
---|
299 | template <class T> |
---|
300 | void NewObjectList<T>::getBaseObjectList(NewObjectListBase::base_list* list) const |
---|
301 | { |
---|
302 | assert (list != NULL); |
---|
303 | const_iterator it; |
---|
304 | for (it = this->_objects.begin(); it != this->_objects.end(); ++it) |
---|
305 | list->push_back(*it); |
---|
306 | } |
---|
307 | |
---|
308 | |
---|
309 | /** |
---|
310 | * @brief registers an Object to the NewObjectList. |
---|
311 | * @param T object the Object to register. |
---|
312 | * @returns a pointer to the iterator inside of the list. |
---|
313 | */ |
---|
314 | template <class T> |
---|
315 | NewObjectListBase::IteratorBase* NewObjectList<T>::registerObject(T* object) |
---|
316 | { |
---|
317 | this->_objects.push_back(object); |
---|
318 | return new Iterator(--this->_objects.end()); |
---|
319 | } |
---|
320 | |
---|
321 | /** |
---|
322 | * @brief removes an Object from the ClassList. |
---|
323 | * @param iterator the Position at which to remove the Object. |
---|
324 | */ |
---|
325 | template <class T> |
---|
326 | void NewObjectList<T>::unregisterObject(IteratorBase* iterator) |
---|
327 | { |
---|
328 | this->_objects.erase(static_cast<Iterator*>(iterator)->it()); |
---|
329 | //_objects.erase(std::find(_objects.begin(), _objects.end(), object)); |
---|
330 | } |
---|
331 | |
---|
332 | #endif /* _NEW_OBJECT_LIST_H */ |
---|