1 /*=============================================================================
2 Copyright (c) 2015 Paul Fultz II
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 ==============================================================================*/
8 #ifndef BOOST_HOF_GUARD_COMBINE_H
9 #define BOOST_HOF_GUARD_COMBINE_H
17 /// The `combine` function adaptor combines several functions together with
18 /// their arguments. It essentially zips each function with an argument before
19 /// calling the main function.
24 /// template<class F, class... Gs>
25 /// constexpr combine_adaptor<F, Gs...> combine(F f, Gs... gs);
30 /// assert(combine(f, gs...)(xs...) == f(gs(xs)...));
37 /// * [ConstInvocable](ConstInvocable)
38 /// * MoveConstructible
43 /// #include <boost/hof.hpp>
44 /// #include <cassert>
46 /// #include <utility>
49 /// auto f = boost::hof::combine(
50 /// boost::hof::construct<std::tuple>(),
51 /// boost::hof::capture(1)(boost::hof::construct<std::pair>()),
52 /// boost::hof::capture(2)(boost::hof::construct<std::pair>()));
53 /// assert(f(3, 7) == std::make_tuple(std::make_pair(1, 3), std::make_pair(2, 7)));
57 #include <boost/hof/pack.hpp>
58 #include <boost/hof/always.hpp>
59 #include <boost/hof/detail/callable_base.hpp>
60 #include <boost/hof/detail/make.hpp>
62 namespace boost { namespace hof { namespace detail {
64 template<class S, class F, class... Gs>
65 struct combine_adaptor_base;
67 template<std::size_t... Ns, class F, class... Gs>
68 struct combine_adaptor_base<seq<Ns...>, F, Gs...>
69 : F, pack_base<seq<Ns...>, Gs...>
71 typedef pack_base<seq<Ns...>, Gs...> base_type;
73 BOOST_HOF_INHERIT_DEFAULT(combine_adaptor_base, base_type, F)
75 template<class X, class... Xs,
76 BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(F, X),
77 BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(base_type, Xs...)>
78 constexpr combine_adaptor_base(X&& x, Xs&&... xs)
79 : F(BOOST_HOF_FORWARD(X)(x)), base_type(BOOST_HOF_FORWARD(Xs)(xs)...)
83 constexpr const F& base_function(Ts&&... xs) const
85 return boost::hof::always_ref(*this)(xs...);
88 BOOST_HOF_RETURNS_CLASS(combine_adaptor_base);
90 // Result needs to be calculated in a separate class to avoid confusing the
92 #if BOOST_HOF_NO_EXPRESSION_SFINAE || BOOST_HOF_HAS_MANUAL_DEDUCTION
95 : result_of<const F&, result_of<const Gs&, id_<Ts>>...>
100 #if BOOST_HOF_NO_EXPRESSION_SFINAE || BOOST_HOF_HAS_MANUAL_DEDUCTION
101 constexpr typename combine_result<Ts...>::type
105 operator()(Ts&&... xs) const BOOST_HOF_SFINAE_MANUAL_RETURNS
107 (BOOST_HOF_MANGLE_CAST(const F&)(BOOST_HOF_CONST_THIS->base_function(xs...)))
108 (boost::hof::alias_value<pack_tag<seq<Ns>, Gs...>, Gs>(*BOOST_HOF_CONST_THIS, xs)(BOOST_HOF_FORWARD(Ts)(xs))...)
114 template<class F, class... Gs>
115 struct combine_adaptor
116 : detail::combine_adaptor_base<typename detail::gens<sizeof...(Gs)>::type, detail::callable_base<F>, detail::callable_base<Gs>...>
118 typedef detail::combine_adaptor_base<typename detail::gens<sizeof...(Gs)>::type, detail::callable_base<F>, detail::callable_base<Gs>...> base_type;
119 BOOST_HOF_INHERIT_CONSTRUCTOR(combine_adaptor, base_type)
122 BOOST_HOF_DECLARE_STATIC_VAR(combine, detail::make<combine_adaptor>);
124 }} // namespace boost::hof