[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> |
---|
[9667] | 44 | #include "Context.h" |
---|
[1574] | 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
| 48 | // ############################### |
---|
| 49 | // ### ObjectListBaseElement ### |
---|
| 50 | // ############################### |
---|
[7401] | 51 | /// The list-element of the ObjectListBase |
---|
[1574] | 52 | class _CoreExport ObjectListBaseElement |
---|
| 53 | { |
---|
| 54 | public: |
---|
| 55 | /** |
---|
| 56 | @brief Constructor: Creates the list-element with an object. |
---|
[11099] | 57 | @param object The object to store |
---|
[1574] | 58 | */ |
---|
[11071] | 59 | ObjectListBaseElement(Listable* object) : next_(nullptr), prev_(nullptr), objectBase_(object), list_(nullptr) {} |
---|
[9667] | 60 | virtual ~ObjectListBaseElement() { this->removeFromList(); } |
---|
[1574] | 61 | |
---|
[9667] | 62 | virtual void changeContext(Context* oldContext, Context* newContext) = 0; |
---|
| 63 | |
---|
[1574] | 64 | ObjectListBaseElement* next_; //!< The next element in the list |
---|
| 65 | ObjectListBaseElement* prev_; //!< The previous element in the list |
---|
[9667] | 66 | Listable* objectBase_; //!< The object |
---|
| 67 | ObjectListBase* list_; //!< The list |
---|
| 68 | |
---|
| 69 | protected: |
---|
| 70 | void removeFromList(); |
---|
[1574] | 71 | }; |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | // ############################### |
---|
[1591] | 75 | // ### ObjectListElement ### |
---|
| 76 | // ############################### |
---|
[7401] | 77 | /// The list-element that actually contains the object |
---|
[1591] | 78 | template <class T> |
---|
| 79 | class ObjectListElement : public ObjectListBaseElement |
---|
| 80 | { |
---|
| 81 | public: |
---|
[9573] | 82 | ObjectListElement(T* object) : ObjectListBaseElement(static_cast<Listable*>(object)), object_(object) {} |
---|
[9667] | 83 | |
---|
[11071] | 84 | virtual void changeContext(Context* oldContext, Context* newContext) override |
---|
[9667] | 85 | { |
---|
| 86 | // add object to new context, but only if this element belongs exactly to the old context (and not to a sub-context to avoid re-adding objects |
---|
| 87 | // multiple times if they are in multiple contexts) |
---|
| 88 | if (oldContext->getObjectList<T>() == this->list_) |
---|
| 89 | newContext->addObject(this->object_); |
---|
| 90 | |
---|
| 91 | // remove from old list |
---|
| 92 | this->removeFromList(); |
---|
| 93 | } |
---|
| 94 | |
---|
[1591] | 95 | T* object_; //!< The object |
---|
| 96 | }; |
---|
| 97 | |
---|
| 98 | |
---|
[9667] | 99 | // ######################################## |
---|
| 100 | // ### ObjectListElementRemovalListener ### |
---|
| 101 | // ######################################## |
---|
| 102 | /// Gets called by the object list if an element is removed |
---|
| 103 | class _CoreExport ObjectListElementRemovalListener |
---|
| 104 | { |
---|
| 105 | public: |
---|
[11071] | 106 | ObjectListElementRemovalListener() = default; |
---|
| 107 | virtual ~ObjectListElementRemovalListener() = default; |
---|
[9667] | 108 | virtual void removedElement(ObjectListBaseElement* element) = 0; |
---|
| 109 | }; |
---|
| 110 | |
---|
[1591] | 111 | // ############################### |
---|
[1574] | 112 | // ### ObjectListBase ### |
---|
| 113 | // ############################### |
---|
| 114 | /** |
---|
[7401] | 115 | @brief The ObjectListBase contains all objects of a given class. |
---|
| 116 | |
---|
| 117 | The ObjectListBase is used by Identifiers to store all objects of their class. |
---|
| 118 | You can use Identifier::getObjects() to get the object-list from an Identifier. |
---|
| 119 | Use @ref Iterator "Iterator<T>" to iterate through them. |
---|
| 120 | |
---|
| 121 | Alternatively you can also use the static helper class @ref orxonox::ObjectList "ObjectList<T>" |
---|
| 122 | to get the list of all objects of type @a T. Use @ref ObjectListIterator "ObjectListIterator<T>" |
---|
| 123 | or @ref Iterator "Iterator<T>" to iterate through them. |
---|
[1574] | 124 | */ |
---|
| 125 | class _CoreExport ObjectListBase |
---|
| 126 | { |
---|
| 127 | public: |
---|
[9593] | 128 | ObjectListBase(); |
---|
[1574] | 129 | ~ObjectListBase(); |
---|
| 130 | |
---|
[9667] | 131 | void addElement(ObjectListBaseElement* element); |
---|
[9593] | 132 | void removeElement(ObjectListBaseElement* element); |
---|
| 133 | |
---|
[9667] | 134 | size_t size() const { return this->size_; } |
---|
[1591] | 135 | |
---|
[7401] | 136 | /// Returns a pointer to the first element in the list. Works only with Iterator. |
---|
[9667] | 137 | inline ObjectListBaseElement* begin() const { return this->first_; } |
---|
[7401] | 138 | /// Returns a pointer to the element after the last element in the list. Works only with Iterator. |
---|
[11071] | 139 | inline ObjectListBaseElement* end() const { return nullptr; } |
---|
[7401] | 140 | /// Returns a pointer to the last element in the list. Works only with Iterator. |
---|
[9667] | 141 | inline ObjectListBaseElement* rbegin() const { return this->last_; } |
---|
[7401] | 142 | /// Returns a pointer to the element in front of the first element in the list. Works only with Iterator. |
---|
[11071] | 143 | inline ObjectListBaseElement* rend() const { return nullptr; } |
---|
[1574] | 144 | |
---|
[9667] | 145 | inline void registerRemovalListener(ObjectListElementRemovalListener* listener) { this->listeners_.push_back(listener); } |
---|
| 146 | inline void unregisterRemovalListener(ObjectListElementRemovalListener* listener) |
---|
[2784] | 147 | { |
---|
[9667] | 148 | for (unsigned int i = 0; i < this->listeners_.size(); ++i) |
---|
[2784] | 149 | { |
---|
[9667] | 150 | if (listeners_[i] == listener) |
---|
[2784] | 151 | { |
---|
[9667] | 152 | listeners_.erase(listeners_.begin() + i); |
---|
[2784] | 153 | break; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | } |
---|
[1574] | 157 | |
---|
| 158 | private: |
---|
[9667] | 159 | void notifyRemovalListeners(ObjectListBaseElement* element) const; |
---|
| 160 | |
---|
| 161 | ObjectListBaseElement* first_; //!< The first element in the list |
---|
| 162 | ObjectListBaseElement* last_; //!< The last element in the list |
---|
| 163 | size_t size_; //!< The number of elements in the list |
---|
| 164 | std::vector<ObjectListElementRemovalListener*> listeners_; //!< A list of Iterators pointing on an element in this list |
---|
[1574] | 165 | }; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | #endif /* _ObjectListBase_H__ */ |
---|