Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core6/src/libraries/core/object/Iterator.h @ 9596

Last change on this file since 9596 was 9596, checked in by landauf, 11 years ago

ObjectListBaseElement now stores a pointer to the object list it belongs to. Removed ObjectListBase::Export. Iterator will now always use the list from the element.

  • Property svn:eol-style set to native
File size: 9.5 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    @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
60namespace orxonox
61{
62    /**
63        @brief The Iterator allows to iterate through a given ObjectList.
64
65        Independent of the object-list's type, the objects in the list are always casted
66        to @a T using @c dynamic_cast.
67
68        @see See @ref IteratorExample "Iterator.h" for more information an example.
69    */
70    template <class T>
71    class Iterator
72    {
73        public:
74            /**
75                @brief Constructor: Sets the element, whereon the iterator points, to zero.
76            */
77            inline Iterator()
78            {
79                this->element_ = NULL;
80                this->list_ = NULL;
81            }
82
83            /**
84                @brief Constructor: Sets this element to the element of another Iterator.
85                @param other The other Iterator
86            */
87            inline Iterator(const Iterator<T>& other)
88            {
89                this->element_ = other.element_;
90                this->registerIterator();
91            }
92
93            /**
94                @brief Constructor: Sets this element to a given element
95                @param element The element
96            */
97            inline Iterator(ObjectListBaseElement* element)
98            {
99                this->element_ = element;
100                this->registerIterator();
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            {
110                this->element_ = other.element_;
111                this->registerIterator();
112            }
113
114            /**
115                @brief Unregisters the Iterator from the ObjectList.
116            */
117            inline ~Iterator()
118            {
119                this->unregisterIterator();
120            }
121
122            /**
123                @brief Assigns the element of another Iterator.
124                @param other The other Iterator
125            */
126            inline Iterator<T>& operator=(const Iterator<T>& other)
127            {
128                this->unregisterIterator();
129                this->element_ = other.element_;
130                this->registerIterator();
131
132                return (*this);
133            }
134
135            /**
136                @brief Assigns a given element.
137                @param element The element
138            */
139            inline Iterator<T>& operator=(ObjectListBaseElement* element)
140            {
141                this->unregisterIterator();
142                this->element_ = element;
143                this->registerIterator();
144
145                return (*this);
146            }
147
148            /**
149                @brief Assigns the element of an ObjectListIterator.
150                @param other The ObjectListIterator
151            */
152            template <class O>
153            inline Iterator<T>& operator=(const ObjectListIterator<O>& other)
154            {
155                this->unregisterIterator();
156                this->element_ = other.element_;
157                this->registerIterator();
158
159                return (*this);
160            }
161
162            /**
163                @brief Overloading of the ++it operator: Iterator points to the next object in the list.
164                @return The Iterator itself
165            */
166            inline const Iterator<T>& operator++()
167            {
168                this->element_ = this->element_->next_;
169                return *this;
170            }
171
172            /**
173                @brief Overloading of the it++ operator: Iterator points to the next object in the list.
174                @return The Iterator itself
175            */
176            inline Iterator<T> operator++(int)
177            {
178                Iterator<T> copy = *this;
179                this->element_ = this->element_->next_;
180                return copy;
181            }
182
183            /**
184                @brief Overloading of the --it operator: Iterator points to the previous object in the list.
185                @return The Iterator itself
186            */
187            inline const Iterator<T>& operator--()
188            {
189                this->element_ = this->element_->prev_;
190                return *this;
191            }
192
193            /**
194                @brief Overloading of the it-- operator: Iterator points to the previous object in the list.
195                @return The Iterator itself
196            */
197            inline Iterator<T> operator--(int i)
198            {
199                Iterator<T> copy = *this;
200                this->element_ = this->element_->prev_;
201                return copy;
202            }
203
204            /**
205                @brief Overloading of the *it operator: returns the pointer to the object.
206                @return The object the Iterator points at
207            */
208            inline T* operator*() const
209            {
210                return orxonox_cast<T*>(this->element_->objectBase_);
211            }
212
213            /**
214                @brief Overloading of the it-> operator: returns the pointer to the object.
215                @return The object the Iterator points at
216            */
217            inline T* operator->() const
218            {
219                return orxonox_cast<T*>(this->element_->objectBase_);
220            }
221
222            /**
223                @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
224                @return True if the Iterator points to an existing object.
225            */
226            inline operator bool() const
227            {
228                return (this->element_ != NULL);
229            }
230
231            /**
232                @brief Overloading of the == operator to compare with another Iterator.
233                @param compare The other Iterator
234                @return True if the iterators point to the same element
235            */
236            inline bool operator==(const Iterator<T>& compare) const
237            {
238                return (this->element_ == compare.element_);
239            }
240
241            /**
242                @brief Overloading of the != operator to compare with another Iterator.
243                @param compare The other Iterator
244                @return True if the iterators point to different elements
245            */
246            inline bool operator!=(const Iterator<T>& compare) const
247            {
248                return (this->element_ != compare.element_);
249            }
250
251            /**
252                @brief Increments the Iterator if it points at the given object.
253                @param object The object to compare with
254            */
255            inline void incrementIfEqual(Listable* object)
256            {
257                if (this->element_ && this->element_->objectBase_ == object)
258                    this->operator++();
259            }
260
261        private:
262            /**
263             * @brief Registers the Iterator at the list to which it belongs
264             */
265            inline void registerIterator()
266            {
267                if (this->element_)
268                {
269                    this->list_ = this->element_->list_;
270                    this->list_->registerIterator(this);
271                }
272                else
273                    this->list_ = NULL;
274            }
275
276            /**
277             * @brief Unregisters the Iterator from the list (if any)
278             */
279            inline void unregisterIterator()
280            {
281                if (this->list_)
282                    this->list_->unregisterIterator(this);
283            }
284
285            ObjectListBaseElement* element_;    //!< The element the Iterator points at
286            ObjectListBase* list_;              //!< The list in which the Iterator registered itself
287    };
288}
289
290#endif /* _Iterator_H__ */
Note: See TracBrowser for help on using the repository browser.