indirect.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_FUNCTIONAL_INDIRECT_HPP
  14. #define RANGES_V3_FUNCTIONAL_INDIRECT_HPP
  15. #include <utility>
  16. #include <concepts/concepts.hpp>
  17. #include <range/v3/functional/invoke.hpp>
  18. #include <range/v3/iterator/traits.hpp>
  19. #include <range/v3/utility/move.hpp>
  20. #include <range/v3/utility/static_const.hpp>
  21. #include <range/v3/detail/prologue.hpp>
  22. namespace ranges
  23. {
  24. /// \addtogroup group-functional
  25. /// @{
  26. template<typename Fn>
  27. struct indirected
  28. {
  29. private:
  30. RANGES_NO_UNIQUE_ADDRESS
  31. Fn fn_;
  32. public:
  33. indirected() = default;
  34. indirected(Fn fn)
  35. : fn_(std::move(fn))
  36. {}
  37. // value_type (needs no impl)
  38. template<typename... Its>
  39. [[noreturn]] invoke_result_t<Fn &, iter_reference_t<Its>...> //
  40. operator()(copy_tag, Its...) const
  41. {
  42. RANGES_EXPECT(false);
  43. }
  44. // Reference
  45. // clang-format off
  46. template<typename... Its>
  47. auto CPP_auto_fun(operator())(Its... its)
  48. (
  49. return invoke(fn_, *its...)
  50. )
  51. template<typename... Its>
  52. auto CPP_auto_fun(operator())(Its... its)(const)
  53. (
  54. return invoke((Fn const &)fn_, *its...)
  55. )
  56. // Rvalue reference
  57. template<typename... Its>
  58. auto CPP_auto_fun(operator())(move_tag, Its... its)
  59. (
  60. return static_cast<
  61. aux::move_t<invoke_result_t<Fn &, iter_reference_t<Its>...>>>(
  62. aux::move(invoke(fn_, *its...)))
  63. )
  64. template<typename... Its>
  65. auto CPP_auto_fun(operator())(move_tag, Its... its)(const)
  66. (
  67. return static_cast<
  68. aux::move_t<invoke_result_t<Fn const &, iter_reference_t<Its>...>>>(
  69. aux::move(invoke((Fn const &)fn_, *its...)))
  70. )
  71. // clang-format on
  72. };
  73. struct indirect_fn
  74. {
  75. template<typename Fn>
  76. constexpr indirected<Fn> operator()(Fn fn) const
  77. {
  78. return indirected<Fn>{detail::move(fn)};
  79. }
  80. };
  81. /// \ingroup group-functional
  82. /// \sa `indirect_fn`
  83. RANGES_INLINE_VARIABLE(indirect_fn, indirect)
  84. /// @}
  85. } // namespace ranges
  86. #include <range/v3/detail/epilogue.hpp>
  87. #endif