[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 | /** |
---|
[2171] | 30 | @file |
---|
[7401] | 31 | @ingroup Object ObjectList |
---|
| 32 | @brief Definition of the Iterator class, used to iterate through object-lists. |
---|
[1505] | 33 | |
---|
[7401] | 34 | @anchor IteratorExample |
---|
[1505] | 35 | |
---|
[7401] | 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 | |
---|
[1505] | 43 | Usage: |
---|
[7401] | 44 | @code |
---|
[1854] | 45 | for (Iterator<myClass> it = anyidentifier->getObjects()->begin(); it != anyidentifier->getObjects()->end(); ++it) |
---|
[1505] | 46 | { |
---|
| 47 | it->someFunction(...); |
---|
[1747] | 48 | myClass* myObject = *it; |
---|
[1505] | 49 | } |
---|
[7401] | 50 | @endcode |
---|
[1505] | 51 | */ |
---|
| 52 | |
---|
| 53 | #ifndef _Iterator_H__ |
---|
| 54 | #define _Iterator_H__ |
---|
| 55 | |
---|
[9557] | 56 | #include "core/CorePrereqs.h" |
---|
[1505] | 57 | |
---|
[1747] | 58 | #include "ObjectListBase.h" |
---|
[9667] | 59 | #include "IteratorBase.h" |
---|
[1505] | 60 | |
---|
| 61 | namespace orxonox |
---|
| 62 | { |
---|
[7401] | 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 | */ |
---|
[9573] | 71 | template <class T> |
---|
[9667] | 72 | class Iterator : public IteratorBase<T, Iterator<T> > |
---|
[1505] | 73 | { |
---|
| 74 | public: |
---|
| 75 | /** |
---|
| 76 | @brief Constructor: Sets the element, whereon the iterator points, to zero. |
---|
| 77 | */ |
---|
[9667] | 78 | inline Iterator() : IteratorBase<T, Iterator<T> >(NULL) {} |
---|
[1505] | 79 | |
---|
| 80 | /** |
---|
[1747] | 81 | @brief Constructor: Sets this element to a given element |
---|
| 82 | @param element The element |
---|
| 83 | */ |
---|
[9667] | 84 | inline Iterator(ObjectListBaseElement* element) : IteratorBase<T, Iterator<T> >(element) {} |
---|
[1747] | 85 | |
---|
| 86 | /** |
---|
[9667] | 87 | @brief Constructor: Sets this element to the element of another Iterator. |
---|
[1747] | 88 | @param other The other Iterator |
---|
| 89 | */ |
---|
[9667] | 90 | inline Iterator(const Iterator<T>& other) : IteratorBase<T, Iterator<T> >(other) {} |
---|
[1747] | 91 | |
---|
| 92 | /** |
---|
| 93 | @brief Assigns a given element. |
---|
[1505] | 94 | @param element The element |
---|
| 95 | */ |
---|
[9667] | 96 | inline Iterator<T>& operator=(ObjectListBaseElement* element) |
---|
[1505] | 97 | { |
---|
[9667] | 98 | this->setElement(element); |
---|
[1747] | 99 | return (*this); |
---|
[1505] | 100 | } |
---|
| 101 | |
---|
| 102 | /** |
---|
| 103 | @brief Overloading of the *it operator: returns the pointer to the object. |
---|
| 104 | @return The object the Iterator points at |
---|
| 105 | */ |
---|
[1747] | 106 | inline T* operator*() const |
---|
[1505] | 107 | { |
---|
[5929] | 108 | return orxonox_cast<T*>(this->element_->objectBase_); |
---|
[1505] | 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | @brief Overloading of the it-> operator: returns the pointer to the object. |
---|
| 113 | @return The object the Iterator points at |
---|
| 114 | */ |
---|
[1747] | 115 | inline T* operator->() const |
---|
[1505] | 116 | { |
---|
[5929] | 117 | return orxonox_cast<T*>(this->element_->objectBase_); |
---|
[1505] | 118 | } |
---|
| 119 | }; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | #endif /* _Iterator_H__ */ |
---|