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