source: OGRE/trunk/ogrenew/OgreMain/include/OgreIteratorWrappers.h @ 692

Revision 692, 11.4 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

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        /** Returns true if there are more items in the collection. */
67        bool hasMoreElements(void) const
68        {
69            return mCurrent != mEnd;
70        }
71
72        /** Returns the next element in the collection, and advances to the next. */
73        typename T::value_type getNext(void)
74        {
75            return *mCurrent++;
76        }
77        /** Returns the next element in the collection, without advancing to the next. */
78        typename T::value_type peekNext(void)
79        {
80            return *mCurrent;
81        }
82        /** Returns a pointer to the next element in the collection, without advancing to the next afterwards. */
83        typename T::pointer peekNextPtr(void)
84        {
85            return &(*mCurrent);
86        }
87        /** Moves the iterator on one element. */
88        void moveNext(void)
89        {
90            ++mCurrent;
91        }
92
93
94
95    };
96
97    /** Wraps iteration over a map.
98    @remarks
99        This class is here just to allow clients to iterate over an internal
100        map of a class without having to have access to the map itself
101        (typically to iterate you need both the iterator and the end() iterator
102        to test for the end condition, which is messy).
103        No updates are allowed through this interface, it is purely for
104        iterating and reading.
105    @par
106        Note that like STL iterators, these iterators are only valid whilst no
107        updates are made to the underlying collection. You should not attempt to
108        use this iterator if a change is made to the collection. In fact, treat this
109        iterator as a transient object, do NOT store it and try to use it repeatedly.
110    */
111    template <class T>
112    class MapIterator
113    {
114    private:
115        typename T::iterator mCurrent;
116        typename T::iterator mEnd;
117        /// Private constructor since only the parameterised constructor should be used
118        MapIterator() {};
119    public:
120        typedef typename T::mapped_type MappedType;
121        typedef typename T::key_type KeyType;
122
123        /** Constructor.
124        @remarks
125            Provide a start and end iterator to initialise.
126        */
127        MapIterator(typename T::iterator start, typename T::iterator end)
128            : mCurrent(start), mEnd(end)
129        {
130        }
131
132        /** Returns true if there are more items in the collection. */
133        bool hasMoreElements(void) const
134        {
135            return mCurrent != mEnd;
136        }
137
138        /** Returns the next value element in the collection, and advances to the next. */
139        typename T::mapped_type getNext(void)
140        {
141            return (mCurrent++)->second;
142        }
143        /** Returns the next value element in the collection, without advancing to the next. */
144        typename T::mapped_type peekNextValue(void)
145        {
146            return mCurrent->second;
147        }
148        /** Returns the next key element in the collection, without advancing to the next. */
149        typename T::key_type peekNextKey(void)
150        {
151            return mCurrent->first;
152        }
153        /** Required to overcome intermittent bug */
154            MapIterator<T> & operator=( MapIterator<T> &rhs )
155            {
156                    mCurrent = rhs.mCurrent;
157                    mEnd = rhs.mEnd;
158                    return *this;
159            }
160        /** Returns a pointer to the next value element in the collection, without
161            advancing to the next afterwards. */
162        typename T::pointer peekNextValuePtr(void)
163        {
164            return &(mCurrent->second);
165        }
166        /** Moves the iterator on one element. */
167        void moveNext(void)
168        {
169            ++mCurrent;
170        }
171
172
173
174    };
175    /** Wraps iteration over a const vector.
176    @remarks
177    This class is here just to allow clients to iterate over an internal
178    vector of a class without having to have access to the vector itself
179    (typically to iterate you need both the iterator and the end() iterator
180    to test for the end condition, which is messy).
181    No updates are allowed through this interface, it is purely for
182    iterating and reading.
183    @par
184    Note that like STL iterators, these iterators are only valid whilst no
185    updates are made to the underlying collection. You should not attempt to
186    use this iterator if a change is made to the collection. In fact, treat this
187    iterator as a transient object, do NOT store it and try to use it repeatedly.
188    */
189    template <class T>
190    class ConstVectorIterator
191    {
192    private:
193        mutable typename T::const_iterator mCurrent;
194        typename T::const_iterator mEnd;
195        /// Private constructor since only the parameterised constructor should be used
196        ConstVectorIterator() {};
197    public:
198        typedef typename T::value_type ValueType;
199
200        /** Constructor.
201        @remarks
202        Provide a start and end iterator to initialise.
203        */
204        ConstVectorIterator(typename T::const_iterator start, typename T::const_iterator end)
205            : mCurrent(start), mEnd(end)
206        {
207        }
208
209        /** Returns true if there are more items in the collection. */
210        bool hasMoreElements(void) const
211        {
212            return mCurrent != mEnd;
213        }
214
215        /** Returns the next element in the collection, and advances to the next. */
216        typename T::value_type getNext(void)
217        {
218            return *mCurrent++;
219        }
220        /** Returns the next element in the collection, without advancing to the next. */
221        typename T::value_type peekNext(void) const
222        {
223            return *mCurrent;
224        }
225        /** Returns a pointer to the next element in the collection, without advancing to the next afterwards. */
226        typename T::const_pointer peekNextPtr(void) const
227        {
228            return &(*mCurrent);
229        }
230        /** Moves the iterator on one element. */
231        void moveNext(void) const
232        {
233            ++mCurrent;
234        }
235
236
237
238    };
239
240    /** Wraps iteration over a const map.
241    @remarks
242    This class is here just to allow clients to iterate over an internal
243    map of a class without having to have access to the map itself
244    (typically to iterate you need both the iterator and the end() iterator
245    to test for the end condition, which is messy).
246    No updates are allowed through this interface, it is purely for
247    iterating and reading.
248    @par
249    Note that like STL iterators, these iterators are only valid whilst no
250    updates are made to the underlying collection. You should not attempt to
251    use this iterator if a change is made to the collection. In fact, treat this
252    iterator as a transient object, do NOT store it and try to use it repeatedly.
253    */
254    template <class T>
255    class ConstMapIterator
256    {
257    private:
258        mutable typename T::const_iterator mCurrent;
259        typename T::const_iterator mEnd;
260        /// Private constructor since only the parameterised constructor should be used
261        ConstMapIterator() {};
262    public:
263        typedef typename T::mapped_type MappedType;
264        typedef typename T::key_type KeyType;
265
266        /** Constructor.
267        @remarks
268        Provide a start and end iterator to initialise.
269        */
270        ConstMapIterator(typename T::const_iterator start, typename T::const_iterator end)
271            : mCurrent(start), mEnd(end)
272        {
273        }
274
275        /** Returns true if there are more items in the collection. */
276        bool hasMoreElements(void) const
277        {
278            return mCurrent != mEnd;
279        }
280
281        /** Returns the next value element in the collection, and advances to the next. */
282        typename T::mapped_type getNext(void)
283        {
284            return (mCurrent++)->second;
285        }
286        /** Returns the next value element in the collection, without advancing to the next. */
287        typename T::mapped_type peekNextValue(void) const
288        {
289            return mCurrent->second;
290        }
291        /** Returns the next key element in the collection, without advancing to the next. */
292        typename T::key_type peekNextKey(void) const
293        {
294            return mCurrent->first;
295        }
296        /** Required to overcome intermittent bug */
297        MapIterator<T> & operator=( MapIterator<T> &rhs )
298        {
299            mCurrent = rhs.mCurrent;
300            mEnd = rhs.mEnd;
301            return *this;
302        }
303        /** Returns a pointer to the next value element in the collection, without
304        advancing to the next afterwards. */
305        typename T::const_pointer peekNextValuePtr(void) const
306        {
307            return &(mCurrent->second);
308        }
309        /** Moves the iterator on one element. */
310        void moveNext(void) const
311        {
312            ++mCurrent;
313        }
314
315
316
317    };
318}
319#endif
Note: See TracBrowser for help on using the repository browser.