Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:17

0001 /*=============================================================================
0002     Copyright (c) 2015 Paul Fultz II
0003     unpack.h
0004     Distributed under the Boost Software License, Version 1.0. (See accompanying
0005     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 ==============================================================================*/
0007 
0008 #ifndef BOOST_HOF_GUARD_UNPACK_H
0009 #define BOOST_HOF_GUARD_UNPACK_H
0010 
0011 /// unpack
0012 /// ======
0013 /// 
0014 /// Description
0015 /// -----------
0016 /// 
0017 /// The `unpack` function adaptor takes a sequence and uses the elements of
0018 /// the sequence for the arguments to the function. Multiple sequences can be
0019 /// passed to the function. All elements from each sequence will be passed
0020 /// into the function. 
0021 /// 
0022 /// 
0023 /// Synopsis
0024 /// --------
0025 /// 
0026 ///     template<class F>
0027 ///     unpack_adaptor<F> unpack(F f);
0028 /// 
0029 /// Requirements
0030 /// ------------
0031 /// 
0032 /// F must be:
0033 /// 
0034 /// * [ConstInvocable](ConstInvocable)
0035 /// * MoveConstructible
0036 /// 
0037 /// Example
0038 /// -------
0039 /// 
0040 ///     #include <boost/hof.hpp>
0041 ///     #include <cassert>
0042 ///     using namespace boost::hof;
0043 /// 
0044 ///     struct sum
0045 ///     {
0046 ///         template<class T, class U>
0047 ///         T operator()(T x, U y) const
0048 ///         {
0049 ///             return x+y;
0050 ///         }
0051 ///     };
0052 /// 
0053 ///     int main() {
0054 ///         int r = unpack(sum())(std::make_tuple(3,2));
0055 ///         assert(r == 5);
0056 ///     }
0057 /// 
0058 /// References
0059 /// ----------
0060 /// 
0061 /// * [std::apply](http://en.cppreference.com/w/cpp/utility/apply) - C++17 function to unpack a tuple
0062 /// * [`unpack_sequence`](unpack_sequence)
0063 /// 
0064 
0065 #include <boost/hof/unpack_sequence.hpp>
0066 #include <boost/hof/is_unpackable.hpp>
0067 #include <boost/hof/detail/seq.hpp>
0068 #include <boost/hof/capture.hpp>
0069 #include <boost/hof/always.hpp>
0070 #include <boost/hof/reveal.hpp>
0071 #include <boost/hof/detail/and.hpp>
0072 #include <boost/hof/detail/delegate.hpp>
0073 #include <boost/hof/detail/holder.hpp>
0074 #include <boost/hof/detail/move.hpp>
0075 #include <boost/hof/detail/make.hpp>
0076 #include <boost/hof/detail/static_const_var.hpp>
0077 
0078 namespace boost { namespace hof {
0079 
0080 namespace detail {
0081 
0082 template<class F, class Sequence>
0083 constexpr auto unpack_simple(F&& f, Sequence&& s) BOOST_HOF_RETURNS
0084 (
0085     detail::unpack_impl(BOOST_HOF_FORWARD(F)(f), BOOST_HOF_FORWARD(Sequence)(s))
0086 )
0087 
0088 template<class F, class... Sequences>
0089 constexpr auto unpack_join(F&& f, Sequences&&... s) BOOST_HOF_RETURNS
0090 (
0091     boost::hof::pack_join(unpack_simple(boost::hof::pack_forward, BOOST_HOF_FORWARD(Sequences)(s))...)(BOOST_HOF_FORWARD(F)(f))
0092 );
0093 
0094 }
0095 
0096 template<class F>
0097 struct unpack_adaptor : detail::callable_base<F>
0098 {
0099     typedef unpack_adaptor fit_rewritable1_tag;
0100     BOOST_HOF_INHERIT_CONSTRUCTOR(unpack_adaptor, detail::callable_base<F>);
0101 
0102     template<class... Ts>
0103     constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const noexcept
0104     {
0105         return boost::hof::always_ref(*this)(xs...);
0106     }
0107 
0108     struct unpack_failure
0109     {
0110         template<class Failure>
0111         struct apply
0112         {
0113             struct deducer
0114             {
0115                 template<class... Ts>
0116                 typename Failure::template of<Ts...> operator()(Ts&&...) const;
0117             };
0118 
0119             template<class T, class=typename std::enable_if<(
0120                 is_unpackable<T>::value
0121             )>::type>
0122             static auto deduce(T&& x)
0123             BOOST_HOF_RETURNS
0124             (
0125                 boost::hof::detail::unpack_simple(deducer(), BOOST_HOF_FORWARD(T)(x))
0126             );
0127 
0128             template<class T, class... Ts, class=typename std::enable_if<(
0129                 is_unpackable<T>::value && BOOST_HOF_AND_UNPACK(is_unpackable<Ts>::value)
0130             )>::type>
0131             static auto deduce(T&& x, Ts&&... xs) BOOST_HOF_RETURNS
0132             (
0133                 boost::hof::detail::unpack_join(deducer(), BOOST_HOF_FORWARD(T)(x), BOOST_HOF_FORWARD(Ts)(xs)...)
0134             );
0135 #ifdef _MSC_VER
0136             template<class... Ts>
0137             struct nop_failure;
0138             template<class... Ts, class=typename std::enable_if<(
0139                 !BOOST_HOF_AND_UNPACK(is_unpackable<Ts>::value)
0140             )>::type>
0141             static as_failure<nop_failure> deduce(Ts&&... xs);
0142 #endif
0143             template<class... Ts>
0144             struct of
0145 #if (defined(__GNUC__) && !defined (__clang__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7) || defined (_MSC_VER)
0146             : std::enable_if<true, decltype(apply::deduce(std::declval<Ts>()...))>::type
0147 #else
0148             : decltype(apply::deduce(std::declval<Ts>()...))
0149 #endif
0150             {};
0151         };
0152     };
0153 
0154     struct failure
0155     : failure_map<unpack_failure, detail::callable_base<F>>
0156     {};
0157 
0158     BOOST_HOF_RETURNS_CLASS(unpack_adaptor);
0159     template<class T, class=typename std::enable_if<(
0160         is_unpackable<T>::value
0161     )>::type>
0162     constexpr auto operator()(T&& x) const
0163     BOOST_HOF_RETURNS
0164     (
0165         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))
0166     );
0167 
0168     template<class T, class... Ts, class=typename std::enable_if<(
0169         is_unpackable<T>::value && BOOST_HOF_AND_UNPACK(is_unpackable<Ts>::value)
0170     )>::type>
0171     constexpr auto operator()(T&& x, Ts&&... xs) const BOOST_HOF_RETURNS
0172     (
0173         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)...)
0174     );
0175 };
0176 
0177 BOOST_HOF_DECLARE_STATIC_VAR(unpack, detail::make<unpack_adaptor>);
0178 
0179 }} // namespace boost::hof
0180 
0181 #endif