infix.hpp 5.9 KB

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