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