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 |
---|
31 | @ingroup Object ObjectList |
---|
32 | @brief Definition of the Iterator class. |
---|
33 | |
---|
34 | @anchor IteratorExample |
---|
35 | |
---|
36 | @ref orxonox::Iterator "Iterator" allows to iterate through an @ref orxonox::ObjectListBase |
---|
37 | "ObjectListBase". Objects in this list are cast to the template argument @a T of Iterator<T> using |
---|
38 | @c dynamic_cast. In contrast to @ref orxonox::ObjectListIterator "ObjectListIterator<T>", |
---|
39 | @ref orxonox::Iterator "Iterator<T>" can iterate through every object-list. In practice though it |
---|
40 | is limited to objects of type @a T and its subclasses. Because of the @c dynamic_cast, this iterator |
---|
41 | is much slower than ObjectListIterator. |
---|
42 | |
---|
43 | Usage: |
---|
44 | @code |
---|
45 | for (Iterator<myClass> it = anyidentifier->getObjects()->begin(); it != anyidentifier->getObjects()->end(); ++it) |
---|
46 | { |
---|
47 | it->someFunction(...); |
---|
48 | myClass* myObject = *it; |
---|
49 | } |
---|
50 | @endcode |
---|
51 | */ |
---|
52 | |
---|
53 | #ifndef _Iterator_H__ |
---|
54 | #define _Iterator_H__ |
---|
55 | |
---|
56 | #include "CorePrereqs.h" |
---|
57 | |
---|
58 | #include "Identifier.h" |
---|
59 | #include "ObjectListBase.h" |
---|
60 | |
---|
61 | namespace orxonox |
---|
62 | { |
---|
63 | /** |
---|
64 | @brief The Iterator allows to iterate through a given ObjectList. |
---|
65 | |
---|
66 | Independent of the object-list's type, the objects in the list are always casted |
---|
67 | to @a T using @c dynamic_cast. |
---|
68 | |
---|
69 | @see See @ref IteratorExample "Iterator.h" for more information an example. |
---|
70 | */ |
---|
71 | template <class T = OrxonoxClass> |
---|
72 | class Iterator |
---|
73 | { |
---|
74 | public: |
---|
75 | /** |
---|
76 | @brief Constructor: Sets the element, whereon the iterator points, to zero. |
---|
77 | */ |
---|
78 | inline Iterator() |
---|
79 | { |
---|
80 | this->element_ = 0; |
---|
81 | this->list_ = 0; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | @brief Constructor: Sets this element to the exported element. |
---|
86 | @param exp The exported element |
---|
87 | */ |
---|
88 | inline Iterator(const ObjectListBase::Export& exp) |
---|
89 | { |
---|
90 | this->element_ = exp.element_; |
---|
91 | this->list_ = exp.list_; |
---|
92 | this->list_->registerIterator(this); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | @brief Constructor: Sets this element to the element of another Iterator. |
---|
97 | @param other The other Iterator |
---|
98 | */ |
---|
99 | inline Iterator(const Iterator<T>& other) |
---|
100 | { |
---|
101 | this->element_ = other.element_; |
---|
102 | this->list_ = other.list_; |
---|
103 | this->list_->registerIterator(this); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | @brief Constructor: Sets this element to a given element |
---|
108 | @param element The element |
---|
109 | */ |
---|
110 | template <class O> |
---|
111 | inline Iterator(ObjectListElement<O>* element) |
---|
112 | { |
---|
113 | this->element_ = element; |
---|
114 | this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects(); |
---|
115 | this->list_->registerIterator(this); |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | @brief Constructor: Sets this element to the element an ObjectListIterator. |
---|
120 | @param other The ObjectListIterator |
---|
121 | */ |
---|
122 | template <class O> |
---|
123 | inline Iterator(const ObjectListIterator<O>& other) |
---|
124 | { |
---|
125 | this->element_ = other.element_; |
---|
126 | this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects(); |
---|
127 | this->list_->registerIterator(this); |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | @brief Unregisters the Iterator from the ObjectList. |
---|
132 | */ |
---|
133 | inline ~Iterator() |
---|
134 | { |
---|
135 | this->list_->unregisterIterator(this); |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | @brief Assigns an exported element. |
---|
140 | @param exp The exported element |
---|
141 | */ |
---|
142 | inline Iterator<T>& operator=(const ObjectListBase::Export& exp) |
---|
143 | { |
---|
144 | if (this->list_) |
---|
145 | this->list_->unregisterIterator(this); |
---|
146 | |
---|
147 | this->element_ = exp.element_; |
---|
148 | this->list_ = exp.list_; |
---|
149 | this->list_->registerIterator(this); |
---|
150 | |
---|
151 | return (*this); |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | @brief Assigns the element of another Iterator. |
---|
156 | @param other The other Iterator |
---|
157 | */ |
---|
158 | inline Iterator<T>& operator=(const Iterator<T>& other) |
---|
159 | { |
---|
160 | if (this->list_) |
---|
161 | this->list_->unregisterIterator(this); |
---|
162 | |
---|
163 | this->element_ = other.element_; |
---|
164 | this->list_ = other.list_; |
---|
165 | this->list_->registerIterator(this); |
---|
166 | |
---|
167 | return (*this); |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | @brief Assigns a given element. |
---|
172 | @param element The element |
---|
173 | */ |
---|
174 | template <class O> |
---|
175 | inline Iterator<T>& operator=(ObjectListElement<O>* element) |
---|
176 | { |
---|
177 | if (this->list_) |
---|
178 | this->list_->unregisterIterator(this); |
---|
179 | |
---|
180 | this->element_ = element; |
---|
181 | this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects(); |
---|
182 | this->list_->registerIterator(this); |
---|
183 | |
---|
184 | return (*this); |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | @brief Assigns the element of an ObjectListIterator. |
---|
189 | @param other The ObjectListIterator |
---|
190 | */ |
---|
191 | template <class O> |
---|
192 | inline Iterator<T>& operator=(const ObjectListIterator<O>& other) |
---|
193 | { |
---|
194 | if (this->list_) |
---|
195 | this->list_->unregisterIterator(this); |
---|
196 | |
---|
197 | this->element_ = other.element_; |
---|
198 | this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects(); |
---|
199 | this->list_->registerIterator(this); |
---|
200 | |
---|
201 | return (*this); |
---|
202 | } |
---|
203 | |
---|
204 | /** |
---|
205 | @brief Overloading of the ++it operator: Iterator points to the next object in the list. |
---|
206 | @return The Iterator itself |
---|
207 | */ |
---|
208 | inline const Iterator<T>& operator++() |
---|
209 | { |
---|
210 | this->element_ = this->element_->next_; |
---|
211 | return *this; |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | @brief Overloading of the it++ operator: Iterator points to the next object in the list. |
---|
216 | @return The Iterator itself |
---|
217 | */ |
---|
218 | inline Iterator<T> operator++(int) |
---|
219 | { |
---|
220 | Iterator<T> copy = *this; |
---|
221 | this->element_ = this->element_->next_; |
---|
222 | return copy; |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | @brief Overloading of the --it operator: Iterator points to the previous object in the list. |
---|
227 | @return The Iterator itself |
---|
228 | */ |
---|
229 | inline const Iterator<T>& operator--() |
---|
230 | { |
---|
231 | this->element_ = this->element_->prev_; |
---|
232 | return *this; |
---|
233 | } |
---|
234 | |
---|
235 | /** |
---|
236 | @brief Overloading of the it-- operator: Iterator points to the previous object in the list. |
---|
237 | @return The Iterator itself |
---|
238 | */ |
---|
239 | inline Iterator<T> operator--(int i) |
---|
240 | { |
---|
241 | Iterator<T> copy = *this; |
---|
242 | this->element_ = this->element_->prev_; |
---|
243 | return copy; |
---|
244 | } |
---|
245 | |
---|
246 | /** |
---|
247 | @brief Overloading of the *it operator: returns the pointer to the object. |
---|
248 | @return The object the Iterator points at |
---|
249 | */ |
---|
250 | inline T* operator*() const |
---|
251 | { |
---|
252 | return orxonox_cast<T*>(this->element_->objectBase_); |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | @brief Overloading of the it-> operator: returns the pointer to the object. |
---|
257 | @return The object the Iterator points at |
---|
258 | */ |
---|
259 | inline T* operator->() const |
---|
260 | { |
---|
261 | return orxonox_cast<T*>(this->element_->objectBase_); |
---|
262 | } |
---|
263 | |
---|
264 | /** |
---|
265 | @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object. |
---|
266 | @return True if the Iterator points to an existing object. |
---|
267 | */ |
---|
268 | inline operator bool() const |
---|
269 | { |
---|
270 | return (this->element_ != 0); |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | @brief Overloading of the == operator to compare with another Iterator. |
---|
275 | @param compare The other Iterator |
---|
276 | @return True if the iterators point to the same element |
---|
277 | */ |
---|
278 | inline bool operator==(const Iterator<T>& compare) const |
---|
279 | { |
---|
280 | return (this->element_ == compare.element_); |
---|
281 | } |
---|
282 | |
---|
283 | /** |
---|
284 | @brief Overloading of the != operator to compare with another Iterator. |
---|
285 | @param compare The other Iterator |
---|
286 | @return True if the iterators point to different elements |
---|
287 | */ |
---|
288 | inline bool operator!=(const Iterator<T>& compare) const |
---|
289 | { |
---|
290 | return (this->element_ != compare.element_); |
---|
291 | } |
---|
292 | |
---|
293 | /** |
---|
294 | @brief Increments the Iterator if it points at the given object. |
---|
295 | @param object The object to compare with |
---|
296 | */ |
---|
297 | inline void incrementIfEqual(OrxonoxClass* object) |
---|
298 | { |
---|
299 | if (this->element_ && this->element_->objectBase_ == object) |
---|
300 | this->operator++(); |
---|
301 | } |
---|
302 | |
---|
303 | protected: |
---|
304 | ObjectListBaseElement* element_; //!< The element the Iterator points at |
---|
305 | ObjectListBase* list_; //!< The list wherein the element is |
---|
306 | }; |
---|
307 | |
---|
308 | typedef Iterator<OrxonoxClass> BaseIterator; |
---|
309 | } |
---|
310 | |
---|
311 | #endif /* _Iterator_H__ */ |
---|