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