Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1642 was 1625, checked in by rgrieder, 16 years ago

merged hud branch back to trunk

  • Property svn:eol-style set to native
File size: 6.1 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
53namespace orxonox
54{
55    //! The iterator allows to iterate through an ObjectList of a given class.
56    template <class T>
57    class Iterator
58    {
59        public:
60            /**
61                @brief Constructor: Sets the element, whereon the iterator points, to zero.
62            */
63            Iterator()
64            {
65                this->element_ = 0;
66                ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this);
67            }
68
69            /**
70                @brief Constructor: Sets the element, whereon the iterator points, to a given element.
71                @param element The element to start with
72            */
73            Iterator(ObjectListElement<T>* element)
74            {
75                this->element_ = element;
76                ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this);
77            }
78
79            /**
80                @brief Unregisters the Iterator from the ObjectList.
81            */
82            ~Iterator()
83            {
84                ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterIterator(this);
85            }
86
87            /**
88                @brief Assigns an element to the iterator.
89                @param element The element
90            */
91            Iterator<T> operator=(ObjectListElement<T>* element)
92            {
93                this->element_ = element;
94                return *this;
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.