Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/Iterator.h @ 8341

Last change on this file since 8341 was 7401, checked in by landauf, 14 years ago

merged doc branch back to trunk

  • Property svn:eol-style set to native
File size: 10.4 KB
RevLine 
[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
56#include "CorePrereqs.h"
57
[3196]58#include "Identifier.h"
[1747]59#include "ObjectListBase.h"
[1505]60
61namespace 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    */
[1747]71    template <class T = OrxonoxClass>
[1505]72    class Iterator
73    {
74        public:
75            /**
76                @brief Constructor: Sets the element, whereon the iterator points, to zero.
77            */
[1747]78            inline Iterator()
[1505]79            {
80                this->element_ = 0;
[1747]81                this->list_ = 0;
[1505]82            }
83
84            /**
[1747]85                @brief Constructor: Sets this element to the exported element.
86                @param exp The exported element
[1505]87            */
[1747]88            inline Iterator(const ObjectListBase::Export& exp)
[1505]89            {
[1747]90                this->element_ = exp.element_;
91                this->list_ = exp.list_;
[2784]92                this->list_->registerIterator(this);
[1505]93            }
94
95            /**
[1747]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_;
[2784]103                this->list_->registerIterator(this);
[1747]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            {
[7271]113                this->element_ = element;
[1747]114                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
[2784]115                this->list_->registerIterator(this);
[1747]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            {
[7271]125                this->element_ = other.element_;
[1747]126                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
[2784]127                this->list_->registerIterator(this);
[1747]128            }
129
130            /**
[1505]131                @brief Unregisters the Iterator from the ObjectList.
132            */
[1747]133            inline ~Iterator()
[1505]134            {
[2784]135                this->list_->unregisterIterator(this);
[1505]136            }
137
138            /**
[1747]139                @brief Assigns an exported element.
140                @param exp The exported element
141            */
[7268]142            inline Iterator<T>& operator=(const ObjectListBase::Export& exp)
[1747]143            {
144                if (this->list_)
[2784]145                    this->list_->unregisterIterator(this);
[1747]146
147                this->element_ = exp.element_;
148                this->list_ = exp.list_;
[2784]149                this->list_->registerIterator(this);
[1747]150
151                return (*this);
152            }
153
154            /**
155                @brief Assigns the element of another Iterator.
156                @param other The other Iterator
157            */
[7268]158            inline Iterator<T>& operator=(const Iterator<T>& other)
[1747]159            {
160                if (this->list_)
[2784]161                    this->list_->unregisterIterator(this);
[1747]162
163                this->element_ = other.element_;
164                this->list_ = other.list_;
[2784]165                this->list_->registerIterator(this);
[1747]166
167                return (*this);
168            }
169
170            /**
171                @brief Assigns a given element.
[1505]172                @param element The element
173            */
[1747]174            template <class O>
[7268]175            inline Iterator<T>& operator=(ObjectListElement<O>* element)
[1505]176            {
[1747]177                if (this->list_)
[2784]178                    this->list_->unregisterIterator(this);
[1747]179
[7271]180                this->element_ = element;
[1747]181                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
[2784]182                this->list_->registerIterator(this);
[1747]183
184                return (*this);
[1505]185            }
186
187            /**
[1747]188                @brief Assigns the element of an ObjectListIterator.
189                @param other The ObjectListIterator
190            */
191            template <class O>
[7268]192            inline Iterator<T>& operator=(const ObjectListIterator<O>& other)
[1747]193            {
194                if (this->list_)
[2784]195                    this->list_->unregisterIterator(this);
[1747]196
[7271]197                this->element_ = other.element_;
[1747]198                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
[2784]199                this->list_->registerIterator(this);
[1747]200
201                return (*this);
202            }
203
204            /**
[1505]205                @brief Overloading of the ++it operator: Iterator points to the next object in the list.
206                @return The Iterator itself
207            */
[1747]208            inline const Iterator<T>& operator++()
[1505]209            {
[5929]210                this->element_ = this->element_->next_;
[1505]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            */
[7401]218            inline Iterator<T> operator++(int)
[1505]219            {
220                Iterator<T> copy = *this;
[5929]221                this->element_ = this->element_->next_;
[1505]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            */
[1747]229            inline const Iterator<T>& operator--()
[1505]230            {
[5929]231                this->element_ = this->element_->prev_;
[1505]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            */
[1747]239            inline Iterator<T> operator--(int i)
[1505]240            {
241                Iterator<T> copy = *this;
[5929]242                this->element_ = this->element_->prev_;
[1505]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            */
[1747]250            inline T* operator*() const
[1505]251            {
[5929]252                return orxonox_cast<T*>(this->element_->objectBase_);
[1505]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            */
[1747]259            inline T* operator->() const
[1505]260            {
[5929]261                return orxonox_cast<T*>(this->element_->objectBase_);
[1505]262            }
263
264            /**
265                @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
[1747]266                @return True if the Iterator points to an existing object.
[1505]267            */
[1747]268            inline operator bool() const
[1505]269            {
270                return (this->element_ != 0);
271            }
272
273            /**
[1747]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
[1505]277            */
[1747]278            inline bool operator==(const Iterator<T>& compare) const
[1505]279            {
[1747]280                return (this->element_ == compare.element_);
[1505]281            }
282
[1747]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
[1505]306    };
[1747]307
308    typedef Iterator<OrxonoxClass> BaseIterator;
[1505]309}
310
311#endif /* _Iterator_H__ */
Note: See TracBrowser for help on using the repository browser.