1/*=============================================================================
2 Copyright (c) 2015 Paul Fultz II
3 unpack.h
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6==============================================================================*/
7
8#ifndef BOOST_HOF_GUARD_UNPACK_H
9#define BOOST_HOF_GUARD_UNPACK_H
10
11/// unpack
12/// ======
13///
14/// Description
15/// -----------
16///
17/// The `unpack` function adaptor takes a sequence and uses the elements of
18/// the sequence for the arguments to the function. Multiple sequences can be
19/// passed to the function. All elements from each sequence will be passed
20/// into the function.
21///
22///
23/// Synopsis
24/// --------
25///
26/// template<class F>
27/// unpack_adaptor<F> unpack(F f);
28///
29/// Requirements
30/// ------------
31///
32/// F must be:
33///
34/// * [ConstInvocable](ConstInvocable)
35/// * MoveConstructible
36///
37/// Example
38/// -------
39///
40/// #include <boost/hof.hpp>
41/// #include <cassert>
42/// using namespace boost::hof;
43///
44/// struct sum
45/// {
46/// template<class T, class U>
47/// T operator()(T x, U y) const
48/// {
49/// return x+y;
50/// }
51/// };
52///
53/// int main() {
54/// int r = unpack(sum())(std::make_tuple(3,2));
55/// assert(r == 5);
56/// }
57///
58/// References
59/// ----------
60///
61/// * [std::apply](http://en.cppreference.com/w/cpp/utility/apply) - C++17 function to unpack a tuple
62/// * [`unpack_sequence`](unpack_sequence)
63///
64
65#include <boost/hof/unpack_sequence.hpp>
66#include <boost/hof/is_unpackable.hpp>
67#include <boost/hof/detail/seq.hpp>
68#include <boost/hof/capture.hpp>
69#include <boost/hof/always.hpp>
70#include <boost/hof/reveal.hpp>
71#include <boost/hof/detail/and.hpp>
72#include <boost/hof/detail/delegate.hpp>
73#include <boost/hof/detail/holder.hpp>
74#include <boost/hof/detail/move.hpp>
75#include <boost/hof/detail/make.hpp>
76#include <boost/hof/detail/static_const_var.hpp>
77
78namespace boost { namespace hof {
79
80namespace detail {
81
82template<class F, class Sequence>
83constexpr auto unpack_simple(F&& f, Sequence&& s) BOOST_HOF_RETURNS
84(
85 detail::unpack_impl(BOOST_HOF_FORWARD(F)(f), BOOST_HOF_FORWARD(Sequence)(s))
86)
87
88template<class F, class... Sequences>
89constexpr auto unpack_join(F&& f, Sequences&&... s) BOOST_HOF_RETURNS
90(
91 boost::hof::pack_join(unpack_simple(boost::hof::pack_forward, BOOST_HOF_FORWARD(Sequences)(s))...)(BOOST_HOF_FORWARD(F)(f))
92);
93
94}
95
96template<class F>
97struct unpack_adaptor : detail::callable_base<F>
98{
99 typedef unpack_adaptor fit_rewritable1_tag;
100 BOOST_HOF_INHERIT_CONSTRUCTOR(unpack_adaptor, detail::callable_base<F>);
101
102 template<class... Ts>
103 constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const noexcept
104 {
105 return boost::hof::always_ref(*this)(xs...);
106 }
107
108 struct unpack_failure
109 {
110 template<class Failure>
111 struct apply
112 {
113 struct deducer
114 {
115 template<class... Ts>
116 typename Failure::template of<Ts...> operator()(Ts&&...) const;
117 };
118
119 template<class T, class=typename std::enable_if<(
120 is_unpackable<T>::value
121 )>::type>
122 static auto deduce(T&& x)
123 BOOST_HOF_RETURNS
124 (
125 boost::hof::detail::unpack_simple(deducer(), BOOST_HOF_FORWARD(T)(x))
126 );
127
128 template<class T, class... Ts, class=typename std::enable_if<(
129 is_unpackable<T>::value && BOOST_HOF_AND_UNPACK(is_unpackable<Ts>::value)
130 )>::type>
131 static auto deduce(T&& x, Ts&&... xs) BOOST_HOF_RETURNS
132 (
133 boost::hof::detail::unpack_join(deducer(), BOOST_HOF_FORWARD(T)(x), BOOST_HOF_FORWARD(Ts)(xs)...)
134 );
135#ifdef _MSC_VER
136 template<class... Ts>
137 struct nop_failure;
138 template<class... Ts, class=typename std::enable_if<(
139 !BOOST_HOF_AND_UNPACK(is_unpackable<Ts>::value)
140 )>::type>
141 static as_failure<nop_failure> deduce(Ts&&... xs);
142#endif
143 template<class... Ts>
144 struct of
145#if (defined(__GNUC__) && !defined (__clang__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7) || defined (_MSC_VER)
146 : std::enable_if<true, decltype(apply::deduce(std::declval<Ts>()...))>::type
147#else
148 : decltype(apply::deduce(std::declval<Ts>()...))
149#endif
150 {};
151 };
152 };
153
154 struct failure
155 : failure_map<unpack_failure, detail::callable_base<F>>
156 {};
157
158 BOOST_HOF_RETURNS_CLASS(unpack_adaptor);
159 template<class T, class=typename std::enable_if<(
160 is_unpackable<T>::value
161 )>::type>
162 constexpr auto operator()(T&& x) const
163 BOOST_HOF_RETURNS
164 (
165 boost::hof::detail::unpack_simple(BOOST_HOF_MANGLE_CAST(const detail::callable_base<F>&)(BOOST_HOF_CONST_THIS->base_function(x)), BOOST_HOF_FORWARD(T)(x))
166 );
167
168 template<class T, class... Ts, class=typename std::enable_if<(
169 is_unpackable<T>::value && BOOST_HOF_AND_UNPACK(is_unpackable<Ts>::value)
170 )>::type>
171 constexpr auto operator()(T&& x, Ts&&... xs) const BOOST_HOF_RETURNS
172 (
173 boost::hof::detail::unpack_join(BOOST_HOF_MANGLE_CAST(const detail::callable_base<F>&)(BOOST_HOF_CONST_THIS->base_function(x)), BOOST_HOF_FORWARD(T)(x), BOOST_HOF_FORWARD(Ts)(xs)...)
174 );
175};
176
177BOOST_HOF_DECLARE_STATIC_VAR(unpack, detail::make<unpack_adaptor>);
178
179}} // namespace boost::hof
180
181#endif
182

source code of boost/libs/hof/include/boost/hof/unpack.hpp