partial.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*=============================================================================
  2. Copyright (c) 2012 Paul Fultz II
  3. partial.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_PARTIAL_H
  8. #define BOOST_HOF_GUARD_FUNCTION_PARTIAL_H
  9. /// partial
  10. /// ========
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `partial` function adaptor allows partial application of the function.
  16. /// If the function can not be called with all the parameters, it will return
  17. /// another function. It will repeatedly do this until the function can
  18. /// finally be called. By default, `partial` captures all of its variables by
  19. /// value, just like bind. As such all parameters must be `MoveConstructible`
  20. /// when the function is aprtial application. `std::ref` can be used to
  21. /// capture references instead.
  22. ///
  23. /// Synopsis
  24. /// --------
  25. ///
  26. /// template<class F>
  27. /// constexpr partial_adaptor<F> partial(F f);
  28. ///
  29. /// Semantics
  30. /// ---------
  31. ///
  32. /// assert(partial(f)(xs...)(ys...) == f(xs..., ys...));
  33. ///
  34. /// Requirements
  35. /// ------------
  36. ///
  37. /// F must be:
  38. ///
  39. /// * [ConstInvocable](ConstInvocable)
  40. /// * MoveConstructible
  41. ///
  42. /// Example
  43. /// -------
  44. ///
  45. /// #include <boost/hof.hpp>
  46. /// #include <cassert>
  47. /// using namespace boost::hof;
  48. ///
  49. /// struct sum
  50. /// {
  51. /// template<class T, class U>
  52. /// T operator()(T x, U y) const
  53. /// {
  54. /// return x+y;
  55. /// }
  56. /// };
  57. ///
  58. /// int main() {
  59. /// assert(3 == partial(sum())(1)(2));
  60. /// }
  61. ///
  62. /// References
  63. /// ----------
  64. ///
  65. /// * [Partial application](https://en.wikipedia.org/wiki/Partial_application)
  66. /// * [Currying](https://en.wikipedia.org/wiki/Currying)
  67. ///
  68. #include <boost/hof/first_of.hpp>
  69. #include <boost/hof/static.hpp>
  70. #include <boost/hof/pipable.hpp>
  71. #include <boost/hof/detail/make.hpp>
  72. #include <boost/hof/detail/static_const_var.hpp>
  73. namespace boost { namespace hof {
  74. // TODO: Get rid of sequence parameter
  75. // Forward declare partial_adaptor, since it will be used below
  76. template<class F, class Pack=void >
  77. struct partial_adaptor;
  78. BOOST_HOF_DECLARE_STATIC_VAR(partial, detail::make<partial_adaptor>);
  79. namespace detail {
  80. template<class Derived, class F, class Pack>
  81. struct partial_adaptor_invoke
  82. {
  83. template<class... Ts>
  84. constexpr const F& get_function(Ts&&...) const noexcept
  85. {
  86. return static_cast<const F&>(static_cast<const Derived&>(*this));
  87. }
  88. template<class... Ts>
  89. constexpr const Pack& get_pack(Ts&&...) const noexcept
  90. {
  91. return static_cast<const Pack&>(static_cast<const Derived&>(*this));
  92. }
  93. BOOST_HOF_RETURNS_CLASS(partial_adaptor_invoke);
  94. template<class... Ts>
  95. constexpr BOOST_HOF_SFINAE_RESULT
  96. (
  97. typename result_of<decltype(boost::hof::pack_join),
  98. id_<const Pack&>,
  99. result_of<decltype(boost::hof::pack_forward), id_<Ts>...>
  100. >::type,
  101. id_<F&&>
  102. )
  103. operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
  104. (
  105. boost::hof::pack_join
  106. (
  107. BOOST_HOF_MANGLE_CAST(const Pack&)(BOOST_HOF_CONST_THIS->get_pack(xs...)),
  108. boost::hof::pack_forward(BOOST_HOF_FORWARD(Ts)(xs)...)
  109. )
  110. (BOOST_HOF_RETURNS_C_CAST(F&&)(BOOST_HOF_CONST_THIS->get_function(xs...)))
  111. );
  112. };
  113. #ifdef _MSC_VER
  114. #define BOOST_HOF_PARTIAL_RETURNS(...) -> decltype(__VA_ARGS__) { return (__VA_ARGS__); }
  115. #else
  116. #define BOOST_HOF_PARTIAL_RETURNS BOOST_HOF_SFINAE_RETURNS
  117. #endif
  118. template<class Derived, class F, class Pack>
  119. struct partial_adaptor_join
  120. {
  121. template<class... Ts>
  122. constexpr const F& get_function(Ts&&...) const noexcept
  123. {
  124. return static_cast<const F&>(static_cast<const Derived&>(*this));
  125. }
  126. template<class... Ts>
  127. constexpr const Pack& get_pack(Ts&&...) const noexcept
  128. {
  129. return static_cast<const Pack&>(static_cast<const Derived&>(*this));
  130. }
  131. BOOST_HOF_RETURNS_CLASS(partial_adaptor_join);
  132. template<class... Ts, class=typename std::enable_if<
  133. ((sizeof...(Ts) + Pack::fit_function_param_limit::value) < function_param_limit<F>::value)
  134. >::type>
  135. constexpr auto operator()(Ts&&... xs) const
  136. #ifdef _MSC_VER
  137. // Workaround ICE on MSVC
  138. noexcept(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(F, F&&) && noexcept(boost::hof::pack_join(std::declval<const Pack&>(), boost::hof::pack(BOOST_HOF_FORWARD(Ts)(xs)...))))
  139. #endif
  140. BOOST_HOF_PARTIAL_RETURNS
  141. (
  142. boost::hof::partial
  143. (
  144. BOOST_HOF_RETURNS_C_CAST(F&&)(BOOST_HOF_CONST_THIS->get_function(xs...)),
  145. boost::hof::pack_join(BOOST_HOF_MANGLE_CAST(const Pack&)(BOOST_HOF_CONST_THIS->get_pack(xs...)), boost::hof::pack(BOOST_HOF_FORWARD(Ts)(xs)...))
  146. )
  147. );
  148. };
  149. template<class Derived, class F>
  150. struct partial_adaptor_pack
  151. {
  152. constexpr partial_adaptor_pack() noexcept
  153. {}
  154. template<class... Ts>
  155. constexpr const F& get_function(Ts&&...) const noexcept
  156. {
  157. return static_cast<const F&>(static_cast<const Derived&>(*this));
  158. }
  159. BOOST_HOF_RETURNS_CLASS(partial_adaptor_pack);
  160. template<class... Ts, class=typename std::enable_if<
  161. (sizeof...(Ts) < function_param_limit<F>::value)
  162. >::type>
  163. constexpr auto operator()(Ts&&... xs) const
  164. #ifdef _MSC_VER
  165. // Workaround ICE on MSVC
  166. noexcept(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(F, F&&) && noexcept(boost::hof::pack(BOOST_HOF_FORWARD(Ts)(xs)...)))
  167. #endif
  168. BOOST_HOF_PARTIAL_RETURNS
  169. (
  170. boost::hof::partial
  171. (
  172. BOOST_HOF_RETURNS_C_CAST(F&&)(BOOST_HOF_CONST_THIS->get_function(xs...)),
  173. boost::hof::pack(BOOST_HOF_FORWARD(Ts)(xs)...)
  174. )
  175. );
  176. };
  177. template<class F, class Pack>
  178. struct partial_adaptor_base
  179. {
  180. typedef basic_first_of_adaptor
  181. <
  182. partial_adaptor_invoke<partial_adaptor<F, Pack>, F, Pack>,
  183. partial_adaptor_join<partial_adaptor<F, Pack>, F, Pack>
  184. > type;
  185. };
  186. template<class Derived, class F>
  187. struct partial_adaptor_pack_base
  188. {
  189. typedef basic_first_of_adaptor
  190. <
  191. F,
  192. partial_adaptor_pack<Derived, F>
  193. > type;
  194. };
  195. }
  196. template<class F, class Pack>
  197. struct partial_adaptor : detail::partial_adaptor_base<F, Pack>::type, F, Pack
  198. {
  199. typedef typename detail::partial_adaptor_base<F, Pack>::type base;
  200. typedef partial_adaptor fit_rewritable1_tag;
  201. template<class... Ts>
  202. constexpr const F& base_function(Ts&&...) const noexcept
  203. {
  204. return *this;
  205. }
  206. constexpr const Pack& get_pack() const noexcept
  207. {
  208. return *this;
  209. }
  210. using base::operator();
  211. BOOST_HOF_INHERIT_DEFAULT(partial_adaptor, base, F, Pack);
  212. template<class X, class S>
  213. constexpr partial_adaptor(X&& x, S&& seq)
  214. BOOST_HOF_NOEXCEPT(BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(F, X&&) && BOOST_HOF_IS_NOTHROW_CONSTRUCTIBLE(Pack, S&&))
  215. : F(BOOST_HOF_FORWARD(X)(x)), Pack(BOOST_HOF_FORWARD(S)(seq))
  216. {}
  217. };
  218. template<class F>
  219. struct partial_adaptor<F, void> : detail::partial_adaptor_pack_base<partial_adaptor<F, void>, detail::callable_base<F>>::type
  220. {
  221. typedef typename detail::partial_adaptor_pack_base<partial_adaptor<F, void>, detail::callable_base<F>>::type base;
  222. typedef partial_adaptor fit_rewritable1_tag;
  223. template<class... Ts>
  224. constexpr const detail::callable_base<F>& base_function(Ts&&...) const noexcept
  225. {
  226. return *this;
  227. }
  228. using base::operator();
  229. BOOST_HOF_INHERIT_CONSTRUCTOR(partial_adaptor, base);
  230. };
  231. // Make partial_adaptor work with pipable_adaptor by removing its pipableness
  232. template<class F>
  233. struct partial_adaptor<pipable_adaptor<F>, void>
  234. : partial_adaptor<F, void>
  235. {
  236. typedef partial_adaptor<F, void> base;
  237. typedef partial_adaptor fit_rewritable1_tag;
  238. BOOST_HOF_INHERIT_CONSTRUCTOR(partial_adaptor, base);
  239. };
  240. template<class F>
  241. struct partial_adaptor<static_<pipable_adaptor<F>>, void>
  242. : partial_adaptor<F, void>
  243. {
  244. typedef partial_adaptor<F, void> base;
  245. typedef partial_adaptor fit_rewritable1_tag;
  246. BOOST_HOF_INHERIT_CONSTRUCTOR(partial_adaptor, base);
  247. };
  248. }} // namespace boost::hof
  249. #endif