rotate.hpp 2.5 KB

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