1 /*=============================================================================
2 Copyright (c) 2014 Paul Fultz II
3 infix.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_FUNCTION_INFIX_H
9 #define BOOST_HOF_GUARD_FUNCTION_INFIX_H
10
11 /// infix
12 /// =====
13 ///
14 /// Description
15 /// -----------
16 ///
17 /// The `infix` function adaptor allows the function to be used as an infix
18 /// operator. The operator must be placed inside the angle brackets(ie `<`
19 /// and `>`).
20 ///
21 /// Synopsis
22 /// --------
23 ///
24 /// template<class F>
25 /// constexpr infix_adaptor<F> infix(F f);
26 ///
27 /// Semantics
28 /// ---------
29 ///
30 /// assert(x <infix(f)> y == f(x, y));
31 ///
32 /// Requirements
33 /// ------------
34 ///
35 /// F must be:
36 ///
37 /// * [BinaryInvocable](BinaryInvocable)
38 /// * MoveConstructible
39 ///
40 /// Operator precedence
41 /// -------------------
42 ///
43 /// Infix operators have the precedence of relational operators. This means
44 /// operators such as `+` or `*` have higher precedence:
45 ///
46 /// assert((x + y <infix(f)> z) == ((x + y) <infix(f)> z));
47 /// assert((x * y <infix(f)> z) == ((x * y) <infix(f)> z));
48 ///
49 /// However, operators such as `|` or `==` have lower precedence::
50 ///
51 /// assert((x | y <infix(f)> z) == (x | (y <infix(f)> z)));
52 /// assert((x == y <infix(f)> z) == (x == (y <infix(f)> z)));
53 ///
54 /// Also, infix operators have left-to-right associativity:
55 ///
56 /// assert(x <infix(f)> y <infix(g)> z == ((x <infix(f)> y) <infix(g)> z));
57 ///
58 /// Example
59 /// -------
60 ///
61 /// #include <boost/hof.hpp>
62 /// #include <cassert>
63 /// using namespace boost::hof;
64 ///
65 /// struct plus_f
66 /// {
67 /// template<class T, class U>
68 /// T operator()(T x, U y) const
69 /// {
70 /// return x+y;
71 /// }
72 /// };
73 ///
74 /// int main() {
75 /// constexpr infix_adaptor<plus_f> plus = {};
76 /// int r = 3 <plus> 2;
77 /// assert(r == 5);
78 /// }
79 ///
80
81 #include <boost/hof/detail/delegate.hpp>
82 #include <boost/hof/detail/callable_base.hpp>
83 #include <boost/hof/always.hpp>
84 #include <boost/hof/reveal.hpp>
85 #include <boost/hof/detail/move.hpp>
86 #include <boost/hof/detail/make.hpp>
87 #include <boost/hof/detail/static_const_var.hpp>
88
89 namespace boost { namespace hof {
90
91 namespace detail{
92 template<class T, class F>
93 struct postfix_adaptor : F
94 {
95 T x;
96
97 template<class X, class XF>
postfix_adaptorboost::hof::detail::postfix_adaptor98 constexpr postfix_adaptor(X&& xp, XF&& fp)
99 BOOST_HOF_NOEXCEPT(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(F, XF&&) && BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(T, X&&))
100 : F(BOOST_HOF_FORWARD(XF)(fp)), x(BOOST_HOF_FORWARD(X)(xp))
101 {}
102
103 template<class... Ts>
base_functionboost::hof::detail::postfix_adaptor104 constexpr const F& base_function(Ts&&... xs) const noexcept
105 {
106 return boost::hof::always_ref(*this)(xs...);
107 }
108
109 BOOST_HOF_RETURNS_CLASS(postfix_adaptor);
110
111 template<class... Ts>
112 constexpr BOOST_HOF_SFINAE_RESULT(const F&, id_<T&&>, id_<Ts>...)
113 operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
114 (
115 (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)...)
116 );
117
118 template<class A>
119 constexpr BOOST_HOF_SFINAE_RESULT(const F&, id_<T&&>, id_<A>)
120 operator>(A&& a) const BOOST_HOF_SFINAE_RETURNS
121 (
122 (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))
123 );
124 };
125
126 template<class T, class F>
make_postfix_adaptor(T && x,F f)127 constexpr postfix_adaptor<T, F> make_postfix_adaptor(T&& x, F f)
128 BOOST_HOF_NOEXCEPT_CONSTRUCTIBLE(postfix_adaptor<T, F>, T&&, F&&)
129 {
130 return postfix_adaptor<T, F>(BOOST_HOF_FORWARD(T)(x), static_cast<F&&>(f));
131 }
132 }
133
134 template<class F>
135 struct infix_adaptor : detail::callable_base<F>
136 {
137 typedef infix_adaptor fit_rewritable1_tag;
138 BOOST_HOF_INHERIT_CONSTRUCTOR(infix_adaptor, detail::callable_base<F>);
139
140 template<class... Ts>
base_functionboost::hof::infix_adaptor141 constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const noexcept
142 {
143 return boost::hof::always_ref(*this)(xs...);
144 }
145
146 template<class... Ts>
infix_base_functionboost::hof::infix_adaptor147 constexpr const detail::callable_base<F>& infix_base_function(Ts&&... xs) const noexcept
148 {
149 return boost::hof::always_ref(*this)(xs...);
150 }
151
152 BOOST_HOF_RETURNS_CLASS(infix_adaptor);
153
154 template<class... Ts>
155 constexpr auto operator()(Ts&&... xs) const BOOST_HOF_RETURNS
156 (
157 (BOOST_HOF_MANGLE_CAST(const detail::callable_base<F>&)(BOOST_HOF_CONST_THIS->base_function(xs...)))(BOOST_HOF_FORWARD(Ts)(xs)...)
158 );
159 };
160
161 template<class T, class F>
162 constexpr auto operator<(T&& x, const infix_adaptor<F>& i) BOOST_HOF_RETURNS
163 (detail::make_postfix_adaptor(BOOST_HOF_FORWARD(T)(x), boost::hof::move(i.base_function(x))));
164
165 // TODO: Operators for static_
166
167 namespace detail {
168
169 template<class F>
170 struct static_function_wrapper;
171
172 // Operators for static_function_wrapper adaptor
173 template<class T, class F>
174 auto operator<(T&& x, const boost::hof::detail::static_function_wrapper<F>& f) BOOST_HOF_RETURNS
175 (
176 detail::make_postfix_adaptor(BOOST_HOF_FORWARD(T)(x), boost::hof::move(f.base_function().infix_base_function()))
177 );
178
179 template<class F>
180 struct static_default_function;
181
182 // Operators for static_default_function adaptor
183 template<class T, class F>
184 auto operator<(T&& x, const boost::hof::detail::static_default_function<F>&) BOOST_HOF_RETURNS
185 (
186 detail::make_postfix_adaptor(BOOST_HOF_FORWARD(T)(x), boost::hof::move(F().infix_base_function()))
187 );
188 }
189 // This overload is needed for gcc
190 template<class T, class F>
191 constexpr auto operator<(T&& x, const boost::hof::reveal_adaptor<F>& f) BOOST_HOF_RETURNS
192 (
193 detail::make_postfix_adaptor(BOOST_HOF_FORWARD(T)(x), f.infix_base_function())
194 );
195
196 BOOST_HOF_DECLARE_STATIC_VAR(infix, detail::make<infix_adaptor>);
197
198 }} // namespace boost::hof
199
200 #endif
201