Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /*=============================================================================
0002     Copyright (c) 2015 Paul Fultz II
0003     combine.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_COMBINE_H
0009 #define BOOST_HOF_GUARD_COMBINE_H
0010 
0011 /// combine
0012 /// =======
0013 /// 
0014 /// Description
0015 /// -----------
0016 /// 
0017 /// The `combine` function adaptor combines several functions together with
0018 /// their arguments. It essentially zips each function with an argument before
0019 /// calling the main function.
0020 /// 
0021 /// Synopsis
0022 /// --------
0023 /// 
0024 ///     template<class F, class... Gs>
0025 ///     constexpr combine_adaptor<F, Gs...> combine(F f, Gs... gs);
0026 /// 
0027 /// Semantics
0028 /// ---------
0029 /// 
0030 ///     assert(combine(f, gs...)(xs...) == f(gs(xs)...));
0031 /// 
0032 /// Requirements
0033 /// ------------
0034 /// 
0035 /// F and Gs must be:
0036 /// 
0037 /// * [ConstInvocable](ConstInvocable)
0038 /// * MoveConstructible
0039 /// 
0040 /// Example
0041 /// -------
0042 /// 
0043 ///     #include <boost/hof.hpp>
0044 ///     #include <cassert>
0045 ///     #include <tuple>
0046 ///     #include <utility>
0047 /// 
0048 ///     int main() {
0049 ///         auto f = boost::hof::combine(
0050 ///             boost::hof::construct<std::tuple>(),
0051 ///             boost::hof::capture(1)(boost::hof::construct<std::pair>()),
0052 ///             boost::hof::capture(2)(boost::hof::construct<std::pair>()));
0053 ///         assert(f(3, 7) == std::make_tuple(std::make_pair(1, 3), std::make_pair(2, 7)));
0054 ///     }
0055 /// 
0056 
0057 #include <boost/hof/pack.hpp>
0058 #include <boost/hof/always.hpp>
0059 #include <boost/hof/detail/callable_base.hpp>
0060 #include <boost/hof/detail/make.hpp>
0061 
0062 namespace boost { namespace hof { namespace detail {
0063 
0064 template<class S, class F, class... Gs>
0065 struct combine_adaptor_base;
0066 
0067 template<std::size_t... Ns, class F, class... Gs>
0068 struct combine_adaptor_base<seq<Ns...>, F, Gs...>
0069 : F, pack_base<seq<Ns...>, Gs...>
0070 {
0071     typedef pack_base<seq<Ns...>, Gs...> base_type;
0072 
0073     BOOST_HOF_INHERIT_DEFAULT(combine_adaptor_base, base_type, F)
0074 
0075     template<class X, class... Xs, 
0076         BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(F, X),
0077         BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(base_type, Xs...)>
0078     constexpr combine_adaptor_base(X&& x, Xs&&... xs) 
0079     : F(BOOST_HOF_FORWARD(X)(x)), base_type(BOOST_HOF_FORWARD(Xs)(xs)...)
0080     {}
0081 
0082     template<class... Ts>
0083     constexpr const F& base_function(Ts&&... xs) const
0084     {
0085         return boost::hof::always_ref(*this)(xs...);
0086     }
0087 
0088     BOOST_HOF_RETURNS_CLASS(combine_adaptor_base);
0089 
0090 // Result needs to be calculated in a separate class to avoid confusing the
0091 // compiler on MSVC
0092 #if BOOST_HOF_NO_EXPRESSION_SFINAE || BOOST_HOF_HAS_MANUAL_DEDUCTION
0093     template<class... Ts>
0094     struct combine_result
0095     : result_of<const F&,  result_of<const Gs&, id_<Ts>>...>
0096     {};
0097 #endif
0098 
0099     template<class... Ts>
0100 #if BOOST_HOF_NO_EXPRESSION_SFINAE || BOOST_HOF_HAS_MANUAL_DEDUCTION
0101     constexpr typename combine_result<Ts...>::type
0102 #else
0103     constexpr auto
0104 #endif
0105     operator()(Ts&&... xs) const BOOST_HOF_SFINAE_MANUAL_RETURNS
0106     (
0107         (BOOST_HOF_MANGLE_CAST(const F&)(BOOST_HOF_CONST_THIS->base_function(xs...)))
0108             (boost::hof::alias_value<pack_tag<seq<Ns>, Gs...>, Gs>(*BOOST_HOF_CONST_THIS, xs)(BOOST_HOF_FORWARD(Ts)(xs))...)
0109     );
0110 };
0111 
0112 }
0113 
0114 template<class F, class... Gs>
0115 struct combine_adaptor
0116 : detail::combine_adaptor_base<typename detail::gens<sizeof...(Gs)>::type, detail::callable_base<F>, detail::callable_base<Gs>...>
0117 {
0118     typedef detail::combine_adaptor_base<typename detail::gens<sizeof...(Gs)>::type, detail::callable_base<F>, detail::callable_base<Gs>...> base_type;
0119     BOOST_HOF_INHERIT_CONSTRUCTOR(combine_adaptor, base_type)
0120 };
0121 
0122 BOOST_HOF_DECLARE_STATIC_VAR(combine, detail::make<combine_adaptor>);
0123 
0124 }} // namespace boost::hof
0125 
0126 #endif