Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/libraries/core/Iterator.h @ 7363

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

assigned a group to each header file in the core library

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