rend.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___RANGES_REND_H
  10. #define _LIBCPP___RANGES_REND_H
  11. #include <__concepts/class_or_enum.h>
  12. #include <__concepts/same_as.h>
  13. #include <__config>
  14. #include <__iterator/concepts.h>
  15. #include <__iterator/readable_traits.h>
  16. #include <__iterator/reverse_iterator.h>
  17. #include <__ranges/access.h>
  18. #include <__ranges/rbegin.h>
  19. #include <__utility/auto_cast.h>
  20. #include <type_traits>
  21. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  22. # pragma GCC system_header
  23. #endif
  24. _LIBCPP_BEGIN_NAMESPACE_STD
  25. #if _LIBCPP_STD_VER > 17
  26. // [range.access.rend]
  27. namespace ranges {
  28. namespace __rend {
  29. template <class _Tp>
  30. concept __member_rend =
  31. __can_borrow<_Tp> &&
  32. __workaround_52970<_Tp> &&
  33. requires(_Tp&& __t) {
  34. ranges::rbegin(__t);
  35. { _LIBCPP_AUTO_CAST(__t.rend()) } -> sentinel_for<decltype(ranges::rbegin(__t))>;
  36. };
  37. void rend(auto&) = delete;
  38. void rend(const auto&) = delete;
  39. template <class _Tp>
  40. concept __unqualified_rend =
  41. !__member_rend<_Tp> &&
  42. __can_borrow<_Tp> &&
  43. __class_or_enum<remove_cvref_t<_Tp>> &&
  44. requires(_Tp&& __t) {
  45. ranges::rbegin(__t);
  46. { _LIBCPP_AUTO_CAST(rend(__t)) } -> sentinel_for<decltype(ranges::rbegin(__t))>;
  47. };
  48. template <class _Tp>
  49. concept __can_reverse =
  50. __can_borrow<_Tp> &&
  51. !__member_rend<_Tp> &&
  52. !__unqualified_rend<_Tp> &&
  53. requires(_Tp&& __t) {
  54. { ranges::begin(__t) } -> same_as<decltype(ranges::end(__t))>;
  55. { ranges::begin(__t) } -> bidirectional_iterator;
  56. };
  57. class __fn {
  58. public:
  59. template <class _Tp>
  60. requires __member_rend<_Tp>
  61. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
  62. noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.rend())))
  63. {
  64. return _LIBCPP_AUTO_CAST(__t.rend());
  65. }
  66. template <class _Tp>
  67. requires __unqualified_rend<_Tp>
  68. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
  69. noexcept(noexcept(_LIBCPP_AUTO_CAST(rend(__t))))
  70. {
  71. return _LIBCPP_AUTO_CAST(rend(__t));
  72. }
  73. template <class _Tp>
  74. requires __can_reverse<_Tp>
  75. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
  76. noexcept(noexcept(ranges::begin(__t)))
  77. {
  78. return std::make_reverse_iterator(ranges::begin(__t));
  79. }
  80. void operator()(auto&&) const = delete;
  81. };
  82. } // namespace __rend
  83. inline namespace __cpo {
  84. inline constexpr auto rend = __rend::__fn{};
  85. } // namespace __cpo
  86. } // namespace ranges
  87. // [range.access.crend]
  88. namespace ranges {
  89. namespace __crend {
  90. struct __fn {
  91. template <class _Tp>
  92. requires is_lvalue_reference_v<_Tp&&>
  93. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  94. constexpr auto operator()(_Tp&& __t) const
  95. noexcept(noexcept(ranges::rend(static_cast<const remove_reference_t<_Tp>&>(__t))))
  96. -> decltype( ranges::rend(static_cast<const remove_reference_t<_Tp>&>(__t)))
  97. { return ranges::rend(static_cast<const remove_reference_t<_Tp>&>(__t)); }
  98. template <class _Tp>
  99. requires is_rvalue_reference_v<_Tp&&>
  100. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  101. constexpr auto operator()(_Tp&& __t) const
  102. noexcept(noexcept(ranges::rend(static_cast<const _Tp&&>(__t))))
  103. -> decltype( ranges::rend(static_cast<const _Tp&&>(__t)))
  104. { return ranges::rend(static_cast<const _Tp&&>(__t)); }
  105. };
  106. } // namespace __crend
  107. inline namespace __cpo {
  108. inline constexpr auto crend = __crend::__fn{};
  109. } // namespace __cpo
  110. } // namespace ranges
  111. #endif // _LIBCPP_STD_VER > 17
  112. _LIBCPP_END_NAMESPACE_STD
  113. #endif // _LIBCPP___RANGES_REND_H