/*============================================================================= Copyright (c) 2014 Paul Fultz II lazy.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_FUNCTION_LAZY_H #define BOOST_HOF_GUARD_FUNCTION_LAZY_H /// lazy /// ==== /// /// Description /// ----------- /// /// The `lazy` function adaptor returns a function object call wrapper for a /// function. Calling this wrapper is equivalent to invoking the function. It /// is a simple form of lambda expressions, but is constexpr friendly. By /// default, `lazy` captures all of its variables by value, just like `bind`. /// `std::ref` can be used to capture references instead. /// /// Ultimately, calling `lazy(f)(x)` is the equivalent to calling /// `std::bind(f, x)` except the lazy version can be called in a constexpr /// context, as well. The `lazy` adaptor is compatible with `std::bind`, so /// most of the time `lazy` and `std::bind` can be used interchangeably. /// /// Synopsis /// -------- /// /// template /// constexpr lazy_adaptor lazy(F f); /// /// Semantics /// --------- /// /// assert(lazy(f)(xs...) == std::bind(f, xs...)) /// assert(lazy(f)(xs...)() == f(xs...)) /// assert(lazy(f)(_1)(x) == f(x)) /// assert(lazy(f)(lazy(g)(_1))(x) == f(g(x))) /// /// Requirements /// ------------ /// /// F must be: /// /// * [ConstInvocable](ConstInvocable) /// * MoveConstructible /// /// Example /// ------- /// /// #include /// #include /// using namespace boost::hof; /// /// int main() { /// auto add = [](auto x, auto y) { return x+y; }; /// auto increment = lazy(add)(_1, 1); /// assert(increment(5) == 6); /// } /// /// References /// ---------- /// /// * [std::bind](http://en.cppreference.com/w/cpp/utility/functional/bind) /// #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace hof { namespace detail { struct placeholder_transformer { template::value > 0), int>::type = 0> constexpr detail::make_args_f::value> operator()(const T&) const noexcept { return {}; } }; struct bind_transformer { template::value, int>::type = 0> constexpr const T& operator()(const T& x) const noexcept { return x; } }; struct ref_transformer { template constexpr auto operator()(std::reference_wrapper x) const BOOST_HOF_SFINAE_RETURNS(boost::hof::always_ref(x.get())); }; struct id_transformer { template constexpr auto operator()(T&& x) const BOOST_HOF_SFINAE_RETURNS(always_detail::always_base(BOOST_HOF_FORWARD(T)(x))); }; BOOST_HOF_DECLARE_STATIC_VAR(pick_transformer, first_of_adaptor); template constexpr auto lazy_transform(T&& x, const Pack& p) BOOST_HOF_RETURNS ( p(boost::hof::detail::pick_transformer(BOOST_HOF_FORWARD(T)(x))) ); template struct lazy_unpack { const F& f; const Pack& p; constexpr lazy_unpack(const F& fp, const Pack& pp) noexcept : f(fp), p(pp) {} template constexpr auto operator()(Ts&&... xs) const BOOST_HOF_RETURNS ( f(lazy_transform(BOOST_HOF_FORWARD(Ts)(xs), p)...) ); }; template constexpr lazy_unpack make_lazy_unpack(const F& f, const Pack& p) noexcept { return lazy_unpack(f, p); } template struct lazy_invoker : detail::compressed_pair { typedef detail::compressed_pair base_type; typedef lazy_invoker fit_rewritable1_tag; #ifdef _MSC_VER BOOST_HOF_INHERIT_CONSTRUCTOR(lazy_invoker, base_type) #else BOOST_HOF_INHERIT_DEFAULT_EMPTY(lazy_invoker, base_type) template constexpr lazy_invoker(X&& x, Y&& y) BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(base_type, X&&, Y&&) : base_type(BOOST_HOF_FORWARD(X)(x), BOOST_HOF_FORWARD(Y)(y)) {} #endif template constexpr const F& base_function(Ts&&... xs) const noexcept { return this->first(xs...); } template constexpr const Pack& get_pack(Ts&&... xs) const noexcept { return this->second(xs...); } BOOST_HOF_RETURNS_CLASS(lazy_invoker); template constexpr auto operator()(Ts&&... xs) const BOOST_HOF_RETURNS ( BOOST_HOF_MANGLE_CAST(const Pack&)(BOOST_HOF_CONST_THIS->get_pack(xs...))( boost::hof::detail::make_lazy_unpack( BOOST_HOF_MANGLE_CAST(const F&)(BOOST_HOF_CONST_THIS->base_function(xs...)), boost::hof::pack_forward(BOOST_HOF_FORWARD(Ts)(xs)...) ) ) ); }; template constexpr lazy_invoker make_lazy_invoker(F f, Pack pack) BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(lazy_invoker, F&&, Pack&&) { return lazy_invoker(static_cast(f), static_cast(pack)); } template struct lazy_nullary_invoker : F { BOOST_HOF_INHERIT_CONSTRUCTOR(lazy_nullary_invoker, F); template constexpr const F& base_function(Ts&&... xs) const noexcept { return boost::hof::always_ref(*this)(xs...); } BOOST_HOF_RETURNS_CLASS(lazy_nullary_invoker); template constexpr auto operator()(Ts&&... xs) const BOOST_HOF_RETURNS ( BOOST_HOF_MANGLE_CAST(const F&)(BOOST_HOF_CONST_THIS->base_function(xs...))() ); }; template constexpr lazy_nullary_invoker make_lazy_nullary_invoker(F f) BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(lazy_nullary_invoker, F&&) { return lazy_nullary_invoker(static_cast(f)); } } template struct lazy_adaptor : detail::callable_base { BOOST_HOF_INHERIT_CONSTRUCTOR(lazy_adaptor, detail::callable_base); template constexpr const detail::callable_base& base_function(Ts&&... xs) const noexcept { return boost::hof::always_ref(*this)(xs...); } BOOST_HOF_RETURNS_CLASS(lazy_adaptor); template constexpr auto operator()(T x, Ts... xs) const BOOST_HOF_RETURNS ( boost::hof::detail::make_lazy_invoker(BOOST_HOF_RETURNS_C_CAST(detail::callable_base&&)(BOOST_HOF_CONST_THIS->base_function(x, xs...)), boost::hof::pack_basic(BOOST_HOF_RETURNS_STATIC_CAST(T&&)(x), BOOST_HOF_RETURNS_STATIC_CAST(Ts&&)(xs)...)) ); // Workaround for gcc 4.7 template constexpr detail::lazy_nullary_invoker operator()() const BOOST_HOF_RETURNS_DEDUCE_NOEXCEPT( boost::hof::detail::make_lazy_nullary_invoker(BOOST_HOF_RETURNS_C_CAST(detail::callable_base&&)( BOOST_HOF_CONST_THIS->base_function(BOOST_HOF_RETURNS_CONSTRUCT(Unused)()) )) ) { return boost::hof::detail::make_lazy_nullary_invoker((detail::callable_base&&)( this->base_function(Unused()) )); } // TODO: Overloads to use with ref qualifiers // template // constexpr auto operator()(Ts... xs) const& BOOST_HOF_RETURNS // ( // boost::hof::detail::make_lazy_invoker(this->base_function(xs...), // pack(boost::hof::move(xs)...)) // ); // template // constexpr auto operator()(Ts... xs) && BOOST_HOF_RETURNS // ( // boost::hof::detail::make_lazy_invoker((F&&)this->base_function(xs...), // pack(boost::hof::move(xs)...)) // ); }; BOOST_HOF_DECLARE_STATIC_VAR(lazy, detail::make); }} // namespace boost::hof namespace std { template struct is_bind_expression> : std::true_type {}; template struct is_bind_expression> : std::true_type {}; } #endif