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