xref: /freebsd/contrib/llvm-project/libcxx/include/__ranges/rbegin.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
181ad6265SDimitry Andric // -*- C++ -*-
281ad6265SDimitry Andric //===----------------------------------------------------------------------===//
381ad6265SDimitry Andric //
481ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
581ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
681ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
781ad6265SDimitry Andric //
881ad6265SDimitry Andric //===----------------------------------------------------------------------===//
9bdd1243dSDimitry Andric 
1081ad6265SDimitry Andric #ifndef _LIBCPP___RANGES_RBEGIN_H
1181ad6265SDimitry Andric #define _LIBCPP___RANGES_RBEGIN_H
1281ad6265SDimitry Andric 
1381ad6265SDimitry Andric #include <__concepts/class_or_enum.h>
1481ad6265SDimitry Andric #include <__concepts/same_as.h>
1581ad6265SDimitry Andric #include <__config>
1681ad6265SDimitry Andric #include <__iterator/concepts.h>
1781ad6265SDimitry Andric #include <__iterator/readable_traits.h>
1881ad6265SDimitry Andric #include <__iterator/reverse_iterator.h>
1981ad6265SDimitry Andric #include <__ranges/access.h>
2006c3fb27SDimitry Andric #include <__type_traits/decay.h>
2106c3fb27SDimitry Andric #include <__type_traits/is_reference.h>
2206c3fb27SDimitry Andric #include <__type_traits/remove_cvref.h>
2306c3fb27SDimitry Andric #include <__type_traits/remove_reference.h>
2481ad6265SDimitry Andric #include <__utility/auto_cast.h>
2581ad6265SDimitry Andric 
2681ad6265SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2781ad6265SDimitry Andric #  pragma GCC system_header
2881ad6265SDimitry Andric #endif
2981ad6265SDimitry Andric 
3081ad6265SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
3181ad6265SDimitry Andric 
3206c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
3381ad6265SDimitry Andric 
3481ad6265SDimitry Andric // [ranges.access.rbegin]
3581ad6265SDimitry Andric 
3681ad6265SDimitry Andric namespace ranges {
3781ad6265SDimitry Andric namespace __rbegin {
3881ad6265SDimitry Andric template <class _Tp>
requires(_Tp && __t)39*0fca6ea1SDimitry Andric concept __member_rbegin = __can_borrow<_Tp> && requires(_Tp&& __t) {
4081ad6265SDimitry Andric   { _LIBCPP_AUTO_CAST(__t.rbegin()) } -> input_or_output_iterator;
4181ad6265SDimitry Andric };
4281ad6265SDimitry Andric 
43*0fca6ea1SDimitry Andric void rbegin() = delete;
4481ad6265SDimitry Andric 
4581ad6265SDimitry Andric template <class _Tp>
4681ad6265SDimitry Andric concept __unqualified_rbegin =
47cb14a3feSDimitry Andric     !__member_rbegin<_Tp> && __can_borrow<_Tp> && __class_or_enum<remove_cvref_t<_Tp>> && requires(_Tp&& __t) {
4881ad6265SDimitry Andric       { _LIBCPP_AUTO_CAST(rbegin(__t)) } -> input_or_output_iterator;
4981ad6265SDimitry Andric     };
5081ad6265SDimitry Andric 
5181ad6265SDimitry Andric template <class _Tp>
5281ad6265SDimitry Andric concept __can_reverse =
53cb14a3feSDimitry Andric     __can_borrow<_Tp> && !__member_rbegin<_Tp> && !__unqualified_rbegin<_Tp> && requires(_Tp&& __t) {
5481ad6265SDimitry Andric       { ranges::begin(__t) } -> same_as<decltype(ranges::end(__t))>;
5581ad6265SDimitry Andric       { ranges::begin(__t) } -> bidirectional_iterator;
5681ad6265SDimitry Andric     };
5781ad6265SDimitry Andric 
5881ad6265SDimitry Andric struct __fn {
5981ad6265SDimitry Andric   template <class _Tp>
6081ad6265SDimitry Andric     requires __member_rbegin<_Tp>
operator__fn6181ad6265SDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
62cb14a3feSDimitry Andric       noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.rbegin()))) {
6381ad6265SDimitry Andric     return _LIBCPP_AUTO_CAST(__t.rbegin());
6481ad6265SDimitry Andric   }
6581ad6265SDimitry Andric 
6681ad6265SDimitry Andric   template <class _Tp>
6781ad6265SDimitry Andric     requires __unqualified_rbegin<_Tp>
operator__fn6881ad6265SDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
69cb14a3feSDimitry Andric       noexcept(noexcept(_LIBCPP_AUTO_CAST(rbegin(__t)))) {
7081ad6265SDimitry Andric     return _LIBCPP_AUTO_CAST(rbegin(__t));
7181ad6265SDimitry Andric   }
7281ad6265SDimitry Andric 
7381ad6265SDimitry Andric   template <class _Tp>
7481ad6265SDimitry Andric     requires __can_reverse<_Tp>
operator__fn75cb14a3feSDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const noexcept(noexcept(ranges::end(__t))) {
7681ad6265SDimitry Andric     return std::make_reverse_iterator(ranges::end(__t));
7781ad6265SDimitry Andric   }
7881ad6265SDimitry Andric 
7981ad6265SDimitry Andric   void operator()(auto&&) const = delete;
8081ad6265SDimitry Andric };
8181ad6265SDimitry Andric } // namespace __rbegin
8281ad6265SDimitry Andric 
8381ad6265SDimitry Andric inline namespace __cpo {
8481ad6265SDimitry Andric inline constexpr auto rbegin = __rbegin::__fn{};
8581ad6265SDimitry Andric } // namespace __cpo
8681ad6265SDimitry Andric } // namespace ranges
8781ad6265SDimitry Andric 
8881ad6265SDimitry Andric // [range.access.crbegin]
8981ad6265SDimitry Andric 
9081ad6265SDimitry Andric namespace ranges {
9181ad6265SDimitry Andric namespace __crbegin {
9281ad6265SDimitry Andric struct __fn {
9381ad6265SDimitry Andric   template <class _Tp>
9481ad6265SDimitry Andric     requires is_lvalue_reference_v<_Tp&&>
95cb14a3feSDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
9681ad6265SDimitry Andric       noexcept(noexcept(ranges::rbegin(static_cast<const remove_reference_t<_Tp>&>(__t))))
97cb14a3feSDimitry Andric           -> decltype(ranges::rbegin(static_cast<const remove_reference_t<_Tp>&>(__t))) {
98cb14a3feSDimitry Andric     return ranges::rbegin(static_cast<const remove_reference_t<_Tp>&>(__t));
99cb14a3feSDimitry Andric   }
10081ad6265SDimitry Andric 
10181ad6265SDimitry Andric   template <class _Tp>
10281ad6265SDimitry Andric     requires is_rvalue_reference_v<_Tp&&>
103cb14a3feSDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
10481ad6265SDimitry Andric       noexcept(noexcept(ranges::rbegin(static_cast<const _Tp&&>(__t))))
105cb14a3feSDimitry Andric           -> decltype(ranges::rbegin(static_cast<const _Tp&&>(__t))) {
106cb14a3feSDimitry Andric     return ranges::rbegin(static_cast<const _Tp&&>(__t));
107cb14a3feSDimitry Andric   }
10881ad6265SDimitry Andric };
10981ad6265SDimitry Andric } // namespace __crbegin
11081ad6265SDimitry Andric 
11181ad6265SDimitry Andric inline namespace __cpo {
11281ad6265SDimitry Andric inline constexpr auto crbegin = __crbegin::__fn{};
11381ad6265SDimitry Andric } // namespace __cpo
11481ad6265SDimitry Andric } // namespace ranges
11581ad6265SDimitry Andric 
11606c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
11781ad6265SDimitry Andric 
11881ad6265SDimitry Andric _LIBCPP_END_NAMESPACE_STD
11981ad6265SDimitry Andric 
12081ad6265SDimitry Andric #endif // _LIBCPP___RANGES_RBEGIN_H
121