1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2013 Torus Knot Software Ltd |
---|
8 | |
---|
9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
10 | of this software and associated documentation files (the "Software"), to deal |
---|
11 | in the Software without restriction, including without limitation the rights |
---|
12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
13 | copies of the Software, and to permit persons to whom the Software is |
---|
14 | furnished to do so, subject to the following conditions: |
---|
15 | |
---|
16 | The above copyright notice and this permission notice shall be included in |
---|
17 | all copies or substantial portions of the Software. |
---|
18 | |
---|
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
25 | THE SOFTWARE. |
---|
26 | ----------------------------------------------------------------------------- |
---|
27 | */ |
---|
28 | #ifndef __Ogre_Iterator_Wrapper_H__ |
---|
29 | #define __Ogre_Iterator_Wrapper_H__ |
---|
30 | |
---|
31 | #include "OgreHeaderPrefix.h" |
---|
32 | |
---|
33 | namespace Ogre{ |
---|
34 | |
---|
35 | /** |
---|
36 | * |
---|
37 | * @brief Basefunctionality for IteratorWrappers |
---|
38 | * |
---|
39 | * |
---|
40 | * @tparam T a Container like vector list map ... |
---|
41 | * @tparam IteratorType T::iterator or T::const_iterator |
---|
42 | * @tparam ValType T::mapped_type in case of a map, T::value_type for vector, list,... |
---|
43 | * |
---|
44 | * Have a look at VectorIteratorWrapper and MapIteratorWrapper for a concrete usage |
---|
45 | */ |
---|
46 | template <typename T, typename IteratorType, typename ValType> |
---|
47 | class IteratorWrapper |
---|
48 | { |
---|
49 | |
---|
50 | private: |
---|
51 | /// Private constructor since only the parameterised constructor should be used |
---|
52 | IteratorWrapper(); |
---|
53 | |
---|
54 | protected: |
---|
55 | IteratorType mBegin; |
---|
56 | IteratorType mCurrent; |
---|
57 | IteratorType mEnd; |
---|
58 | |
---|
59 | |
---|
60 | public: |
---|
61 | |
---|
62 | /// Type you expect to get by funktions like peekNext(Value) |
---|
63 | typedef ValType ValueType; |
---|
64 | /// Type you expect to get by funktions like peekNext(Value)Ptr |
---|
65 | typedef ValType* PointerType; |
---|
66 | |
---|
67 | /** |
---|
68 | @brief Typedef to fulfill container interface |
---|
69 | |
---|
70 | Useful if you want to use BOOST_FOREACH |
---|
71 | @note there is no distinction between const_iterator and iterator. |
---|
72 | \n keep this in mind if you want to derivate from this class. |
---|
73 | */ |
---|
74 | typedef IteratorType iterator; |
---|
75 | |
---|
76 | /** |
---|
77 | @brief Typedef to fulfill container interface |
---|
78 | |
---|
79 | Useful if you want to use BOOST_FOREACH |
---|
80 | @note there is no distinction between const_iterator and iterator. |
---|
81 | \n keep this in mind if you want to derivate from this class. |
---|
82 | */ |
---|
83 | typedef IteratorType const_iterator; |
---|
84 | |
---|
85 | |
---|
86 | /** Constructor. |
---|
87 | @remarks |
---|
88 | Provide a start and end iterator to initialise. |
---|
89 | */ |
---|
90 | IteratorWrapper ( IteratorType start, IteratorType last ) |
---|
91 | : mBegin( start ), mCurrent ( start ), mEnd ( last ) |
---|
92 | { |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | /** Returns true if there are more items in the collection. */ |
---|
97 | bool hasMoreElements ( ) const |
---|
98 | { |
---|
99 | return mCurrent != mEnd; |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | /** Moves the iterator on one element. */ |
---|
104 | void moveNext ( ) |
---|
105 | { |
---|
106 | ++mCurrent; |
---|
107 | } |
---|
108 | |
---|
109 | /** Bookmark to the begin of the underlying collection */ |
---|
110 | const IteratorType& begin() {return mBegin;} |
---|
111 | |
---|
112 | |
---|
113 | /** Full access to the current iterator */ |
---|
114 | IteratorType& current(){return mCurrent;} |
---|
115 | |
---|
116 | /** Bookmark to the end (one behind the last element) of the underlying collection */ |
---|
117 | const IteratorType& end() {return mEnd;} |
---|
118 | |
---|
119 | |
---|
120 | }; |
---|
121 | |
---|
122 | |
---|
123 | |
---|
124 | /** |
---|
125 | * |
---|
126 | * @brief Prepared IteratorWrapper for container like std::vector |
---|
127 | * |
---|
128 | * |
---|
129 | * @tparam T = Container eg vector |
---|
130 | * @tparam IteratorType T::iterator or T::const_iterator |
---|
131 | * |
---|
132 | * Have a look at VectorIterator and ConstVectorIterator for a more concrete usage |
---|
133 | */ |
---|
134 | template <typename T, typename IteratorType> |
---|
135 | class VectorIteratorWrapper : public IteratorWrapper<T, IteratorType, typename T::value_type> |
---|
136 | { |
---|
137 | |
---|
138 | public: |
---|
139 | typedef typename IteratorWrapper<T, IteratorType, typename T::value_type>::ValueType ValueType ; |
---|
140 | typedef typename IteratorWrapper<T, IteratorType, typename T::value_type>::PointerType PointerType ; |
---|
141 | |
---|
142 | |
---|
143 | /** |
---|
144 | * @brief c'tor |
---|
145 | * |
---|
146 | * Constructor that provide a start and end iterator to initialise. |
---|
147 | * |
---|
148 | * @param start Start iterator |
---|
149 | * @param last End iterator |
---|
150 | */ |
---|
151 | VectorIteratorWrapper ( IteratorType start, IteratorType last ) |
---|
152 | : IteratorWrapper<T, IteratorType, typename T::value_type>( start, last ) |
---|
153 | { |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | /** Returns the next(=current) element in the collection, without advancing to the next. */ |
---|
158 | ValueType peekNext ( ) const |
---|
159 | { |
---|
160 | return *(this->mCurrent); |
---|
161 | } |
---|
162 | |
---|
163 | /** Returns a pointer to the next(=current) element in the collection, without advancing to the next afterwards. */ |
---|
164 | PointerType peekNextPtr ( ) const |
---|
165 | { |
---|
166 | return &(*(this->mCurrent) ); |
---|
167 | } |
---|
168 | |
---|
169 | /** Returns the next(=current) value element in the collection, and advances to the next. */ |
---|
170 | ValueType getNext ( ) |
---|
171 | { |
---|
172 | return *(this->mCurrent++); |
---|
173 | } |
---|
174 | |
---|
175 | }; |
---|
176 | |
---|
177 | |
---|
178 | /** |
---|
179 | * |
---|
180 | * @brief Concrete IteratorWrapper for nonconst access to the underlying container |
---|
181 | * |
---|
182 | * @tparam T Container |
---|
183 | * |
---|
184 | */ |
---|
185 | template <typename T> |
---|
186 | class VectorIterator : public VectorIteratorWrapper<T, typename T::iterator>{ |
---|
187 | public: |
---|
188 | /** Constructor. |
---|
189 | @remarks |
---|
190 | Provide a start and end iterator to initialise. |
---|
191 | */ |
---|
192 | VectorIterator( typename T::iterator start, typename T::iterator last ) |
---|
193 | : VectorIteratorWrapper<T, typename T::iterator>(start , last ) |
---|
194 | { |
---|
195 | } |
---|
196 | |
---|
197 | /** Constructor. |
---|
198 | @remarks |
---|
199 | Provide a container to initialise. |
---|
200 | */ |
---|
201 | explicit VectorIterator( T& c ) |
---|
202 | : VectorIteratorWrapper<T, typename T::iterator> ( c.begin(), c.end() ) |
---|
203 | { |
---|
204 | } |
---|
205 | |
---|
206 | }; |
---|
207 | |
---|
208 | /** |
---|
209 | * |
---|
210 | * @brief Concrete IteratorWrapper for const access to the underlying container |
---|
211 | * |
---|
212 | * |
---|
213 | * @tparam T = Container |
---|
214 | * |
---|
215 | */ |
---|
216 | template <typename T> |
---|
217 | class ConstVectorIterator : public VectorIteratorWrapper<T, typename T::const_iterator>{ |
---|
218 | public: |
---|
219 | /** Constructor. |
---|
220 | @remarks |
---|
221 | Provide a start and end iterator to initialise. |
---|
222 | */ |
---|
223 | ConstVectorIterator( typename T::const_iterator start, typename T::const_iterator last ) |
---|
224 | : VectorIteratorWrapper<T, typename T::const_iterator> (start , last ) |
---|
225 | { |
---|
226 | } |
---|
227 | |
---|
228 | /** Constructor. |
---|
229 | @remarks |
---|
230 | Provide a container to initialise. |
---|
231 | */ |
---|
232 | explicit ConstVectorIterator ( const T& c ) |
---|
233 | : VectorIteratorWrapper<T, typename T::const_iterator> (c.begin() , c.end() ) |
---|
234 | { |
---|
235 | } |
---|
236 | }; |
---|
237 | |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | |
---|
242 | /** |
---|
243 | * |
---|
244 | * @brief Prepared IteratorWrapper for key-value container |
---|
245 | * |
---|
246 | * |
---|
247 | * @tparam T Container (map - or also set ) |
---|
248 | * @tparam IteratorType T::iterator or T::const_iterator |
---|
249 | * |
---|
250 | * Have a look at MapIterator and ConstMapIterator for a concrete usage |
---|
251 | */ |
---|
252 | template <typename T, typename IteratorType> |
---|
253 | class MapIteratorWrapper : public IteratorWrapper<T, IteratorType, typename T::mapped_type> |
---|
254 | { |
---|
255 | |
---|
256 | public: |
---|
257 | /// Redefined ValueType for a map/set |
---|
258 | typedef typename IteratorWrapper<T, IteratorType, typename T::mapped_type>::ValueType ValueType ; |
---|
259 | /// Redefined PointerType for a map/set |
---|
260 | typedef typename IteratorWrapper<T, IteratorType, typename T::mapped_type>::PointerType PointerType ; |
---|
261 | |
---|
262 | /// Unused, just to make it clear that map/set::value_type is not a ValueType |
---|
263 | typedef typename T::value_type PairType ; |
---|
264 | /// Type you expect to get by funktions like peekNextKey |
---|
265 | typedef typename T::key_type KeyType; |
---|
266 | |
---|
267 | /** Constructor. |
---|
268 | @remarks |
---|
269 | Provide a start and end iterator to initialise. |
---|
270 | */ |
---|
271 | MapIteratorWrapper ( IteratorType start, IteratorType last ) |
---|
272 | : IteratorWrapper<T, IteratorType, typename T::mapped_type>( start, last ) |
---|
273 | { |
---|
274 | } |
---|
275 | |
---|
276 | /** Returns the next(=current) key element in the collection, without advancing to the next. */ |
---|
277 | KeyType peekNextKey(void) const |
---|
278 | { |
---|
279 | return this->mCurrent->first; |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | /** Returns the next(=current) value element in the collection, without advancing to the next. */ |
---|
284 | ValueType peekNextValue ( ) const |
---|
285 | { |
---|
286 | return this->mCurrent->second; |
---|
287 | } |
---|
288 | |
---|
289 | |
---|
290 | /** Returns a pointer to the next/current value element in the collection, without |
---|
291 | advancing to the next afterwards. */ |
---|
292 | const PointerType peekNextValuePtr ( ) const |
---|
293 | { |
---|
294 | return &(this->mCurrent->second); |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | /** Returns the next(=current) value element in the collection, and advances to the next. */ |
---|
299 | ValueType getNext() |
---|
300 | { |
---|
301 | return ((this->mCurrent++)->second) ; |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | }; |
---|
306 | |
---|
307 | |
---|
308 | |
---|
309 | |
---|
310 | /** |
---|
311 | * |
---|
312 | * @brief Concrete IteratorWrapper for nonconst access to the underlying key-value container |
---|
313 | * |
---|
314 | * |
---|
315 | * @remarks Template key-value container |
---|
316 | * |
---|
317 | */ |
---|
318 | template <typename T> |
---|
319 | class MapIterator : public MapIteratorWrapper<T, typename T::iterator>{ |
---|
320 | public: |
---|
321 | |
---|
322 | /** Constructor. |
---|
323 | @remarks |
---|
324 | Provide a start and end iterator to initialise. |
---|
325 | */ |
---|
326 | MapIterator( typename T::iterator start, typename T::iterator last ) |
---|
327 | : MapIteratorWrapper<T, typename T::iterator>(start , last ) |
---|
328 | { |
---|
329 | } |
---|
330 | |
---|
331 | /** Constructor. |
---|
332 | @remarks |
---|
333 | Provide a container to initialise. |
---|
334 | */ |
---|
335 | explicit MapIterator( T& c ) |
---|
336 | : MapIteratorWrapper<T, typename T::iterator> ( c.begin(), c.end() ) |
---|
337 | { |
---|
338 | } |
---|
339 | |
---|
340 | }; |
---|
341 | |
---|
342 | |
---|
343 | /** |
---|
344 | * |
---|
345 | * @brief Concrete IteratorWrapper for const access to the underlying key-value container |
---|
346 | * |
---|
347 | * |
---|
348 | * @tparam T key-value container |
---|
349 | * |
---|
350 | */ |
---|
351 | template <typename T> |
---|
352 | class ConstMapIterator : public MapIteratorWrapper<T, typename T::const_iterator>{ |
---|
353 | public: |
---|
354 | |
---|
355 | /** Constructor. |
---|
356 | @remarks |
---|
357 | Provide a start and end iterator to initialise. |
---|
358 | */ |
---|
359 | ConstMapIterator( typename T::const_iterator start, typename T::const_iterator last ) |
---|
360 | : MapIteratorWrapper<T, typename T::const_iterator> (start , last ) |
---|
361 | { |
---|
362 | } |
---|
363 | |
---|
364 | /** Constructor. |
---|
365 | @remarks |
---|
366 | Provide a container to initialise. |
---|
367 | */ |
---|
368 | explicit ConstMapIterator ( const T& c ) |
---|
369 | : MapIteratorWrapper<T, typename T::const_iterator> (c.begin() , c.end() ) |
---|
370 | { |
---|
371 | } |
---|
372 | }; |
---|
373 | |
---|
374 | |
---|
375 | |
---|
376 | |
---|
377 | } |
---|
378 | |
---|
379 | #include "OgreHeaderSuffix.h" |
---|
380 | |
---|
381 | #endif |
---|