aboutsummaryrefslogtreecommitdiff
path: root/cutl/container/pointer-iterator.hxx
blob: 9c64e6c5d44199b93e4a5c6b154a652b37f02548 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// file      : cutl/container/pointer-iterator.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#ifndef CUTL_CONTAINER_POINTER_ITERATOR_HXX
#define CUTL_CONTAINER_POINTER_ITERATOR_HXX

#include <iterator> // std::iterator_traits

#include <cutl/meta/remove-p.hxx>

namespace cutl
{
  namespace container
  {
    template <typename I>
    class pointer_iterator
    {
    public:
      typedef
      typename meta::remove_p<typename std::iterator_traits<I>::value_type>::r
      value_type;

      typedef
      typename std::iterator_traits<I>::iterator_category
      iterator_category;

      typedef
      typename std::iterator_traits<I>::difference_type
      difference_type;

      typedef value_type& reference;
      typedef value_type* pointer;
      typedef I base_iterator;

    public:
      pointer_iterator ()
          : i_ () // I can be of a pointer type.
      {
      }

      pointer_iterator (I const& i)
          : i_ (i)
      {
      }

    public:
      reference
      operator* () const
      {
        return **i_;
      }

      pointer
      operator-> () const
      {
        return *i_;
      }

      I const&
      base () const
      {
        return i_;
      }

    public:
      // Forward iterator requirements.
      //
      pointer_iterator&
      operator++ ()
      {
        ++i_;
        return *this;
      }

      pointer_iterator
      operator++ (int)
      {
        pointer_iterator r (*this);
        ++i_;
        return r;
      }

      pointer_iterator&
      operator-- ()
      {
        --i_;
        return *this;
      }

      pointer_iterator
      operator-- (int)
      {
        pointer_iterator r (*this);
        --i_;
        return r;
      }

    private:
      I i_;
    };

    template <typename I>
    inline bool
    operator== (pointer_iterator<I> const& a, pointer_iterator<I> const& b)
    {
      return a.base () == b.base ();
    }

    template <typename I>
    inline bool
    operator!= (pointer_iterator<I> const& a, pointer_iterator<I> const& b)
    {
      return a.base () != b.base ();
    }

    template <typename I>
    inline typename pointer_iterator<I>::difference_type
    operator- (pointer_iterator<I> const& a, pointer_iterator<I> const& b)
    {
      return a.base () - b.base ();
    }
  }
}

#endif // CUTL_CONTAINER_POINTER_ITERATOR_HXX