Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1394 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
RevLine 
[790]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1056]3 *                    > www.orxonox.net <
[790]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
[871]29/**
[790]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
[221]46#ifndef _Iterator_H__
47#define _Iterator_H__
48
[790]49#include "CorePrereqs.h"
50
51#include "ObjectList.h"
52#include "Debug.h"
53
[221]54namespace orxonox
55{
[790]56    //! The iterator allows to iterate through an ObjectList of a given class.
[221]57    template <class T>
58    class Iterator
59    {
60        public:
[790]61            /**
62                @brief Constructor: Sets the element, whereon the iterator points, to zero.
63            */
[221]64            Iterator()
65            {
[248]66                this->element_ = 0;
[1063]67                ClassManager<T>::getIdentifier()->getObjects()->registerIterator(this);
[221]68            }
69
[790]70            /**
71                @brief Constructor: Sets the element, whereon the iterator points, to a given element.
72                @param element The element to start with
73            */
[248]74            Iterator(ObjectListElement<T>* element)
[221]75            {
[248]76                this->element_ = element;
[1063]77                ClassManager<T>::getIdentifier()->getObjects()->registerIterator(this);
[221]78            }
79
[790]80            /**
[1063]81                @brief Unregisters the Iterator from the ObjectList.
82            */
83            ~Iterator()
84            {
85                ClassManager<T>::getIdentifier()->getObjects()->unregisterIterator(this);
86            }
87
88            /**
[871]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            /**
[790]98                @brief Overloading of the ++it operator: Iterator points to the next object in the list.
99                @return The Iterator itself
100            */
[225]101            Iterator<T> operator++()
102            {
[1063]103                if (this->element_)
104                    this->element_ = this->element_->next_;
[225]105                return *this;
106            }
107
[790]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;
[1063]115                if (this->element_)
116                    this->element_ = this->element_->next_;
[790]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            */
[225]124            Iterator<T> operator--()
125            {
[1063]126                if (this->element_)
127                    this->element_ = this->element_->prev_;
[225]128                return *this;
[221]129            }
130
[790]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;
[1063]138                if (this->element_)
139                    this->element_ = this->element_->prev_;
[790]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            */
[221]147            T* operator*()
148            {
[1063]149                if (this->element_)
150                    return this->element_->object_;
151                else
152                    return 0;
[221]153            }
154
[790]155            /**
156                @brief Overloading of the it-> operator: returns the pointer to the object.
157                @return The object the Iterator points at
158            */
[221]159            T* operator->() const
160            {
[1063]161                if (this->element_)
162                    return this->element_->object_;
163                else
164                    return 0;
[221]165
166            }
167
[790]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            */
[221]172            operator bool()
173            {
[248]174                return (this->element_ != 0);
[221]175            }
176
[790]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            */
[871]182            bool operator!=(ObjectListElement<T>* compare)
[221]183            {
[871]184                return (this->element_ != compare);
[221]185            }
186
187        private:
[790]188            ObjectListElement<T>* element_;     //!< The element the Iterator points at
[221]189    };
190}
191
[790]192#endif /* _Iterator_H__ */
Note: See TracBrowser for help on using the repository browser.