Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/Iterator.h @ 1348

Last change on this file since 1348 was 1063, checked in by landauf, 17 years ago

fixed an interesting bug in ObjectList/MetaObjectList/Iterator: it's now possible to delete multiple objects in one function-call while iterating through the objects. (and it would have crashed with std::list as well, so no stupid comments please :D)

added a feature in Timer.cc, allowing timers to tick faster than orxonox. this is from core2-branch but got lost while merging.

File size: 6.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 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, containing all objects of that type.
34    This is the only way to access the objects stored in an ObjectList.
35
36    Usage:
37    for (Iterator<class> it = ObjectList<class>::start(); it != 0; ++it)
38    {
39        it->someFunction(...);
40        class* myObject = *it;
41    }
42
43    Warning: Don't delete objects directly through the iterator.
44*/
45
46#ifndef _Iterator_H__
47#define _Iterator_H__
48
49#include "CorePrereqs.h"
50
51#include "ObjectList.h"
52#include "Debug.h"
53
54namespace orxonox
55{
56    //! The iterator allows to iterate through an ObjectList of a given class.
57    template <class T>
58    class Iterator
59    {
60        public:
61            /**
62                @brief Constructor: Sets the element, whereon the iterator points, to zero.
63            */
64            Iterator()
65            {
66                this->element_ = 0;
67                ClassManager<T>::getIdentifier()->getObjects()->registerIterator(this);
68            }
69
70            /**
71                @brief Constructor: Sets the element, whereon the iterator points, to a given element.
72                @param element The element to start with
73            */
74            Iterator(ObjectListElement<T>* element)
75            {
76                this->element_ = element;
77                ClassManager<T>::getIdentifier()->getObjects()->registerIterator(this);
78            }
79
80            /**
81                @brief Unregisters the Iterator from the ObjectList.
82            */
83            ~Iterator()
84            {
85                ClassManager<T>::getIdentifier()->getObjects()->unregisterIterator(this);
86            }
87
88            /**
89                @brief Assigns an element to the iterator.
90                @param element The element
91            */
92            Iterator<T> operator=(ObjectListElement<T>* element)
93            {
94                this->element_ = element;
95            }
96
97            /**
98                @brief Overloading of the ++it operator: Iterator points to the next object in the list.
99                @return The Iterator itself
100            */
101            Iterator<T> operator++()
102            {
103                if (this->element_)
104                    this->element_ = this->element_->next_;
105                return *this;
106            }
107
108            /**
109                @brief Overloading of the it++ operator: Iterator points to the next object in the list.
110                @return The Iterator itself
111            */
112            Iterator<T> operator++(int i)
113            {
114                Iterator<T> copy = *this;
115                if (this->element_)
116                    this->element_ = this->element_->next_;
117                return copy;
118            }
119
120            /**
121                @brief Overloading of the --it operator: Iterator points to the previous object in the list.
122                @return The Iterator itself
123            */
124            Iterator<T> operator--()
125            {
126                if (this->element_)
127                    this->element_ = this->element_->prev_;
128                return *this;
129            }
130
131            /**
132                @brief Overloading of the it-- operator: Iterator points to the previous object in the list.
133                @return The Iterator itself
134            */
135            Iterator<T> operator--(int i)
136            {
137                Iterator<T> copy = *this;
138                if (this->element_)
139                    this->element_ = this->element_->prev_;
140                return copy;
141            }
142
143            /**
144                @brief Overloading of the *it operator: returns the pointer to the object.
145                @return The object the Iterator points at
146            */
147            T* operator*()
148            {
149                if (this->element_)
150                    return this->element_->object_;
151                else
152                    return 0;
153            }
154
155            /**
156                @brief Overloading of the it-> operator: returns the pointer to the object.
157                @return The object the Iterator points at
158            */
159            T* operator->() const
160            {
161                if (this->element_)
162                    return this->element_->object_;
163                else
164                    return 0;
165
166            }
167
168            /**
169                @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
170                @return True if the iterator points to an existing object.
171            */
172            operator bool()
173            {
174                return (this->element_ != 0);
175            }
176
177            /**
178                @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool.
179                @param compare The integer (must be zero, everything else makes no sense).
180                @return True if the iterator points to an existing object.
181            */
182            bool operator!=(ObjectListElement<T>* compare)
183            {
184                return (this->element_ != compare);
185            }
186
187        private:
188            ObjectListElement<T>* element_;     //!< The element the Iterator points at
189    };
190}
191
192#endif /* _Iterator_H__ */
Note: See TracBrowser for help on using the repository browser.