[1574] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
[2171] | 30 | @file |
---|
[7401] | 31 | @ingroup Object ObjectList |
---|
| 32 | @brief Declaration of the ObjectListBase class which stores all objects of each class. |
---|
[1574] | 33 | |
---|
[7401] | 34 | orxonox::ObjectListBase is a double-linked list, used by @ref orxonox::Identifier "Identifiers" |
---|
| 35 | to store all objects of a given class. Newly created objects are added to the list through the |
---|
| 36 | @c RegisterObject() macro in the constructor. |
---|
[1574] | 37 | */ |
---|
| 38 | |
---|
| 39 | #ifndef _ObjectListBase_H__ |
---|
| 40 | #define _ObjectListBase_H__ |
---|
| 41 | |
---|
[9557] | 42 | #include "core/CorePrereqs.h" |
---|
[2784] | 43 | #include <vector> |
---|
[1574] | 44 | |
---|
| 45 | namespace orxonox |
---|
| 46 | { |
---|
| 47 | // ############################### |
---|
| 48 | // ### ObjectListBaseElement ### |
---|
| 49 | // ############################### |
---|
[7401] | 50 | /// The list-element of the ObjectListBase |
---|
[1574] | 51 | class _CoreExport ObjectListBaseElement |
---|
| 52 | { |
---|
| 53 | public: |
---|
| 54 | /** |
---|
| 55 | @brief Constructor: Creates the list-element with an object. |
---|
[7401] | 56 | @param objectBase The object to store |
---|
[1574] | 57 | */ |
---|
[9604] | 58 | ObjectListBaseElement(Listable* object) : next_(0), prev_(0), objectBase_(object), list_(0) {} |
---|
[9596] | 59 | ~ObjectListBaseElement(); |
---|
[1574] | 60 | |
---|
| 61 | ObjectListBaseElement* next_; //!< The next element in the list |
---|
| 62 | ObjectListBaseElement* prev_; //!< The previous element in the list |
---|
[9596] | 63 | Listable* objectBase_; //!< The object |
---|
| 64 | ObjectListBase* list_; //!< The list |
---|
[1574] | 65 | }; |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | // ############################### |
---|
[1591] | 69 | // ### ObjectListElement ### |
---|
| 70 | // ############################### |
---|
[7401] | 71 | /// The list-element that actually contains the object |
---|
[1591] | 72 | template <class T> |
---|
| 73 | class ObjectListElement : public ObjectListBaseElement |
---|
| 74 | { |
---|
| 75 | public: |
---|
[9604] | 76 | ObjectListElement(T* object) : ObjectListBaseElement(static_cast<Listable*>(object)), object_(object) {} |
---|
[1591] | 77 | T* object_; //!< The object |
---|
| 78 | }; |
---|
| 79 | |
---|
| 80 | |
---|
[9599] | 81 | // ######################################## |
---|
| 82 | // ### ObjectListElementRemovalListener ### |
---|
| 83 | // ######################################## |
---|
| 84 | /// Gets called by the object list if an element is removed |
---|
| 85 | class _CoreExport ObjectListElementRemovalListener |
---|
| 86 | { |
---|
| 87 | public: |
---|
| 88 | virtual ~ObjectListElementRemovalListener() {} |
---|
| 89 | virtual void removedElement(ObjectListBaseElement* element) = 0; |
---|
| 90 | }; |
---|
| 91 | |
---|
[1591] | 92 | // ############################### |
---|
[1574] | 93 | // ### ObjectListBase ### |
---|
| 94 | // ############################### |
---|
| 95 | /** |
---|
[7401] | 96 | @brief The ObjectListBase contains all objects of a given class. |
---|
| 97 | |
---|
| 98 | The ObjectListBase is used by Identifiers to store all objects of their class. |
---|
| 99 | You can use Identifier::getObjects() to get the object-list from an Identifier. |
---|
| 100 | Use @ref Iterator "Iterator<T>" to iterate through them. |
---|
| 101 | |
---|
| 102 | Alternatively you can also use the static helper class @ref orxonox::ObjectList "ObjectList<T>" |
---|
| 103 | to get the list of all objects of type @a T. Use @ref ObjectListIterator "ObjectListIterator<T>" |
---|
| 104 | or @ref Iterator "Iterator<T>" to iterate through them. |
---|
[1574] | 105 | */ |
---|
| 106 | class _CoreExport ObjectListBase |
---|
| 107 | { |
---|
| 108 | public: |
---|
[9593] | 109 | ObjectListBase(); |
---|
[1574] | 110 | ~ObjectListBase(); |
---|
| 111 | |
---|
[9593] | 112 | template <class T> |
---|
| 113 | inline ObjectListBaseElement* add(T* object) |
---|
[9597] | 114 | { |
---|
[9604] | 115 | ObjectListBaseElement* element = new ObjectListElement<T>(object); |
---|
[9597] | 116 | this->addElement(element); |
---|
| 117 | return element; |
---|
| 118 | } |
---|
[1574] | 119 | |
---|
[9597] | 120 | void addElement(ObjectListBaseElement* element); |
---|
[9593] | 121 | void removeElement(ObjectListBaseElement* element); |
---|
| 122 | |
---|
[9604] | 123 | size_t size() const { return this->size_; } |
---|
| 124 | |
---|
[7401] | 125 | /// Returns a pointer to the first element in the list. Works only with Iterator. |
---|
[9596] | 126 | inline ObjectListBaseElement* begin() { return this->first_; } |
---|
[7401] | 127 | /// Returns a pointer to the element after the last element in the list. Works only with Iterator. |
---|
[9596] | 128 | inline ObjectListBaseElement* end() { return 0; } |
---|
[7401] | 129 | /// Returns a pointer to the last element in the list. Works only with Iterator. |
---|
[9596] | 130 | inline ObjectListBaseElement* rbegin() { return this->last_; } |
---|
[7401] | 131 | /// Returns a pointer to the element in front of the first element in the list. Works only with Iterator. |
---|
[9596] | 132 | inline ObjectListBaseElement* rend() { return 0; } |
---|
[1574] | 133 | |
---|
[9599] | 134 | inline void registerRemovalListener(ObjectListElementRemovalListener* listener) { this->listeners_.push_back(listener); } |
---|
| 135 | inline void unregisterRemovalListener(ObjectListElementRemovalListener* listener) |
---|
[2784] | 136 | { |
---|
[9599] | 137 | for (unsigned int i = 0; i < this->listeners_.size(); ++i) |
---|
[2784] | 138 | { |
---|
[9599] | 139 | if (listeners_[i] == listener) |
---|
[2784] | 140 | { |
---|
[9599] | 141 | listeners_.erase(listeners_.begin() + i); |
---|
[2784] | 142 | break; |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | } |
---|
[1574] | 146 | |
---|
| 147 | private: |
---|
[9599] | 148 | void notifyRemovalListeners(ObjectListBaseElement* element) const; |
---|
| 149 | |
---|
| 150 | ObjectListBaseElement* first_; //!< The first element in the list |
---|
| 151 | ObjectListBaseElement* last_; //!< The last element in the list |
---|
[9604] | 152 | size_t size_; //!< The number of elements in the list |
---|
[9599] | 153 | std::vector<ObjectListElementRemovalListener*> listeners_; //!< A list of Iterators pointing on an element in this list |
---|
[1574] | 154 | }; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | #endif /* _ObjectListBase_H__ */ |
---|