flip.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*=============================================================================
  2. Copyright (c) 2015 Paul Fultz II
  3. flip.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_FLIP_H
  8. #define BOOST_HOF_GUARD_FLIP_H
  9. /// flip
  10. /// ====
  11. ///
  12. /// Description
  13. /// -----------
  14. ///
  15. /// The `flip` function adaptor swaps the first two parameters.
  16. ///
  17. /// Synopsis
  18. /// --------
  19. ///
  20. /// template<class F>
  21. /// flip_adaptor<F> flip(F f);
  22. ///
  23. /// Semantics
  24. /// ---------
  25. ///
  26. /// assert(flip(f)(x, y, xs...) == f(y, x, xs...));
  27. ///
  28. /// Requirements
  29. /// ------------
  30. ///
  31. /// F must be at least:
  32. ///
  33. /// * [BinaryInvocable](BinaryInvocable)
  34. ///
  35. /// Or:
  36. ///
  37. /// * [Invocable](Invocable) with more than two argurments
  38. ///
  39. /// And:
  40. ///
  41. /// * MoveConstructible
  42. ///
  43. /// Example
  44. /// -------
  45. ///
  46. /// #include <boost/hof.hpp>
  47. /// #include <cassert>
  48. ///
  49. /// int main() {
  50. /// int r = boost::hof::flip(boost::hof::_ - boost::hof::_)(2, 5);
  51. /// assert(r == 3);
  52. /// }
  53. ///
  54. #include <boost/hof/detail/callable_base.hpp>
  55. #include <boost/hof/reveal.hpp>
  56. #include <boost/hof/detail/make.hpp>
  57. #include <boost/hof/detail/static_const_var.hpp>
  58. namespace boost { namespace hof {
  59. template<class F>
  60. struct flip_adaptor : detail::callable_base<F>
  61. {
  62. typedef flip_adaptor fit_rewritable1_tag;
  63. BOOST_HOF_INHERIT_CONSTRUCTOR(flip_adaptor, detail::callable_base<F>);
  64. template<class... Ts>
  65. constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const
  66. {
  67. return boost::hof::always_ref(*this)(xs...);
  68. }
  69. struct flip_failure
  70. {
  71. template<class Failure>
  72. struct apply
  73. {
  74. template<class T, class U, class... Ts>
  75. struct of
  76. : Failure::template of<U, T, Ts...>
  77. {};
  78. };
  79. };
  80. struct failure
  81. : failure_map<flip_failure, detail::callable_base<F>>
  82. {};
  83. BOOST_HOF_RETURNS_CLASS(flip_adaptor);
  84. template<class T, class U, class... Ts>
  85. constexpr BOOST_HOF_SFINAE_RESULT(const detail::callable_base<F>&, id_<U>, id_<T>, id_<Ts>...)
  86. operator()(T&& x, U&& y, Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
  87. (
  88. (BOOST_HOF_MANGLE_CAST(const detail::callable_base<F>&)(BOOST_HOF_CONST_THIS->base_function(xs...)))
  89. (BOOST_HOF_FORWARD(U)(y), BOOST_HOF_FORWARD(T)(x), BOOST_HOF_FORWARD(Ts)(xs)...)
  90. );
  91. };
  92. BOOST_HOF_DECLARE_STATIC_VAR(flip, detail::make<flip_adaptor>);
  93. }} // namespace boost::hof
  94. #endif