Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/src/orxonox/core/Iterator.h @ 859

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

more or less a copy of the 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 Overloading of the ++it operator: Iterator points to the next object in the list.
79                @return The Iterator itself
80            */
81            Iterator<T> operator++()
82            {
83                this->element_ = this->element_->next_;
84                return *this;
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++(int i)
92            {
93                Iterator<T> copy = *this;
94                this->element_ = this->element_->next_;
95                return copy;
96            }
97
98            /**
99                @brief Overloading of the --it operator: Iterator points to the previous object in the list.
100                @return The Iterator itself
101            */
102            Iterator<T> operator--()
103            {
104                this->element_ = this->element_->prev_;
105                return *this;
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--(int i)
113            {
114                Iterator<T> copy = *this;
115                this->element_ = this->element_->prev_;
116                return copy;
117            }
118
119            /**
120                @brief Overloading of the *it operator: returns the pointer to the object.
121                @return The object the Iterator points at
122            */
123            T* operator*()
124            {
125                return this->element_->object_;
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->() const
133            {
134                return this->element_->object_;
135
136            }
137
138            /**
139                @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
140                @return True if the iterator points to an existing object.
141            */
142            operator bool()
143            {
144                return (this->element_ != 0);
145            }
146
147            /**
148                @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool.
149                @param compare The integer (must be zero, everything else makes no sense).
150                @return True if the iterator points to an existing object.
151            */
152            bool operator!=(int compare)
153            {
154                // Comparing with anything except zero makes no sense
155                if (compare != 0)
156                    COUT(2) << "Warning: Comparing the " << ClassIdentifier<T>::getIdentifier()->getName() << "-List-Iterator with " << compare << " has no effect. Only comparison with 0 works." << std::endl;
157
158                return (this->element_ != 0);
159            }
160
161        private:
162            ObjectListElement<T>* element_;     //!< The element the Iterator points at
163    };
164}
165
166#endif /* _Iterator_H__ */
Note: See TracBrowser for help on using the repository browser.