/*============================================================================= Copyright (c) 2015 Paul Fultz II unpack.h Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) ==============================================================================*/ #ifndef BOOST_HOF_GUARD_UNPACK_H #define BOOST_HOF_GUARD_UNPACK_H /// unpack /// ====== /// /// Description /// ----------- /// /// The `unpack` function adaptor takes a sequence and uses the elements of /// the sequence for the arguments to the function. Multiple sequences can be /// passed to the function. All elements from each sequence will be passed /// into the function. /// /// /// Synopsis /// -------- /// /// template /// unpack_adaptor unpack(F f); /// /// Requirements /// ------------ /// /// F must be: /// /// * [ConstInvocable](ConstInvocable) /// * MoveConstructible /// /// Example /// ------- /// /// #include /// #include /// using namespace boost::hof; /// /// struct sum /// { /// template /// T operator()(T x, U y) const /// { /// return x+y; /// } /// }; /// /// int main() { /// int r = unpack(sum())(std::make_tuple(3,2)); /// assert(r == 5); /// } /// /// References /// ---------- /// /// * [std::apply](http://en.cppreference.com/w/cpp/utility/apply) - C++17 function to unpack a tuple /// * [`unpack_sequence`](unpack_sequence) /// #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace hof { namespace detail { template constexpr auto unpack_simple(F&& f, Sequence&& s) BOOST_HOF_RETURNS ( detail::unpack_impl(BOOST_HOF_FORWARD(F)(f), BOOST_HOF_FORWARD(Sequence)(s)) ) template constexpr auto unpack_join(F&& f, Sequences&&... s) BOOST_HOF_RETURNS ( boost::hof::pack_join(unpack_simple(boost::hof::pack_forward, BOOST_HOF_FORWARD(Sequences)(s))...)(BOOST_HOF_FORWARD(F)(f)) ); } template struct unpack_adaptor : detail::callable_base { typedef unpack_adaptor fit_rewritable1_tag; BOOST_HOF_INHERIT_CONSTRUCTOR(unpack_adaptor, detail::callable_base); template constexpr const detail::callable_base& base_function(Ts&&... xs) const noexcept { return boost::hof::always_ref(*this)(xs...); } struct unpack_failure { template struct apply { struct deducer { template typename Failure::template of operator()(Ts&&...) const; }; template::value )>::type> static auto deduce(T&& x) BOOST_HOF_RETURNS ( boost::hof::detail::unpack_simple(deducer(), BOOST_HOF_FORWARD(T)(x)) ); template::value && BOOST_HOF_AND_UNPACK(is_unpackable::value) )>::type> static auto deduce(T&& x, Ts&&... xs) BOOST_HOF_RETURNS ( boost::hof::detail::unpack_join(deducer(), BOOST_HOF_FORWARD(T)(x), BOOST_HOF_FORWARD(Ts)(xs)...) ); #ifdef _MSC_VER template struct nop_failure; template::value) )>::type> static as_failure deduce(Ts&&... xs); #endif template struct of #if (defined(__GNUC__) && !defined (__clang__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7) || defined (_MSC_VER) : std::enable_if()...))>::type #else : decltype(apply::deduce(std::declval()...)) #endif {}; }; }; struct failure : failure_map> {}; BOOST_HOF_RETURNS_CLASS(unpack_adaptor); template::value )>::type> constexpr auto operator()(T&& x) const BOOST_HOF_RETURNS ( boost::hof::detail::unpack_simple(BOOST_HOF_MANGLE_CAST(const detail::callable_base&)(BOOST_HOF_CONST_THIS->base_function(x)), BOOST_HOF_FORWARD(T)(x)) ); template::value && BOOST_HOF_AND_UNPACK(is_unpackable::value) )>::type> constexpr auto operator()(T&& x, Ts&&... xs) const BOOST_HOF_RETURNS ( boost::hof::detail::unpack_join(BOOST_HOF_MANGLE_CAST(const detail::callable_base&)(BOOST_HOF_CONST_THIS->base_function(x)), BOOST_HOF_FORWARD(T)(x), BOOST_HOF_FORWARD(Ts)(xs)...) ); }; BOOST_HOF_DECLARE_STATIC_VAR(unpack, detail::make); }} // namespace boost::hof #endif