source: GTP/trunk/Lib/Geom/OgreStuff/include/OgreIteratorWrappers.h @ 1809

Revision 1809, 12.2 KB checked in by gumbau, 18 years ago (diff)
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-2005 The OGRE Team
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-----------------------------------------------------------------------------
24*/
25#ifndef __IteratorWrappers_H__
26#define __IteratorWrappers_H__
27
28#include "OgrePrerequisites.h"
29
30namespace Ogre {
31
32    /** Wraps iteration over a vector.
33    @remarks
34        This class is here just to allow clients to iterate over an internal
35        vector of a class without having to have access to the vector itself
36        (typically to iterate you need both the iterator and the end() iterator
37        to test for the end condition, which is messy).
38        No updates are allowed through this interface, it is purely for
39        iterating and reading.
40    @par
41        Note that like STL iterators, these iterators are only valid whilst no
42        updates are made to the underlying collection. You should not attempt to
43        use this iterator if a change is made to the collection. In fact, treat this
44        iterator as a transient object, do NOT store it and try to use it repeatedly.
45    */
46    template <class T>
47    class VectorIterator
48    {
49    private:
50        typename T::iterator mCurrent;
51        typename T::iterator mEnd;
52                /// Private constructor since only the parameterised constructor should be used
53        VectorIterator() {};
54    public:
55        typedef typename T::value_type ValueType;
56
57        /** Constructor.
58        @remarks
59            Provide a start and end iterator to initialise.
60        */
61        VectorIterator(typename T::iterator start, typename T::iterator end)
62            : mCurrent(start), mEnd(end)
63        {
64        }
65
66        /** Constructor.
67        @remarks
68            Provide a container to initialise.
69        */
70        explicit VectorIterator(T& c)
71            : mCurrent(c.begin()), mEnd(c.end())
72        {
73        }
74
75        /** Returns true if there are more items in the collection. */
76        bool hasMoreElements(void) const
77        {
78            return mCurrent != mEnd;
79        }
80
81        /** Returns the next element in the collection, and advances to the next. */
82        typename T::value_type getNext(void)
83        {
84            return *mCurrent++;
85        }
86        /** Returns the next element in the collection, without advancing to the next. */
87        typename T::value_type peekNext(void)
88        {
89            return *mCurrent;
90        }
91        /** Returns a pointer to the next element in the collection, without advancing to the next afterwards. */
92        typename T::pointer peekNextPtr(void)
93        {
94            return &(*mCurrent);
95        }
96        /** Moves the iterator on one element. */
97        void moveNext(void)
98        {
99            ++mCurrent;
100        }
101
102
103
104    };
105
106    /** Wraps iteration over a map.
107    @remarks
108        This class is here just to allow clients to iterate over an internal
109        map of a class without having to have access to the map itself
110        (typically to iterate you need both the iterator and the end() iterator
111        to test for the end condition, which is messy).
112        No updates are allowed through this interface, it is purely for
113        iterating and reading.
114    @par
115        Note that like STL iterators, these iterators are only valid whilst no
116        updates are made to the underlying collection. You should not attempt to
117        use this iterator if a change is made to the collection. In fact, treat this
118        iterator as a transient object, do NOT store it and try to use it repeatedly.
119    */
120    template <class T>
121    class MapIterator
122    {
123    private:
124        typename T::iterator mCurrent;
125        typename T::iterator mEnd;
126        /// Private constructor since only the parameterised constructor should be used
127        MapIterator() {};
128    public:
129        typedef typename T::mapped_type MappedType;
130        typedef typename T::key_type KeyType;
131
132        /** Constructor.
133        @remarks
134            Provide a start and end iterator to initialise.
135        */
136        MapIterator(typename T::iterator start, typename T::iterator end)
137            : mCurrent(start), mEnd(end)
138        {
139        }
140
141        /** Constructor.
142        @remarks
143            Provide a container to initialise.
144        */
145        explicit MapIterator(T& c)
146            : mCurrent(c.begin()), mEnd(c.end())
147        {
148        }
149
150        /** Returns true if there are more items in the collection. */
151        bool hasMoreElements(void) const
152        {
153            return mCurrent != mEnd;
154        }
155
156        /** Returns the next value element in the collection, and advances to the next. */
157        typename T::mapped_type getNext(void)
158        {
159            return (mCurrent++)->second;
160        }
161        /** Returns the next value element in the collection, without advancing to the next. */
162        typename T::mapped_type peekNextValue(void)
163        {
164            return mCurrent->second;
165        }
166        /** Returns the next key element in the collection, without advancing to the next. */
167        typename T::key_type peekNextKey(void)
168        {
169            return mCurrent->first;
170        }
171        /** Required to overcome intermittent bug */
172            MapIterator<T> & operator=( MapIterator<T> &rhs )
173            {
174                    mCurrent = rhs.mCurrent;
175                    mEnd = rhs.mEnd;
176                    return *this;
177            }
178        /** Returns a pointer to the next value element in the collection, without
179            advancing to the next afterwards. */
180        typename T::mapped_type* peekNextValuePtr(void)
181        {
182            return &(mCurrent->second);
183        }
184        /** Moves the iterator on one element. */
185        void moveNext(void)
186        {
187            ++mCurrent;
188        }
189
190
191
192    };
193    /** Wraps iteration over a const vector.
194    @remarks
195    This class is here just to allow clients to iterate over an internal
196    vector of a class without having to have access to the vector itself
197    (typically to iterate you need both the iterator and the end() iterator
198    to test for the end condition, which is messy).
199    No updates are allowed through this interface, it is purely for
200    iterating and reading.
201    @par
202    Note that like STL iterators, these iterators are only valid whilst no
203    updates are made to the underlying collection. You should not attempt to
204    use this iterator if a change is made to the collection. In fact, treat this
205    iterator as a transient object, do NOT store it and try to use it repeatedly.
206    */
207    template <class T>
208    class ConstVectorIterator
209    {
210    private:
211        mutable typename T::const_iterator mCurrent;
212        typename T::const_iterator mEnd;
213        /// Private constructor since only the parameterised constructor should be used
214        ConstVectorIterator() {};
215    public:
216        typedef typename T::value_type ValueType;
217
218        /** Constructor.
219        @remarks
220        Provide a start and end iterator to initialise.
221        */
222        ConstVectorIterator(typename T::const_iterator start, typename T::const_iterator end)
223            : mCurrent(start), mEnd(end)
224        {
225        }
226
227        /** Constructor.
228        @remarks
229            Provide a container to initialise.
230        */
231        explicit ConstVectorIterator(const T& c)
232            : mCurrent(c.begin()), mEnd(c.end())
233        {
234        }
235
236        /** Returns true if there are more items in the collection. */
237        bool hasMoreElements(void) const
238        {
239            return mCurrent != mEnd;
240        }
241
242        /** Returns the next element in the collection, and advances to the next. */
243        typename T::value_type getNext(void)
244        {
245            return *mCurrent++;
246        }
247        /** Returns the next element in the collection, without advancing to the next. */
248        typename T::value_type peekNext(void) const
249        {
250            return *mCurrent;
251        }
252        /** Returns a pointer to the next element in the collection, without advancing to the next afterwards. */
253        typename T::const_pointer peekNextPtr(void) const
254        {
255            return &(*mCurrent);
256        }
257        /** Moves the iterator on one element. */
258        void moveNext(void) const
259        {
260            ++mCurrent;
261        }
262
263
264
265    };
266
267    /** Wraps iteration over a const map.
268    @remarks
269    This class is here just to allow clients to iterate over an internal
270    map of a class without having to have access to the map itself
271    (typically to iterate you need both the iterator and the end() iterator
272    to test for the end condition, which is messy).
273    No updates are allowed through this interface, it is purely for
274    iterating and reading.
275    @par
276    Note that like STL iterators, these iterators are only valid whilst no
277    updates are made to the underlying collection. You should not attempt to
278    use this iterator if a change is made to the collection. In fact, treat this
279    iterator as a transient object, do NOT store it and try to use it repeatedly.
280    */
281    template <class T>
282    class ConstMapIterator
283    {
284    private:
285        mutable typename T::const_iterator mCurrent;
286        typename T::const_iterator mEnd;
287        /// Private constructor since only the parameterised constructor should be used
288        ConstMapIterator() {};
289    public:
290        typedef typename T::mapped_type MappedType;
291        typedef typename T::key_type KeyType;
292
293        /** Constructor.
294        @remarks
295        Provide a start and end iterator to initialise.
296        */
297        ConstMapIterator(typename T::const_iterator start, typename T::const_iterator end)
298            : mCurrent(start), mEnd(end)
299        {
300        }
301
302        /** Constructor.
303        @remarks
304            Provide a container to initialise.
305        */
306        explicit ConstMapIterator(const T& c)
307            : mCurrent(c.begin()), mEnd(c.end())
308        {
309        }
310
311        /** Returns true if there are more items in the collection. */
312        bool hasMoreElements(void) const
313        {
314            return mCurrent != mEnd;
315        }
316
317        /** Returns the next value element in the collection, and advances to the next. */
318        typename T::mapped_type getNext(void)
319        {
320            return (mCurrent++)->second;
321        }
322        /** Returns the next value element in the collection, without advancing to the next. */
323        typename T::mapped_type peekNextValue(void) const
324        {
325            return mCurrent->second;
326        }
327        /** Returns the next key element in the collection, without advancing to the next. */
328        typename T::key_type peekNextKey(void) const
329        {
330            return mCurrent->first;
331        }
332        /** Required to overcome intermittent bug */
333        MapIterator<T> & operator=( MapIterator<T> &rhs )
334        {
335            mCurrent = rhs.mCurrent;
336            mEnd = rhs.mEnd;
337            return *this;
338        }
339        /** Returns a pointer to the next value element in the collection, without
340        advancing to the next afterwards. */
341        const typename T::mapped_type* peekNextValuePtr(void) const
342        {
343            return &(mCurrent->second);
344        }
345        /** Moves the iterator on one element. */
346        void moveNext(void) const
347        {
348            ++mCurrent;
349        }
350
351
352
353    };
354}
355#endif
Note: See TracBrowser for help on using the repository browser.