Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /*=============================================================================
0002     Copyright (c) 2014 Paul Fultz II
0003     compose.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_COMPOSE_H
0009 #define BOOST_HOF_GUARD_FUNCTION_COMPOSE_H
0010 
0011 /// compose
0012 /// =======
0013 /// 
0014 /// Description
0015 /// -----------
0016 /// 
0017 /// The `compose` function adaptor provides function composition. It produces
0018 /// a function object that composes a set of functions, ie the output of one
0019 /// function becomes the input of the second function. So, `compose(f, g)(0)`
0020 /// is equivalent to `f(g(0))`.
0021 /// 
0022 /// 
0023 /// Synopsis
0024 /// --------
0025 /// 
0026 ///     template<class... Fs>
0027 ///     constexpr compose_adaptor<Fs...> compose(Fs... fs);
0028 /// 
0029 /// Semantics
0030 /// ---------
0031 /// 
0032 ///     assert(compose(f, g)(xs...) == f(g(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 = compose(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 
0079 #include <boost/hof/detail/callable_base.hpp>
0080 #include <boost/hof/always.hpp>
0081 #include <boost/hof/detail/delegate.hpp>
0082 #include <boost/hof/detail/compressed_pair.hpp>
0083 #include <boost/hof/detail/join.hpp>
0084 #include <tuple>
0085 #include <boost/hof/detail/move.hpp>
0086 #include <boost/hof/detail/make.hpp>
0087 #include <boost/hof/detail/result_type.hpp>
0088 #include <boost/hof/detail/static_const_var.hpp>
0089 
0090 namespace boost { namespace hof { namespace detail {
0091 
0092 template<class F1, class F2>
0093 struct compose_kernel : detail::compressed_pair<F1, F2>, compose_function_result_type<F1, F2>
0094 {
0095     typedef detail::compressed_pair<F1, F2> base_type;
0096 
0097     BOOST_HOF_INHERIT_CONSTRUCTOR(compose_kernel, base_type)
0098 
0099     BOOST_HOF_RETURNS_CLASS(compose_kernel);
0100 
0101     template<class... Ts>
0102     constexpr BOOST_HOF_SFINAE_RESULT(const F1&, result_of<const F2&, id_<Ts>...>) 
0103     operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
0104     (
0105         BOOST_HOF_MANGLE_CAST(const F1&)(BOOST_HOF_CONST_THIS->first(xs...))(
0106             BOOST_HOF_MANGLE_CAST(const F2&)(BOOST_HOF_CONST_THIS->second(xs...))(BOOST_HOF_FORWARD(Ts)(xs)...)
0107         )
0108     );
0109 };
0110 }
0111 
0112 template<class F, class... Fs>
0113 struct compose_adaptor 
0114 : detail::compose_kernel<detail::callable_base<F>, BOOST_HOF_JOIN(compose_adaptor, detail::callable_base<Fs>...)>
0115 {
0116     typedef compose_adaptor fit_rewritable_tag;
0117     typedef BOOST_HOF_JOIN(compose_adaptor, detail::callable_base<Fs>...) tail;
0118     typedef detail::compose_kernel<detail::callable_base<F>, tail> base_type;
0119 
0120     BOOST_HOF_INHERIT_DEFAULT(compose_adaptor, base_type)
0121 
0122     template<class X, class... Xs, 
0123         BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(detail::callable_base<F>, X), 
0124         BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(tail, Xs...)
0125     >
0126     constexpr compose_adaptor(X&& f1, Xs&& ... fs)
0127     BOOST_HOF_NOEXCEPT(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(base_type, X&&, tail) && BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(tail, Xs&&...))
0128     : base_type(BOOST_HOF_FORWARD(X)(f1), tail(BOOST_HOF_FORWARD(Xs)(fs)...))
0129     {}
0130 
0131     template<class X,
0132         BOOST_HOF_ENABLE_IF_CONSTRUCTIBLE(detail::callable_base<F>, X)
0133     >
0134     constexpr compose_adaptor(X&& f1) 
0135     BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(base_type, X&&)
0136     : base_type(BOOST_HOF_FORWARD(X)(f1))
0137     {}
0138 };
0139 
0140 template<class F>
0141 struct compose_adaptor<F> : detail::callable_base<F>
0142 {
0143     typedef compose_adaptor fit_rewritable_tag;
0144 
0145     BOOST_HOF_INHERIT_DEFAULT(compose_adaptor, detail::callable_base<F>)
0146 
0147     template<class X, BOOST_HOF_ENABLE_IF_CONVERTIBLE(X, detail::callable_base<F>)>
0148     constexpr compose_adaptor(X&& f1) 
0149     BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(detail::callable_base<F>, X&&)
0150     : detail::callable_base<F>(BOOST_HOF_FORWARD(X)(f1))
0151     {}
0152 
0153 };
0154 
0155 template<class F1, class F2>
0156 struct compose_adaptor<F1, F2>
0157 : detail::compose_kernel<detail::callable_base<F1>, detail::callable_base<F2>>
0158 {
0159     typedef compose_adaptor fit_rewritable_tag;
0160     typedef detail::compose_kernel<detail::callable_base<F1>, detail::callable_base<F2>> base_type;
0161 
0162     BOOST_HOF_INHERIT_CONSTRUCTOR(compose_adaptor, base_type)
0163 };
0164 
0165 BOOST_HOF_DECLARE_STATIC_VAR(compose, detail::make<compose_adaptor>);
0166 
0167 }} // namespace boost::hof
0168 
0169 #endif