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