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     infix.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_INFIX_H
0009 #define BOOST_HOF_GUARD_FUNCTION_INFIX_H
0010 
0011 /// infix
0012 /// =====
0013 /// 
0014 /// Description
0015 /// -----------
0016 /// 
0017 /// The `infix` function adaptor allows the function to be used as an infix
0018 /// operator. The operator must be placed inside the angle brackets(ie `<`
0019 /// and `>`).
0020 /// 
0021 /// Synopsis
0022 /// --------
0023 /// 
0024 ///     template<class F>
0025 ///     constexpr infix_adaptor<F> infix(F f);
0026 /// 
0027 /// Semantics
0028 /// ---------
0029 /// 
0030 ///     assert(x <infix(f)> y == f(x, y));
0031 /// 
0032 /// Requirements
0033 /// ------------
0034 /// 
0035 /// F must be:
0036 /// 
0037 /// * [BinaryInvocable](BinaryInvocable)
0038 /// * MoveConstructible
0039 /// 
0040 /// Operator precedence
0041 /// -------------------
0042 /// 
0043 /// Infix operators have the precedence of relational operators. This means
0044 /// operators such as `+` or `*` have higher precedence:
0045 /// 
0046 ///     assert((x + y <infix(f)> z) == ((x + y) <infix(f)> z));
0047 ///     assert((x * y <infix(f)> z) == ((x * y) <infix(f)> z));
0048 /// 
0049 /// However, operators such as `|` or `==` have lower precedence::
0050 /// 
0051 ///     assert((x | y <infix(f)> z) == (x | (y <infix(f)> z)));
0052 ///     assert((x == y <infix(f)> z) == (x == (y <infix(f)> z)));
0053 /// 
0054 /// Also, infix operators have left-to-right associativity:
0055 /// 
0056 ///     assert(x <infix(f)> y <infix(g)> z == ((x <infix(f)> y) <infix(g)> z));
0057 /// 
0058 /// Example
0059 /// -------
0060 /// 
0061 ///     #include <boost/hof.hpp>
0062 ///     #include <cassert>
0063 ///     using namespace boost::hof;
0064 /// 
0065 ///     struct plus_f
0066 ///     {
0067 ///         template<class T, class U>
0068 ///         T operator()(T x, U y) const
0069 ///         {
0070 ///             return x+y;
0071 ///         }
0072 ///     };
0073 ///     
0074 ///     int main() {
0075 ///         constexpr infix_adaptor<plus_f> plus = {};
0076 ///         int r = 3 <plus> 2;
0077 ///         assert(r == 5);
0078 ///     }
0079 /// 
0080 
0081 #include <boost/hof/detail/delegate.hpp>
0082 #include <boost/hof/detail/callable_base.hpp>
0083 #include <boost/hof/always.hpp>
0084 #include <boost/hof/reveal.hpp>
0085 #include <boost/hof/detail/move.hpp>
0086 #include <boost/hof/detail/make.hpp>
0087 #include <boost/hof/detail/static_const_var.hpp>
0088 
0089 namespace boost { namespace hof {
0090  
0091 namespace detail{
0092 template<class T, class F>
0093 struct postfix_adaptor : F
0094 {
0095     T x;
0096 
0097     template<class X, class XF>
0098     constexpr postfix_adaptor(X&& xp, XF&& fp)
0099     BOOST_HOF_NOEXCEPT(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(F, XF&&) && BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(T, X&&)) 
0100     : F(BOOST_HOF_FORWARD(XF)(fp)), x(BOOST_HOF_FORWARD(X)(xp))
0101     {}
0102 
0103     template<class... Ts>
0104     constexpr const F& base_function(Ts&&... xs) const noexcept
0105     {
0106         return boost::hof::always_ref(*this)(xs...);
0107     }
0108 
0109     BOOST_HOF_RETURNS_CLASS(postfix_adaptor);
0110 
0111     template<class... Ts>
0112     constexpr BOOST_HOF_SFINAE_RESULT(const F&, id_<T&&>, id_<Ts>...)
0113     operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
0114     (
0115         (BOOST_HOF_MANGLE_CAST(const F&)(BOOST_HOF_CONST_THIS->base_function(xs...)))(BOOST_HOF_RETURNS_C_CAST(T&&)(BOOST_HOF_CONST_THIS->x), BOOST_HOF_FORWARD(Ts)(xs)...)
0116     );
0117 
0118     template<class A>
0119     constexpr BOOST_HOF_SFINAE_RESULT(const F&, id_<T&&>, id_<A>)
0120     operator>(A&& a) const BOOST_HOF_SFINAE_RETURNS
0121     (
0122         (BOOST_HOF_MANGLE_CAST(const F&)(BOOST_HOF_CONST_THIS->base_function(a)))(BOOST_HOF_RETURNS_C_CAST(T&&)(BOOST_HOF_CONST_THIS->x), BOOST_HOF_FORWARD(A)(a))
0123     );
0124 };
0125 
0126 template<class T, class F>
0127 constexpr postfix_adaptor<T, F> make_postfix_adaptor(T&& x, F f)
0128 BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(postfix_adaptor<T, F>, T&&, F&&)
0129 {
0130     return postfix_adaptor<T, F>(BOOST_HOF_FORWARD(T)(x), static_cast<F&&>(f));
0131 }
0132 }
0133 
0134 template<class F>
0135 struct infix_adaptor : detail::callable_base<F>
0136 {
0137     typedef infix_adaptor fit_rewritable1_tag;
0138     BOOST_HOF_INHERIT_CONSTRUCTOR(infix_adaptor, detail::callable_base<F>);
0139 
0140     template<class... Ts>
0141     constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const noexcept
0142     {
0143         return boost::hof::always_ref(*this)(xs...);
0144     }
0145 
0146     template<class... Ts>
0147     constexpr const detail::callable_base<F>& infix_base_function(Ts&&... xs) const noexcept
0148     {
0149         return boost::hof::always_ref(*this)(xs...);
0150     }
0151 
0152     BOOST_HOF_RETURNS_CLASS(infix_adaptor);
0153 
0154     template<class... Ts>
0155     constexpr auto operator()(Ts&&... xs) const BOOST_HOF_RETURNS
0156     (
0157         (BOOST_HOF_MANGLE_CAST(const detail::callable_base<F>&)(BOOST_HOF_CONST_THIS->base_function(xs...)))(BOOST_HOF_FORWARD(Ts)(xs)...)
0158     );
0159 };
0160 
0161 template<class T, class F>
0162 constexpr auto operator<(T&& x, const infix_adaptor<F>& i) BOOST_HOF_RETURNS
0163 (detail::make_postfix_adaptor(BOOST_HOF_FORWARD(T)(x), boost::hof::move(i.base_function(x))));
0164 
0165 // TODO: Operators for static_
0166 
0167 namespace detail {
0168 
0169 template<class F>
0170 struct static_function_wrapper;
0171 
0172 // Operators for static_function_wrapper adaptor
0173 template<class T, class F>
0174 auto operator<(T&& x, const boost::hof::detail::static_function_wrapper<F>& f) BOOST_HOF_RETURNS
0175 (
0176     detail::make_postfix_adaptor(BOOST_HOF_FORWARD(T)(x), boost::hof::move(f.base_function().infix_base_function()))
0177 );
0178 
0179 template<class F>
0180 struct static_default_function;
0181 
0182 // Operators for static_default_function adaptor
0183 template<class T, class F>
0184 auto operator<(T&& x, const boost::hof::detail::static_default_function<F>&) BOOST_HOF_RETURNS
0185 (
0186     detail::make_postfix_adaptor(BOOST_HOF_FORWARD(T)(x), boost::hof::move(F().infix_base_function()))
0187 );
0188 }
0189 // This overload is needed for gcc
0190 template<class T, class F>
0191 constexpr auto operator<(T&& x, const boost::hof::reveal_adaptor<F>& f) BOOST_HOF_RETURNS
0192 (
0193     detail::make_postfix_adaptor(BOOST_HOF_FORWARD(T)(x), f.infix_base_function())
0194 );
0195 
0196 BOOST_HOF_DECLARE_STATIC_VAR(infix, detail::make<infix_adaptor>);
0197 
0198 }} // namespace boost::hof
0199 
0200 #endif