Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/include/OgreIteratorWrappers.h @ 1

Last change on this file since 1 was 1, checked in by landauf, 17 years ago
File size: 12.4 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29#ifndef __IteratorWrappers_H__
30#define __IteratorWrappers_H__
31
32#include "OgrePrerequisites.h"
33
34namespace Ogre {
35
36    /** Wraps iteration over a vector.
37    @remarks
38        This class is here just to allow clients to iterate over an internal
39        vector of a class without having to have access to the vector itself
40        (typically to iterate you need both the iterator and the end() iterator
41        to test for the end condition, which is messy).
42        No updates are allowed through this interface, it is purely for
43        iterating and reading.
44    @par
45        Note that like STL iterators, these iterators are only valid whilst no
46        updates are made to the underlying collection. You should not attempt to
47        use this iterator if a change is made to the collection. In fact, treat this
48        iterator as a transient object, do NOT store it and try to use it repeatedly.
49    */
50    template <class T>
51    class VectorIterator
52    {
53    private:
54        typename T::iterator mCurrent;
55        typename T::iterator mEnd;
56                /// Private constructor since only the parameterised constructor should be used
57        VectorIterator() {};
58    public:
59        typedef typename T::value_type ValueType;
60
61        /** Constructor.
62        @remarks
63            Provide a start and end iterator to initialise.
64        */
65        VectorIterator(typename T::iterator start, typename T::iterator end)
66            : mCurrent(start), mEnd(end)
67        {
68        }
69
70        /** Constructor.
71        @remarks
72            Provide a container to initialise.
73        */
74        explicit VectorIterator(T& c)
75            : mCurrent(c.begin()), mEnd(c.end())
76        {
77        }
78
79        /** Returns true if there are more items in the collection. */
80        bool hasMoreElements(void) const
81        {
82            return mCurrent != mEnd;
83        }
84
85        /** Returns the next element in the collection, and advances to the next. */
86        typename T::value_type getNext(void)
87        {
88            return *mCurrent++;
89        }
90        /** Returns the next element in the collection, without advancing to the next. */
91        typename T::value_type peekNext(void)
92        {
93            return *mCurrent;
94        }
95        /** Returns a pointer to the next element in the collection, without advancing to the next afterwards. */
96        typename T::pointer peekNextPtr(void)
97        {
98            return &(*mCurrent);
99        }
100        /** Moves the iterator on one element. */
101        void moveNext(void)
102        {
103            ++mCurrent;
104        }
105
106
107
108    };
109
110    /** Wraps iteration over a map.
111    @remarks
112        This class is here just to allow clients to iterate over an internal
113        map of a class without having to have access to the map itself
114        (typically to iterate you need both the iterator and the end() iterator
115        to test for the end condition, which is messy).
116        No updates are allowed through this interface, it is purely for
117        iterating and reading.
118    @par
119        Note that like STL iterators, these iterators are only valid whilst no
120        updates are made to the underlying collection. You should not attempt to
121        use this iterator if a change is made to the collection. In fact, treat this
122        iterator as a transient object, do NOT store it and try to use it repeatedly.
123    */
124    template <class T>
125    class MapIterator
126    {
127    private:
128        typename T::iterator mCurrent;
129        typename T::iterator mEnd;
130        /// Private constructor since only the parameterised constructor should be used
131        MapIterator() {};
132    public:
133        typedef typename T::mapped_type MappedType;
134        typedef typename T::key_type KeyType;
135
136        /** Constructor.
137        @remarks
138            Provide a start and end iterator to initialise.
139        */
140        MapIterator(typename T::iterator start, typename T::iterator end)
141            : mCurrent(start), mEnd(end)
142        {
143        }
144
145        /** Constructor.
146        @remarks
147            Provide a container to initialise.
148        */
149        explicit MapIterator(T& c)
150            : mCurrent(c.begin()), mEnd(c.end())
151        {
152        }
153
154        /** Returns true if there are more items in the collection. */
155        bool hasMoreElements(void) const
156        {
157            return mCurrent != mEnd;
158        }
159
160        /** Returns the next value element in the collection, and advances to the next. */
161        typename T::mapped_type getNext(void)
162        {
163            return (mCurrent++)->second;
164        }
165        /** Returns the next value element in the collection, without advancing to the next. */
166        typename T::mapped_type peekNextValue(void)
167        {
168            return mCurrent->second;
169        }
170        /** Returns the next key element in the collection, without advancing to the next. */
171        typename T::key_type peekNextKey(void)
172        {
173            return mCurrent->first;
174        }
175        /** Required to overcome intermittent bug */
176            MapIterator<T> & operator=( MapIterator<T> &rhs )
177            {
178                    mCurrent = rhs.mCurrent;
179                    mEnd = rhs.mEnd;
180                    return *this;
181            }
182        /** Returns a pointer to the next value element in the collection, without
183            advancing to the next afterwards. */
184        typename T::mapped_type* peekNextValuePtr(void)
185        {
186            return &(mCurrent->second);
187        }
188        /** Moves the iterator on one element. */
189        void moveNext(void)
190        {
191            ++mCurrent;
192        }
193
194
195
196    };
197    /** Wraps iteration over a const vector.
198    @remarks
199    This class is here just to allow clients to iterate over an internal
200    vector of a class without having to have access to the vector itself
201    (typically to iterate you need both the iterator and the end() iterator
202    to test for the end condition, which is messy).
203    No updates are allowed through this interface, it is purely for
204    iterating and reading.
205    @par
206    Note that like STL iterators, these iterators are only valid whilst no
207    updates are made to the underlying collection. You should not attempt to
208    use this iterator if a change is made to the collection. In fact, treat this
209    iterator as a transient object, do NOT store it and try to use it repeatedly.
210    */
211    template <class T>
212    class ConstVectorIterator
213    {
214    private:
215        mutable typename T::const_iterator mCurrent;
216        typename T::const_iterator mEnd;
217        /// Private constructor since only the parameterised constructor should be used
218        ConstVectorIterator() {};
219    public:
220        typedef typename T::value_type ValueType;
221
222        /** Constructor.
223        @remarks
224        Provide a start and end iterator to initialise.
225        */
226        ConstVectorIterator(typename T::const_iterator start, typename T::const_iterator end)
227            : mCurrent(start), mEnd(end)
228        {
229        }
230
231        /** Constructor.
232        @remarks
233            Provide a container to initialise.
234        */
235        explicit ConstVectorIterator(const T& c)
236            : mCurrent(c.begin()), mEnd(c.end())
237        {
238        }
239
240        /** Returns true if there are more items in the collection. */
241        bool hasMoreElements(void) const
242        {
243            return mCurrent != mEnd;
244        }
245
246        /** Returns the next element in the collection, and advances to the next. */
247        typename T::value_type getNext(void)
248        {
249            return *mCurrent++;
250        }
251        /** Returns the next element in the collection, without advancing to the next. */
252        typename T::value_type peekNext(void) const
253        {
254            return *mCurrent;
255        }
256        /** Returns a pointer to the next element in the collection, without advancing to the next afterwards. */
257        typename T::const_pointer peekNextPtr(void) const
258        {
259            return &(*mCurrent);
260        }
261        /** Moves the iterator on one element. */
262        void moveNext(void) const
263        {
264            ++mCurrent;
265        }
266
267
268
269    };
270
271    /** Wraps iteration over a const map.
272    @remarks
273    This class is here just to allow clients to iterate over an internal
274    map of a class without having to have access to the map itself
275    (typically to iterate you need both the iterator and the end() iterator
276    to test for the end condition, which is messy).
277    No updates are allowed through this interface, it is purely for
278    iterating and reading.
279    @par
280    Note that like STL iterators, these iterators are only valid whilst no
281    updates are made to the underlying collection. You should not attempt to
282    use this iterator if a change is made to the collection. In fact, treat this
283    iterator as a transient object, do NOT store it and try to use it repeatedly.
284    */
285    template <class T>
286    class ConstMapIterator
287    {
288    private:
289        mutable typename T::const_iterator mCurrent;
290        typename T::const_iterator mEnd;
291        /// Private constructor since only the parameterised constructor should be used
292        ConstMapIterator() {};
293    public:
294        typedef typename T::mapped_type MappedType;
295        typedef typename T::key_type KeyType;
296
297        /** Constructor.
298        @remarks
299        Provide a start and end iterator to initialise.
300        */
301        ConstMapIterator(typename T::const_iterator start, typename T::const_iterator end)
302            : mCurrent(start), mEnd(end)
303        {
304        }
305
306        /** Constructor.
307        @remarks
308            Provide a container to initialise.
309        */
310        explicit ConstMapIterator(const T& c)
311            : mCurrent(c.begin()), mEnd(c.end())
312        {
313        }
314
315        /** Returns true if there are more items in the collection. */
316        bool hasMoreElements(void) const
317        {
318            return mCurrent != mEnd;
319        }
320
321        /** Returns the next value element in the collection, and advances to the next. */
322        typename T::mapped_type getNext(void)
323        {
324            return (mCurrent++)->second;
325        }
326        /** Returns the next value element in the collection, without advancing to the next. */
327        typename T::mapped_type peekNextValue(void) const
328        {
329            return mCurrent->second;
330        }
331        /** Returns the next key element in the collection, without advancing to the next. */
332        typename T::key_type peekNextKey(void) const
333        {
334            return mCurrent->first;
335        }
336        /** Required to overcome intermittent bug */
337        ConstMapIterator<T> & operator=( ConstMapIterator<T> &rhs )
338        {
339            mCurrent = rhs.mCurrent;
340            mEnd = rhs.mEnd;
341            return *this;
342        }
343        /** Returns a pointer to the next value element in the collection, without
344        advancing to the next afterwards. */
345        const typename T::mapped_type* peekNextValuePtr(void) const
346        {
347            return &(mCurrent->second);
348        }
349        /** Moves the iterator on one element. */
350        void moveNext(void) const
351        {
352            ++mCurrent;
353        }
354
355
356
357    };
358}
359#endif
Note: See TracBrowser for help on using the repository browser.