PGE API 0.4
PR00F's Game Engine full documentation
Loading...
Searching...
No Matches
blIterator.hpp
Go to the documentation of this file.
1#ifndef BL_ITERATOR_HPP
2#define BL_ITERATOR_HPP
3
4
5//-------------------------------------------------------------------
6// FILE: blIterator.hpp
7// CLASS: blIterator
8// BASE CLASS: None
9//
10// PURPOSE: This class provides a generic iterator which
11// wraps a user specified container.
12// The iterator is customizable through user-provided
13// begin, end, advance and distance functors.
14// Through the user-provided functors, the iterator
15// can for example be made circular, be made to never
16// go pass its end, be made to skip every other place
17// while moving forward, be made into a reverse iterator
18// or much more.
19//
20// AUTHOR: Vincenzo Barbato
21// http://www.barbatolabs.com
22// navyenzo@gmail.com
23//
24// LISENSE: MIT-LICENCE
25// http://www.opensource.org/licenses/mit-license.php
26//
27// DEPENDENCIES:
28//
29// NOTES:
30//
31// DATE CREATED: Dec/02/2013
32//
33// DATE UPDATED:
34//-------------------------------------------------------------------
35
36
37//-------------------------------------------------------------------
38template<typename blContainerType,
39 typename blAdvanceDistanceFunctorType,
40 typename blBeginEndFunctorType>
41
42class blIterator : public std::iterator< std::random_access_iterator_tag,
43 typename std::iterator_traits<decltype(blBeginEndFunctorType::begin(std::declval<blContainerType&>()))>::value_type,
44 typename std::iterator_traits<decltype(blBeginEndFunctorType::begin(std::declval<blContainerType&>()))>::difference_type,
45 typename std::iterator_traits<decltype(blBeginEndFunctorType::begin(std::declval<blContainerType&>()))>::pointer,
46 typename std::iterator_traits<decltype(blBeginEndFunctorType::begin(std::declval<blContainerType&>()))>::reference >
47{
48public: // Public typedefs
49
50 typedef typename std::shared_ptr<blContainerType> blContainerPtr;
51
52 typedef typename std::iterator_traits<typename blContainerType::iterator>::value_type blDataType;
53 typedef typename std::iterator_traits<typename blContainerType::iterator>::pointer blDataTypePtr;
54 typedef typename std::iterator_traits<typename blContainerType::iterator>::reference blDataTypeRef;
55
56 typedef decltype(blBeginEndFunctorType::begin(std::declval<blContainerType&>())) iterator;
57
58private: // Private variables
59
60 // The iterator
61 // to the data
62 // point
63
65
66 // The raw pointer
67 // to the dat Container
68
70
71public: // Constructors and destructors
72
73 // Default constructors
74
75 blIterator() = default;
76
77 // Construct from container
78
79 blIterator(blContainerType& container)
80 {
81 m_containerPtr = get_shared_ptr(container);
82
84 m_ptr = blBeginEndFunctorType::begin(*m_containerPtr);
85 }
86
87 // Construct from container
88 // pointer
89
90 blIterator(blContainerType* containerPtr)
91 {
92 m_containerPtr = get_shared_ptr(containerPtr);
93
95 m_ptr = blBeginEndFunctorType::begin(*m_containerPtr);
96 }
97
98 // Construct from container
99 // shared pointer
100
101 blIterator(const blContainerPtr& containerPtr)
102 {
103 m_containerPtr = containerPtr;
104
106 m_ptr = blBeginEndFunctorType::begin(*m_containerPtr);
107 }
108
109 // Construct from iterator
110 // and container shared pointer
111
113 const blContainerPtr& containerPtr)
114 {
115 m_ptr = ptr;
116 m_containerPtr = containerPtr;
117 }
118
119 // Copy constructor
120
122
123 // Move constructor
124
126
127 // Destructor
128
130 {
131 }
132
133public: // Assignment operator
134
135 blIterator<blContainerType,
136 blAdvanceDistanceFunctorType,
138
139public: // Dereferencing operators
140
142 const blDataTypeRef operator*()const{return (*m_ptr);}
144
145public: // Overloaded operators
146
147 // Equality and
148 // inequality
149 // operators
150
152 {
153 return ( (m_ptr == iterator.getPtr()) &&
154 (m_containerPtr == iterator.getContainerPtr()) );
155 }
156
158 {
159 return ( (m_ptr != iterator.getPtr()) ||
160 (m_containerPtr != iterator.getContainerPtr()) );
161 }
162
163 // Bool operator
164 // so that this
165 // iterator can be
166 // used in if statements
167
168 explicit operator bool()const
169 {
170 if(this->m_containerPtr)
171 return true;
172 else
173 return false;
174 }
175
176 // Increment/decrement
177 // operators
178
179 blIterator<blContainerType,
180 blAdvanceDistanceFunctorType,
181 blBeginEndFunctorType>& operator++()
182 {
183 this->advance(1);
184 return (*this);
185 }
186
187 blIterator<blContainerType,
188 blAdvanceDistanceFunctorType,
189 blBeginEndFunctorType> operator++(int)
190 {
191 auto TempIter(*this);
192
193 this->advance(1);
194
195 return TempIter;
196 }
197
198 blIterator<blContainerType,
199 blAdvanceDistanceFunctorType,
200 blBeginEndFunctorType>& operator--()
201 {
202 this->advance(-1);
203 return (*this);
204 }
205
206 blIterator<blContainerType,
207 blAdvanceDistanceFunctorType,
208 blBeginEndFunctorType> operator--(int)
209 {
210 auto TempIter(*this);
211
212 this->advance(-1);
213
214 return TempIter;
215 }
216
217 // Operators used to
218 // advance a random
219 // access iterator
220
221 blIterator<blContainerType,
222 blAdvanceDistanceFunctorType,
223 blBeginEndFunctorType>& operator+=(const ptrdiff_t& Offset)
224 {
225 this->advance(Offset);
226 return (*this);
227 }
228
229 blIterator<blContainerType,
230 blAdvanceDistanceFunctorType,
231 blBeginEndFunctorType>& operator-=(const ptrdiff_t& Offset)
232 {
233 this->advance(-Offset);
234 return (*this);
235 }
236
237 blIterator<blContainerType,
238 blAdvanceDistanceFunctorType,
239 blBeginEndFunctorType> operator+(const ptrdiff_t& Offset)const
240 {
241 auto NewIter = (*this);
242 NewIter.advance(Offset);
243 return NewIter;
244 }
245
246 blIterator<blContainerType,
247 blAdvanceDistanceFunctorType,
248 blBeginEndFunctorType> operator-(const ptrdiff_t& Offset)const
249 {
250 auto NewIter = (*this);
251 NewIter.advance(-Offset);
252 return NewIter;
253 }
254
255 // Operator used
256 // to calculate
257 // the distance
258 // between two
259 // iterators
260
262 {
263 return blAdvanceDistanceFunctorType::distance(this->getDistanceFromBeginToIter(),
265 this->getDistanceFromIterToEnd(),
266 iterator.getDistanceFromIterToEnd());
267 }
268
269public: // Public functions
270
271 // Functions used to
272 // set the Container pointer
273
274 void setContainerPtr(blContainerType& container)
275 {
276 auto containerPtr = get_shared_ptr(container);
277
278 if(containerPtr != m_containerPtr)
279 {
280 m_containerPtr = containerPtr;
281
283 {
284 (*this) = this->begin();
285 }
286 }
287 }
288
289 void setContainerPtr(const blContainerType& container)
290 {
291 auto containerPtr = get_shared_ptr(container);
292
293 if(containerPtr != m_containerPtr)
294 {
295 m_containerPtr = containerPtr;
296
298 {
299 (*this) = this->begin();
300 }
301 }
302 }
303
304 void setContainerPtr(blContainerType* rawContainerPtr)
305 {
306 auto containerPtr = get_shared_ptr(rawContainerPtr);
307
308 if(containerPtr != m_containerPtr)
309 {
310 m_containerPtr = containerPtr;
311
313 {
314 (*this) = this->begin();
315 }
316 }
317 }
318
319 // Functions used to
320 // get the raw pointer
321 // and iterator
322
323 const iterator& getPtr()const{return m_ptr;}
325
326 // Functions used to
327 // get the distance
328 // from the begin
329 // to the iterator
330 // and from the iterator
331 // to the end
332
334 {
336 return std::distance(blBeginEndFunctorType::begin(*m_containerPtr),this->m_ptr);
337 else
338 return 0;
339 }
340
342 {
344 return std::distance(this->m_ptr,blBeginEndFunctorType::end(*m_containerPtr));
345 else
346 return 0;
347 }
348
349 // Functions used to
350 // return iterators
351 // to the "begin" and
352 // "end" data points
353 // of the Container
354
355 blIterator<blContainerType,
356 blAdvanceDistanceFunctorType,
357 blBeginEndFunctorType> begin()const
358 {
360 {
361 return blIterator<blContainerType,
362 blAdvanceDistanceFunctorType,
363 blBeginEndFunctorType>(blBeginEndFunctorType::begin(*m_containerPtr),
365 }
366 else
367 return blIterator<blContainerType,
368 blAdvanceDistanceFunctorType,
369 blBeginEndFunctorType>(m_containerPtr);
370 }
371
372 blIterator<blContainerType,
373 blAdvanceDistanceFunctorType,
374 blBeginEndFunctorType> end()const
375 {
377 {
378 return blIterator<blContainerType,
379 blAdvanceDistanceFunctorType,
380 blBeginEndFunctorType>(blBeginEndFunctorType::end(*m_containerPtr),
382 }
383 else
384 return blIterator<blContainerType,
385 blAdvanceDistanceFunctorType,
386 blBeginEndFunctorType>(m_containerPtr);
387 }
388
389 // Operators used to
390 // access data elements
391 // from the container
392 // directly
393 //
394 // NOTE: These operators do
395 // not check for out of
396 // bound indices
397
398 blDataTypeRef operator[](const size_t& index){return (*(this->begin() + index));}
399 const blDataTypeRef operator[](const size_t& index)const{return (*(this->begin() + index));}
400
401
402
403 // Functions used
404 // to return the
405 // container's size
406
407 size_t size()const
408 {
410 return m_containerPtr->size();
411 else
412 return 0;
413 }
414
415 size_t length()const{return this->size();}
416 size_t max_size()const{return this->size();}
417 bool empty()const{return (this->size() == 0);}
418
419private: // Special functions
420
421 // Function used
422 // to advance the
423 // iterator
424
425 void advance(const ptrdiff_t& HowManyStepsToAdvanceIter)
426 {
428 {
429 blAdvanceDistanceFunctorType::advance(m_ptr,
430 HowManyStepsToAdvanceIter,
431 this->begin().m_ptr,
432 this->end().m_ptr,
435 }
436 }
437};
438//-------------------------------------------------------------------
439
440
441#endif // BL_ITERATOR_HPP
std::shared_ptr< blResourceType > get_shared_ptr(blResourceType &theResource)
std::iterator_traits< typenameblContainerType::iterator >::pointer blDataTypePtr
const blDataTypeRef operator[](const size_t &index) const
blIterator(const blContainerPtr &containerPtr)
blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > end() const
void setContainerPtr(blContainerType &container)
size_t max_size() const
bool operator!=(const blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > &iterator) const
blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > & operator++()
blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > & operator=(const blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > &iterator)=default
blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > & operator--()
std::iterator_traits< typenameblContainerType::iterator >::value_type blDataType
ptrdiff_t getDistanceFromBeginToIter() const
std::iterator_traits< typenameblContainerType::iterator >::reference blDataTypeRef
blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > operator-(const ptrdiff_t &Offset) const
decltype(blBeginEndFunctorType::begin(std::declval< blContainerType & >())) iterator
bool empty() const
blDataTypeRef operator[](const size_t &index)
blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > & operator+=(const ptrdiff_t &Offset)
bool operator==(const blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > &iterator) const
ptrdiff_t operator-(const blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > &iterator) const
ptrdiff_t getDistanceFromIterToEnd() const
blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > begin() const
void setContainerPtr(const blContainerType &container)
blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > & operator-=(const ptrdiff_t &Offset)
void setContainerPtr(blContainerType *rawContainerPtr)
std::shared_ptr< blContainerType > blContainerPtr
blContainerPtr m_containerPtr
const iterator & getPtr() const
iterator operator->()
const blDataTypeRef operator*() const
blIterator(blContainerType &container)
void advance(const ptrdiff_t &HowManyStepsToAdvanceIter)
blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > operator+(const ptrdiff_t &Offset) const
size_t size() const
blIterator(blContainerType *containerPtr)
blIterator(const blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > &)=default
const blContainerPtr & getContainerPtr() const
blIterator(blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > &&)=default
blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > operator--(int)
blIterator()=default
blIterator< blContainerType, blAdvanceDistanceFunctorType, blBeginEndFunctorType > operator++(int)
size_t length() const
blDataTypeRef operator*()
iterator m_ptr
blIterator(const iterator &ptr, const blContainerPtr &containerPtr)