Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / gil / position_iterator.hpp
1 //
2 // Copyright 2005-2007 Adobe Systems Incorporated
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 #ifndef BOOST_GIL_POSITION_ITERATOR_HPP
9 #define BOOST_GIL_POSITION_ITERATOR_HPP
10
11 #include <boost/gil/locator.hpp>
12
13 #include <boost/iterator/iterator_facade.hpp>
14
15 #include <type_traits>
16
17 namespace boost { namespace gil {
18
19 /// \defgroup PixelIteratorModelVirtual position_iterator
20 /// \ingroup PixelIteratorModel
21 /// \brief An iterator that remembers its current X,Y position and invokes a function object with it upon dereferencing.
22 /// Models PixelIteratorConcept, PixelBasedConcept, HasDynamicXStepTypeConcept. Used to create virtual image views.
23
24 /// \brief An iterator that remembers its current X,Y position and invokes a function object with it upon dereferencing.
25 /// Used to create virtual image views.
26 /// Models: StepIteratorConcept, PixelIteratorConcept, PixelBasedConcept, HasDynamicXStepTypeConcept
27 /// \ingroup PixelIteratorModelVirtual PixelBasedModel
28 /// \tparam Deref A function object that given a point returns a pixel reference. Models PixelDereferenceAdaptorConcept
29 /// \tparam Dim Dimension to advance along
30 template <typename Deref, int Dim>
31 struct position_iterator : public iterator_facade<position_iterator<Deref,Dim>,
32                                                   typename Deref::value_type,
33                                                   std::random_access_iterator_tag,
34                                                   typename Deref::reference,
35                                                   typename Deref::argument_type::template axis<Dim>::coord_t> {
36     using parent_t = iterator_facade<position_iterator<Deref,Dim>,
37                             typename Deref::value_type,
38                             std::random_access_iterator_tag,
39                             typename Deref::reference,
40                             typename Deref::argument_type::template axis<Dim>::coord_t>;
41     using difference_type = typename parent_t::difference_type;
42     using reference = typename parent_t::reference;
43     using point_t = typename Deref::argument_type;
44
45     position_iterator() {}
46     position_iterator(const point_t& p, const point_t& step, const Deref& d) : _p(p), _step(step), _d(d) {}
47
48     position_iterator(const position_iterator& p) : _p(p._p), _step(p._step), _d(p._d) {}
49     template <typename D> position_iterator(const position_iterator<D,Dim>& p) : _p(p._p), _step(p._step), _d(p._d) {}
50     position_iterator& operator=(const position_iterator& p) { _p=p._p; _d=p._d; _step=p._step; return *this; }
51
52     const point_t&   pos()      const { return _p; }
53     const point_t&   step()     const { return _step; }
54     const Deref&     deref_fn() const { return _d; }
55
56     void set_step(difference_type s) { _step[Dim]=s; }
57     /// For some reason operator[] provided by iterator_adaptor returns a custom class that is convertible to reference
58     /// We require our own reference because it is registered in iterator_traits
59     reference operator[](difference_type d) const { point_t p=_p; p[Dim]+=d*_step[Dim]; return _d(p); }
60
61 private:
62     point_t _p, _step;
63     Deref   _d;
64
65     template <typename DE, int DI> friend struct position_iterator;
66     friend class boost::iterator_core_access;
67     reference dereference()     const { return _d(_p); }
68     void increment()                  { _p[Dim]+=_step[Dim]; }
69     void decrement()                  { _p[Dim]-=_step[Dim]; }
70     void advance(difference_type d)   { _p[Dim]+=d*_step[Dim]; }
71
72     difference_type distance_to(const position_iterator& it) const { return (it._p[Dim]-_p[Dim])/_step[Dim]; }
73     bool equal(const position_iterator& it) const { return _p==it._p; }
74 };
75
76 template <typename Deref,int Dim>
77 struct const_iterator_type<position_iterator<Deref,Dim> > {
78     using type = position_iterator<typename Deref::const_t,Dim>;
79 };
80
81 template <typename Deref, int Dim>
82 struct iterator_is_mutable<position_iterator<Deref, Dim>>
83     : std::integral_constant<bool, Deref::is_mutable>
84 {
85 };
86
87 /////////////////////////////
88 //  PixelBasedConcept
89 /////////////////////////////
90
91 template <typename Deref,int Dim>
92 struct color_space_type<position_iterator<Deref,Dim> > : public color_space_type<typename Deref::value_type> {};
93
94 template <typename Deref,int Dim>
95 struct channel_mapping_type<position_iterator<Deref,Dim> > : public channel_mapping_type<typename Deref::value_type> {};
96
97 template <typename Deref,int Dim>
98 struct is_planar<position_iterator<Deref, Dim>> : std::false_type {};
99
100 template <typename Deref,int Dim>
101 struct channel_type<position_iterator<Deref,Dim> > : public channel_type<typename Deref::value_type> {};
102
103 /////////////////////////////
104 //  HasDynamicXStepTypeConcept
105 /////////////////////////////
106
107 template <typename Deref,int Dim>
108 struct dynamic_x_step_type<position_iterator<Deref,Dim> > {
109     using type = position_iterator<Deref,Dim>;
110 };
111
112 } }  // namespace boost::gil
113
114 #endif