proj.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*=============================================================================
  2. Copyright (c) 2014 Paul Fultz II
  3. proj.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_ON_H
  8. #define BOOST_HOF_GUARD_FUNCTION_ON_H
  9. /// proj
  10. /// ====
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `proj` function adaptor applies a projection onto the parameters of
  16. /// another function. This is useful, for example, to define a function for
  17. /// sorting such that the ordering is based off of the value of one of its
  18. /// member fields.
  19. ///
  20. /// Also, if just a projection is given, then the projection will be called
  21. /// for each of its arguments.
  22. ///
  23. /// Note: All projections are always evaluated in order from left-to-right.
  24. ///
  25. /// Synopsis
  26. /// --------
  27. ///
  28. /// template<class Projection, class F>
  29. /// constexpr proj_adaptor<Projection, F> proj(Projection p, F f);
  30. ///
  31. /// template<class Projection>
  32. /// constexpr proj_adaptor<Projection> proj(Projection p);
  33. ///
  34. /// Semantics
  35. /// ---------
  36. ///
  37. /// assert(proj(p, f)(xs...) == f(p(xs)...));
  38. /// assert(proj(p)(xs...) == p(xs)...);
  39. ///
  40. /// Requirements
  41. /// ------------
  42. ///
  43. /// Projection must be:
  44. ///
  45. /// * [UnaryInvocable](UnaryInvocable)
  46. /// * MoveConstructible
  47. ///
  48. /// F must be:
  49. ///
  50. /// * [ConstInvocable](ConstInvocable)
  51. /// * MoveConstructible
  52. ///
  53. /// Example
  54. /// -------
  55. ///
  56. /// #include <boost/hof.hpp>
  57. /// #include <cassert>
  58. /// using namespace boost::hof;
  59. ///
  60. /// struct foo
  61. /// {
  62. /// foo(int x_) : x(x_)
  63. /// {}
  64. /// int x;
  65. /// };
  66. ///
  67. /// int main() {
  68. /// assert(boost::hof::proj(&foo::x, _ + _)(foo(1), foo(2)) == 3);
  69. /// }
  70. ///
  71. /// References
  72. /// ----------
  73. ///
  74. /// * [Projections](Projections)
  75. /// * [Variadic print](<Variadic print>)
  76. ///
  77. #include <utility>
  78. #include <boost/hof/always.hpp>
  79. #include <boost/hof/detail/callable_base.hpp>
  80. #include <boost/hof/detail/result_of.hpp>
  81. #include <boost/hof/detail/move.hpp>
  82. #include <boost/hof/detail/make.hpp>
  83. #include <boost/hof/detail/static_const_var.hpp>
  84. #include <boost/hof/detail/compressed_pair.hpp>
  85. #include <boost/hof/detail/result_type.hpp>
  86. #include <boost/hof/apply_eval.hpp>
  87. namespace boost { namespace hof {
  88. namespace detail {
  89. template<class T, class Projection>
  90. struct project_eval
  91. {
  92. T&& x;
  93. const Projection& p;
  94. template<class X, class P>
  95. constexpr project_eval(X&& xp, const P& pp) : x(BOOST_HOF_FORWARD(X)(xp)), p(pp)
  96. {}
  97. constexpr auto operator()() const BOOST_HOF_RETURNS
  98. (p(BOOST_HOF_FORWARD(T)(x)));
  99. };
  100. template<class T, class Projection>
  101. constexpr project_eval<T, Projection> make_project_eval(T&& x, const Projection& p)
  102. {
  103. return project_eval<T, Projection>(BOOST_HOF_FORWARD(T)(x), p);
  104. }
  105. template<class T, class Projection>
  106. struct project_void_eval
  107. {
  108. T&& x;
  109. const Projection& p;
  110. template<class X, class P>
  111. constexpr project_void_eval(X&& xp, const P& pp) : x(BOOST_HOF_FORWARD(X)(xp)), p(pp)
  112. {}
  113. struct void_ {};
  114. constexpr void_ operator()() const
  115. {
  116. return p(BOOST_HOF_FORWARD(T)(x)), void_();
  117. }
  118. };
  119. template<class T, class Projection>
  120. constexpr project_void_eval<T, Projection> make_project_void_eval(T&& x, const Projection& p)
  121. {
  122. return project_void_eval<T, Projection>(BOOST_HOF_FORWARD(T)(x), p);
  123. }
  124. template<class Projection, class F, class... Ts,
  125. class R=decltype(
  126. std::declval<const F&>()(std::declval<const Projection&>()(std::declval<Ts>())...)
  127. )>
  128. constexpr R by_eval(const Projection& p, const F& f, Ts&&... xs)
  129. {
  130. return boost::hof::apply_eval(f, make_project_eval(BOOST_HOF_FORWARD(Ts)(xs), p)...);
  131. }
  132. #if BOOST_HOF_NO_ORDERED_BRACE_INIT
  133. #define BOOST_HOF_BY_VOID_RETURN BOOST_HOF_ALWAYS_VOID_RETURN
  134. #else
  135. #if BOOST_HOF_NO_CONSTEXPR_VOID
  136. #define BOOST_HOF_BY_VOID_RETURN boost::hof::detail::swallow
  137. #else
  138. #define BOOST_HOF_BY_VOID_RETURN void
  139. #endif
  140. #endif
  141. template<class Projection, class... Ts>
  142. constexpr BOOST_HOF_ALWAYS_VOID_RETURN by_void_eval(const Projection& p, Ts&&... xs)
  143. {
  144. return boost::hof::apply_eval(boost::hof::always(), boost::hof::detail::make_project_void_eval(BOOST_HOF_FORWARD(Ts)(xs), p)...);
  145. }
  146. struct swallow
  147. {
  148. template<class... Ts>
  149. constexpr swallow(Ts&&...)
  150. {}
  151. };
  152. }
  153. template<class Projection, class F=void>
  154. struct proj_adaptor;
  155. template<class Projection, class F>
  156. struct proj_adaptor : detail::compressed_pair<detail::callable_base<Projection>, detail::callable_base<F>>, detail::function_result_type<F>
  157. {
  158. typedef proj_adaptor fit_rewritable_tag;
  159. typedef detail::compressed_pair<detail::callable_base<Projection>, detail::callable_base<F>> base;
  160. template<class... Ts>
  161. constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const
  162. {
  163. return this->second(xs...);;
  164. }
  165. template<class... Ts>
  166. constexpr const detail::callable_base<Projection>& base_projection(Ts&&... xs) const
  167. {
  168. return this->first(xs...);
  169. }
  170. struct by_failure
  171. {
  172. template<class Failure>
  173. struct apply
  174. {
  175. template<class... Ts>
  176. struct of
  177. : Failure::template of<decltype(std::declval<detail::callable_base<Projection>>()(std::declval<Ts>()))...>
  178. {};
  179. };
  180. };
  181. struct failure
  182. : failure_map<by_failure, detail::callable_base<F>>
  183. {};
  184. BOOST_HOF_INHERIT_CONSTRUCTOR(proj_adaptor, base)
  185. BOOST_HOF_RETURNS_CLASS(proj_adaptor);
  186. template<class... Ts>
  187. constexpr BOOST_HOF_SFINAE_RESULT(const detail::callable_base<F>&, result_of<const detail::callable_base<Projection>&, id_<Ts>>...)
  188. operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
  189. (
  190. boost::hof::detail::by_eval(
  191. BOOST_HOF_MANGLE_CAST(const detail::callable_base<Projection>&)(BOOST_HOF_CONST_THIS->base_projection(xs...)),
  192. BOOST_HOF_MANGLE_CAST(const detail::callable_base<F>&)(BOOST_HOF_CONST_THIS->base_function(xs...)),
  193. BOOST_HOF_FORWARD(Ts)(xs)...
  194. )
  195. );
  196. };
  197. template<class Projection>
  198. struct proj_adaptor<Projection, void> : detail::callable_base<Projection>
  199. {
  200. typedef proj_adaptor fit_rewritable1_tag;
  201. template<class... Ts>
  202. constexpr const detail::callable_base<Projection>& base_projection(Ts&&... xs) const
  203. {
  204. return boost::hof::always_ref(*this)(xs...);
  205. }
  206. BOOST_HOF_INHERIT_DEFAULT(proj_adaptor, detail::callable_base<Projection>)
  207. template<class P, BOOST_HOF_ENABLE_IF_CONVERTIBLE(P, detail::callable_base<Projection>)>
  208. constexpr proj_adaptor(P&& p)
  209. : detail::callable_base<Projection>(BOOST_HOF_FORWARD(P)(p))
  210. {}
  211. BOOST_HOF_RETURNS_CLASS(proj_adaptor);
  212. template<class... Ts, class=detail::holder<decltype(std::declval<Projection>()(std::declval<Ts>()))...>>
  213. constexpr BOOST_HOF_BY_VOID_RETURN operator()(Ts&&... xs) const
  214. {
  215. #if BOOST_HOF_NO_ORDERED_BRACE_INIT
  216. return boost::hof::detail::by_void_eval(this->base_projection(xs...), BOOST_HOF_FORWARD(Ts)(xs)...);
  217. #else
  218. #if BOOST_HOF_NO_CONSTEXPR_VOID
  219. return
  220. #endif
  221. boost::hof::detail::swallow{
  222. (this->base_projection(xs...)(BOOST_HOF_FORWARD(Ts)(xs)), 0)...
  223. };
  224. #endif
  225. }
  226. };
  227. BOOST_HOF_DECLARE_STATIC_VAR(proj, detail::make<proj_adaptor>);
  228. }} // namespace boost::hof
  229. #endif