Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/core/Iterator.h @ 920

Last change on this file since 920 was 871, checked in by landauf, 17 years ago

merged core branch to trunk

File size: 5.5 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28/**
29    @file Iterator.h
30    @brief Definition and implementation of the Iterator class.
31
32    The Iterator of a given class allows to iterate through an ObjectList, containing all objects of that type.
33    This is the only way to access the objects stored in an ObjectList.
34
35    Usage:
36    for (Iterator<class> it = ObjectList<class>::start(); it != 0; ++it)
37    {
38        it->someFunction(...);
39        class* myObject = *it;
40    }
41
42    Warning: Don't delete objects directly through the iterator.
43*/
44
45#ifndef _Iterator_H__
46#define _Iterator_H__
47
48#include "CorePrereqs.h"
49
50#include "ObjectList.h"
51#include "Debug.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            }
67
68            /**
69                @brief Constructor: Sets the element, whereon the iterator points, to a given element.
70                @param element The element to start with
71            */
72            Iterator(ObjectListElement<T>* element)
73            {
74                this->element_ = element;
75            }
76
77            /**
78                @brief Assigns an element to the iterator.
79                @param element The element
80            */
81            Iterator<T> operator=(ObjectListElement<T>* element)
82            {
83                this->element_ = element;
84            }
85
86            /**
87                @brief Overloading of the ++it operator: Iterator points to the next object in the list.
88                @return The Iterator itself
89            */
90            Iterator<T> operator++()
91            {
92                this->element_ = this->element_->next_;
93                return *this;
94            }
95
96            /**
97                @brief Overloading of the it++ operator: Iterator points to the next object in the list.
98                @return The Iterator itself
99            */
100            Iterator<T> operator++(int i)
101            {
102                Iterator<T> copy = *this;
103                this->element_ = this->element_->next_;
104                return copy;
105            }
106
107            /**
108                @brief Overloading of the --it operator: Iterator points to the previous object in the list.
109                @return The Iterator itself
110            */
111            Iterator<T> operator--()
112            {
113                this->element_ = this->element_->prev_;
114                return *this;
115            }
116
117            /**
118                @brief Overloading of the it-- operator: Iterator points to the previous object in the list.
119                @return The Iterator itself
120            */
121            Iterator<T> operator--(int i)
122            {
123                Iterator<T> copy = *this;
124                this->element_ = this->element_->prev_;
125                return copy;
126            }
127
128            /**
129                @brief Overloading of the *it operator: returns the pointer to the object.
130                @return The object the Iterator points at
131            */
132            T* operator*()
133            {
134                return this->element_->object_;
135            }
136
137            /**
138                @brief Overloading of the it-> operator: returns the pointer to the object.
139                @return The object the Iterator points at
140            */
141            T* operator->() const
142            {
143                return this->element_->object_;
144
145            }
146
147            /**
148                @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
149                @return True if the iterator points to an existing object.
150            */
151            operator bool()
152            {
153                return (this->element_ != 0);
154            }
155
156            /**
157                @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool.
158                @param compare The integer (must be zero, everything else makes no sense).
159                @return True if the iterator points to an existing object.
160            */
161            bool operator!=(ObjectListElement<T>* compare)
162            {
163                return (this->element_ != compare);
164            }
165
166        private:
167            ObjectListElement<T>* element_;     //!< The element the Iterator points at
168    };
169}
170
171#endif /* _Iterator_H__ */
Note: See TracBrowser for help on using the repository browser.