Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /*=============================================================================
0002     Copyright (c) 2012 Paul Fultz II
0003     partial.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_FUNCTION_PARTIAL_H
0009 #define BOOST_HOF_GUARD_FUNCTION_PARTIAL_H
0010 
0011 /// partial
0012 /// ========
0013 /// 
0014 /// Description
0015 /// -----------
0016 /// 
0017 /// The `partial` function adaptor allows partial application of the function.
0018 /// If the function can not be called with all the parameters, it will return
0019 /// another function. It will repeatedly do this until the function can
0020 /// finally be called. By default, `partial` captures all of its variables by
0021 /// value, just like bind. As such all parameters must be `MoveConstructible`
0022 /// when the function is aprtial application. `std::ref` can be used to
0023 /// capture references instead.
0024 /// 
0025 /// Synopsis
0026 /// --------
0027 /// 
0028 ///     template<class F>
0029 ///     constexpr partial_adaptor<F> partial(F f);
0030 /// 
0031 /// Semantics
0032 /// ---------
0033 /// 
0034 ///     assert(partial(f)(xs...)(ys...) == f(xs..., ys...));
0035 /// 
0036 /// Requirements
0037 /// ------------
0038 /// 
0039 /// F must be:
0040 /// 
0041 /// * [ConstInvocable](ConstInvocable)
0042 /// * MoveConstructible
0043 /// 
0044 /// Example
0045 /// -------
0046 /// 
0047 ///     #include <boost/hof.hpp>
0048 ///     #include <cassert>
0049 ///     using namespace boost::hof;
0050 /// 
0051 ///     struct sum
0052 ///     {
0053 ///         template<class T, class U>
0054 ///         T operator()(T x, U y) const
0055 ///         {
0056 ///             return x+y;
0057 ///         }
0058 ///     };
0059 /// 
0060 ///     int main() {
0061 ///         assert(3 == partial(sum())(1)(2));
0062 ///     }
0063 /// 
0064 /// References
0065 /// ----------
0066 /// 
0067 /// * [Partial application](https://en.wikipedia.org/wiki/Partial_application)
0068 /// * [Currying](https://en.wikipedia.org/wiki/Currying)
0069 /// 
0070 
0071 #include <boost/hof/first_of.hpp>
0072 #include <boost/hof/static.hpp>
0073 #include <boost/hof/pipable.hpp>
0074 #include <boost/hof/detail/make.hpp>
0075 #include <boost/hof/detail/static_const_var.hpp>
0076 
0077 
0078 namespace boost { namespace hof { 
0079 
0080 // TODO: Get rid of sequence parameter
0081 // Forward declare partial_adaptor, since it will be used below
0082 template<class F, class Pack=void >
0083 struct partial_adaptor;
0084 
0085 BOOST_HOF_DECLARE_STATIC_VAR(partial, detail::make<partial_adaptor>);
0086 
0087 namespace detail {
0088 
0089 template<class Derived, class F, class Pack>
0090 struct partial_adaptor_invoke
0091 {
0092     template<class... Ts>
0093     constexpr const F& get_function(Ts&&...) const noexcept
0094     {
0095         return static_cast<const F&>(static_cast<const Derived&>(*this));
0096     }
0097 
0098     template<class... Ts>
0099     constexpr const Pack& get_pack(Ts&&...) const noexcept
0100     {
0101         return static_cast<const Pack&>(static_cast<const Derived&>(*this));
0102     }
0103 
0104     BOOST_HOF_RETURNS_CLASS(partial_adaptor_invoke);
0105 
0106     template<class... Ts>
0107     constexpr BOOST_HOF_SFINAE_RESULT
0108     (
0109         typename result_of<decltype(boost::hof::pack_join), 
0110             id_<const Pack&>, 
0111             result_of<decltype(boost::hof::pack_forward), id_<Ts>...> 
0112         >::type,
0113         id_<F&&>
0114     ) 
0115     operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
0116     (
0117         boost::hof::pack_join
0118         (
0119             BOOST_HOF_MANGLE_CAST(const Pack&)(BOOST_HOF_CONST_THIS->get_pack(xs...)), 
0120             boost::hof::pack_forward(BOOST_HOF_FORWARD(Ts)(xs)...)
0121         )
0122         (BOOST_HOF_RETURNS_C_CAST(F&&)(BOOST_HOF_CONST_THIS->get_function(xs...)))
0123     );
0124 };
0125 
0126 #ifdef _MSC_VER
0127 #define BOOST_HOF_PARTIAL_RETURNS(...) -> decltype(__VA_ARGS__) { return (__VA_ARGS__); }
0128 #else
0129 #define BOOST_HOF_PARTIAL_RETURNS BOOST_HOF_SFINAE_RETURNS
0130 #endif
0131 
0132 template<class Derived, class F, class Pack>
0133 struct partial_adaptor_join
0134 {
0135     template<class... Ts>
0136     constexpr const F& get_function(Ts&&...) const noexcept
0137     {
0138         return static_cast<const F&>(static_cast<const Derived&>(*this));
0139     }
0140 
0141     template<class... Ts>
0142     constexpr const Pack& get_pack(Ts&&...) const noexcept
0143     {
0144         return static_cast<const Pack&>(static_cast<const Derived&>(*this));
0145     }
0146 
0147     BOOST_HOF_RETURNS_CLASS(partial_adaptor_join);
0148 
0149     template<class... Ts, class=typename std::enable_if<
0150         ((sizeof...(Ts) + Pack::fit_function_param_limit::value) < function_param_limit<F>::value)
0151     >::type>
0152     constexpr auto operator()(Ts&&... xs) const 
0153 #ifdef _MSC_VER
0154     // Workaround ICE on MSVC
0155     noexcept(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(F, F&&) && noexcept(boost::hof::pack_join(std::declval<const Pack&>(), boost::hof::pack(BOOST_HOF_FORWARD(Ts)(xs)...))))
0156 #endif
0157     BOOST_HOF_PARTIAL_RETURNS
0158     (
0159         boost::hof::partial
0160         (
0161             BOOST_HOF_RETURNS_C_CAST(F&&)(BOOST_HOF_CONST_THIS->get_function(xs...)), 
0162             boost::hof::pack_join(BOOST_HOF_MANGLE_CAST(const Pack&)(BOOST_HOF_CONST_THIS->get_pack(xs...)), boost::hof::pack(BOOST_HOF_FORWARD(Ts)(xs)...))
0163         )
0164     );
0165 };
0166 
0167 template<class Derived, class F>
0168 struct partial_adaptor_pack
0169 {
0170 
0171     constexpr partial_adaptor_pack() noexcept
0172     {}
0173     
0174     template<class... Ts>
0175     constexpr const F& get_function(Ts&&...) const noexcept
0176     {
0177         return static_cast<const F&>(static_cast<const Derived&>(*this));
0178     }
0179 
0180     BOOST_HOF_RETURNS_CLASS(partial_adaptor_pack);
0181 
0182     template<class... Ts, class=typename std::enable_if<
0183         (sizeof...(Ts) < function_param_limit<F>::value)
0184     >::type>
0185     constexpr auto operator()(Ts&&... xs) const 
0186 #ifdef _MSC_VER
0187     // Workaround ICE on MSVC
0188     noexcept(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(F, F&&) && noexcept(boost::hof::pack(BOOST_HOF_FORWARD(Ts)(xs)...)))
0189 #endif
0190     BOOST_HOF_PARTIAL_RETURNS
0191     (
0192         boost::hof::partial
0193         (
0194             BOOST_HOF_RETURNS_C_CAST(F&&)(BOOST_HOF_CONST_THIS->get_function(xs...)), 
0195             boost::hof::pack(BOOST_HOF_FORWARD(Ts)(xs)...)
0196         )
0197     );
0198 };
0199 template<class F, class Pack>
0200 struct partial_adaptor_base 
0201 {
0202     typedef basic_first_of_adaptor
0203     <
0204         partial_adaptor_invoke<partial_adaptor<F, Pack>, F, Pack>,
0205         partial_adaptor_join<partial_adaptor<F, Pack>, F, Pack> 
0206     > type;
0207 };
0208 
0209 template<class Derived, class F>
0210 struct partial_adaptor_pack_base
0211 {
0212     typedef basic_first_of_adaptor
0213     <
0214         F,
0215         partial_adaptor_pack<Derived, F> 
0216     > type;
0217 };
0218 
0219 }
0220 
0221 template<class F, class Pack>
0222 struct partial_adaptor : detail::partial_adaptor_base<F, Pack>::type, F, Pack
0223 {
0224     typedef typename detail::partial_adaptor_base<F, Pack>::type base;
0225 
0226     typedef partial_adaptor fit_rewritable1_tag;
0227     
0228     template<class... Ts>
0229     constexpr const F& base_function(Ts&&...) const noexcept
0230     {
0231         return *this;
0232     }
0233 
0234     constexpr const Pack& get_pack() const noexcept
0235     {
0236         return *this;
0237     }
0238 
0239     using base::operator();
0240 
0241     BOOST_HOF_INHERIT_DEFAULT(partial_adaptor, base, F, Pack);
0242 
0243     template<class X, class S>
0244     constexpr partial_adaptor(X&& x, S&& seq) 
0245     BOOST_HOF_NOEXCEPT(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(F, X&&) && BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(Pack, S&&))
0246     : F(BOOST_HOF_FORWARD(X)(x)), Pack(BOOST_HOF_FORWARD(S)(seq))
0247     {}
0248 };
0249 
0250 template<class F>
0251 struct partial_adaptor<F, void> : detail::partial_adaptor_pack_base<partial_adaptor<F, void>, detail::callable_base<F>>::type
0252 {
0253     typedef typename detail::partial_adaptor_pack_base<partial_adaptor<F, void>, detail::callable_base<F>>::type base;
0254 
0255     typedef partial_adaptor fit_rewritable1_tag;
0256     
0257     template<class... Ts>
0258     constexpr const detail::callable_base<F>& base_function(Ts&&...) const noexcept
0259     {
0260         return *this;
0261     }
0262 
0263     using base::operator();
0264 
0265     BOOST_HOF_INHERIT_CONSTRUCTOR(partial_adaptor, base);
0266 
0267 };
0268 
0269 // Make partial_adaptor work with pipable_adaptor by removing its pipableness
0270 template<class F>
0271 struct partial_adaptor<pipable_adaptor<F>, void>
0272 : partial_adaptor<F, void>
0273 {
0274     typedef partial_adaptor<F, void> base;
0275 
0276     typedef partial_adaptor fit_rewritable1_tag;
0277     BOOST_HOF_INHERIT_CONSTRUCTOR(partial_adaptor, base);
0278 };
0279 
0280 template<class F>
0281 struct partial_adaptor<static_<pipable_adaptor<F>>, void>
0282 : partial_adaptor<F, void>
0283 {
0284     typedef partial_adaptor<F, void> base;
0285 
0286     typedef partial_adaptor fit_rewritable1_tag;
0287 
0288     BOOST_HOF_INHERIT_CONSTRUCTOR(partial_adaptor, base);
0289 };
0290 }} // namespace boost::hof
0291 
0292 #endif