[1505] | 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 |
---|
[1505] | 31 | @brief Definition and implementation of the Iterator class. |
---|
| 32 | |
---|
[1747] | 33 | The Iterator of a given class allows to iterate through an ObjectList. Objects in |
---|
| 34 | this list are casted to the template argument of the Iterator. |
---|
[1505] | 35 | |
---|
| 36 | Usage: |
---|
[1854] | 37 | for (Iterator<myClass> it = anyidentifier->getObjects()->begin(); it != anyidentifier->getObjects()->end(); ++it) |
---|
[1505] | 38 | { |
---|
| 39 | it->someFunction(...); |
---|
[1747] | 40 | myClass* myObject = *it; |
---|
[1505] | 41 | } |
---|
| 42 | */ |
---|
| 43 | |
---|
| 44 | #ifndef _Iterator_H__ |
---|
| 45 | #define _Iterator_H__ |
---|
| 46 | |
---|
| 47 | #include "CorePrereqs.h" |
---|
| 48 | |
---|
[3196] | 49 | #include "Identifier.h" |
---|
[1747] | 50 | #include "ObjectListBase.h" |
---|
[1505] | 51 | |
---|
| 52 | namespace orxonox |
---|
| 53 | { |
---|
[1747] | 54 | //! The Iterator allows to iterate through a given ObjectList |
---|
| 55 | template <class T = OrxonoxClass> |
---|
[1505] | 56 | class Iterator |
---|
| 57 | { |
---|
| 58 | public: |
---|
| 59 | /** |
---|
| 60 | @brief Constructor: Sets the element, whereon the iterator points, to zero. |
---|
| 61 | */ |
---|
[1747] | 62 | inline Iterator() |
---|
[1505] | 63 | { |
---|
| 64 | this->element_ = 0; |
---|
[1747] | 65 | this->list_ = 0; |
---|
[1505] | 66 | } |
---|
| 67 | |
---|
| 68 | /** |
---|
[1747] | 69 | @brief Constructor: Sets this element to the exported element. |
---|
| 70 | @param exp The exported element |
---|
[1505] | 71 | */ |
---|
[1747] | 72 | inline Iterator(const ObjectListBase::Export& exp) |
---|
[1505] | 73 | { |
---|
[1747] | 74 | this->element_ = exp.element_; |
---|
| 75 | this->list_ = exp.list_; |
---|
[2784] | 76 | this->list_->registerIterator(this); |
---|
[1505] | 77 | } |
---|
| 78 | |
---|
| 79 | /** |
---|
[1747] | 80 | @brief Constructor: Sets this element to the element of another Iterator. |
---|
| 81 | @param other The other Iterator |
---|
| 82 | */ |
---|
| 83 | inline Iterator(const Iterator<T>& other) |
---|
| 84 | { |
---|
| 85 | this->element_ = other.element_; |
---|
| 86 | this->list_ = other.list_; |
---|
[2784] | 87 | this->list_->registerIterator(this); |
---|
[1747] | 88 | } |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | @brief Constructor: Sets this element to a given element |
---|
| 92 | @param element The element |
---|
| 93 | */ |
---|
| 94 | template <class O> |
---|
| 95 | inline Iterator(ObjectListElement<O>* element) |
---|
| 96 | { |
---|
[2662] | 97 | this->element_ = (element) ? static_cast<ObjectListBaseElement*>(element) : 0; |
---|
[1747] | 98 | this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects(); |
---|
[2784] | 99 | this->list_->registerIterator(this); |
---|
[1747] | 100 | } |
---|
| 101 | |
---|
| 102 | /** |
---|
| 103 | @brief Constructor: Sets this element to the element an ObjectListIterator. |
---|
| 104 | @param other The ObjectListIterator |
---|
| 105 | */ |
---|
| 106 | template <class O> |
---|
| 107 | inline Iterator(const ObjectListIterator<O>& other) |
---|
| 108 | { |
---|
[2662] | 109 | this->element_ = (other.element_) ? static_cast<ObjectListBaseElement*>(other.element_) : 0; |
---|
[1747] | 110 | this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects(); |
---|
[2784] | 111 | this->list_->registerIterator(this); |
---|
[1747] | 112 | } |
---|
| 113 | |
---|
| 114 | /** |
---|
[1505] | 115 | @brief Unregisters the Iterator from the ObjectList. |
---|
| 116 | */ |
---|
[1747] | 117 | inline ~Iterator() |
---|
[1505] | 118 | { |
---|
[2784] | 119 | this->list_->unregisterIterator(this); |
---|
[1505] | 120 | } |
---|
| 121 | |
---|
| 122 | /** |
---|
[1747] | 123 | @brief Assigns an exported element. |
---|
| 124 | @param exp The exported element |
---|
| 125 | */ |
---|
| 126 | inline const Iterator<T>& operator=(const ObjectListBase::Export& exp) |
---|
| 127 | { |
---|
| 128 | if (this->list_) |
---|
[2784] | 129 | this->list_->unregisterIterator(this); |
---|
[1747] | 130 | |
---|
| 131 | this->element_ = exp.element_; |
---|
| 132 | this->list_ = exp.list_; |
---|
[2784] | 133 | this->list_->registerIterator(this); |
---|
[1747] | 134 | |
---|
| 135 | return (*this); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | @brief Assigns the element of another Iterator. |
---|
| 140 | @param other The other Iterator |
---|
| 141 | */ |
---|
| 142 | inline const Iterator<T>& operator=(const Iterator<T>& other) |
---|
| 143 | { |
---|
| 144 | if (this->list_) |
---|
[2784] | 145 | this->list_->unregisterIterator(this); |
---|
[1747] | 146 | |
---|
| 147 | this->element_ = other.element_; |
---|
| 148 | this->list_ = other.list_; |
---|
[2784] | 149 | this->list_->registerIterator(this); |
---|
[1747] | 150 | |
---|
| 151 | return (*this); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | @brief Assigns a given element. |
---|
[1505] | 156 | @param element The element |
---|
| 157 | */ |
---|
[1747] | 158 | template <class O> |
---|
| 159 | inline const Iterator<T>& operator=(ObjectListElement<O>* element) |
---|
[1505] | 160 | { |
---|
[1747] | 161 | if (this->list_) |
---|
[2784] | 162 | this->list_->unregisterIterator(this); |
---|
[1747] | 163 | |
---|
[2662] | 164 | this->element_ = (element) ? static_cast<ObjectListBaseElement*>(element) : 0; |
---|
[1747] | 165 | this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects(); |
---|
[2784] | 166 | this->list_->registerIterator(this); |
---|
[1747] | 167 | |
---|
| 168 | return (*this); |
---|
[1625] | 169 | return *this; |
---|
[1505] | 170 | } |
---|
| 171 | |
---|
| 172 | /** |
---|
[1747] | 173 | @brief Assigns the element of an ObjectListIterator. |
---|
| 174 | @param other The ObjectListIterator |
---|
| 175 | */ |
---|
| 176 | template <class O> |
---|
| 177 | inline const Iterator<T>& operator=(const ObjectListIterator<O>& other) |
---|
| 178 | { |
---|
| 179 | if (this->list_) |
---|
[2784] | 180 | this->list_->unregisterIterator(this); |
---|
[1747] | 181 | |
---|
[2896] | 182 | this->element_ = (other.element_) ? static_cast<ObjectListBaseElement*>(other.element_) : 0; |
---|
[1747] | 183 | this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects(); |
---|
[2784] | 184 | this->list_->registerIterator(this); |
---|
[1747] | 185 | |
---|
| 186 | return (*this); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | /** |
---|
[1505] | 190 | @brief Overloading of the ++it operator: Iterator points to the next object in the list. |
---|
| 191 | @return The Iterator itself |
---|
| 192 | */ |
---|
[1747] | 193 | inline const Iterator<T>& operator++() |
---|
[1505] | 194 | { |
---|
| 195 | if (this->element_) |
---|
| 196 | this->element_ = this->element_->next_; |
---|
| 197 | return *this; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | /** |
---|
| 201 | @brief Overloading of the it++ operator: Iterator points to the next object in the list. |
---|
| 202 | @return The Iterator itself |
---|
| 203 | */ |
---|
[1747] | 204 | inline Iterator<T> operator++(int i) |
---|
[1505] | 205 | { |
---|
| 206 | Iterator<T> copy = *this; |
---|
| 207 | if (this->element_) |
---|
| 208 | this->element_ = this->element_->next_; |
---|
| 209 | return copy; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /** |
---|
| 213 | @brief Overloading of the --it operator: Iterator points to the previous object in the list. |
---|
| 214 | @return The Iterator itself |
---|
| 215 | */ |
---|
[1747] | 216 | inline const Iterator<T>& operator--() |
---|
[1505] | 217 | { |
---|
| 218 | if (this->element_) |
---|
| 219 | this->element_ = this->element_->prev_; |
---|
| 220 | return *this; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | /** |
---|
| 224 | @brief Overloading of the it-- operator: Iterator points to the previous object in the list. |
---|
| 225 | @return The Iterator itself |
---|
| 226 | */ |
---|
[1747] | 227 | inline Iterator<T> operator--(int i) |
---|
[1505] | 228 | { |
---|
| 229 | Iterator<T> copy = *this; |
---|
| 230 | if (this->element_) |
---|
| 231 | this->element_ = this->element_->prev_; |
---|
| 232 | return copy; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | /** |
---|
| 236 | @brief Overloading of the *it operator: returns the pointer to the object. |
---|
| 237 | @return The object the Iterator points at |
---|
| 238 | */ |
---|
[1747] | 239 | inline T* operator*() const |
---|
[1505] | 240 | { |
---|
| 241 | if (this->element_) |
---|
[3325] | 242 | return orxonox_cast<T*>(this->element_->objectBase_); |
---|
[1505] | 243 | else |
---|
| 244 | return 0; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /** |
---|
| 248 | @brief Overloading of the it-> operator: returns the pointer to the object. |
---|
| 249 | @return The object the Iterator points at |
---|
| 250 | */ |
---|
[1747] | 251 | inline T* operator->() const |
---|
[1505] | 252 | { |
---|
| 253 | if (this->element_) |
---|
[3325] | 254 | return orxonox_cast<T*>(this->element_->objectBase_); |
---|
[1505] | 255 | else |
---|
| 256 | return 0; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | /** |
---|
| 260 | @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object. |
---|
[1747] | 261 | @return True if the Iterator points to an existing object. |
---|
[1505] | 262 | */ |
---|
[1747] | 263 | inline operator bool() const |
---|
[1505] | 264 | { |
---|
| 265 | return (this->element_ != 0); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | /** |
---|
[1747] | 269 | @brief Overloading of the == operator to compare with another Iterator. |
---|
| 270 | @param compare The other Iterator |
---|
| 271 | @return True if the iterators point to the same element |
---|
[1505] | 272 | */ |
---|
[1747] | 273 | inline bool operator==(const Iterator<T>& compare) const |
---|
[1505] | 274 | { |
---|
[1747] | 275 | return (this->element_ == compare.element_); |
---|
[1505] | 276 | } |
---|
| 277 | |
---|
[1747] | 278 | /** |
---|
| 279 | @brief Overloading of the != operator to compare with another Iterator. |
---|
| 280 | @param compare The other Iterator |
---|
| 281 | @return True if the iterators point to different elements |
---|
| 282 | */ |
---|
| 283 | inline bool operator!=(const Iterator<T>& compare) const |
---|
| 284 | { |
---|
| 285 | return (this->element_ != compare.element_); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | /** |
---|
| 289 | @brief Increments the Iterator if it points at the given object. |
---|
| 290 | @param object The object to compare with |
---|
| 291 | */ |
---|
| 292 | inline void incrementIfEqual(OrxonoxClass* object) |
---|
| 293 | { |
---|
| 294 | if (this->element_ && this->element_->objectBase_ == object) |
---|
| 295 | this->operator++(); |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | protected: |
---|
| 299 | ObjectListBaseElement* element_; //!< The element the Iterator points at |
---|
| 300 | ObjectListBase* list_; //!< The list wherein the element is |
---|
[1505] | 301 | }; |
---|
[1747] | 302 | |
---|
| 303 | typedef Iterator<OrxonoxClass> BaseIterator; |
---|
[1505] | 304 | } |
---|
| 305 | |
---|
| 306 | #endif /* _Iterator_H__ */ |
---|