Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/ObjectListIterator.h @ 7268

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

operator=() should not return constant references.

  • Property svn:eol-style set to native
File size: 7.9 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 ObjectListIterator of a given class allows to iterate through the
34    ObjectList of this class, containing all objects of that type.
35    This is the only way to access the objects stored in an ObjectList.
36
37    Usage:
38    for (ObjectListIterator<myClass> it = ObjectList<myClass>::begin(); it != ObjectList<myClass>::end(); ++it)
39    {
40        it->someFunction(...);
41        myClass* myObject = *it;
42    }
43*/
44
45#ifndef _ObjectListIterator_H__
46#define _ObjectListIterator_H__
47
48#include "CorePrereqs.h"
49#include "Identifier.h"
50#include "ObjectList.h"
51
52namespace orxonox
53{
54    //! The Iterator allows to iterate through the ObjectList of a given class.
55    template <class T>
56    class ObjectListIterator
57    {
58        template <class I>
59        friend class Iterator;
60
61        public:
62            /**
63                @brief Constructor: Sets the element, whereon the ObjectListIterator points, to zero.
64            */
65            inline ObjectListIterator()
66            {
67                this->element_ = 0;
68                ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
69            }
70
71            /**
72                @brief Constructor: Sets this element to a given element.
73                @param element The element to start with
74            */
75            inline ObjectListIterator(ObjectListElement<T>* element)
76            {
77                this->element_ = element;
78                ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
79            }
80
81            /**
82                @brief Constructor: Sets this element to the element of another ObjectListIterator.
83                @param other The other ObjectListIterator
84            */
85            inline ObjectListIterator(const ObjectListIterator<T>& other)
86            {
87                this->element_ = other.element_;
88                ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
89            }
90
91            /**
92                @brief Unregisters the ObjectListIterator from the ObjectList.
93            */
94            inline ~ObjectListIterator()
95            {
96                ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterObjectListIterator(this);
97            }
98
99            /**
100                @brief Assigns an ObjectListElement.
101                @param element The ObjectListElement
102            */
103            inline ObjectListIterator<T>& operator=(ObjectListElement<T>* element)
104            {
105                this->element_ = element;
106                return (*this);
107            }
108
109            /**
110                @brief Assigns the element of another ObjectListIterator.
111                @param element The other ObjectListIterator
112            */
113            inline ObjectListIterator<T>& operator=(const ObjectListIterator<T>& other)
114            {
115                this->element_ = other.element_;
116                return (*this);
117            }
118
119            /**
120                @brief Overloading of the ++it operator: ObjectListIterator points to the next object in the list.
121                @return The ObjectListIterator itself
122            */
123            inline const ObjectListIterator<T>& operator++()
124            {
125                this->element_ = static_cast<ObjectListElement<T>*>(this->element_->next_);
126                return *this;
127            }
128
129            /**
130                @brief Overloading of the it++ operator: ObjectListIterator points to the next object in the list.
131                @return The ObjectListIterator itself
132            */
133            inline ObjectListIterator<T> operator++(int i)
134            {
135                ObjectListIterator<T> copy = *this;
136                this->element_ = static_cast<ObjectListElement<T>*>(this->element_->next_);
137                return copy;
138            }
139
140            /**
141                @brief Overloading of the --it operator: ObjectListIterator points to the previous object in the list.
142                @return The ObjectListIterator itself
143            */
144            inline const ObjectListIterator<T>& operator--()
145            {
146                this->element_ = static_cast<ObjectListElement<T>*>(this->element_->prev_);
147                return *this;
148            }
149
150            /**
151                @brief Overloading of the it-- operator: ObjectListIterator points to the previous object in the list.
152                @return The ObjectListIterator itself
153            */
154            inline ObjectListIterator<T> operator--(int i)
155            {
156                ObjectListIterator<T> copy = *this;
157                this->element_ = static_cast<ObjectListElement<T>*>(this->element_->prev_);
158                return copy;
159            }
160
161            /**
162                @brief Overloading of the *it operator: returns the pointer to the object.
163                @return The object the ObjectListIterator points at
164            */
165            inline T* operator*() const
166            {
167                return this->element_->object_;
168            }
169
170            /**
171                @brief Overloading of the it-> operator: returns the pointer to the object.
172                @return The object the ObjectListIterator points at
173            */
174            inline T* operator->() const
175            {
176                return this->element_->object_;
177            }
178
179            /**
180                @brief Overloading of the typecast-operator to bool: returns true if the ObjectListIterator points to an existing object.
181                @return True if the ObjectListIterator points to an existing object.
182            */
183            inline operator bool() const
184            {
185                return (this->element_ != 0);
186            }
187
188            /**
189                @brief Overloading of the == operator to compare with another ObjectListIterator.
190                @param compare The other ObjectListIterator
191                @return True if the ObjectListIterator point to the same element
192            */
193            inline bool operator==(const ObjectListIterator<T>& compare) const
194            {
195                return (this->element_ == compare.element_);
196            }
197
198            /**
199                @brief Overloading of the != operator to compare with another ObjectListIterator.
200                @param compare The other ObjectListIterator
201                @return True if the ObjectListIterator point to different elements
202            */
203            inline bool operator!=(const ObjectListIterator<T>& compare) const
204            {
205                return (this->element_ != compare.element_);
206            }
207
208            /**
209                @brief Increments the ObjectListIterator if it points at the given object.
210                @param object The object to compare with
211            */
212            inline void incrementIfEqual(OrxonoxClass* object)
213            {
214                if (this->element_ && this->element_->objectBase_ == object)
215                    this->operator++();
216            }
217
218        private:
219            ObjectListElement<T>* element_;        //!< The element the Iterator points at
220    };
221}
222
223#endif /* _ObjectListIterator_H__ */
Note: See TracBrowser for help on using the repository browser.