1 #ifndef BOOST_MP11_TUPLE_HPP_INCLUDED
2 #define BOOST_MP11_TUPLE_HPP_INCLUDED
3
4 // Copyright 2015-2020 Peter Dimov.
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 //
8 // See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt
10
11 #include <boost/mp11/integer_sequence.hpp>
12 #include <boost/mp11/list.hpp>
13 #include <boost/mp11/function.hpp>
14 #include <boost/mp11/detail/config.hpp>
15 #include <tuple>
16 #include <utility>
17 #include <type_traits>
18 #include <cstddef>
19
20 #if BOOST_MP11_MSVC
21 # pragma warning( push )
22 # pragma warning( disable: 4100 ) // unreferenced formal parameter 'tp'
23 #endif
24
25 namespace boost
26 {
27 namespace mp11
28 {
29
30 // tuple_apply
31 namespace detail
32 {
33
tuple_apply_impl(F && f,Tp && tp,integer_sequence<std::size_t,J...>)34 template<class F, class Tp, std::size_t... J> BOOST_MP11_CONSTEXPR auto tuple_apply_impl( F && f, Tp && tp, integer_sequence<std::size_t, J...> )
35 -> decltype( std::forward<F>(f)( std::get<J>(std::forward<Tp>(tp))... ) )
36 {
37 return std::forward<F>(f)( std::get<J>(std::forward<Tp>(tp))... );
38 }
39
40 } // namespace detail
41
42 template<class F, class Tp,
43 class Seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp>::type>::value>>
tuple_apply(F && f,Tp && tp)44 BOOST_MP11_CONSTEXPR auto tuple_apply( F && f, Tp && tp )
45 -> decltype( detail::tuple_apply_impl( std::forward<F>(f), std::forward<Tp>(tp), Seq() ) )
46 {
47 return detail::tuple_apply_impl( std::forward<F>(f), std::forward<Tp>(tp), Seq() );
48 }
49
50 // construct_from_tuple
51 namespace detail
52 {
53
construct_from_tuple_impl(Tp && tp,integer_sequence<std::size_t,J...>)54 template<class T, class Tp, std::size_t... J> BOOST_MP11_CONSTEXPR T construct_from_tuple_impl( Tp && tp, integer_sequence<std::size_t, J...> )
55 {
56 return T( std::get<J>(std::forward<Tp>(tp))... );
57 }
58
59 } // namespace detail
60
61 template<class T, class Tp,
62 class Seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp>::type>::value>>
construct_from_tuple(Tp && tp)63 BOOST_MP11_CONSTEXPR T construct_from_tuple( Tp && tp )
64 {
65 return detail::construct_from_tuple_impl<T>( std::forward<Tp>(tp), Seq() );
66 }
67
68 // tuple_for_each
69 namespace detail
70 {
71
tuple_for_each_impl(Tp && tp,integer_sequence<std::size_t,J...>,F && f)72 template<class Tp, std::size_t... J, class F> BOOST_MP11_CONSTEXPR F tuple_for_each_impl( Tp && tp, integer_sequence<std::size_t, J...>, F && f )
73 {
74 using A = int[sizeof...(J)];
75 return (void)A{ ((void)f(std::get<J>(std::forward<Tp>(tp))), 0)... }, std::forward<F>(f);
76 }
77
tuple_for_each_impl(Tp &&,integer_sequence<std::size_t>,F && f)78 template<class Tp, class F> BOOST_MP11_CONSTEXPR F tuple_for_each_impl( Tp && /*tp*/, integer_sequence<std::size_t>, F && f )
79 {
80 return std::forward<F>(f);
81 }
82
83 } // namespace detail
84
tuple_for_each(Tp && tp,F && f)85 template<class Tp, class F> BOOST_MP11_CONSTEXPR F tuple_for_each( Tp && tp, F && f )
86 {
87 using seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp>::type>::value>;
88 return detail::tuple_for_each_impl( std::forward<Tp>(tp), seq(), std::forward<F>(f) );
89 }
90
91 // tuple_transform
92
93 namespace detail
94 {
95
96 // std::forward_as_tuple is not constexpr in C++11 or libstdc++ 5.x
tp_forward_r(T &&...t)97 template<class... T> BOOST_MP11_CONSTEXPR auto tp_forward_r( T&&... t ) -> std::tuple<T&&...>
98 {
99 return std::tuple<T&&...>( std::forward<T>( t )... );
100 }
101
tp_forward_v(T &&...t)102 template<class... T> BOOST_MP11_CONSTEXPR auto tp_forward_v( T&&... t ) -> std::tuple<T...>
103 {
104 return std::tuple<T...>( std::forward<T>( t )... );
105 }
106
107 template<std::size_t J, class... Tp>
tp_extract(Tp &&...tp)108 BOOST_MP11_CONSTEXPR auto tp_extract( Tp&&... tp )
109 -> decltype( tp_forward_r( std::get<J>( std::forward<Tp>( tp ) )... ) )
110 {
111 return tp_forward_r( std::get<J>( std::forward<Tp>( tp ) )... );
112 }
113
114 #if !BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1900 )
115
116 template<class F, class... Tp, std::size_t... J>
tuple_transform_impl(integer_sequence<std::size_t,J...>,F const & f,Tp &&...tp)117 BOOST_MP11_CONSTEXPR auto tuple_transform_impl( integer_sequence<std::size_t, J...>, F const& f, Tp&&... tp )
118 -> decltype( tp_forward_v( tuple_apply( f, tp_extract<J>( std::forward<Tp>(tp)... ) )... ) )
119 {
120 return tp_forward_v( tuple_apply( f, tp_extract<J>( std::forward<Tp>(tp)... ) )... );
121 }
122
123 #else
124
125 template<class F, class Tp1, std::size_t... J>
tuple_transform_impl(integer_sequence<std::size_t,J...>,F const & f,Tp1 && tp1)126 BOOST_MP11_CONSTEXPR auto tuple_transform_impl( integer_sequence<std::size_t, J...>, F const& f, Tp1&& tp1 )
127 -> decltype( tp_forward_v( f( std::get<J>( std::forward<Tp1>(tp1) ) )... ) )
128 {
129 return tp_forward_v( f( std::get<J>( std::forward<Tp1>(tp1) ) )... );
130 }
131
132 template<class F, class Tp1, class Tp2, std::size_t... J>
tuple_transform_impl(integer_sequence<std::size_t,J...>,F const & f,Tp1 && tp1,Tp2 && tp2)133 BOOST_MP11_CONSTEXPR auto tuple_transform_impl( integer_sequence<std::size_t, J...>, F const& f, Tp1&& tp1, Tp2&& tp2 )
134 -> decltype( tp_forward_v( f( std::get<J>( std::forward<Tp1>(tp1) ), std::get<J>( std::forward<Tp2>(tp2) ) )... ) )
135 {
136 return tp_forward_v( f( std::get<J>( std::forward<Tp1>(tp1) ), std::get<J>( std::forward<Tp2>(tp2) ) )... );
137 }
138
139 template<class F, class Tp1, class Tp2, class Tp3, std::size_t... J>
tuple_transform_impl(integer_sequence<std::size_t,J...>,F const & f,Tp1 && tp1,Tp2 && tp2,Tp3 && tp3)140 BOOST_MP11_CONSTEXPR auto tuple_transform_impl( integer_sequence<std::size_t, J...>, F const& f, Tp1&& tp1, Tp2&& tp2, Tp3&& tp3 )
141 -> decltype( tp_forward_v( f( std::get<J>( std::forward<Tp1>(tp1) ), std::get<J>( std::forward<Tp2>(tp2) ), std::get<J>( std::forward<Tp3>(tp3) ) )... ) )
142 {
143 return tp_forward_v( f( std::get<J>( std::forward<Tp1>(tp1) ), std::get<J>( std::forward<Tp2>(tp2) ), std::get<J>( std::forward<Tp3>(tp3) ) )... );
144 }
145
146 #endif // !BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1900 )
147
148 } // namespace detail
149
150 #if BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1910 )
151
152 template<class F, class Tp1, class... Tp,
153 class Seq = make_index_sequence<std::tuple_size<typename std::remove_reference<Tp1>::type>::value>>
tuple_transform(F const & f,Tp1 && tp1,Tp &&...tp)154 BOOST_MP11_CONSTEXPR auto tuple_transform( F const& f, Tp1&& tp1, Tp&&... tp )
155 -> decltype( detail::tuple_transform_impl( Seq(), f, std::forward<Tp1>(tp1), std::forward<Tp>(tp)... ) )
156 {
157 return detail::tuple_transform_impl( Seq(), f, std::forward<Tp1>(tp1), std::forward<Tp>(tp)... );
158 }
159
160 #else
161
162 template<class F, class... Tp,
163 class Z = mp_list<mp_size_t<std::tuple_size<typename std::remove_reference<Tp>::type>::value>...>,
164 class E = mp_if<mp_apply<mp_same, Z>, mp_front<Z>>,
165 class Seq = make_index_sequence<E::value>>
tuple_transform(F const & f,Tp &&...tp)166 BOOST_MP11_CONSTEXPR auto tuple_transform( F const& f, Tp&&... tp )
167 -> decltype( detail::tuple_transform_impl( Seq(), f, std::forward<Tp>(tp)... ) )
168 {
169 return detail::tuple_transform_impl( Seq(), f, std::forward<Tp>(tp)... );
170 }
171
172 #endif // BOOST_MP11_WORKAROUND( BOOST_MP11_MSVC, < 1910 )
173
174 } // namespace mp11
175 } // namespace boost
176
177 #if BOOST_MP11_MSVC
178 # pragma warning( pop )
179 #endif
180
181 #endif // #ifndef BOOST_TUPLE_HPP_INCLUDED
182