|
| 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_TO_INPUT_VIEW_H |
| 11 | +#define _LIBCPP___RANGES_TO_INPUT_VIEW_H |
| 12 | + |
| 13 | +#include <__concepts/constructible.h> |
| 14 | +#include <__concepts/convertible_to.h> |
| 15 | +#include <__config> |
| 16 | +#include <__iterator/concepts.h> |
| 17 | +#include <__iterator/iter_move.h> |
| 18 | +#include <__iterator/iter_swap.h> |
| 19 | +#include <__iterator/iterator_traits.h> |
| 20 | +#include <__ranges/access.h> |
| 21 | +#include <__ranges/all.h> |
| 22 | +#include <__ranges/concepts.h> |
| 23 | +#include <__ranges/enable_borrowed_range.h> |
| 24 | +#include <__ranges/range_adaptor.h> |
| 25 | +#include <__ranges/size.h> |
| 26 | +#include <__ranges/view_interface.h> |
| 27 | +#include <__type_traits/maybe_const.h> |
| 28 | +#include <__utility/forward.h> |
| 29 | +#include <__utility/move.h> |
| 30 | + |
| 31 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 32 | +# pragma GCC system_header |
| 33 | +#endif |
| 34 | + |
| 35 | +_LIBCPP_PUSH_MACROS |
| 36 | +#include <__undef_macros> |
| 37 | + |
| 38 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 39 | + |
| 40 | +#if _LIBCPP_STD_VER >= 26 |
| 41 | + |
| 42 | +namespace ranges { |
| 43 | + |
| 44 | +// [range.to.input.view |
| 45 | + |
| 46 | +template <input_range _View> |
| 47 | + requires view<_View> |
| 48 | +class to_input_view : public view_interface<to_input_view<_View>> { |
| 49 | + _View __base_ = _View(); // exposition only |
| 50 | + |
| 51 | + // [range.to.input.iterator], class template to_input_view::iterator |
| 52 | + template <bool _Const> |
| 53 | + class iterator; // exposition only |
| 54 | + |
| 55 | +public: |
| 56 | + _LIBCPP_HIDE_FROM_ABI to_input_view() |
| 57 | + requires default_initializable<_View> |
| 58 | + = default; |
| 59 | + _LIBCPP_HIDE_FROM_ABI constexpr explicit to_input_view(_View __base) : __base_(std::move(__base)) {} |
| 60 | + |
| 61 | + _LIBCPP_HIDE_FROM_ABI constexpr _View base() const& |
| 62 | + requires copy_constructible<_View> |
| 63 | + { |
| 64 | + return __base_; |
| 65 | + } |
| 66 | + _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); } |
| 67 | + |
| 68 | + _LIBCPP_HIDE_FROM_ABI constexpr auto begin() |
| 69 | + requires(!__simple_view<_View>) |
| 70 | + { |
| 71 | + return iterator<false>{ranges::begin(__base_)}; |
| 72 | + } |
| 73 | + _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const |
| 74 | + requires range<const _View> |
| 75 | + { |
| 76 | + return iterator<true>{ranges::begin(__base_)}; |
| 77 | + } |
| 78 | + |
| 79 | + _LIBCPP_HIDE_FROM_ABI constexpr auto end() |
| 80 | + requires(!__simple_view<_View>) |
| 81 | + { |
| 82 | + return ranges::end(__base_); |
| 83 | + } |
| 84 | + _LIBCPP_HIDE_FROM_ABI constexpr auto end() const |
| 85 | + requires range<const _View> |
| 86 | + { |
| 87 | + return ranges::end(__base_); |
| 88 | + } |
| 89 | + _LIBCPP_HIDE_FROM_ABI constexpr auto size() |
| 90 | + requires sized_range<_View> |
| 91 | + { |
| 92 | + return ranges::size(__base_); |
| 93 | + } |
| 94 | + _LIBCPP_HIDE_FROM_ABI constexpr auto size() const |
| 95 | + requires sized_range<const _View> |
| 96 | + { |
| 97 | + return ranges::size(__base_); |
| 98 | + } |
| 99 | + |
| 100 | + // TODO: Implement when P2846R6 is available. |
| 101 | + // constexpr auto reserve_hint() |
| 102 | + // requires approximately_sized_range<_View> |
| 103 | + // { |
| 104 | + // return ranges::reserve_hint(__base_); |
| 105 | + // } |
| 106 | + // constexpr auto reserve_hint() const |
| 107 | + // requires approximately_sized_range<const _View> |
| 108 | + // { |
| 109 | + // return ranges::reserve_hint(__base_); |
| 110 | + // } |
| 111 | +}; |
| 112 | + |
| 113 | +template <class _Range> |
| 114 | +to_input_view(_Range&&) -> to_input_view<views::all_t<_Range>>; |
| 115 | + |
| 116 | +// [range.to.input.iterator] |
| 117 | + |
| 118 | +template <input_range _View> |
| 119 | + requires view<_View> |
| 120 | +template <bool _Const> |
| 121 | +class to_input_view<_View>::iterator { |
| 122 | + using _Base _LIBCPP_NODEBUG = __maybe_const<_Const, _View>; // exposition only |
| 123 | + |
| 124 | + iterator_t<_Base> __current_ = iterator_t<_Base>(); // exposition only |
| 125 | + |
| 126 | + _LIBCPP_HIDE_FROM_ABI constexpr explicit iterator(iterator_t<_Base> __current) |
| 127 | + : __current_(std::move(__current)) {} // exposition only |
| 128 | + |
| 129 | + friend class to_input_view<_View>; |
| 130 | + |
| 131 | +public: |
| 132 | + using difference_type = range_difference_t<_Base>; |
| 133 | + using value_type = range_value_t<_Base>; |
| 134 | + using iterator_concept = input_iterator_tag; |
| 135 | + |
| 136 | + _LIBCPP_HIDE_FROM_ABI iterator() |
| 137 | + requires default_initializable<iterator_t<_Base>> |
| 138 | + = default; |
| 139 | + |
| 140 | + _LIBCPP_HIDE_FROM_ABI iterator(iterator&&) = default; |
| 141 | + _LIBCPP_HIDE_FROM_ABI iterator& operator=(iterator&&) = default; |
| 142 | + |
| 143 | + _LIBCPP_HIDE_FROM_ABI constexpr iterator(iterator<!_Const> __i) |
| 144 | + requires _Const && convertible_to<iterator_t<_View>, iterator_t<_Base>> |
| 145 | + : __current_(std::move(__i.__current_)) {} |
| 146 | + |
| 147 | + _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Base> base() && { return std::move(__current_); } |
| 148 | + _LIBCPP_HIDE_FROM_ABI constexpr const iterator_t<_Base>& base() const& noexcept { return __current_; } |
| 149 | + |
| 150 | + _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const { return *__current_; } |
| 151 | + |
| 152 | + _LIBCPP_HIDE_FROM_ABI constexpr iterator& operator++() { |
| 153 | + ++__current_; |
| 154 | + return *this; |
| 155 | + } |
| 156 | + _LIBCPP_HIDE_FROM_ABI constexpr void operator++(int) { ++*this; } |
| 157 | + |
| 158 | + _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const iterator& __x, const sentinel_t<_Base>& __y) { |
| 159 | + return __x.__current_ == __y; |
| 160 | + } |
| 161 | + |
| 162 | + _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type operator-(const sentinel_t<_Base>& __y, const iterator& __x) |
| 163 | + requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base>> |
| 164 | + { |
| 165 | + return __y - __x.__current_; |
| 166 | + } |
| 167 | + _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type operator-(const iterator& __x, const sentinel_t<_Base>& __y) |
| 168 | + requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<_Base>> |
| 169 | + { |
| 170 | + return __x.__current_ - __y; |
| 171 | + } |
| 172 | + |
| 173 | + _LIBCPP_HIDE_FROM_ABI friend constexpr range_rvalue_reference_t<_Base> _LIBCPP_HIDE_FROM_ABI |
| 174 | + iter_move(const iterator& __i) noexcept(noexcept(ranges::iter_move(__i.__current_))) { |
| 175 | + return ranges::iter_move(__i.__current_); |
| 176 | + } |
| 177 | + |
| 178 | + _LIBCPP_HIDE_FROM_ABI friend constexpr void |
| 179 | + iter_swap(const iterator& __x, |
| 180 | + const iterator& __y) noexcept(noexcept(ranges::iter_swap(__x.__current_, __y.__current_))) |
| 181 | + requires indirectly_swappable<iterator_t<_Base>> |
| 182 | + { |
| 183 | + ranges::iter_swap(__x.__current_, __y.__current_); |
| 184 | + } |
| 185 | +}; |
| 186 | + |
| 187 | +template <class _View> |
| 188 | +constexpr bool enable_borrowed_range<to_input_view<_View>> = enable_borrowed_range<_View>; |
| 189 | + |
| 190 | +namespace views { |
| 191 | +namespace __to_input_view { |
| 192 | + |
| 193 | +struct __fn : __range_adaptor_closure<__fn> { |
| 194 | + template <class _Range> |
| 195 | + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto |
| 196 | + operator()(_Range&& __range) noexcept(noexcept(/**/ to_input_view(std::forward<_Range>(__range)))) |
| 197 | + -> decltype(/*--*/ to_input_view(std::forward<_Range>(__range))) { |
| 198 | + return /*---------*/ to_input_view(std::forward<_Range>(__range)); |
| 199 | + } |
| 200 | + |
| 201 | + template <class _Range> |
| 202 | + requires(!common_range<_Range> && !forward_range<_Range>) |
| 203 | + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto |
| 204 | + operator()(_Range&& __range) noexcept(noexcept(views::all(std::forward<_Range>(__range)))) |
| 205 | + -> decltype(/*--------------------------*/ views::all(std::forward<_Range>(__range))) { |
| 206 | + return /*---------------------------------*/ views::all(std::forward<_Range>(__range)); |
| 207 | + } |
| 208 | +}; |
| 209 | + |
| 210 | +} // namespace __to_input_view |
| 211 | + |
| 212 | +inline namespace __cpo { |
| 213 | +inline constexpr auto to_input = __to_input_view::__fn{}; |
| 214 | +} // namespace __cpo |
| 215 | +} // namespace views |
| 216 | +} // namespace ranges |
| 217 | + |
| 218 | +#endif // _LIBCPP_STD_VER >= 26 |
| 219 | + |
| 220 | +_LIBCPP_END_NAMESPACE_STD |
| 221 | + |
| 222 | +_LIBCPP_POP_MACROS |
| 223 | + |
| 224 | +#endif // _LIBCPP___RANGES_TO_INPUT_VIEW_H |
0 commit comments