pipable.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*=============================================================================
  2. Copyright (c) 2012 Paul Fultz II
  3. pipable.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_PIPABLE_H
  8. #define BOOST_HOF_GUARD_FUNCTION_PIPABLE_H
  9. /// pipable
  10. /// =======
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `pipable` function adaptor provides an extension method. The first
  16. /// parameter of the function can be piped into the function using the pipe
  17. /// `|` operator. This can be especially convenient when there are a lot of
  18. /// nested function calls. Functions that are made pipable can still be called
  19. /// the traditional way without piping in the first parameter.
  20. ///
  21. /// Synopsis
  22. /// --------
  23. ///
  24. /// template<class F>
  25. /// constexpr pipable_adaptor<F> pipable(F f);
  26. ///
  27. /// Semantics
  28. /// ---------
  29. ///
  30. /// assert(x | pipable(f)(ys...) == f(x, ys...));
  31. ///
  32. /// Requirements
  33. /// ------------
  34. ///
  35. /// F must be:
  36. ///
  37. /// * [ConstInvocable](ConstInvocable)
  38. /// * MoveConstructible
  39. ///
  40. /// Example
  41. /// -------
  42. ///
  43. /// #include <boost/hof.hpp>
  44. /// #include <cassert>
  45. /// using namespace boost::hof;
  46. ///
  47. /// struct sum
  48. /// {
  49. /// template<class T, class U>
  50. /// T operator()(T x, U y) const
  51. /// {
  52. /// return x+y;
  53. /// }
  54. /// };
  55. ///
  56. /// int main() {
  57. /// assert(3 == (1 | pipable(sum())(2)));
  58. /// assert(3 == pipable(sum())(1, 2));
  59. /// }
  60. ///
  61. /// References
  62. /// ----------
  63. ///
  64. /// * [Extension methods](<Extension methods>)
  65. ///
  66. #include <boost/hof/first_of.hpp>
  67. #include <boost/hof/pack.hpp>
  68. #include <boost/hof/detail/delegate.hpp>
  69. #include <boost/hof/detail/move.hpp>
  70. #include <boost/hof/detail/make.hpp>
  71. #include <boost/hof/detail/static_const_var.hpp>
  72. #include <boost/hof/limit.hpp>
  73. namespace boost { namespace hof {
  74. template<class F>
  75. struct pipable_adaptor;
  76. namespace detail {
  77. template<class F, class Pack>
  78. struct pipe_closure : F, Pack
  79. {
  80. template<class X, class P>
  81. constexpr pipe_closure(X&& fp, P&& packp)
  82. BOOST_HOF_NOEXCEPT(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(F, X&&) && BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(Pack, P&&))
  83. : F(BOOST_HOF_FORWARD(X)(fp)), Pack(BOOST_HOF_FORWARD(P)(packp))
  84. {}
  85. template<class... Ts>
  86. constexpr const F& base_function(Ts&&...) const noexcept
  87. {
  88. return *this;
  89. }
  90. template<class... Ts>
  91. constexpr const Pack& get_pack(Ts&&...) const noexcept
  92. {
  93. return *this;
  94. }
  95. template<class A>
  96. struct invoke
  97. {
  98. A a;
  99. const pipe_closure * self;
  100. template<class X>
  101. constexpr invoke(X&& xp, const pipe_closure * selfp)
  102. BOOST_HOF_NOEXCEPT(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(A, X&&))
  103. : a(BOOST_HOF_FORWARD(X)(xp)), self(selfp)
  104. {}
  105. BOOST_HOF_RETURNS_CLASS(invoke);
  106. template<class... Ts>
  107. constexpr BOOST_HOF_SFINAE_RESULT(const F&, id_<A>, id_<Ts>...)
  108. operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
  109. (BOOST_HOF_RETURNS_STATIC_CAST(const F&)(*BOOST_HOF_CONST_THIS->self)(BOOST_HOF_FORWARD(A)(a), BOOST_HOF_FORWARD(Ts)(xs)...));
  110. };
  111. BOOST_HOF_RETURNS_CLASS(pipe_closure);
  112. template<class A>
  113. constexpr BOOST_HOF_SFINAE_RESULT(const Pack&, id_<invoke<A&&>>)
  114. operator()(A&& a) const BOOST_HOF_SFINAE_RETURNS
  115. (BOOST_HOF_MANGLE_CAST(const Pack&)(BOOST_HOF_CONST_THIS->get_pack(a))(invoke<A&&>(BOOST_HOF_FORWARD(A)(a), BOOST_HOF_CONST_THIS)));
  116. };
  117. template<class F, class Pack>
  118. constexpr auto make_pipe_closure(F f, Pack&& p) BOOST_HOF_RETURNS
  119. (
  120. pipe_closure<F, typename std::remove_reference<Pack>::type>(BOOST_HOF_RETURNS_STATIC_CAST(F&&)(f), BOOST_HOF_FORWARD(Pack)(p))
  121. );
  122. template<class Derived, class F>
  123. struct pipe_pack
  124. {
  125. template<class... Ts>
  126. constexpr const F& get_function(Ts&&...) const noexcept
  127. {
  128. return static_cast<const F&>(static_cast<const Derived&>(*this));
  129. }
  130. BOOST_HOF_RETURNS_CLASS(pipe_pack);
  131. template<class... Ts, class=typename std::enable_if<
  132. (sizeof...(Ts) < function_param_limit<F>::value)
  133. >::type>
  134. constexpr auto operator()(Ts&&... xs) const BOOST_HOF_RETURNS
  135. (make_pipe_closure(BOOST_HOF_RETURNS_C_CAST(F&&)(BOOST_HOF_CONST_THIS->get_function(xs...)), boost::hof::pack_forward(BOOST_HOF_FORWARD(Ts)(xs)...)));
  136. };
  137. template<class A, class F, class Pack>
  138. constexpr auto operator|(A&& a, const pipe_closure<F, Pack>& p) BOOST_HOF_RETURNS
  139. (p(BOOST_HOF_FORWARD(A)(a)));
  140. }
  141. template<class F>
  142. struct pipable_adaptor
  143. : detail::basic_first_of_adaptor<detail::callable_base<F>, detail::pipe_pack<pipable_adaptor<F>, detail::callable_base<F>> >
  144. {
  145. typedef detail::basic_first_of_adaptor<detail::callable_base<F>, detail::pipe_pack<pipable_adaptor<F>, detail::callable_base<F>> > base;
  146. typedef pipable_adaptor fit_rewritable_tag;
  147. BOOST_HOF_INHERIT_CONSTRUCTOR(pipable_adaptor, base);
  148. constexpr const detail::callable_base<F>& base_function() const noexcept
  149. {
  150. return *this;
  151. }
  152. };
  153. template<class A, class F>
  154. constexpr auto operator|(A&& a, const pipable_adaptor<F>& p) BOOST_HOF_RETURNS
  155. (p(BOOST_HOF_FORWARD(A)(a)));
  156. BOOST_HOF_DECLARE_STATIC_VAR(pipable, detail::make<pipable_adaptor>);
  157. namespace detail {
  158. template<class F>
  159. struct static_function_wrapper;
  160. // Operators for static_function_wrapper adaptor
  161. template<class A, class F>
  162. auto operator|(A&& a, const boost::hof::detail::static_function_wrapper<F>& f) BOOST_HOF_RETURNS
  163. (f(BOOST_HOF_FORWARD(A)(a)));
  164. template<class F>
  165. struct static_default_function;
  166. // Operators for static_default_function adaptor
  167. template<class A, class F>
  168. auto operator|(A&& a, const boost::hof::detail::static_default_function<F>& f) BOOST_HOF_RETURNS
  169. (f(BOOST_HOF_FORWARD(A)(a)));
  170. }
  171. template<class F>
  172. struct static_;
  173. // Operators for static_ adaptor
  174. template<class A, class F>
  175. auto operator|(A&& a, static_<F> f) BOOST_HOF_RETURNS
  176. (f.base_function().base_function()(BOOST_HOF_FORWARD(A)(a)));
  177. }} // namespace boost::hof
  178. #endif