rend.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <__type_traits/decay.h>
  20. #include <__type_traits/is_reference.h>
  21. #include <__type_traits/remove_cvref.h>
  22. #include <__type_traits/remove_reference.h>
  23. #include <__utility/auto_cast.h>
  24. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  25. # pragma GCC system_header
  26. #endif
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. #if _LIBCPP_STD_VER >= 20
  29. // [range.access.rend]
  30. namespace ranges {
  31. namespace __rend {
  32. template <class _Tp>
  33. concept __member_rend = __can_borrow<_Tp> && requires(_Tp&& __t) {
  34. ranges::rbegin(__t);
  35. { _LIBCPP_AUTO_CAST(__t.rend()) } -> sentinel_for<decltype(ranges::rbegin(__t))>;
  36. };
  37. void rend() = delete;
  38. template <class _Tp>
  39. concept __unqualified_rend =
  40. !__member_rend<_Tp> && __can_borrow<_Tp> && __class_or_enum<remove_cvref_t<_Tp>> && requires(_Tp&& __t) {
  41. ranges::rbegin(__t);
  42. { _LIBCPP_AUTO_CAST(rend(__t)) } -> sentinel_for<decltype(ranges::rbegin(__t))>;
  43. };
  44. template <class _Tp>
  45. concept __can_reverse = __can_borrow<_Tp> && !__member_rend<_Tp> && !__unqualified_rend<_Tp> && requires(_Tp&& __t) {
  46. { ranges::begin(__t) } -> same_as<decltype(ranges::end(__t))>;
  47. { ranges::begin(__t) } -> bidirectional_iterator;
  48. };
  49. class __fn {
  50. public:
  51. template <class _Tp>
  52. requires __member_rend<_Tp>
  53. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
  54. noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.rend()))) {
  55. return _LIBCPP_AUTO_CAST(__t.rend());
  56. }
  57. template <class _Tp>
  58. requires __unqualified_rend<_Tp>
  59. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
  60. noexcept(noexcept(_LIBCPP_AUTO_CAST(rend(__t)))) {
  61. return _LIBCPP_AUTO_CAST(rend(__t));
  62. }
  63. template <class _Tp>
  64. requires __can_reverse<_Tp>
  65. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
  66. noexcept(noexcept(ranges::begin(__t))) {
  67. return std::make_reverse_iterator(ranges::begin(__t));
  68. }
  69. void operator()(auto&&) const = delete;
  70. };
  71. } // namespace __rend
  72. inline namespace __cpo {
  73. inline constexpr auto rend = __rend::__fn{};
  74. } // namespace __cpo
  75. } // namespace ranges
  76. // [range.access.crend]
  77. namespace ranges {
  78. namespace __crend {
  79. struct __fn {
  80. template <class _Tp>
  81. requires is_lvalue_reference_v<_Tp&&>
  82. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
  83. noexcept(noexcept(ranges::rend(static_cast<const remove_reference_t<_Tp>&>(__t))))
  84. -> decltype(ranges::rend(static_cast<const remove_reference_t<_Tp>&>(__t))) {
  85. return ranges::rend(static_cast<const remove_reference_t<_Tp>&>(__t));
  86. }
  87. template <class _Tp>
  88. requires is_rvalue_reference_v<_Tp&&>
  89. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
  90. noexcept(noexcept(ranges::rend(static_cast<const _Tp&&>(__t))))
  91. -> decltype(ranges::rend(static_cast<const _Tp&&>(__t))) {
  92. return ranges::rend(static_cast<const _Tp&&>(__t));
  93. }
  94. };
  95. } // namespace __crend
  96. inline namespace __cpo {
  97. inline constexpr auto crend = __crend::__fn{};
  98. } // namespace __cpo
  99. } // namespace ranges
  100. #endif // _LIBCPP_STD_VER >= 20
  101. _LIBCPP_END_NAMESPACE_STD
  102. #endif // _LIBCPP___RANGES_REND_H