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, used to iterate through object-lists. |
---|
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 "core/CorePrereqs.h" |
---|
57 | |
---|
58 | #include "ObjectListBase.h" |
---|
59 | #include "IteratorBase.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> |
---|
72 | class Iterator : public IteratorBase<T, Iterator<T>> |
---|
73 | { |
---|
74 | public: |
---|
75 | /** |
---|
76 | @brief Constructor: Sets the element, whereon the iterator points, to zero. |
---|
77 | */ |
---|
78 | inline Iterator() : IteratorBase<T, Iterator<T>>() {} |
---|
79 | |
---|
80 | /** |
---|
81 | @brief Constructor: Sets this element to the element of another Iterator. |
---|
82 | @param other The other Iterator |
---|
83 | */ |
---|
84 | template <class OT, class OI> |
---|
85 | inline Iterator(const IteratorBase<OT, OI>& other) : IteratorBase<T, Iterator<T>>(other) {} |
---|
86 | |
---|
87 | /** |
---|
88 | @brief Assigns a given element. |
---|
89 | @param element The element |
---|
90 | */ |
---|
91 | inline Iterator<T>& operator=(ObjectListBaseElement* element) |
---|
92 | { |
---|
93 | this->setElement(element); |
---|
94 | return (*this); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | @brief Overloading of the *it operator: returns the pointer to the object. |
---|
99 | @return The object the Iterator points at |
---|
100 | */ |
---|
101 | inline T* operator*() const |
---|
102 | { |
---|
103 | return orxonox_cast<T*>(this->element_->objectBase_); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | @brief Overloading of the it-> operator: returns the pointer to the object. |
---|
108 | @return The object the Iterator points at |
---|
109 | */ |
---|
110 | inline T* operator->() const |
---|
111 | { |
---|
112 | return orxonox_cast<T*>(this->element_->objectBase_); |
---|
113 | } |
---|
114 | }; |
---|
115 | } |
---|
116 | |
---|
117 | #endif /* _Iterator_H__ */ |
---|