Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core3/src/core/Iterator.h @ 1591

Last change on this file since 1591 was 1591, checked in by landauf, 16 years ago

Again some heavy changes in ObjectList and Iterator:
there are now two types of iterators:

Iterator<ClassName> can iterate through any objectlist, either given by ObjectList<AnyClassName>::begin() or anyidentifier→getObjects()→begin(). Important note Iterator<ClassName> uses dynamic_cast.
And yes, it's possible to do this: Iterator<WorldEntity> it = ObjectList<SpaceShip>::begin()

ObjectList<ClassName>::iterator is the second iterator - it uses the ObjectList in a templated manner and therefore doesn't need dynamic_cast. But the only thing you can do is iterating through exactly the right ObjectList: ObjectList<ClassName>::iterator it = ObjectList<ClassName>::begin(). Anything else fails.

Those changes bring, at my system, something around +12% FPS compared with trunk and +25% FPS compared with the last revision of core3. Although I have to admit the FPS gain is only that high because iterating through objects is the main thing we're doing ingame right now. It would look totally different with physics, sound, AI, scripts, triggers and so on.

  • Property svn:eol-style set to native
File size: 10.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 Iterator.h
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 casted 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 "ObjectListBase.h"
50#include "ObjectListIterator.h"
51#include "OrxonoxClass.h"
52
53namespace orxonox
54{
55    //! The Iterator allows to iterate through a given ObjectList
56    template <class T = OrxonoxClass>
57    class Iterator
58    {
59        public:
60            /**
61                @brief Constructor: Sets the element, whereon the iterator points, to zero.
62            */
63            inline Iterator()
64            {
65                this->element_ = 0;
66                this->list_ = 0;
67            }
68
69            /**
70                @brief Constructor: Sets this element to the exported element.
71                @param exp The exported element
72            */
73            inline Iterator(const ObjectListBase::Export& exp)
74            {
75                this->element_ = exp.element_;
76                this->list_ = exp.list_;
77                this->iterator_ = this->list_->registerIterator(this);
78            }
79
80            /**
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_;
88                this->iterator_ = this->list_->registerIterator(this);
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            {
98                this->element_ = (element) ? (ObjectListBaseElement*)element : 0;
99                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
100                this->iterator_ = this->list_->registerIterator(this);
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_) ? (ObjectListBaseElement*)other.element_ : 0;
111                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
112                this->iterator_ = this->list_->registerIterator(this);
113            }
114
115            /**
116                @brief Unregisters the Iterator from the ObjectList.
117            */
118            inline ~Iterator()
119            {
120                this->list_->unregisterIterator(this->iterator_);
121            }
122
123            /**
124                @brief Assigns an exported element.
125                @param exp The exported element
126            */
127            inline const Iterator<T>& operator=(const ObjectListBase::Export& exp)
128            {
129                if (this->list_)
130                    this->list_->unregisterIterator(this->iterator_);
131
132                this->element_ = exp.element_;
133                this->list_ = exp.list_;
134                this->iterator_ = this->list_->registerIterator(this);
135
136                return (*this);
137            }
138
139            /**
140                @brief Assigns the element of another Iterator.
141                @param other The other Iterator
142            */
143            inline const Iterator<T>& operator=(const Iterator<T>& other)
144            {
145                if (this->list_)
146                    this->list_->unregisterIterator(this->iterator_);
147
148                this->element_ = other.element_;
149                this->list_ = other.list_;
150                this->iterator_ = this->list_->registerIterator(this);
151
152                return (*this);
153            }
154
155            /**
156                @brief Assigns a given element.
157                @param element The element
158            */
159            template <class O>
160            inline const Iterator<T>& operator=(ObjectListElement<O>* element)
161            {
162                if (this->list_)
163                    this->list_->unregisterIterator(this->iterator_);
164
165                this->element_ = (element) ? (ObjectListBaseElement*)element : 0;
166                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
167                this->iterator_ = this->list_->registerIterator(this);
168
169                return (*this);
170            }
171
172            /**
173                @brief Assigns the element of an ObjectListIterator.
174                @param other The ObjectListIterator
175            */
176            template <class O>
177            inline const Iterator<T>& operator=(const ObjectListIterator<O>& other)
178            {
179                if (this->list_)
180                    this->list_->unregisterIterator(this->iterator_);
181
182                this->element_ = (other.element_) ? (ObjectListBaseElement*)other.element_ : 0;
183                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
184                this->iterator_ = this->list_->registerIterator(this);
185
186                return (*this);
187            }
188
189            /**
190                @brief Overloading of the ++it operator: Iterator points to the next object in the list.
191                @return The Iterator itself
192            */
193            inline const Iterator<T>& operator++()
194            {
195                if (this->element_)
196                    this->element_ = this->element_->next_;
197                return *this;
198            }
199
200            /**
201                @brief Overloading of the it++ operator: Iterator points to the next object in the list.
202                @return The Iterator itself
203            */
204            inline Iterator<T> operator++(int i)
205            {
206                Iterator<T> copy = *this;
207                if (this->element_)
208                    this->element_ = this->element_->next_;
209                return copy;
210            }
211
212            /**
213                @brief Overloading of the --it operator: Iterator points to the previous object in the list.
214                @return The Iterator itself
215            */
216            inline const Iterator<T>& operator--()
217            {
218                if (this->element_)
219                    this->element_ = this->element_->prev_;
220                return *this;
221            }
222
223            /**
224                @brief Overloading of the it-- operator: Iterator points to the previous object in the list.
225                @return The Iterator itself
226            */
227            inline Iterator<T> operator--(int i)
228            {
229                Iterator<T> copy = *this;
230                if (this->element_)
231                    this->element_ = this->element_->prev_;
232                return copy;
233            }
234
235            /**
236                @brief Overloading of the *it operator: returns the pointer to the object.
237                @return The object the Iterator points at
238            */
239            inline T* operator*() const
240            {
241                if (this->element_)
242                    return dynamic_cast<T*>(this->element_->objectBase_);
243                else
244                    return 0;
245            }
246
247            /**
248                @brief Overloading of the it-> operator: returns the pointer to the object.
249                @return The object the Iterator points at
250            */
251            inline T* operator->() const
252            {
253                if (this->element_)
254                    return dynamic_cast<T*>(this->element_->objectBase_);
255                else
256                    return 0;
257            }
258
259            /**
260                @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
261                @return True if the Iterator points to an existing object.
262            */
263            inline operator bool() const
264            {
265                return (this->element_ != 0);
266            }
267
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 the same element
272            */
273            inline bool operator==(const Iterator<T>& compare) const
274            {
275                return (this->element_ == compare.element_);
276            }
277
278            /**
279                @brief Overloading of the != operator to compare with another Iterator.
280                @param compare The other Iterator
281                @return True if the iterators point to different elements
282            */
283            inline bool operator!=(const Iterator<T>& compare) const
284            {
285                return (this->element_ != compare.element_);
286            }
287
288            /**
289                @brief Increments the Iterator if it points at the given object.
290                @param object The object to compare with
291            */
292            inline void incrementIfEqual(OrxonoxClass* object)
293            {
294                if (this->element_ && this->element_->objectBase_ == object)
295                    this->operator++();
296            }
297
298        protected:
299            ObjectListBaseElement* element_;       //!< The element the Iterator points at
300            ObjectListBase* list_;                 //!< The list wherein the element is
301            std::list<void*>::iterator iterator_;  //!< The iterator in the notifying list of the ObjectList
302    };
303
304    typedef Iterator<OrxonoxClass> BaseIterator;
305}
306
307// Include ObjectList.h so the user only has to include one file: Iterator.h
308#include "ObjectList.h"
309
310#endif /* _Iterator_H__ */
Note: See TracBrowser for help on using the repository browser.