Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core3/src/core/Iterator.h @ 1574

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

big change in ObjectList: separated the class into a non-template base and a template wrapper for the base. this also changes the Iterator, there is now a non-template IteratorBase. this brings much more flexibility, like iterating through all objects of a given identifier without knowing the type. however this needs a dynamic_cast, which isn't quite optimal, but I think there are much worser things than that out there. ;)

there isn't much you have to know about this, except there is no more ObjectList<myClass>::start() function but a ObjectList<myClass>::begin() to be more STLish. another thing: ObjectList<myClass>::end() points now to the element _after_ the last element, so it's possible to iterate in a for-loop until (it != ObjectList<myClass>::end()). the reason is the same as above. however, (it) as a boolean still works perfectly fine.

  • Property svn:eol-style set to native
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<myClass> it = ObjectList<myClass>::begin(); it != ObjectList<myClass>::end(); ++it)
38    {
39        it->someFunction(...);
40        myClass* myObject = *it;
41    }
42*/
43
44#ifndef _Iterator_H__
45#define _Iterator_H__
46
47#include "CorePrereqs.h"
48
49#include "IteratorBase.h"
50
51namespace orxonox
52{
53    //! The Iterator allows to iterate through the ObjectList of a given class.
54    template <class T>
55    class Iterator : public IteratorBase
56    {
57        public:
58            /**
59                @brief Constructor: Sets the element, whereon the iterator points, to zero.
60            */
61            inline Iterator()
62            {
63                ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this);
64            }
65
66            /**
67                @brief Constructor: Sets the element, whereon the iterator points, to a given element.
68                @param element The element to start with
69            */
70            inline Iterator(ObjectListBaseElement* element) : IteratorBase((ObjectListBaseElement*)element)
71            {
72                ClassIdentifier<T>::getIdentifier()->getObjects()->registerIterator(this);
73            }
74
75            /**
76                @brief Unregisters the Iterator from the ObjectList.
77            */
78            inline ~Iterator()
79            {
80                ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterIterator(this);
81            }
82
83            /**
84                @brief Overloading of the ++it operator: Iterator points to the next object in the list.
85                @return The Iterator itself
86            */
87            inline Iterator<T> operator++()
88            {
89                IteratorBase::operator++();
90                return *this;
91            }
92
93            /**
94                @brief Overloading of the it++ operator: Iterator points to the next object in the list.
95                @return The Iterator itself
96            */
97            inline Iterator<T> operator++(int i)
98            {
99                Iterator<T> copy = *this;
100                IteratorBase::operator++();
101                return copy;
102            }
103
104            /**
105                @brief Overloading of the --it operator: Iterator points to the previous object in the list.
106                @return The Iterator itself
107            */
108            inline Iterator<T> operator--()
109            {
110                IteratorBase::operator--();
111                return *this;
112            }
113
114            /**
115                @brief Overloading of the it-- operator: Iterator points to the previous object in the list.
116                @return The Iterator itself
117            */
118            inline Iterator<T> operator--(int i)
119            {
120                Iterator<T> copy = *this;
121                IteratorBase::operator--();
122                return copy;
123            }
124
125            /**
126                @brief Overloading of the *it operator: returns the pointer to the object.
127                @return The object the Iterator points at
128            */
129            inline T* operator*()
130            {
131                return dynamic_cast<T*>(IteratorBase::operator*());
132            }
133
134            /**
135                @brief Overloading of the it-> operator: returns the pointer to the object.
136                @return The object the Iterator points at
137            */
138            inline T* operator->() const
139            {
140                return dynamic_cast<T*>(IteratorBase::operator->());
141            }
142
143            /**
144                @brief Overloading of the == operator to compare with another Iterator.
145                @param compare The other Iterator
146                @return True if the iterators point to the same element
147            */
148            inline bool operator==(const Iterator<T>& compare)
149            {
150                return (this->element_ == compare.element_);
151            }
152
153            /**
154                @brief Overloading of the != operator to compare with another Iterator.
155                @param compare The other Iterator
156                @return True if the iterators point to different elements
157            */
158            inline bool operator!=(const Iterator<T>& compare)
159            {
160                return (this->element_ != compare.element_);
161            }
162    };
163}
164
165// Include ObjectList.h so the user only has to include one file: Iterator.h
166#include "ObjectList.h"
167
168#endif /* _Iterator_H__ */
Note: See TracBrowser for help on using the repository browser.