1 /*=============================================================================
2 Copyright (c) 2001-2006 Joel de Guzman
3 Copyright (c) 2008 Eric Niebler
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 #ifndef BOOST_PROTO_DETAIL_FUSION_REVERSE_EAH_01_22_2008
9 #define BOOST_PROTO_DETAIL_FUSION_REVERSE_EAH_01_22_2008
11 #include <boost/spirit/fusion/detail/access.hpp>
12 #include <boost/spirit/fusion/iterator/as_fusion_iterator.hpp>
13 #include <boost/spirit/fusion/iterator/detail/iterator_base.hpp>
14 #include <boost/spirit/fusion/sequence/detail/sequence_base.hpp>
15 #include <boost/spirit/fusion/iterator/next.hpp>
16 #include <boost/spirit/fusion/iterator/prior.hpp>
17 #include <boost/spirit/fusion/iterator/deref.hpp>
18 #include <boost/spirit/fusion/iterator/value_of.hpp>
19 #include <boost/spirit/fusion/sequence/begin.hpp>
20 #include <boost/spirit/fusion/sequence/end.hpp>
22 namespace boost { namespace fusion
24 struct reverse_view_tag;
25 struct reverse_view_iterator_tag;
27 template <typename First>
28 struct reverse_view_iterator
29 : iterator_base<reverse_view_iterator<First> >
31 typedef as_fusion_iterator<First> converter;
32 typedef typename converter::type first_type;
33 typedef reverse_view_iterator_tag tag;
35 reverse_view_iterator(First const& first)
36 : first(converter::convert(first)) {}
41 template <typename Sequence>
42 struct reverse_view : sequence_base<reverse_view<Sequence> >
44 typedef as_fusion_sequence<Sequence> seq_converter;
45 typedef typename seq_converter::type seq;
47 typedef reverse_view_tag tag;
48 typedef typename meta::begin<seq>::type first_type;
49 typedef typename meta::end<seq>::type last_type;
51 reverse_view(Sequence& seq)
52 : first(fusion::begin(seq))
53 , last(fusion::end(seq))
63 struct deref_impl<reverse_view_iterator_tag>
65 template <typename Iterator>
71 typename Iterator::first_type
77 call(Iterator const& i)
79 return *fusion::prior(i.first);
85 struct prior_impl<reverse_view_iterator_tag>
87 template <typename Iterator>
90 typedef typename Iterator::first_type first_type;
91 typedef typename next_impl<typename first_type::tag>::
92 template apply<first_type>
95 typedef reverse_view_iterator<typename wrapped::type> type;
98 call(Iterator const& i)
100 return type(wrapped::call(i.first));
106 struct next_impl<reverse_view_iterator_tag>
108 template <typename Iterator>
111 typedef typename Iterator::first_type first_type;
112 typedef typename prior_impl<typename first_type::tag>::
113 template apply<first_type>
116 typedef reverse_view_iterator<typename wrapped::type> type;
119 call(Iterator const& i)
121 return type(wrapped::call(i.first));
127 struct value_impl<reverse_view_iterator_tag>
129 template <typename Iterator>
134 typename meta::prior<
135 typename Iterator::first_type
143 struct begin_impl<reverse_view_tag>
145 template <typename Sequence>
148 typedef reverse_view_iterator<typename Sequence::last_type> type;
151 call(Sequence const& s)
159 struct end_impl<reverse_view_tag>
161 template <typename Sequence>
164 typedef reverse_view_iterator<typename Sequence::first_type> type;
167 call(Sequence const& s)
169 return type(s.first);
174 template <typename Sequence>
177 typedef reverse_view<Sequence> type;
181 template <typename Sequence>
182 inline reverse_view<Sequence const>
183 reverse(Sequence const& view)
185 return reverse_view<Sequence const>(view);