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