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