xref: /freebsd/contrib/llvm-project/libcxx/include/__ranges/rend.h (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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> && __workaround_52970<_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(auto&)       = delete;
46 void rend(const auto&) = delete;
47 
48 template <class _Tp>
49 concept __unqualified_rend =
50     !__member_rend<_Tp> && __can_borrow<_Tp> && __class_or_enum<remove_cvref_t<_Tp>> && requires(_Tp&& __t) {
51       ranges::rbegin(__t);
52       { _LIBCPP_AUTO_CAST(rend(__t)) } -> sentinel_for<decltype(ranges::rbegin(__t))>;
53     };
54 
55 template <class _Tp>
56 concept __can_reverse = __can_borrow<_Tp> && !__member_rend<_Tp> && !__unqualified_rend<_Tp> && requires(_Tp&& __t) {
57   { ranges::begin(__t) } -> same_as<decltype(ranges::end(__t))>;
58   { ranges::begin(__t) } -> bidirectional_iterator;
59 };
60 
61 class __fn {
62 public:
63   template <class _Tp>
64     requires __member_rend<_Tp>
65   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
66       noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.rend()))) {
67     return _LIBCPP_AUTO_CAST(__t.rend());
68   }
69 
70   template <class _Tp>
71     requires __unqualified_rend<_Tp>
72   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
73       noexcept(noexcept(_LIBCPP_AUTO_CAST(rend(__t)))) {
74     return _LIBCPP_AUTO_CAST(rend(__t));
75   }
76 
77   template <class _Tp>
78     requires __can_reverse<_Tp>
79   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
80       noexcept(noexcept(ranges::begin(__t))) {
81     return std::make_reverse_iterator(ranges::begin(__t));
82   }
83 
84   void operator()(auto&&) const = delete;
85 };
86 } // namespace __rend
87 
88 inline namespace __cpo {
89 inline constexpr auto rend = __rend::__fn{};
90 } // namespace __cpo
91 } // namespace ranges
92 
93 // [range.access.crend]
94 
95 namespace ranges {
96 namespace __crend {
97 struct __fn {
98   template <class _Tp>
99     requires is_lvalue_reference_v<_Tp&&>
100   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
101       noexcept(noexcept(ranges::rend(static_cast<const remove_reference_t<_Tp>&>(__t))))
102           -> decltype(ranges::rend(static_cast<const remove_reference_t<_Tp>&>(__t))) {
103     return ranges::rend(static_cast<const remove_reference_t<_Tp>&>(__t));
104   }
105 
106   template <class _Tp>
107     requires is_rvalue_reference_v<_Tp&&>
108   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
109       noexcept(noexcept(ranges::rend(static_cast<const _Tp&&>(__t))))
110           -> decltype(ranges::rend(static_cast<const _Tp&&>(__t))) {
111     return ranges::rend(static_cast<const _Tp&&>(__t));
112   }
113 };
114 } // namespace __crend
115 
116 inline namespace __cpo {
117 inline constexpr auto crend = __crend::__fn{};
118 } // namespace __cpo
119 } // namespace ranges
120 
121 #endif // _LIBCPP_STD_VER >= 20
122 
123 _LIBCPP_END_NAMESPACE_STD
124 
125 #endif // _LIBCPP___RANGES_REND_H
126