| 1 |
// file : cutl/container/pointer-iterator.hxx |
| 2 |
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC |
| 3 |
// license : MIT; see accompanying LICENSE file |
| 4 |
|
| 5 |
#ifndef CUTL_CONTAINER_POINTER_ITERATOR_HXX |
| 6 |
#define CUTL_CONTAINER_POINTER_ITERATOR_HXX |
| 7 |
|
| 8 |
#include <iterator> // std::iterator_traits |
| 9 |
|
| 10 |
#include <cutl/meta/remove-p.hxx> |
| 11 |
|
| 12 |
namespace cutl |
| 13 |
{ |
| 14 |
namespace container |
| 15 |
{ |
| 16 |
template <typename I> |
| 17 |
class pointer_iterator |
| 18 |
{ |
| 19 |
public: |
| 20 |
typedef |
| 21 |
typename meta::remove_p<typename std::iterator_traits<I>::value_type>::r |
| 22 |
value_type; |
| 23 |
|
| 24 |
typedef |
| 25 |
typename std::iterator_traits<I>::iterator_category |
| 26 |
iterator_category; |
| 27 |
|
| 28 |
typedef |
| 29 |
typename std::iterator_traits<I>::difference_type |
| 30 |
difference_type; |
| 31 |
|
| 32 |
typedef value_type& reference; |
| 33 |
typedef value_type* pointer; |
| 34 |
typedef I base_iterator; |
| 35 |
|
| 36 |
public: |
| 37 |
pointer_iterator () |
| 38 |
: i_ () // I can be of a pointer type. |
| 39 |
{ |
| 40 |
} |
| 41 |
|
| 42 |
pointer_iterator (I const& i) |
| 43 |
: i_ (i) |
| 44 |
{ |
| 45 |
} |
| 46 |
|
| 47 |
public: |
| 48 |
reference |
| 49 |
operator* () const |
| 50 |
{ |
| 51 |
return **i_; |
| 52 |
} |
| 53 |
|
| 54 |
pointer |
| 55 |
operator-> () const |
| 56 |
{ |
| 57 |
return *i_; |
| 58 |
} |
| 59 |
|
| 60 |
I const& |
| 61 |
base () const |
| 62 |
{ |
| 63 |
return i_; |
| 64 |
} |
| 65 |
|
| 66 |
public: |
| 67 |
// Forward iterator requirements. |
| 68 |
// |
| 69 |
pointer_iterator& |
| 70 |
operator++ () |
| 71 |
{ |
| 72 |
++i_; |
| 73 |
return *this; |
| 74 |
} |
| 75 |
|
| 76 |
pointer_iterator |
| 77 |
operator++ (int) |
| 78 |
{ |
| 79 |
pointer_iterator r (*this); |
| 80 |
++i_; |
| 81 |
return r; |
| 82 |
} |
| 83 |
|
| 84 |
pointer_iterator& |
| 85 |
operator-- () |
| 86 |
{ |
| 87 |
--i_; |
| 88 |
return *this; |
| 89 |
} |
| 90 |
|
| 91 |
pointer_iterator |
| 92 |
operator-- (int) |
| 93 |
{ |
| 94 |
pointer_iterator r (*this); |
| 95 |
--i_; |
| 96 |
return r; |
| 97 |
} |
| 98 |
|
| 99 |
private: |
| 100 |
I i_; |
| 101 |
}; |
| 102 |
|
| 103 |
template <typename I> |
| 104 |
inline bool |
| 105 |
operator== (pointer_iterator<I> const& a, pointer_iterator<I> const& b) |
| 106 |
{ |
| 107 |
return a.base () == b.base (); |
| 108 |
} |
| 109 |
|
| 110 |
template <typename I> |
| 111 |
inline bool |
| 112 |
operator!= (pointer_iterator<I> const& a, pointer_iterator<I> const& b) |
| 113 |
{ |
| 114 |
return a.base () != b.base (); |
| 115 |
} |
| 116 |
|
| 117 |
template <typename I> |
| 118 |
inline typename pointer_iterator<I>::difference_type |
| 119 |
operator- (pointer_iterator<I> const& a, pointer_iterator<I> const& b) |
| 120 |
{ |
| 121 |
return a.base () - b.base (); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
#endif // CUTL_CONTAINER_POINTER_ITERATOR_HXX |