Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1056 was 1056, checked in by landauf, 16 years ago

don't panic, no codechanges!
added a link to www.orxonox.net

File size: 5.5 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            }
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            }
77
78            /**
79                @brief Assigns an element to the iterator.
80                @param element The element
81            */
82            Iterator<T> operator=(ObjectListElement<T>* element)
83            {
84                this->element_ = element;
85            }
86
87            /**
88                @brief Overloading of the ++it operator: Iterator points to the next object in the list.
89                @return The Iterator itself
90            */
91            Iterator<T> operator++()
92            {
93                this->element_ = this->element_->next_;
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++(int i)
102            {
103                Iterator<T> copy = *this;
104                this->element_ = this->element_->next_;
105                return copy;
106            }
107
108            /**
109                @brief Overloading of the --it operator: Iterator points to the previous object in the list.
110                @return The Iterator itself
111            */
112            Iterator<T> operator--()
113            {
114                this->element_ = this->element_->prev_;
115                return *this;
116            }
117
118            /**
119                @brief Overloading of the it-- operator: Iterator points to the previous object in the list.
120                @return The Iterator itself
121            */
122            Iterator<T> operator--(int i)
123            {
124                Iterator<T> copy = *this;
125                this->element_ = this->element_->prev_;
126                return copy;
127            }
128
129            /**
130                @brief Overloading of the *it operator: returns the pointer to the object.
131                @return The object the Iterator points at
132            */
133            T* operator*()
134            {
135                return this->element_->object_;
136            }
137
138            /**
139                @brief Overloading of the it-> operator: returns the pointer to the object.
140                @return The object the Iterator points at
141            */
142            T* operator->() const
143            {
144                return this->element_->object_;
145
146            }
147
148            /**
149                @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
150                @return True if the iterator points to an existing object.
151            */
152            operator bool()
153            {
154                return (this->element_ != 0);
155            }
156
157            /**
158                @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool.
159                @param compare The integer (must be zero, everything else makes no sense).
160                @return True if the iterator points to an existing object.
161            */
162            bool operator!=(ObjectListElement<T>* compare)
163            {
164                return (this->element_ != compare);
165            }
166
167        private:
168            ObjectListElement<T>* element_;     //!< The element the Iterator points at
169    };
170}
171
172#endif /* _Iterator_H__ */
Note: See TracBrowser for help on using the repository browser.