[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 | /** |
---|
| 30 | @file Iterator.h |
---|
| 31 | @brief Definition and implementation of the Iterator class. |
---|
| 32 | |
---|
| 33 | The Iterator of a given class allows to iterate through an ObjectList, containing all objects of that type. |
---|
| 34 | This is the only way to access the objects stored in an ObjectList. |
---|
| 35 | |
---|
| 36 | Usage: |
---|
[1574] | 37 | for (Iterator<myClass> it = ObjectList<myClass>::begin(); it != ObjectList<myClass>::end(); ++it) |
---|
[1505] | 38 | { |
---|
| 39 | it->someFunction(...); |
---|
[1574] | 40 | myClass* myObject = *it; |
---|
[1505] | 41 | } |
---|
| 42 | */ |
---|
| 43 | |
---|
| 44 | #ifndef _Iterator_H__ |
---|
| 45 | #define _Iterator_H__ |
---|
| 46 | |
---|
| 47 | #include "CorePrereqs.h" |
---|
| 48 | |
---|
[1574] | 49 | #include "IteratorBase.h" |
---|
[1505] | 50 | |
---|
| 51 | namespace orxonox |
---|
| 52 | { |
---|
[1574] | 53 | //! The Iterator allows to iterate through the ObjectList of a given class. |
---|
[1505] | 54 | template <class T> |
---|
[1574] | 55 | class Iterator : public IteratorBase |
---|
[1505] | 56 | { |
---|
| 57 | public: |
---|
| 58 | /** |
---|
| 59 | @brief Constructor: Sets the element, whereon the iterator points, to zero. |
---|
| 60 | */ |
---|
[1574] | 61 | inline Iterator() |
---|
[1505] | 62 | { |
---|
[1543] | 63 | ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this); |
---|
[1505] | 64 | } |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | @brief Constructor: Sets the element, whereon the iterator points, to a given element. |
---|
| 68 | @param element The element to start with |
---|
| 69 | */ |
---|
[1574] | 70 | inline Iterator(ObjectListBaseElement* element) : IteratorBase((ObjectListBaseElement*)element) |
---|
[1505] | 71 | { |
---|
[1543] | 72 | ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this); |
---|
[1505] | 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | @brief Unregisters the Iterator from the ObjectList. |
---|
| 77 | */ |
---|
[1574] | 78 | inline ~Iterator() |
---|
[1505] | 79 | { |
---|
[1543] | 80 | ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterIterator(this); |
---|
[1505] | 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | @brief Overloading of the ++it operator: Iterator points to the next object in the list. |
---|
| 85 | @return The Iterator itself |
---|
| 86 | */ |
---|
[1574] | 87 | inline Iterator<T> operator++() |
---|
[1505] | 88 | { |
---|
[1574] | 89 | IteratorBase::operator++(); |
---|
[1505] | 90 | return *this; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | @brief Overloading of the it++ operator: Iterator points to the next object in the list. |
---|
| 95 | @return The Iterator itself |
---|
| 96 | */ |
---|
[1574] | 97 | inline Iterator<T> operator++(int i) |
---|
[1505] | 98 | { |
---|
| 99 | Iterator<T> copy = *this; |
---|
[1574] | 100 | IteratorBase::operator++(); |
---|
[1505] | 101 | return copy; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | @brief Overloading of the --it operator: Iterator points to the previous object in the list. |
---|
| 106 | @return The Iterator itself |
---|
| 107 | */ |
---|
[1574] | 108 | inline Iterator<T> operator--() |
---|
[1505] | 109 | { |
---|
[1574] | 110 | IteratorBase::operator--(); |
---|
[1505] | 111 | return *this; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /** |
---|
| 115 | @brief Overloading of the it-- operator: Iterator points to the previous object in the list. |
---|
| 116 | @return The Iterator itself |
---|
| 117 | */ |
---|
[1574] | 118 | inline Iterator<T> operator--(int i) |
---|
[1505] | 119 | { |
---|
| 120 | Iterator<T> copy = *this; |
---|
[1574] | 121 | IteratorBase::operator--(); |
---|
[1505] | 122 | return copy; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | @brief Overloading of the *it operator: returns the pointer to the object. |
---|
| 127 | @return The object the Iterator points at |
---|
| 128 | */ |
---|
[1574] | 129 | inline T* operator*() |
---|
[1505] | 130 | { |
---|
[1574] | 131 | return dynamic_cast<T*>(IteratorBase::operator*()); |
---|
[1505] | 132 | } |
---|
| 133 | |
---|
| 134 | /** |
---|
| 135 | @brief Overloading of the it-> operator: returns the pointer to the object. |
---|
| 136 | @return The object the Iterator points at |
---|
| 137 | */ |
---|
[1574] | 138 | inline T* operator->() const |
---|
[1505] | 139 | { |
---|
[1574] | 140 | return dynamic_cast<T*>(IteratorBase::operator->()); |
---|
[1505] | 141 | } |
---|
| 142 | |
---|
| 143 | /** |
---|
[1574] | 144 | @brief Overloading of the == operator to compare with another Iterator. |
---|
| 145 | @param compare The other Iterator |
---|
| 146 | @return True if the iterators point to the same element |
---|
[1505] | 147 | */ |
---|
[1574] | 148 | inline bool operator==(const Iterator<T>& compare) |
---|
[1505] | 149 | { |
---|
[1574] | 150 | return (this->element_ == compare.element_); |
---|
[1505] | 151 | } |
---|
| 152 | |
---|
| 153 | /** |
---|
[1574] | 154 | @brief Overloading of the != operator to compare with another Iterator. |
---|
| 155 | @param compare The other Iterator |
---|
| 156 | @return True if the iterators point to different elements |
---|
[1505] | 157 | */ |
---|
[1574] | 158 | inline bool operator!=(const Iterator<T>& compare) |
---|
[1505] | 159 | { |
---|
[1574] | 160 | return (this->element_ != compare.element_); |
---|
[1505] | 161 | } |
---|
| 162 | }; |
---|
| 163 | } |
---|
| 164 | |
---|
[1574] | 165 | // Include ObjectList.h so the user only has to include one file: Iterator.h |
---|
| 166 | #include "ObjectList.h" |
---|
| 167 | |
---|
[1505] | 168 | #endif /* _Iterator_H__ */ |
---|