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