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