[790] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Fabian 'x3n' Landau |
---|
| 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
[871] | 28 | /** |
---|
[790] | 29 | @file Iterator.h |
---|
| 30 | @brief Definition and implementation of the Iterator class. |
---|
| 31 | |
---|
| 32 | The Iterator of a given class allows to iterate through an ObjectList, containing all objects of that type. |
---|
| 33 | This is the only way to access the objects stored in an ObjectList. |
---|
| 34 | |
---|
| 35 | Usage: |
---|
| 36 | for (Iterator<class> it = ObjectList<class>::start(); it != 0; ++it) |
---|
| 37 | { |
---|
| 38 | it->someFunction(...); |
---|
| 39 | class* myObject = *it; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | Warning: Don't delete objects directly through the iterator. |
---|
| 43 | */ |
---|
| 44 | |
---|
[221] | 45 | #ifndef _Iterator_H__ |
---|
| 46 | #define _Iterator_H__ |
---|
| 47 | |
---|
[790] | 48 | #include "CorePrereqs.h" |
---|
| 49 | |
---|
| 50 | #include "ObjectList.h" |
---|
| 51 | #include "Debug.h" |
---|
| 52 | |
---|
[221] | 53 | namespace orxonox |
---|
| 54 | { |
---|
[790] | 55 | //! The iterator allows to iterate through an ObjectList of a given class. |
---|
[221] | 56 | template <class T> |
---|
| 57 | class Iterator |
---|
| 58 | { |
---|
| 59 | public: |
---|
[790] | 60 | /** |
---|
| 61 | @brief Constructor: Sets the element, whereon the iterator points, to zero. |
---|
| 62 | */ |
---|
[221] | 63 | Iterator() |
---|
| 64 | { |
---|
[248] | 65 | this->element_ = 0; |
---|
[221] | 66 | } |
---|
| 67 | |
---|
[790] | 68 | /** |
---|
| 69 | @brief Constructor: Sets the element, whereon the iterator points, to a given element. |
---|
| 70 | @param element The element to start with |
---|
| 71 | */ |
---|
[248] | 72 | Iterator(ObjectListElement<T>* element) |
---|
[221] | 73 | { |
---|
[248] | 74 | this->element_ = element; |
---|
[221] | 75 | } |
---|
| 76 | |
---|
[790] | 77 | /** |
---|
[871] | 78 | @brief Assigns an element to the iterator. |
---|
| 79 | @param element The element |
---|
| 80 | */ |
---|
| 81 | Iterator<T> operator=(ObjectListElement<T>* element) |
---|
| 82 | { |
---|
| 83 | this->element_ = element; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
[790] | 87 | @brief Overloading of the ++it operator: Iterator points to the next object in the list. |
---|
| 88 | @return The Iterator itself |
---|
| 89 | */ |
---|
[225] | 90 | Iterator<T> operator++() |
---|
| 91 | { |
---|
[248] | 92 | this->element_ = this->element_->next_; |
---|
[225] | 93 | return *this; |
---|
| 94 | } |
---|
| 95 | |
---|
[790] | 96 | /** |
---|
| 97 | @brief Overloading of the it++ operator: Iterator points to the next object in the list. |
---|
| 98 | @return The Iterator itself |
---|
| 99 | */ |
---|
| 100 | Iterator<T> operator++(int i) |
---|
| 101 | { |
---|
| 102 | Iterator<T> copy = *this; |
---|
| 103 | this->element_ = this->element_->next_; |
---|
| 104 | return copy; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /** |
---|
| 108 | @brief Overloading of the --it operator: Iterator points to the previous object in the list. |
---|
| 109 | @return The Iterator itself |
---|
| 110 | */ |
---|
[225] | 111 | Iterator<T> operator--() |
---|
| 112 | { |
---|
[248] | 113 | this->element_ = this->element_->prev_; |
---|
[225] | 114 | return *this; |
---|
[221] | 115 | } |
---|
| 116 | |
---|
[790] | 117 | /** |
---|
| 118 | @brief Overloading of the it-- operator: Iterator points to the previous object in the list. |
---|
| 119 | @return The Iterator itself |
---|
| 120 | */ |
---|
| 121 | Iterator<T> operator--(int i) |
---|
| 122 | { |
---|
| 123 | Iterator<T> copy = *this; |
---|
| 124 | this->element_ = this->element_->prev_; |
---|
| 125 | return copy; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | @brief Overloading of the *it operator: returns the pointer to the object. |
---|
| 130 | @return The object the Iterator points at |
---|
| 131 | */ |
---|
[221] | 132 | T* operator*() |
---|
| 133 | { |
---|
[248] | 134 | return this->element_->object_; |
---|
[221] | 135 | } |
---|
| 136 | |
---|
[790] | 137 | /** |
---|
| 138 | @brief Overloading of the it-> operator: returns the pointer to the object. |
---|
| 139 | @return The object the Iterator points at |
---|
| 140 | */ |
---|
[221] | 141 | T* operator->() const |
---|
| 142 | { |
---|
[248] | 143 | return this->element_->object_; |
---|
[221] | 144 | |
---|
| 145 | } |
---|
| 146 | |
---|
[790] | 147 | /** |
---|
| 148 | @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object. |
---|
| 149 | @return True if the iterator points to an existing object. |
---|
| 150 | */ |
---|
[221] | 151 | operator bool() |
---|
| 152 | { |
---|
[248] | 153 | return (this->element_ != 0); |
---|
[221] | 154 | } |
---|
| 155 | |
---|
[790] | 156 | /** |
---|
| 157 | @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool. |
---|
| 158 | @param compare The integer (must be zero, everything else makes no sense). |
---|
| 159 | @return True if the iterator points to an existing object. |
---|
| 160 | */ |
---|
[871] | 161 | bool operator!=(ObjectListElement<T>* compare) |
---|
[221] | 162 | { |
---|
[871] | 163 | return (this->element_ != compare); |
---|
[221] | 164 | } |
---|
| 165 | |
---|
| 166 | private: |
---|
[790] | 167 | ObjectListElement<T>* element_; //!< The element the Iterator points at |
---|
[221] | 168 | }; |
---|
| 169 | } |
---|
| 170 | |
---|
[790] | 171 | #endif /* _Iterator_H__ */ |
---|