Edit: /usr/include/boost/hof/decorate.hpp (6329B)
/*=============================================================================
Copyright (c) 2015 Paul Fultz II
decorate.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_DECORATE_H
#define BOOST_HOF_GUARD_DECORATE_H
/// decorate
/// ========
///
/// Description
/// -----------
///
/// The `decorate` function adaptor helps create simple function decorators.
///
/// A function adaptor takes a function and returns a new functions whereas a
/// decorator takes some parameters and returns a function adaptor. The
/// `decorate` function adaptor will return a decorator that returns a
/// function adaptor. Eventually, it will invoke the function with the user-
/// provided parameter and function.
///
/// Synopsis
/// --------
///
/// template
/// constexpr decorate_adaptor decorate(F f);
///
/// Semantics
/// ---------
///
/// assert(decorate(f)(x)(g)(xs...) == f(x, g, xs...));
///
/// Requirements
/// ------------
///
/// F must be:
///
/// * [ConstInvocable](ConstInvocable)
/// * MoveConstructible
///
/// Example
/// -------
///
/// #include
/// #include
/// #include
/// #include
/// using namespace boost::hof;
///
/// struct logger_f
/// {
/// template
/// auto operator()(const std::string& message, F&& f, Ts&&... xs) const
/// -> decltype(f(std::forward(xs)...))
/// {
/// // Message to print out when the function is called
/// std::cout << message << std::endl;
/// // Call the function
/// return f(std::forward(xs)...);
/// }
/// };
/// // The logger decorator
/// BOOST_HOF_STATIC_FUNCTION(logger) = boost::hof::decorate(logger_f());
///
/// struct sum_f
/// {
/// template
/// T operator()(T x, U y) const
/// {
/// return x+y;
/// }
/// };
///
/// BOOST_HOF_STATIC_FUNCTION(sum) = sum_f();
/// int main() {
/// // Use the logger decorator to print "Calling sum" when the function is called
/// assert(3 == logger("Calling sum")(sum)(1, 2));
/// }
///
#include
#include
#include
#include
#include
#include
#include
namespace boost { namespace hof { namespace detail {
template
struct decorator_invoke
// : compressed_pair, D>
: compressed_pair, F>
{
// typedef compressed_pair base;
typedef compressed_pair, F> base;
BOOST_HOF_INHERIT_CONSTRUCTOR(decorator_invoke, base)
template
constexpr const compressed_pair& get_pair(Ts&&... xs) const noexcept
{
return this->first(xs...);
}
template
constexpr const F& base_function(Ts&&... xs) const noexcept
{
return this->second(xs...);
}
template
constexpr const D& get_decorator(Ts&&... xs) const noexcept
{
return this->get_pair(xs...).first(xs...);
}
template
constexpr const T& get_data(Ts&&... xs) const noexcept
{
return this->get_pair(xs...).second(xs...);
}
BOOST_HOF_RETURNS_CLASS(decorator_invoke);
struct decorator_invoke_failure
{
template
struct apply
{
template
struct of
: Failure::template of
{};
};
};
struct failure
: failure_map
{};
template
constexpr BOOST_HOF_SFINAE_RESULT(const D&, id_, id_, id_...)
operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
(
BOOST_HOF_MANGLE_CAST(const D&)(BOOST_HOF_CONST_THIS->get_decorator(xs...))(
BOOST_HOF_MANGLE_CAST(const T&)(BOOST_HOF_CONST_THIS->get_data(xs...)),
BOOST_HOF_MANGLE_CAST(const F&)(BOOST_HOF_CONST_THIS->base_function(xs...)),
BOOST_HOF_FORWARD(Ts)(xs)...
)
);
};
template
struct decoration
: compressed_pair
{
typedef compressed_pair base;
BOOST_HOF_INHERIT_CONSTRUCTOR(decoration, base)
template
constexpr const D& get_decorator(Ts&&... xs) const noexcept
{
return this->first(xs...);
}
template
constexpr const T& get_data(Ts&&... xs) const noexcept
{
return this->second(xs...);
}
template
constexpr decorator_invoke> operator()(F f) const
BOOST_HOF_NOEXCEPT(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(decorator_invoke>, compressed_pair, detail::callable_base&&))
{
return decorator_invoke>(
*this, static_cast&&>(f)
);
}
};
}
template
struct decorate_adaptor : detail::callable_base
{
typedef decorate_adaptor fit_rewritable1_tag;
typedef detail::callable_base base;
BOOST_HOF_INHERIT_CONSTRUCTOR(decorate_adaptor, detail::callable_base)
template
constexpr const base& base_function(Ts&&... xs) const noexcept
{
return boost::hof::always_ref(*this)(xs...);
}
// TODO: Add predicate for constraints
template
constexpr detail::decoration operator()(T x) const
BOOST_HOF_NOEXCEPT(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(base, const base&) && BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(T, T&&))
{
return detail::decoration(this->base_function(x), static_cast(x));
}
};
BOOST_HOF_DECLARE_STATIC_VAR(decorate, detail::make);
}} // namespace boost::hof
#endif