[1574] | 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 IteratorBase.h |
---|
| 31 | @brief Definition and implementation of the Iterator class. |
---|
| 32 | |
---|
| 33 | The Iterator of a given class allows to iterate through an ObjectListBase, containing all objects of that type. |
---|
| 34 | This is the only way to access the objects stored in an ObjectListBase. |
---|
| 35 | */ |
---|
| 36 | |
---|
| 37 | #ifndef _IteratorBase_H__ |
---|
| 38 | #define _IteratorBase_H__ |
---|
| 39 | |
---|
| 40 | #include "CorePrereqs.h" |
---|
| 41 | |
---|
| 42 | #include "ObjectListBase.h" |
---|
| 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
| 46 | //! The IteratorBase allows to iterate through an ObjectListBase |
---|
| 47 | class _CoreExport IteratorBase |
---|
| 48 | { |
---|
| 49 | public: |
---|
| 50 | /** |
---|
| 51 | @brief Default Constructor: Sets the element to zero. |
---|
| 52 | */ |
---|
| 53 | inline IteratorBase() |
---|
| 54 | { |
---|
| 55 | this->element_ = 0; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | @brief Constructor: Sets the element, whereon the iterator points, to a given element. |
---|
| 60 | @param element The element to start with |
---|
| 61 | */ |
---|
| 62 | inline IteratorBase(ObjectListBaseElement* element) |
---|
| 63 | { |
---|
| 64 | this->element_ = element; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | @brief Constructor: Sets this element to the element of the other IteratorBase. |
---|
| 69 | @param other The other IteratorBase |
---|
| 70 | */ |
---|
| 71 | inline IteratorBase(const IteratorBase& other) |
---|
| 72 | { |
---|
| 73 | this->element_ = other.element_; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | @brief Assigns the element of another IteratorBase. |
---|
| 78 | @param element The other IteratorBase |
---|
| 79 | */ |
---|
| 80 | inline IteratorBase operator=(const IteratorBase& other) |
---|
| 81 | { |
---|
| 82 | this->element_ = other.element_; |
---|
| 83 | return (*this); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | @brief Overloading of the ++it operator: IteratorBase points to the next object in the list. |
---|
| 88 | @return The IteratorBase itself |
---|
| 89 | */ |
---|
| 90 | inline IteratorBase operator++() |
---|
| 91 | { |
---|
| 92 | if (this->element_) |
---|
| 93 | this->element_ = this->element_->next_; |
---|
| 94 | return (*this); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | @brief Overloading of the it++ operator: IteratorBase points to the next object in the list. |
---|
| 99 | @return The IteratorBase itself |
---|
| 100 | */ |
---|
| 101 | inline IteratorBase operator++(int i) |
---|
| 102 | { |
---|
| 103 | IteratorBase copy = *this; |
---|
| 104 | if (this->element_) |
---|
| 105 | this->element_ = this->element_->next_; |
---|
| 106 | return copy; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | @brief Overloading of the --it operator: IteratorBase points to the previous object in the list. |
---|
| 111 | @return The IteratorBase itself |
---|
| 112 | */ |
---|
| 113 | inline IteratorBase operator--() |
---|
| 114 | { |
---|
| 115 | if (this->element_) |
---|
| 116 | this->element_ = this->element_->prev_; |
---|
| 117 | return (*this); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | @brief Overloading of the it-- operator: IteratorBase points to the previous object in the list. |
---|
| 122 | @return The IteratorBase itself |
---|
| 123 | */ |
---|
| 124 | inline IteratorBase operator--(int i) |
---|
| 125 | { |
---|
| 126 | IteratorBase copy = *this; |
---|
| 127 | if (this->element_) |
---|
| 128 | this->element_ = this->element_->prev_; |
---|
| 129 | return copy; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | @brief Overloading of the *it operator: returns the pointer to the object. |
---|
| 134 | @return The object the IteratorBase points at |
---|
| 135 | */ |
---|
| 136 | inline OrxonoxClass* operator*() |
---|
| 137 | { |
---|
| 138 | if (this->element_) |
---|
| 139 | return this->element_->object_; |
---|
| 140 | else |
---|
| 141 | return 0; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /** |
---|
| 145 | @brief Overloading of the it-> operator: returns the pointer to the object. |
---|
| 146 | @return The object the IteratorBase points at |
---|
| 147 | */ |
---|
| 148 | inline OrxonoxClass* operator->() const |
---|
| 149 | { |
---|
| 150 | if (this->element_) |
---|
| 151 | return this->element_->object_; |
---|
| 152 | else |
---|
| 153 | return 0; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | /** |
---|
| 157 | @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object. |
---|
| 158 | @return True if the IteratorBase points to an existing object. |
---|
| 159 | */ |
---|
| 160 | inline operator bool() |
---|
| 161 | { |
---|
| 162 | return (this->element_ != 0); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | /** |
---|
| 166 | @brief Overloading of the == operator to compare with another IteratorBase. |
---|
| 167 | @param compare The other IteratorBase |
---|
| 168 | @return True if the iterators point to the same element |
---|
| 169 | */ |
---|
| 170 | inline bool operator==(const IteratorBase& compare) |
---|
| 171 | { |
---|
| 172 | return (this->element_ == compare.element_); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | /** |
---|
| 176 | @brief Overloading of the != operator to compare with another IteratorBase. |
---|
| 177 | @param compare The other IteratorBase |
---|
| 178 | @return True if the iterators point to different elements |
---|
| 179 | */ |
---|
| 180 | inline bool operator!=(const IteratorBase& compare) |
---|
| 181 | { |
---|
| 182 | return (this->element_ != compare.element_); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | protected: |
---|
| 186 | ObjectListBaseElement* element_; //!< The element the Iterator points at |
---|
| 187 | }; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | #endif /* _IteratorBase_H__ */ |
---|