Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1621 was 1543, checked in by rgrieder, 17 years ago
  • applied patch to remove ClassManager (wouldn't wanna maintain it anymore ;))
  • sorted core CMLs a little bit
  • updated OrxonoxStableHeaders.h
  • 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            }
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++()
101            {
102                if (this->element_)
103                    this->element_ = this->element_->next_;
104                return *this;
105            }
106
107            /**
108                @brief Overloading of the it++ operator: Iterator points to the next object in the list.
109                @return The Iterator itself
110            */
111            Iterator<T> operator++(int i)
112            {
113                Iterator<T> copy = *this;
114                if (this->element_)
115                    this->element_ = this->element_->next_;
116                return copy;
117            }
118
119            /**
120                @brief Overloading of the --it operator: Iterator points to the previous object in the list.
121                @return The Iterator itself
122            */
123            Iterator<T> operator--()
124            {
125                if (this->element_)
126                    this->element_ = this->element_->prev_;
127                return *this;
128            }
129
130            /**
131                @brief Overloading of the it-- operator: Iterator points to the previous object in the list.
132                @return The Iterator itself
133            */
134            Iterator<T> operator--(int i)
135            {
136                Iterator<T> copy = *this;
137                if (this->element_)
138                    this->element_ = this->element_->prev_;
139                return copy;
140            }
141
142            /**
143                @brief Overloading of the *it operator: returns the pointer to the object.
144                @return The object the Iterator points at
145            */
146            T* operator*()
147            {
148                if (this->element_)
149                    return this->element_->object_;
150                else
151                    return 0;
152            }
153
154            /**
155                @brief Overloading of the it-> operator: returns the pointer to the object.
156                @return The object the Iterator points at
157            */
158            T* operator->() const
159            {
160                if (this->element_)
161                    return this->element_->object_;
162                else
163                    return 0;
164
165            }
166
167            /**
168                @brief Overloading of the typecast-operator to bool: returns true if the iterator points to an existing object.
169                @return True if the iterator points to an existing object.
170            */
171            operator bool()
172            {
173                return (this->element_ != 0);
174            }
175
176            /**
177                @brief Overloading of the (it != int) operator: Used for (it != 0) instead of typecast-operator to bool.
178                @param compare The integer (must be zero, everything else makes no sense).
179                @return True if the iterator points to an existing object.
180            */
181            bool operator!=(ObjectListElement<T>* compare)
182            {
183                return (this->element_ != compare);
184            }
185
186        private:
187            ObjectListElement<T>* element_;     //!< The element the Iterator points at
188    };
189}
190
191#endif /* _Iterator_H__ */
Note: See TracBrowser for help on using the repository browser.