[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 | |
---|
| 56 | protected: |
---|
| 57 | /** |
---|
| 58 | @brief Constructor: Sets the element, whereon the iterator points, to the given element. |
---|
| 59 | This constructor is protected and only for internal usage (don't mess with the BaseElements directly). |
---|
| 60 | */ |
---|
| 61 | inline IteratorBase(ObjectListBaseElement* element = NULL) |
---|
| 62 | { |
---|
| 63 | this->element_ = element; |
---|
| 64 | this->registerIterator(); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | |
---|
[9598] | 68 | public: |
---|
| 69 | /** |
---|
[10624] | 70 | @brief Constructor: Sets the element, whereon the iterator points, to the given element. |
---|
[9598] | 71 | */ |
---|
[10624] | 72 | inline IteratorBase(ObjectListElement<T>* element) |
---|
[9598] | 73 | { |
---|
| 74 | this->element_ = element; |
---|
| 75 | this->registerIterator(); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
[10624] | 79 | @brief Constructor: Sets the element, whereon the iterator points, to the given element of another type. |
---|
| 80 | The element's type O must be a derivative of the Iterator's type T. |
---|
| 81 | */ |
---|
| 82 | template <class O> |
---|
| 83 | inline IteratorBase(ObjectListElement<O>* element) |
---|
| 84 | { |
---|
| 85 | (void)static_cast<T*>((O*)NULL); // Check type: The element's type O must be a derivative of the Iterator's type T. |
---|
| 86 | this->element_ = element; |
---|
| 87 | this->registerIterator(); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /** |
---|
[9598] | 91 | @brief Constructor: Sets this element to the element of another Iterator. |
---|
| 92 | @param other The other Iterator |
---|
| 93 | */ |
---|
| 94 | inline IteratorBase(const IteratorBase& other) |
---|
| 95 | { |
---|
| 96 | this->element_ = other.element_; |
---|
| 97 | this->registerIterator(); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | @brief Unregisters the Iterator from the ObjectList. |
---|
| 102 | */ |
---|
| 103 | inline ~IteratorBase() |
---|
| 104 | { |
---|
| 105 | this->unregisterIterator(); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | @brief Assigns a given element. |
---|
| 110 | @param element The element |
---|
| 111 | */ |
---|
| 112 | inline IteratorBase<T, I>& operator=(ObjectListElement<T>* element) |
---|
| 113 | { |
---|
| 114 | this->setElement(element); |
---|
| 115 | return (*this); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | @brief Assigns the element of another Iterator. |
---|
| 120 | @param other The other Iterator |
---|
| 121 | */ |
---|
| 122 | inline IteratorBase<T, I>& operator=(const IteratorBase<T, I>& other) |
---|
| 123 | { |
---|
| 124 | this->setElement(other.element_); |
---|
| 125 | return (*this); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | @brief Overloading of the ++it operator: Iterator points to the next object in the list. |
---|
| 130 | @return The Iterator itself |
---|
| 131 | */ |
---|
| 132 | inline const IteratorBase<T, I>& operator++() |
---|
| 133 | { |
---|
| 134 | this->element_ = this->element_->next_; |
---|
| 135 | return *this; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | @brief Overloading of the it++ operator: Iterator points to the next object in the list. |
---|
| 140 | @return The Iterator itself |
---|
| 141 | */ |
---|
| 142 | inline I operator++(int) |
---|
| 143 | { |
---|
| 144 | I copy = *this; |
---|
| 145 | this->element_ = this->element_->next_; |
---|
| 146 | return copy; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /** |
---|
| 150 | @brief Overloading of the --it operator: Iterator points to the previous object in the list. |
---|
| 151 | @return The Iterator itself |
---|
| 152 | */ |
---|
| 153 | inline const IteratorBase<T, I>& operator--() |
---|
| 154 | { |
---|
| 155 | this->element_ = this->element_->prev_; |
---|
| 156 | return *this; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
| 160 | @brief Overloading of the it-- operator: Iterator points to the previous object in the list. |
---|
| 161 | @return The Iterator itself |
---|
| 162 | */ |
---|
| 163 | inline I operator--(int i) |
---|
| 164 | { |
---|
| 165 | I copy = *this; |
---|
| 166 | this->element_ = this->element_->prev_; |
---|
| 167 | return copy; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | /** |
---|
| 171 | @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object. |
---|
| 172 | @return True if the Iterator points to an existing object. |
---|
| 173 | */ |
---|
| 174 | inline operator bool() const |
---|
| 175 | { |
---|
| 176 | return (this->element_ != NULL); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | @brief Overloading of the == operator to compare with another Iterator. |
---|
| 181 | @param compare The other Iterator |
---|
| 182 | @return True if the iterators point to the same element |
---|
| 183 | */ |
---|
| 184 | inline bool operator==(const IteratorBase<T, I>& compare) const |
---|
| 185 | { |
---|
| 186 | return (this->element_ == compare.element_); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | /** |
---|
| 190 | @brief Overloading of the != operator to compare with another Iterator. |
---|
| 191 | @param compare The other Iterator |
---|
| 192 | @return True if the iterators point to different elements |
---|
| 193 | */ |
---|
| 194 | inline bool operator!=(const IteratorBase<T, I>& compare) const |
---|
| 195 | { |
---|
| 196 | return (this->element_ != compare.element_); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | /** |
---|
[9599] | 200 | @brief Increments the Iterator if it points at the given element. |
---|
[9598] | 201 | @param object The object to compare with |
---|
| 202 | */ |
---|
[9599] | 203 | virtual void removedElement(ObjectListBaseElement* element) |
---|
[9598] | 204 | { |
---|
[9599] | 205 | if (this->element_ == element) |
---|
[9598] | 206 | this->operator++(); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | protected: |
---|
| 210 | inline void setElement(ObjectListBaseElement* element) |
---|
| 211 | { |
---|
| 212 | this->unregisterIterator(); |
---|
| 213 | this->element_ = element; |
---|
| 214 | this->registerIterator(); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | /** |
---|
| 218 | * @brief Registers the Iterator at the list to which it belongs |
---|
| 219 | */ |
---|
| 220 | inline void registerIterator() |
---|
| 221 | { |
---|
| 222 | if (this->element_) |
---|
| 223 | { |
---|
| 224 | this->list_ = this->element_->list_; |
---|
[9599] | 225 | this->list_->registerRemovalListener(this); |
---|
[9598] | 226 | } |
---|
| 227 | else |
---|
| 228 | this->list_ = NULL; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | /** |
---|
| 232 | * @brief Unregisters the Iterator from the list (if any) |
---|
| 233 | */ |
---|
| 234 | inline void unregisterIterator() |
---|
| 235 | { |
---|
| 236 | if (this->list_) |
---|
[9599] | 237 | this->list_->unregisterRemovalListener(this); |
---|
[9598] | 238 | } |
---|
| 239 | |
---|
| 240 | ObjectListBaseElement* element_; //!< The element the Iterator points at |
---|
| 241 | ObjectListBase* list_; //!< The list in which the Iterator registered itself |
---|
| 242 | }; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | #endif /* _IteratorBase_H__ */ |
---|