Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5738 was 5738, checked in by landauf, 15 years ago

merged libraries2 back to trunk

  • Property svn:eol-style set to native
File size: 8.3 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 const 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 const 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                if (this->element_)
126                    this->element_ = static_cast<ObjectListElement<T>*>(this->element_->next_);
127                return *this;
128            }
129
130            /**
131                @brief Overloading of the it++ operator: ObjectListIterator points to the next object in the list.
132                @return The ObjectListIterator itself
133            */
134            inline ObjectListIterator<T> operator++(int i)
135            {
136                ObjectListIterator<T> copy = *this;
137                if (this->element_)
138                    this->element_ = static_cast<ObjectListElement<T>*>(this->element_->next_);
139                return copy;
140            }
141
142            /**
143                @brief Overloading of the --it operator: ObjectListIterator points to the previous object in the list.
144                @return The ObjectListIterator itself
145            */
146            inline const ObjectListIterator<T>& operator--()
147            {
148                if (this->element_)
149                    this->element_ = static_cast<ObjectListElement<T>*>(this->element_->prev_);
150                return *this;
151            }
152
153            /**
154                @brief Overloading of the it-- operator: ObjectListIterator points to the previous object in the list.
155                @return The ObjectListIterator itself
156            */
157            inline ObjectListIterator<T> operator--(int i)
158            {
159                ObjectListIterator<T> copy = *this;
160                if (this->element_)
161                    this->element_ = static_cast<ObjectListElement<T>*>(this->element_->prev_);
162                return copy;
163            }
164
165            /**
166                @brief Overloading of the *it operator: returns the pointer to the object.
167                @return The object the ObjectListIterator points at
168            */
169            inline T* operator*() const
170            {
171                if (this->element_)
172                    return this->element_->object_;
173                else
174                    return 0;
175            }
176
177            /**
178                @brief Overloading of the it-> operator: returns the pointer to the object.
179                @return The object the ObjectListIterator points at
180            */
181            inline T* operator->() const
182            {
183                if (this->element_)
184                    return this->element_->object_;
185                else
186                    return 0;
187            }
188
189            /**
190                @brief Overloading of the typecast-operator to bool: returns true if the ObjectListIterator points to an existing object.
191                @return True if the ObjectListIterator points to an existing object.
192            */
193            inline operator bool() const
194            {
195                return (this->element_ != 0);
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 the same element
202            */
203            inline bool operator==(const ObjectListIterator<T>& compare) const
204            {
205                return (this->element_ == compare.element_);
206            }
207
208            /**
209                @brief Overloading of the != operator to compare with another ObjectListIterator.
210                @param compare The other ObjectListIterator
211                @return True if the ObjectListIterator point to different elements
212            */
213            inline bool operator!=(const ObjectListIterator<T>& compare) const
214            {
215                return (this->element_ != compare.element_);
216            }
217
218            /**
219                @brief Increments the ObjectListIterator if it points at the given object.
220                @param object The object to compare with
221            */
222            inline void incrementIfEqual(OrxonoxClass* object)
223            {
224                if (this->element_ && this->element_->objectBase_ == object)
225                    this->operator++();
226            }
227
228        private:
229            ObjectListElement<T>* element_;        //!< The element the Iterator points at
230    };
231}
232
233#endif /* _ObjectListIterator_H__ */
Note: See TracBrowser for help on using the repository browser.