• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*=============================================================================
2    Copyright (c) 2015 Paul Fultz II
3    decorate.h
4    Distributed under the Boost Software License, Version 1.0. (See accompanying
5    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6==============================================================================*/
7
8#ifndef BOOST_HOF_GUARD_DECORATE_H
9#define BOOST_HOF_GUARD_DECORATE_H
10
11/// decorate
12/// ========
13///
14/// Description
15/// -----------
16///
17/// The `decorate` function adaptor helps create simple function decorators.
18///
19/// A function adaptor takes a function and returns a new functions whereas a
20/// decorator takes some parameters and returns a function adaptor. The
21/// `decorate` function adaptor will return a decorator that returns a
22/// function adaptor. Eventually, it will invoke the function with the user-
23/// provided parameter and function.
24///
25/// Synopsis
26/// --------
27///
28///     template<class F>
29///     constexpr decorate_adaptor<F> decorate(F f);
30///
31/// Semantics
32/// ---------
33///
34///     assert(decorate(f)(x)(g)(xs...) == f(x, g, xs...));
35///
36/// Requirements
37/// ------------
38///
39/// F must be:
40///
41/// * [ConstInvocable](ConstInvocable)
42/// * MoveConstructible
43///
44/// Example
45/// -------
46///
47///     #include <boost/hof.hpp>
48///     #include <cassert>
49///     #include <iostream>
50///     #include <string>
51///     using namespace boost::hof;
52///
53///     struct logger_f
54///     {
55///         template<class F, class... Ts>
56///         auto operator()(const std::string& message, F&& f, Ts&&... xs) const
57///             -> decltype(f(std::forward<Ts>(xs)...))
58///         {
59///             // Message to print out when the function is called
60///             std::cout << message << std::endl;
61///             // Call the function
62///             return f(std::forward<Ts>(xs)...);
63///         }
64///     };
65///     // The logger decorator
66///     BOOST_HOF_STATIC_FUNCTION(logger) = boost::hof::decorate(logger_f());
67///
68///     struct sum_f
69///     {
70///         template<class T, class U>
71///         T operator()(T x, U y) const
72///         {
73///             return x+y;
74///         }
75///     };
76///
77///     BOOST_HOF_STATIC_FUNCTION(sum) = sum_f();
78///     int main() {
79///         // Use the logger decorator to print "Calling sum" when the function is called
80///         assert(3 == logger("Calling sum")(sum)(1, 2));
81///     }
82///
83
84#include <boost/hof/reveal.hpp>
85#include <boost/hof/detail/delegate.hpp>
86#include <boost/hof/detail/move.hpp>
87#include <boost/hof/detail/make.hpp>
88#include <boost/hof/detail/callable_base.hpp>
89#include <boost/hof/detail/static_const_var.hpp>
90#include <boost/hof/detail/compressed_pair.hpp>
91
92namespace boost { namespace hof { namespace detail {
93
94template<class D, class T, class F>
95struct decorator_invoke
96// : compressed_pair<compressed_pair<F, T>, D>
97: compressed_pair<compressed_pair<D, T>, F>
98{
99    // typedef compressed_pair<F, T> base;
100    typedef compressed_pair<compressed_pair<D, T>, F> base;
101
102    BOOST_HOF_INHERIT_CONSTRUCTOR(decorator_invoke, base)
103
104    template<class... Ts>
105    constexpr const compressed_pair<D, T>& get_pair(Ts&&... xs) const noexcept
106    {
107        return this->first(xs...);
108    }
109
110    template<class... Ts>
111    constexpr const F& base_function(Ts&&... xs) const noexcept
112    {
113        return this->second(xs...);
114    }
115
116    template<class... Ts>
117    constexpr const D& get_decorator(Ts&&... xs) const noexcept
118    {
119        return this->get_pair(xs...).first(xs...);
120    }
121
122    template<class... Ts>
123    constexpr const T& get_data(Ts&&... xs) const noexcept
124    {
125        return this->get_pair(xs...).second(xs...);
126    }
127
128    BOOST_HOF_RETURNS_CLASS(decorator_invoke);
129
130    struct decorator_invoke_failure
131    {
132        template<class Failure>
133        struct apply
134        {
135            template<class... Ts>
136            struct of
137            : Failure::template of<const T&, const F&, Ts...>
138            {};
139        };
140    };
141
142    struct failure
143    : failure_map<decorator_invoke_failure, D>
144    {};
145
146    template<class... Ts>
147    constexpr BOOST_HOF_SFINAE_RESULT(const D&, id_<const T&>, id_<const F&>, id_<Ts>...)
148    operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
149    (
150        BOOST_HOF_MANGLE_CAST(const D&)(BOOST_HOF_CONST_THIS->get_decorator(xs...))(
151            BOOST_HOF_MANGLE_CAST(const T&)(BOOST_HOF_CONST_THIS->get_data(xs...)),
152            BOOST_HOF_MANGLE_CAST(const F&)(BOOST_HOF_CONST_THIS->base_function(xs...)),
153            BOOST_HOF_FORWARD(Ts)(xs)...
154        )
155    );
156};
157
158template<class D, class T>
159struct decoration
160: compressed_pair<D, T>
161{
162    typedef compressed_pair<D, T> base;
163    BOOST_HOF_INHERIT_CONSTRUCTOR(decoration, base)
164
165    template<class... Ts>
166    constexpr const D& get_decorator(Ts&&... xs) const noexcept
167    {
168        return this->first(xs...);
169    }
170
171    template<class... Ts>
172    constexpr const T& get_data(Ts&&... xs) const noexcept
173    {
174        return this->second(xs...);
175    }
176
177    template<class F>
178    constexpr decorator_invoke<D, T, detail::callable_base<F>> operator()(F f) const
179    BOOST_HOF_NOEXCEPT(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(decorator_invoke<D, T, detail::callable_base<F>>, compressed_pair<D, T>, detail::callable_base<F>&&))
180    {
181        return decorator_invoke<D, T, detail::callable_base<F>>(
182            *this, static_cast<detail::callable_base<F>&&>(f)
183        );
184    }
185};
186
187}
188
189template<class F>
190struct decorate_adaptor : detail::callable_base<F>
191{
192    typedef decorate_adaptor fit_rewritable1_tag;
193    typedef detail::callable_base<F> base;
194    BOOST_HOF_INHERIT_CONSTRUCTOR(decorate_adaptor, detail::callable_base<F>)
195
196    template<class... Ts>
197    constexpr const base& base_function(Ts&&... xs) const noexcept
198    {
199        return boost::hof::always_ref(*this)(xs...);
200    }
201
202    // TODO: Add predicate for constraints
203
204    template<class T>
205    constexpr detail::decoration<base, T> operator()(T x) const
206    BOOST_HOF_NOEXCEPT(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(base, const base&) && BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(T, T&&))
207    {
208        return detail::decoration<base, T>(this->base_function(x), static_cast<T&&>(x));
209    }
210
211};
212
213BOOST_HOF_DECLARE_STATIC_VAR(decorate, detail::make<decorate_adaptor>);
214
215}} // namespace boost::hof
216
217#endif
218