rbegin.h 3.7 KB

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