Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7271 was 7271, checked in by rgrieder, 14 years ago

There is no such word as "casted".
Also inserted 4 static_casts and replaced dynamic_cast with orxonox_cast in WeakPtr and SmartPtr.
Moreover found some weird code in Iterator.h with casting. Casting a NULL pointer always yields another NULL pointer.

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