Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/libraries/core/ObjectListIterator.h @ 7363

Last change on this file since 7363 was 7363, checked in by landauf, 14 years ago

assigned a group to each header file in the core library

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