[1591] | 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 Iterator.h |
---|
| 31 | @brief Definition and implementation of the Iterator class. |
---|
| 32 | |
---|
| 33 | The ObjectListIterator of a given class allows to iterate through the |
---|
| 34 | ObjectList of this class, containing all objects of that type. |
---|
| 35 | This is the only way to access the objects stored in an ObjectList. |
---|
| 36 | |
---|
| 37 | Usage: |
---|
[1596] | 38 | for (ObjectListIterator<myClass> it = ObjectList<myClass>::begin(); it != ObjectList<myClass>::end(); ++it) |
---|
[1591] | 39 | { |
---|
| 40 | it->someFunction(...); |
---|
| 41 | myClass* myObject = *it; |
---|
| 42 | } |
---|
| 43 | */ |
---|
| 44 | |
---|
| 45 | #ifndef _ObjectListIterator_H__ |
---|
| 46 | #define _ObjectListIterator_H__ |
---|
| 47 | |
---|
| 48 | #include "CorePrereqs.h" |
---|
| 49 | #include "ObjectListBase.h" |
---|
| 50 | |
---|
| 51 | namespace orxonox |
---|
| 52 | { |
---|
| 53 | //! The Iterator allows to iterate through the ObjectList of a given class. |
---|
| 54 | template <class T> |
---|
| 55 | class ObjectListIterator |
---|
| 56 | { |
---|
| 57 | template <class I> |
---|
| 58 | friend class Iterator; |
---|
| 59 | |
---|
| 60 | public: |
---|
| 61 | /** |
---|
| 62 | @brief Constructor: Sets the element, whereon the ObjectListIterator points, to zero. |
---|
| 63 | */ |
---|
| 64 | inline ObjectListIterator() |
---|
| 65 | { |
---|
| 66 | this->element_ = 0; |
---|
[2784] | 67 | ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this); |
---|
[1591] | 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | @brief Constructor: Sets this element to a given element. |
---|
| 72 | @param element The element to start with |
---|
| 73 | */ |
---|
| 74 | inline ObjectListIterator(ObjectListElement<T>* element) |
---|
| 75 | { |
---|
| 76 | this->element_ = element; |
---|
[2784] | 77 | ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this); |
---|
[1591] | 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | @brief Constructor: Sets this element to the element of another ObjectListIterator. |
---|
| 82 | @param other The other ObjectListIterator |
---|
| 83 | */ |
---|
| 84 | inline ObjectListIterator(const ObjectListIterator<T>& other) |
---|
| 85 | { |
---|
| 86 | this->element_ = other.element_; |
---|
[2784] | 87 | ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this); |
---|
[1591] | 88 | } |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | @brief Unregisters the ObjectListIterator from the ObjectList. |
---|
| 92 | */ |
---|
| 93 | inline ~ObjectListIterator() |
---|
| 94 | { |
---|
[2784] | 95 | ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterObjectListIterator(this); |
---|
[1591] | 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | @brief Assigns an ObjectListElement. |
---|
| 100 | @param element The ObjectListElement |
---|
| 101 | */ |
---|
| 102 | inline const ObjectListIterator<T>& operator=(ObjectListElement<T>* element) |
---|
| 103 | { |
---|
| 104 | this->element_ = element; |
---|
| 105 | return (*this); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | @brief Assigns the element of another ObjectListIterator. |
---|
| 110 | @param element The other ObjectListIterator |
---|
| 111 | */ |
---|
| 112 | inline const ObjectListIterator<T>& operator=(const ObjectListIterator<T>& other) |
---|
| 113 | { |
---|
| 114 | this->element_ = other.element_; |
---|
| 115 | return (*this); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | @brief Overloading of the ++it operator: ObjectListIterator points to the next object in the list. |
---|
| 120 | @return The ObjectListIterator itself |
---|
| 121 | */ |
---|
| 122 | inline const ObjectListIterator<T>& operator++() |
---|
| 123 | { |
---|
| 124 | if (this->element_) |
---|
[2662] | 125 | this->element_ = static_cast<ObjectListElement<T>*>(this->element_->next_); |
---|
[1591] | 126 | return *this; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | /** |
---|
| 130 | @brief Overloading of the it++ operator: ObjectListIterator points to the next object in the list. |
---|
| 131 | @return The ObjectListIterator itself |
---|
| 132 | */ |
---|
| 133 | inline ObjectListIterator<T> operator++(int i) |
---|
| 134 | { |
---|
| 135 | ObjectListIterator<T> copy = *this; |
---|
| 136 | if (this->element_) |
---|
[2662] | 137 | this->element_ = static_cast<ObjectListElement<T>*>(this->element_->next_); |
---|
[1591] | 138 | return copy; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | @brief Overloading of the --it operator: ObjectListIterator points to the previous object in the list. |
---|
| 143 | @return The ObjectListIterator itself |
---|
| 144 | */ |
---|
| 145 | inline const ObjectListIterator<T>& operator--() |
---|
| 146 | { |
---|
| 147 | if (this->element_) |
---|
[2662] | 148 | this->element_ = static_cast<ObjectListElement<T>*>(this->element_->prev_); |
---|
[1591] | 149 | return *this; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | /** |
---|
| 153 | @brief Overloading of the it-- operator: ObjectListIterator points to the previous object in the list. |
---|
| 154 | @return The ObjectListIterator itself |
---|
| 155 | */ |
---|
| 156 | inline ObjectListIterator<T> operator--(int i) |
---|
| 157 | { |
---|
| 158 | ObjectListIterator<T> copy = *this; |
---|
| 159 | if (this->element_) |
---|
[2662] | 160 | this->element_ = static_cast<ObjectListElement<T>*>(this->element_->prev_); |
---|
[1591] | 161 | return copy; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | @brief Overloading of the *it operator: returns the pointer to the object. |
---|
| 166 | @return The object the ObjectListIterator points at |
---|
| 167 | */ |
---|
| 168 | inline T* operator*() const |
---|
| 169 | { |
---|
| 170 | if (this->element_) |
---|
| 171 | return this->element_->object_; |
---|
| 172 | else |
---|
| 173 | return 0; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | /** |
---|
| 177 | @brief Overloading of the it-> operator: returns the pointer to the object. |
---|
| 178 | @return The object the ObjectListIterator points at |
---|
| 179 | */ |
---|
| 180 | inline T* operator->() const |
---|
| 181 | { |
---|
| 182 | if (this->element_) |
---|
| 183 | return this->element_->object_; |
---|
| 184 | else |
---|
| 185 | return 0; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | /** |
---|
| 189 | @brief Overloading of the typecast-operator to bool: returns true if the ObjectListIterator points to an existing object. |
---|
| 190 | @return True if the ObjectListIterator points to an existing object. |
---|
| 191 | */ |
---|
| 192 | inline operator bool() const |
---|
| 193 | { |
---|
| 194 | return (this->element_ != 0); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | /** |
---|
| 198 | @brief Overloading of the == operator to compare with another ObjectListIterator. |
---|
| 199 | @param compare The other ObjectListIterator |
---|
| 200 | @return True if the ObjectListIterator point to the same element |
---|
| 201 | */ |
---|
| 202 | inline bool operator==(const ObjectListIterator<T>& compare) const |
---|
| 203 | { |
---|
| 204 | return (this->element_ == compare.element_); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | /** |
---|
| 208 | @brief Overloading of the != operator to compare with another ObjectListIterator. |
---|
| 209 | @param compare The other ObjectListIterator |
---|
| 210 | @return True if the ObjectListIterator point to different elements |
---|
| 211 | */ |
---|
| 212 | inline bool operator!=(const ObjectListIterator<T>& compare) const |
---|
| 213 | { |
---|
| 214 | return (this->element_ != compare.element_); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | /** |
---|
| 218 | @brief Increments the ObjectListIterator if it points at the given object. |
---|
| 219 | @param object The object to compare with |
---|
| 220 | */ |
---|
| 221 | inline void incrementIfEqual(OrxonoxClass* object) |
---|
| 222 | { |
---|
| 223 | if (this->element_ && this->element_->objectBase_ == object) |
---|
| 224 | this->operator++(); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | private: |
---|
| 228 | ObjectListElement<T>* element_; //!< The element the Iterator points at |
---|
| 229 | }; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | // Include ObjectList.h so the user only has to include one file: Iterator.h |
---|
| 233 | #include "ObjectList.h" |
---|
| 234 | |
---|
| 235 | #endif /* _ObjectListIterator_H__ */ |
---|