Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

5 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2013-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_CONTAINER_ACTION_HPP
  14. #define RANGES_V3_CONTAINER_ACTION_HPP
  15. #include <type_traits>
  16. #include <meta/meta.hpp>
  17. #include <range/v3/range_fwd.hpp>
  18. #include <range/v3/action/concepts.hpp>
  19. #include <range/v3/functional/arithmetic.hpp>
  20. #include <range/v3/functional/concepts.hpp>
  21. #include <range/v3/functional/invoke.hpp>
  22. #include <range/v3/range/concepts.hpp>
  23. #include <range/v3/utility/static_const.hpp>
  24. #include <range/v3/view/ref.hpp>
  25. namespace ranges
  26. {
  27. /// \addtogroup group-actions
  28. /// @{
  29. namespace actions
  30. {
  31. struct action_access
  32. {
  33. template<typename Action>
  34. struct impl
  35. {
  36. // clang-format off
  37. template<typename... Ts, typename A = Action>
  38. static constexpr auto CPP_auto_fun(bind)(Ts &&... ts)
  39. (
  40. return A::bind(static_cast<Ts &&>(ts)...)
  41. )
  42. // clang-format on
  43. };
  44. };
  45. struct make_action_fn
  46. {
  47. template<typename Fun>
  48. constexpr action<Fun> operator()(Fun fun) const
  49. {
  50. return action<Fun>{detail::move(fun)};
  51. }
  52. };
  53. /// \ingroup group-actions
  54. /// \relates make_action_fn
  55. RANGES_INLINE_VARIABLE(make_action_fn, make_action)
  56. template<typename Action>
  57. struct action : pipeable_base
  58. {
  59. private:
  60. Action action_;
  61. friend pipeable_access;
  62. // Piping requires things are passed by value.
  63. template<typename Rng, typename Act>
  64. static auto pipe(Rng && rng, Act && act)
  65. -> CPP_ret(invoke_result_t<Action &, Rng>)( //
  66. requires range<Rng> && invocable<Action &, Rng> &&
  67. (!std::is_reference<Rng>::value))
  68. {
  69. return invoke(act.action_, detail::move(rng));
  70. }
  71. public:
  72. action() = default;
  73. constexpr explicit action(Action a) noexcept(
  74. std::is_nothrow_move_constructible<Action>::value)
  75. : action_(detail::move(a))
  76. {}
  77. // Calling directly requires things are passed by reference.
  78. template<typename Rng, typename... Rest>
  79. auto operator()(Rng & rng, Rest &&... rest) const
  80. -> CPP_ret(invoke_result_t<Action const &, Rng &, Rest...>)( //
  81. requires range<Rng> && invocable<Action const &, Rng &, Rest...>)
  82. {
  83. return invoke(action_, rng, static_cast<Rest &&>(rest)...);
  84. }
  85. // Currying overload.
  86. // clang-format off
  87. template<typename T, typename... Rest, typename A = Action>
  88. auto CPP_auto_fun(operator())(T &&t, Rest &&... rest)(const)
  89. (
  90. return make_action(
  91. action_access::impl<A>::bind(action_,
  92. static_cast<T &&>(t),
  93. static_cast<Rest &&>(rest)...))
  94. )
  95. // clang-format on
  96. };
  97. template<typename Rng, typename Action>
  98. auto operator|=(Rng & rng, Action && action) -> CPP_ret(Rng &)( //
  99. requires is_pipeable<Action>::value && range<Rng &> &&
  100. invocable<bitwise_or, ref_view<Rng>, Action &> && same_as<
  101. ref_view<Rng>, invoke_result_t<bitwise_or, ref_view<Rng>, Action &>>)
  102. {
  103. views::ref(rng) | action;
  104. return rng;
  105. }
  106. } // namespace actions
  107. /// @}
  108. } // namespace ranges
  109. #endif