Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:43:50

0001 /*=============================================================================
0002     Copyright (c) 2015 Paul Fultz II
0003     flow.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_FLOW_H
0009 #define BOOST_HOF_GUARD_FUNCTION_FLOW_H
0010 
0011 /// flow
0012 /// ====
0013 /// 
0014 /// Description
0015 /// -----------
0016 /// 
0017 /// The `flow` function adaptor provides function composition. It is useful as
0018 /// an alternative to using the pipe operator `|` when chaining functions. It is
0019 /// similiar to [`compose`](compose.md) except the evaluation order is
0020 /// reversed. So, `flow(f, g)(0)` is equivalent to `g(f(0))`.
0021 /// 
0022 /// 
0023 /// Synopsis
0024 /// --------
0025 /// 
0026 ///     template<class... Fs>
0027 ///     constexpr flow_adaptor<Fs...> flow(Fs... fs);
0028 /// 
0029 /// Semantics
0030 /// ---------
0031 /// 
0032 ///     assert(flow(f, g)(xs...) == g(f(xs...)));
0033 /// 
0034 /// Requirements
0035 /// ------------
0036 /// 
0037 /// Fs must be:
0038 /// 
0039 /// * [ConstInvocable](ConstInvocable)
0040 /// * MoveConstructible
0041 /// 
0042 /// Example
0043 /// -------
0044 /// 
0045 ///     #include <boost/hof.hpp>
0046 ///     #include <cassert>
0047 ///     using namespace boost::hof;
0048 /// 
0049 ///     struct increment
0050 ///     {
0051 ///         template<class T>
0052 ///         T operator()(T x) const
0053 ///         {
0054 ///             return x + 1;
0055 ///         }
0056 ///     };
0057 /// 
0058 ///     struct decrement
0059 ///     {
0060 ///         template<class T>
0061 ///         T operator()(T x) const
0062 ///         {
0063 ///             return x - 1;
0064 ///         }
0065 ///     };
0066 /// 
0067 ///     int main() {
0068 ///         int r = flow(increment(), decrement(), increment())(3);
0069 ///         assert(r == 4);
0070 ///     }
0071 /// 
0072 /// References
0073 /// ----------
0074 /// 
0075 /// * [Function composition](https://en.wikipedia.org/wiki/Function_composition)
0076 /// 
0077 
0078 #include <boost/hof/detail/callable_base.hpp>
0079 #include <boost/hof/always.hpp>
0080 #include <boost/hof/detail/delegate.hpp>
0081 #include <boost/hof/detail/compressed_pair.hpp>
0082 #include <boost/hof/detail/join.hpp>
0083 #include <tuple>
0084 #include <boost/hof/detail/move.hpp>
0085 #include <boost/hof/detail/make.hpp>
0086 #include <boost/hof/detail/result_type.hpp>
0087 #include <boost/hof/detail/static_const_var.hpp>
0088 
0089 namespace boost { namespace hof { namespace detail {
0090 
0091 template<class F1, class F2>
0092 struct flow_kernel : detail::compressed_pair<detail::callable_base<F1>, detail::callable_base<F2>>, compose_function_result_type<F2, F1>
0093 {
0094     typedef detail::compressed_pair<detail::callable_base<F1>, detail::callable_base<F2>> base_type;
0095 
0096     BOOST_HOF_INHERIT_CONSTRUCTOR(flow_kernel, base_type)
0097 
0098     BOOST_HOF_RETURNS_CLASS(flow_kernel);
0099 
0100     template<class... Ts>
0101     constexpr BOOST_HOF_SFINAE_RESULT(const detail::callable_base<F2>&, result_of<const detail::callable_base<F1>&, id_<Ts>...>) 
0102     operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
0103     (
0104         BOOST_HOF_MANGLE_CAST(const detail::callable_base<F2>&)(BOOST_HOF_CONST_THIS->second(xs...))(
0105             BOOST_HOF_MANGLE_CAST(const detail::callable_base<F1>&)(BOOST_HOF_CONST_THIS->first(xs...))(BOOST_HOF_FORWARD(Ts)(xs)...)
0106         )
0107     );
0108 };
0109 }
0110 
0111 template<class F, class... Fs>
0112 struct flow_adaptor : detail::flow_kernel<F, BOOST_HOF_JOIN(flow_adaptor, Fs...)>
0113 {
0114     typedef flow_adaptor fit_rewritable_tag;
0115     typedef BOOST_HOF_JOIN(flow_adaptor, Fs...) tail;
0116     typedef detail::flow_kernel<F, tail> base_type;
0117 
0118     BOOST_HOF_INHERIT_DEFAULT(flow_adaptor, base_type)
0119 
0120     template<class X, class... Xs, 
0121         BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(detail::callable_base<F>, X), 
0122         BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(tail, Xs...)
0123     >
0124     constexpr flow_adaptor(X&& f1, Xs&& ... fs) 
0125     BOOST_HOF_NOEXCEPT(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(base_type, X&&, tail) && BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(tail, Xs&&...))
0126     : base_type(BOOST_HOF_FORWARD(X)(f1), tail(BOOST_HOF_FORWARD(Xs)(fs)...))
0127     {}
0128 
0129     template<class X,
0130         BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(detail::callable_base<F>, X)
0131     >
0132     constexpr flow_adaptor(X&& f1) 
0133     BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(base_type, X&&)
0134     : base_type(BOOST_HOF_FORWARD(X)(f1))
0135     {}
0136 };
0137 
0138 template<class F>
0139 struct flow_adaptor<F> : detail::callable_base<F>
0140 {
0141     typedef flow_adaptor fit_rewritable_tag;
0142     BOOST_HOF_INHERIT_DEFAULT(flow_adaptor, detail::callable_base<F>)
0143 
0144     template<class X, BOOST_HOF_ENABLE_IF_CONVERTIBLE(X, detail::callable_base<F>)>
0145     constexpr flow_adaptor(X&& f1) 
0146     BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(detail::callable_base<F>, X&&)
0147     : detail::callable_base<F>(BOOST_HOF_FORWARD(X)(f1))
0148     {}
0149 
0150 };
0151 
0152 template<class F1, class F2>
0153 struct flow_adaptor<F1, F2>
0154 : detail::flow_kernel<detail::callable_base<F1>, detail::callable_base<F2>>
0155 {
0156     typedef flow_adaptor fit_rewritable_tag;
0157     typedef detail::flow_kernel<detail::callable_base<F1>, detail::callable_base<F2>> base_type;
0158 
0159     BOOST_HOF_INHERIT_CONSTRUCTOR(flow_adaptor, base_type)
0160 };
0161 
0162 BOOST_HOF_DECLARE_STATIC_VAR(flow, detail::make<flow_adaptor>);
0163 
0164 }} // namespace boost::hof
0165 
0166 #endif