| 1 | //===----------------------------------------------------------------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef _LIBCPP___RANGES_AS_RVALUE_H |
| 10 | #define _LIBCPP___RANGES_AS_RVALUE_H |
| 11 | |
| 12 | #include <__concepts/constructible.h> |
| 13 | #include <__concepts/same_as.h> |
| 14 | #include <__config> |
| 15 | #include <__iterator/move_iterator.h> |
| 16 | #include <__iterator/move_sentinel.h> |
| 17 | #include <__ranges/access.h> |
| 18 | #include <__ranges/all.h> |
| 19 | #include <__ranges/concepts.h> |
| 20 | #include <__ranges/enable_borrowed_range.h> |
| 21 | #include <__ranges/range_adaptor.h> |
| 22 | #include <__ranges/size.h> |
| 23 | #include <__ranges/view_interface.h> |
| 24 | #include <__utility/forward.h> |
| 25 | #include <__utility/move.h> |
| 26 | |
| 27 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 28 | # pragma GCC system_header |
| 29 | #endif |
| 30 | |
| 31 | _LIBCPP_PUSH_MACROS |
| 32 | #include <__undef_macros> |
| 33 | |
| 34 | #if _LIBCPP_STD_VER >= 23 |
| 35 | |
| 36 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 37 | |
| 38 | namespace ranges { |
| 39 | template <view _View> |
| 40 | requires input_range<_View> |
| 41 | class as_rvalue_view : public view_interface<as_rvalue_view<_View>> { |
| 42 | _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View(); |
| 43 | |
| 44 | public: |
| 45 | _LIBCPP_HIDE_FROM_ABI as_rvalue_view() |
| 46 | requires default_initializable<_View> |
| 47 | = default; |
| 48 | |
| 49 | _LIBCPP_HIDE_FROM_ABI constexpr explicit as_rvalue_view(_View __base) : __base_(std::move(__base)) {} |
| 50 | |
| 51 | _LIBCPP_HIDE_FROM_ABI constexpr _View base() const& |
| 52 | requires copy_constructible<_View> |
| 53 | { |
| 54 | return __base_; |
| 55 | } |
| 56 | |
| 57 | _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); } |
| 58 | |
| 59 | _LIBCPP_HIDE_FROM_ABI constexpr auto begin() |
| 60 | requires(!__simple_view<_View>) |
| 61 | { |
| 62 | return move_iterator(ranges::begin(__base_)); |
| 63 | } |
| 64 | |
| 65 | _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const |
| 66 | requires range<const _View> |
| 67 | { |
| 68 | return move_iterator(ranges::begin(__base_)); |
| 69 | } |
| 70 | |
| 71 | _LIBCPP_HIDE_FROM_ABI constexpr auto end() |
| 72 | requires(!__simple_view<_View>) |
| 73 | { |
| 74 | if constexpr (common_range<_View>) { |
| 75 | return move_iterator(ranges::end(__base_)); |
| 76 | } else { |
| 77 | return move_sentinel(ranges::end(__base_)); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | _LIBCPP_HIDE_FROM_ABI constexpr auto end() const |
| 82 | requires range<const _View> |
| 83 | { |
| 84 | if constexpr (common_range<const _View>) { |
| 85 | return move_iterator(ranges::end(__base_)); |
| 86 | } else { |
| 87 | return move_sentinel(ranges::end(__base_)); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | _LIBCPP_HIDE_FROM_ABI constexpr auto size() |
| 92 | requires sized_range<_View> |
| 93 | { |
| 94 | return ranges::size(__base_); |
| 95 | } |
| 96 | |
| 97 | _LIBCPP_HIDE_FROM_ABI constexpr auto size() const |
| 98 | requires sized_range<const _View> |
| 99 | { |
| 100 | return ranges::size(__base_); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | template <class _Range> |
| 105 | as_rvalue_view(_Range&&) -> as_rvalue_view<views::all_t<_Range>>; |
| 106 | |
| 107 | template <class _View> |
| 108 | inline constexpr bool enable_borrowed_range<as_rvalue_view<_View>> = enable_borrowed_range<_View>; |
| 109 | |
| 110 | namespace views { |
| 111 | namespace __as_rvalue { |
| 112 | struct __fn : __range_adaptor_closure<__fn> { |
| 113 | template <class _Range> |
| 114 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto |
| 115 | operator()(_Range&& __range) noexcept(noexcept(as_rvalue_view(std::forward<_Range>(__range)))) |
| 116 | -> decltype(/*--------------------------*/ as_rvalue_view(std::forward<_Range>(__range))) { |
| 117 | return /*---------------------------------*/ as_rvalue_view(std::forward<_Range>(__range)); |
| 118 | } |
| 119 | |
| 120 | template <class _Range> |
| 121 | requires same_as<range_rvalue_reference_t<_Range>, range_reference_t<_Range>> |
| 122 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto |
| 123 | operator()(_Range&& __range) noexcept(noexcept(views::all(std::forward<_Range>(__range)))) |
| 124 | -> decltype(/*--------------------------*/ views::all(std::forward<_Range>(__range))) { |
| 125 | return /*---------------------------------*/ views::all(std::forward<_Range>(__range)); |
| 126 | } |
| 127 | }; |
| 128 | } // namespace __as_rvalue |
| 129 | |
| 130 | inline namespace __cpo { |
| 131 | inline constexpr auto as_rvalue = __as_rvalue::__fn{}; |
| 132 | } // namespace __cpo |
| 133 | } // namespace views |
| 134 | } // namespace ranges |
| 135 | |
| 136 | _LIBCPP_END_NAMESPACE_STD |
| 137 | |
| 138 | #endif // _LIBCPP_STD_VER >= 23 |
| 139 | |
| 140 | _LIBCPP_POP_MACROS |
| 141 | |
| 142 | #endif // _LIBCPP___RANGES_AS_RVALUE_H |
| 143 |