1 /*=============================================================================
2     Copyright (c) 2001-2011 Joel de Guzman
3     Copyright (c) 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(FUSION_ZIP_VIEW_23012006_0813)
9 #define FUSION_ZIP_VIEW_23012006_0813
10 
11 #include <boost/fusion/support/config.hpp>
12 #include <boost/fusion/support/sequence_base.hpp>
13 #include <boost/fusion/support/unused.hpp>
14 #include <boost/fusion/iterator/equal_to.hpp>
15 #include <boost/fusion/view/detail/strictest_traversal.hpp>
16 #include <boost/fusion/view/zip_view/detail/begin_impl.hpp>
17 #include <boost/fusion/view/zip_view/detail/end_impl.hpp>
18 #include <boost/fusion/view/zip_view/detail/size_impl.hpp>
19 #include <boost/fusion/view/zip_view/detail/at_impl.hpp>
20 #include <boost/fusion/view/zip_view/detail/value_at_impl.hpp>
21 #include <boost/fusion/container/vector/convert.hpp>
22 #include <boost/fusion/algorithm/query/find_if.hpp>
23 #include <boost/fusion/sequence/intrinsic/end.hpp>
24 #include <boost/fusion/sequence/intrinsic/size.hpp>
25 #include <boost/fusion/mpl.hpp>
26 #include <boost/fusion/algorithm/transformation/remove.hpp>
27 
28 #include <boost/mpl/assert.hpp>
29 #include <boost/mpl/not.hpp>
30 #include <boost/mpl/placeholders.hpp>
31 #include <boost/mpl/transform_view.hpp>
32 #include <boost/mpl/at.hpp>
33 #include <boost/mpl/find_if.hpp>
34 #include <boost/mpl/equal_to.hpp>
35 #include <boost/mpl/bool.hpp>
36 #include <boost/mpl/eval_if.hpp>
37 
38 #include <boost/type_traits/remove_reference.hpp>
39 #include <boost/type_traits/is_reference.hpp>
40 
41 #include <boost/config.hpp>
42 
43 namespace boost { namespace fusion {
44 
45     namespace detail
46     {
47         template<typename Sequences>
48         struct all_references
49             : fusion::result_of::equal_to<typename fusion::result_of::find_if<Sequences, mpl::not_<is_reference<mpl::_> > >::type, typename fusion::result_of::end<Sequences>::type>
50         {};
51 
52         struct seq_ref_size
53         {
54             template<typename Params>
55             struct result;
56 
57             template<typename Seq>
58             struct result<seq_ref_size(Seq)>
59             {
60                 static int const high_int = static_cast<int>(
61                     (static_cast<unsigned>(~0) >> 1) - 1);
62 
63                 typedef typename remove_reference<Seq>::type SeqClass;
64 
65                 typedef typename mpl::eval_if<
66                     traits::is_forward<SeqClass>,
67                     result_of::size<SeqClass>,
68                     mpl::int_<high_int> >::type type;
69             };
70 
71             // never called, but needed for decltype-based result_of (C++0x)
72 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
73             template<typename Seq>
74             BOOST_FUSION_GPU_ENABLED
75             typename result<seq_ref_size(Seq)>::type
76             operator()(Seq&&) const;
77 #endif
78         };
79 
80         struct poly_min
81         {
82             template<typename T>
83             struct result;
84 
85             template<typename Lhs, typename Rhs>
86             struct result<poly_min(Lhs, Rhs)>
87             {
88                 typedef typename remove_reference<Lhs>::type lhs;
89                 typedef typename remove_reference<Rhs>::type rhs;
90                 typedef typename mpl::min<lhs, rhs>::type type;
91             };
92 
93             // never called, but needed for decltype-based result_of (C++0x)
94 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
95             template<typename Lhs, typename Rhs>
96             BOOST_FUSION_GPU_ENABLED
97             typename result<poly_min(Lhs, Rhs)>::type
98             operator()(Lhs&&, Rhs&&) const;
99 #endif
100         };
101 
102         template<typename Sequences>
103         struct min_size
104         {
105             typedef typename result_of::transform<Sequences, detail::seq_ref_size>::type sizes;
106             typedef typename result_of::fold<sizes, typename result_of::front<sizes>::type, detail::poly_min>::type type;
107         };
108     }
109 
110     struct zip_view_tag;
111     struct fusion_sequence_tag;
112 
113     template<typename Sequences>
114     struct zip_view : sequence_base< zip_view<Sequences> >
115     {
116         typedef typename result_of::remove<Sequences, unused_type const&>::type real_sequences;
117         BOOST_MPL_ASSERT((detail::all_references<Sequences>));
118         typedef typename detail::strictest_traversal<real_sequences>::type category;
119         typedef zip_view_tag fusion_tag;
120         typedef fusion_sequence_tag tag; // this gets picked up by MPL
121         typedef mpl::true_ is_view;
122         typedef typename fusion::result_of::as_vector<Sequences>::type sequences;
123         typedef typename detail::min_size<real_sequences>::type size;
124 
125         BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
zip_viewboost::fusion::zip_view126         zip_view(
127             const Sequences& seqs)
128             : sequences_(seqs)
129         {}
130 
131         sequences sequences_;
132     };
133 }}
134 
135 #endif
136