[9598] | 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 | /** |
---|
| 30 | @file |
---|
| 31 | @ingroup Object ObjectList |
---|
| 32 | @brief Definition of the IteratorBase class, used to iterate through object-lists. |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | #ifndef _IteratorBase_H__ |
---|
| 36 | #define _IteratorBase_H__ |
---|
| 37 | |
---|
| 38 | #include "core/CorePrereqs.h" |
---|
| 39 | |
---|
| 40 | #include "ObjectListBase.h" |
---|
| 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
| 44 | /** |
---|
| 45 | @brief The Iterator allows to iterate through object lists. |
---|
| 46 | It serves as base class for @ref ObjectListIterator and @ref Iterator |
---|
| 47 | */ |
---|
| 48 | template <class T, class I> |
---|
[9599] | 49 | class IteratorBase : public ObjectListElementRemovalListener |
---|
[9598] | 50 | { |
---|
[11071] | 51 | static_assert(std::is_base_of<Listable, T>::value, "IteratorBase can only be used with Listables"); |
---|
[10624] | 52 | |
---|
[9598] | 53 | public: |
---|
| 54 | /** |
---|
[10624] | 55 | @brief Constructor: Sets the element, whereon the iterator points, to the given element. |
---|
[9598] | 56 | */ |
---|
[11071] | 57 | inline IteratorBase(ObjectListElement<T>* element = nullptr) |
---|
[9598] | 58 | { |
---|
| 59 | this->element_ = element; |
---|
| 60 | this->registerIterator(); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
[10624] | 64 | @brief Constructor: Sets the element, whereon the iterator points, to the given element of another type. |
---|
| 65 | The element's type O must be a derivative of the Iterator's type T. |
---|
| 66 | */ |
---|
[11071] | 67 | template <class OT, class OI> |
---|
| 68 | inline IteratorBase(const IteratorBase<OT, OI>& other) |
---|
[10624] | 69 | { |
---|
[11071] | 70 | this->element_ = other.getElement(); |
---|
[10624] | 71 | this->registerIterator(); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /** |
---|
[9598] | 75 | @brief Unregisters the Iterator from the ObjectList. |
---|
| 76 | */ |
---|
| 77 | inline ~IteratorBase() |
---|
| 78 | { |
---|
| 79 | this->unregisterIterator(); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | @brief Assigns a given element. |
---|
| 84 | @param element The element |
---|
| 85 | */ |
---|
| 86 | inline IteratorBase<T, I>& operator=(ObjectListElement<T>* element) |
---|
| 87 | { |
---|
| 88 | this->setElement(element); |
---|
| 89 | return (*this); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | @brief Assigns the element of another Iterator. |
---|
| 94 | @param other The other Iterator |
---|
| 95 | */ |
---|
| 96 | inline IteratorBase<T, I>& operator=(const IteratorBase<T, I>& other) |
---|
| 97 | { |
---|
| 98 | this->setElement(other.element_); |
---|
| 99 | return (*this); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | /** |
---|
| 103 | @brief Overloading of the ++it operator: Iterator points to the next object in the list. |
---|
| 104 | @return The Iterator itself |
---|
| 105 | */ |
---|
| 106 | inline const IteratorBase<T, I>& operator++() |
---|
| 107 | { |
---|
| 108 | this->element_ = this->element_->next_; |
---|
| 109 | return *this; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | @brief Overloading of the it++ operator: Iterator points to the next object in the list. |
---|
| 114 | @return The Iterator itself |
---|
| 115 | */ |
---|
| 116 | inline I operator++(int) |
---|
| 117 | { |
---|
| 118 | I copy = *this; |
---|
| 119 | this->element_ = this->element_->next_; |
---|
| 120 | return copy; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | @brief Overloading of the --it operator: Iterator points to the previous object in the list. |
---|
| 125 | @return The Iterator itself |
---|
| 126 | */ |
---|
| 127 | inline const IteratorBase<T, I>& operator--() |
---|
| 128 | { |
---|
| 129 | this->element_ = this->element_->prev_; |
---|
| 130 | return *this; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | @brief Overloading of the it-- operator: Iterator points to the previous object in the list. |
---|
| 135 | @return The Iterator itself |
---|
| 136 | */ |
---|
| 137 | inline I operator--(int i) |
---|
| 138 | { |
---|
| 139 | I copy = *this; |
---|
| 140 | this->element_ = this->element_->prev_; |
---|
| 141 | return copy; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /** |
---|
| 145 | @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object. |
---|
| 146 | @return True if the Iterator points to an existing object. |
---|
| 147 | */ |
---|
[11071] | 148 | inline explicit operator bool() const |
---|
[9598] | 149 | { |
---|
[11071] | 150 | return (this->element_ != nullptr); |
---|
[9598] | 151 | } |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | @brief Overloading of the == operator to compare with another Iterator. |
---|
| 155 | @param compare The other Iterator |
---|
| 156 | @return True if the iterators point to the same element |
---|
| 157 | */ |
---|
| 158 | inline bool operator==(const IteratorBase<T, I>& compare) const |
---|
| 159 | { |
---|
| 160 | return (this->element_ == compare.element_); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | /** |
---|
| 164 | @brief Overloading of the != operator to compare with another Iterator. |
---|
| 165 | @param compare The other Iterator |
---|
| 166 | @return True if the iterators point to different elements |
---|
| 167 | */ |
---|
| 168 | inline bool operator!=(const IteratorBase<T, I>& compare) const |
---|
| 169 | { |
---|
| 170 | return (this->element_ != compare.element_); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | /** |
---|
[9599] | 174 | @brief Increments the Iterator if it points at the given element. |
---|
[11099] | 175 | @param element The element to compare with |
---|
[9598] | 176 | */ |
---|
[11071] | 177 | virtual void removedElement(ObjectListBaseElement* element) override |
---|
[9598] | 178 | { |
---|
[9599] | 179 | if (this->element_ == element) |
---|
[9598] | 180 | this->operator++(); |
---|
| 181 | } |
---|
| 182 | |
---|
[11071] | 183 | inline ObjectListBaseElement* getElement() const |
---|
| 184 | { |
---|
| 185 | return this->element_; |
---|
| 186 | } |
---|
| 187 | |
---|
[9598] | 188 | protected: |
---|
| 189 | inline void setElement(ObjectListBaseElement* element) |
---|
| 190 | { |
---|
| 191 | this->unregisterIterator(); |
---|
| 192 | this->element_ = element; |
---|
| 193 | this->registerIterator(); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /** |
---|
| 197 | * @brief Registers the Iterator at the list to which it belongs |
---|
| 198 | */ |
---|
| 199 | inline void registerIterator() |
---|
| 200 | { |
---|
| 201 | if (this->element_) |
---|
| 202 | { |
---|
| 203 | this->list_ = this->element_->list_; |
---|
[9599] | 204 | this->list_->registerRemovalListener(this); |
---|
[9598] | 205 | } |
---|
| 206 | else |
---|
[11071] | 207 | this->list_ = nullptr; |
---|
[9598] | 208 | } |
---|
| 209 | |
---|
| 210 | /** |
---|
| 211 | * @brief Unregisters the Iterator from the list (if any) |
---|
| 212 | */ |
---|
| 213 | inline void unregisterIterator() |
---|
| 214 | { |
---|
| 215 | if (this->list_) |
---|
[9599] | 216 | this->list_->unregisterRemovalListener(this); |
---|
[9598] | 217 | } |
---|
| 218 | |
---|
| 219 | ObjectListBaseElement* element_; //!< The element the Iterator points at |
---|
| 220 | ObjectListBase* list_; //!< The list in which the Iterator registered itself |
---|
| 221 | }; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | #endif /* _IteratorBase_H__ */ |
---|