1 /*=============================================================================
2     Copyright (c) 2013 Mateusz Loskot
3     Copyright (c) 2001-2011 Joel de Guzman
4     Copyright (c) 2005-2006 Dan Marsden
5 
6     Distributed under the Boost Software License, Version 1.0. (See accompanying
7     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 ==============================================================================*/
9 #if !defined(BOOST_FUSION_STD_ARRAY_ARRAY_ITERATOR_01062013_1700)
10 #define BOOST_FUSION_STD_ARRAY_ARRAY_ITERATOR_01062013_1700
11 
12 #include <cstddef>
13 #include <boost/config.hpp>
14 #include <boost/mpl/int.hpp>
15 #include <boost/mpl/assert.hpp>
16 #include <boost/mpl/if.hpp>
17 #include <boost/mpl/minus.hpp>
18 #include <boost/type_traits/is_const.hpp>
19 #include <boost/fusion/iterator/iterator_facade.hpp>
20 #include <boost/fusion/adapted/std_array/detail/array_size.hpp>
21 
22 namespace boost { namespace fusion
23 {
24     struct random_access_traversal_tag;
25 
26     template <typename Array, int Pos>
27     struct std_array_iterator
28         : iterator_facade<std_array_iterator<Array, Pos>, random_access_traversal_tag>
29     {
30         BOOST_MPL_ASSERT_RELATION(Pos, >=, 0);
31         BOOST_MPL_ASSERT_RELATION(Pos, <=, std::tuple_size<Array>::value);
32 
33         typedef mpl::int_<Pos> index;
34         typedef Array array_type;
35 
std_array_iteratorboost::fusion::std_array_iterator36         std_array_iterator(Array& a)
37             : array(a) {}
38 
39         Array& array;
40 
41         template <typename Iterator>
42         struct value_of
43         {
44             typedef typename Iterator::array_type array_type;
45             typedef typename array_type::value_type type;
46         };
47 
48         template <typename Iterator>
49         struct deref
50         {
51             typedef typename Iterator::array_type array_type;
52             typedef typename
53                 mpl::if_<
54                     is_const<array_type>
55                   , typename array_type::const_reference
56                   , typename array_type::reference
57                 >::type
58             type;
59 
60             static type
callboost::fusion::std_array_iterator::deref61             call(Iterator const & it)
62             {
63                 return it.array[Iterator::index::value];
64             }
65         };
66 
67         template <typename Iterator, typename N>
68         struct advance
69         {
70             typedef typename Iterator::index index;
71             typedef typename Iterator::array_type array_type;
72             typedef std_array_iterator<array_type, index::value + N::value> type;
73 
74             static type
callboost::fusion::std_array_iterator::advance75             call(Iterator const& i)
76             {
77                 return type(i.array);
78             }
79         };
80 
81         template <typename Iterator>
82         struct next : advance<Iterator, mpl::int_<1> > {};
83 
84         template <typename Iterator>
85         struct prior : advance<Iterator, mpl::int_<-1> > {};
86 
87         template <typename I1, typename I2>
88         struct distance : mpl::minus<typename I2::index, typename I1::index>
89         {
90             typedef typename
91                 mpl::minus<
92                     typename I2::index, typename I1::index
93                 >::type
94             type;
95 
96             static type
callboost::fusion::std_array_iterator::distance97             call(I1 const&, I2 const&)
98             {
99                 return type();
100             }
101         };
102 
103         BOOST_DELETED_FUNCTION(std_array_iterator& operator=(std_array_iterator const&))
104     };
105 }}
106 
107 #endif
108