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___ITERATOR_MOVE_ITERATOR_H
11 #define _LIBCPP___ITERATOR_MOVE_ITERATOR_H
12 
13 #include <__compare/compare_three_way_result.h>
14 #include <__compare/three_way_comparable.h>
15 #include <__concepts/assignable.h>
16 #include <__concepts/convertible_to.h>
17 #include <__concepts/derived_from.h>
18 #include <__concepts/same_as.h>
19 #include <__config>
20 #include <__iterator/concepts.h>
21 #include <__iterator/incrementable_traits.h>
22 #include <__iterator/iter_move.h>
23 #include <__iterator/iter_swap.h>
24 #include <__iterator/iterator_traits.h>
25 #include <__iterator/move_sentinel.h>
26 #include <__iterator/readable_traits.h>
27 #include <__utility/move.h>
28 #include <type_traits>
29 
30 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31 #  pragma GCC system_header
32 #endif
33 
34 _LIBCPP_BEGIN_NAMESPACE_STD
35 
36 #if _LIBCPP_STD_VER > 17
37 template<class _Iter, class = void>
38 struct __move_iter_category_base {};
39 
40 template<class _Iter>
41   requires requires { typename iterator_traits<_Iter>::iterator_category; }
42 struct __move_iter_category_base<_Iter> {
43     using iterator_category = _If<
44         derived_from<typename iterator_traits<_Iter>::iterator_category, random_access_iterator_tag>,
45         random_access_iterator_tag,
46         typename iterator_traits<_Iter>::iterator_category
47     >;
48 };
49 
50 template<class _Iter, class _Sent>
51 concept __move_iter_comparable = requires {
52     { declval<const _Iter&>() == declval<_Sent>() } -> convertible_to<bool>;
53 };
54 #endif // _LIBCPP_STD_VER > 17
55 
56 template <class _Iter>
57 class _LIBCPP_TEMPLATE_VIS move_iterator
58 #if _LIBCPP_STD_VER > 17
59     : public __move_iter_category_base<_Iter>
60 #endif
61 {
62 public:
63 #if _LIBCPP_STD_VER > 17
64     using iterator_type = _Iter;
65     using iterator_concept = input_iterator_tag;
66     // iterator_category is inherited and not always present
67     using value_type = iter_value_t<_Iter>;
68     using difference_type = iter_difference_t<_Iter>;
69     using pointer = _Iter;
70     using reference = iter_rvalue_reference_t<_Iter>;
71 #else
72     typedef _Iter iterator_type;
73     typedef _If<
74         __is_cpp17_random_access_iterator<_Iter>::value,
75         random_access_iterator_tag,
76         typename iterator_traits<_Iter>::iterator_category
77     > iterator_category;
78     typedef typename iterator_traits<iterator_type>::value_type value_type;
79     typedef typename iterator_traits<iterator_type>::difference_type difference_type;
80     typedef iterator_type pointer;
81 
82     typedef typename iterator_traits<iterator_type>::reference __reference;
83     typedef typename conditional<
84             is_reference<__reference>::value,
85             typename remove_reference<__reference>::type&&,
86             __reference
87         >::type reference;
88 #endif // _LIBCPP_STD_VER > 17
89 
90     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
91     explicit move_iterator(_Iter __i) : __current_(std::move(__i)) {}
92 
93     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
94     move_iterator& operator++() { ++__current_; return *this; }
95 
96 #if _LIBCPP_STD_VER > 17
97     _LIBCPP_HIDE_FROM_ABI constexpr
98     move_iterator() requires is_constructible_v<_Iter> : __current_() {}
99 
100     template <class _Up>
101         requires (!_IsSame<_Up, _Iter>::value) && convertible_to<const _Up&, _Iter>
102     _LIBCPP_HIDE_FROM_ABI constexpr
103     move_iterator(const move_iterator<_Up>& __u) : __current_(__u.base()) {}
104 
105     template <class _Up>
106         requires (!_IsSame<_Up, _Iter>::value) &&
107                  convertible_to<const _Up&, _Iter> &&
108                  assignable_from<_Iter&, const _Up&>
109     _LIBCPP_HIDE_FROM_ABI constexpr
110     move_iterator& operator=(const move_iterator<_Up>& __u) {
111         __current_ = __u.base();
112         return *this;
113     }
114 
115     _LIBCPP_HIDE_FROM_ABI constexpr const _Iter& base() const & noexcept { return __current_; }
116     _LIBCPP_HIDE_FROM_ABI constexpr _Iter base() && { return std::move(__current_); }
117 
118     _LIBCPP_HIDE_FROM_ABI constexpr
119     reference operator*() const { return ranges::iter_move(__current_); }
120     _LIBCPP_HIDE_FROM_ABI constexpr
121     reference operator[](difference_type __n) const { return ranges::iter_move(__current_ + __n); }
122 
123     _LIBCPP_HIDE_FROM_ABI constexpr
124     auto operator++(int)
125         requires forward_iterator<_Iter>
126     {
127         move_iterator __tmp(*this); ++__current_; return __tmp;
128     }
129 
130     _LIBCPP_HIDE_FROM_ABI constexpr
131     void operator++(int) { ++__current_; }
132 #else
133     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
134     move_iterator() : __current_() {}
135 
136     template <class _Up, class = __enable_if_t<
137         !is_same<_Up, _Iter>::value && is_convertible<const _Up&, _Iter>::value
138     > >
139     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
140     move_iterator(const move_iterator<_Up>& __u) : __current_(__u.base()) {}
141 
142     template <class _Up, class = __enable_if_t<
143         !is_same<_Up, _Iter>::value &&
144         is_convertible<const _Up&, _Iter>::value &&
145         is_assignable<_Iter&, const _Up&>::value
146     > >
147     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
148     move_iterator& operator=(const move_iterator<_Up>& __u) {
149         __current_ = __u.base();
150         return *this;
151     }
152 
153     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
154     _Iter base() const { return __current_; }
155 
156     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
157     reference operator*() const { return static_cast<reference>(*__current_); }
158     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
159     pointer operator->() const { return __current_; }
160     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
161     reference operator[](difference_type __n) const { return static_cast<reference>(__current_[__n]); }
162 
163     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
164     move_iterator operator++(int) { move_iterator __tmp(*this); ++__current_; return __tmp; }
165 #endif // _LIBCPP_STD_VER > 17
166 
167     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
168     move_iterator& operator--() { --__current_; return *this; }
169     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
170     move_iterator operator--(int) { move_iterator __tmp(*this); --__current_; return __tmp; }
171     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
172     move_iterator operator+(difference_type __n) const { return move_iterator(__current_ + __n); }
173     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
174     move_iterator& operator+=(difference_type __n) { __current_ += __n; return *this; }
175     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
176     move_iterator operator-(difference_type __n) const { return move_iterator(__current_ - __n); }
177     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
178     move_iterator& operator-=(difference_type __n) { __current_ -= __n; return *this; }
179 
180 #if _LIBCPP_STD_VER > 17
181     template<sentinel_for<_Iter> _Sent>
182     friend _LIBCPP_HIDE_FROM_ABI constexpr
183     bool operator==(const move_iterator& __x, const move_sentinel<_Sent>& __y)
184         requires __move_iter_comparable<_Iter, _Sent>
185     {
186         return __x.base() == __y.base();
187     }
188 
189     template<sized_sentinel_for<_Iter> _Sent>
190     friend _LIBCPP_HIDE_FROM_ABI constexpr
191     iter_difference_t<_Iter> operator-(const move_sentinel<_Sent>& __x, const move_iterator& __y)
192     {
193         return __x.base() - __y.base();
194     }
195 
196     template<sized_sentinel_for<_Iter> _Sent>
197     friend _LIBCPP_HIDE_FROM_ABI constexpr
198     iter_difference_t<_Iter> operator-(const move_iterator& __x, const move_sentinel<_Sent>& __y)
199     {
200         return __x.base() - __y.base();
201     }
202 
203     friend _LIBCPP_HIDE_FROM_ABI constexpr
204     iter_rvalue_reference_t<_Iter> iter_move(const move_iterator& __i)
205         noexcept(noexcept(ranges::iter_move(__i.__current_)))
206     {
207         return ranges::iter_move(__i.__current_);
208     }
209 
210     template<indirectly_swappable<_Iter> _It2>
211     friend _LIBCPP_HIDE_FROM_ABI constexpr
212     void iter_swap(const move_iterator& __x, const move_iterator<_It2>& __y)
213         noexcept(noexcept(ranges::iter_swap(__x.__current_, __y.__current_)))
214     {
215         return ranges::iter_swap(__x.__current_, __y.__current_);
216     }
217 #endif // _LIBCPP_STD_VER > 17
218 
219 private:
220     template<class _It2> friend class move_iterator;
221 
222     _Iter __current_;
223 };
224 
225 template <class _Iter1, class _Iter2>
226 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
227 bool operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
228 {
229     return __x.base() == __y.base();
230 }
231 
232 #if _LIBCPP_STD_VER <= 17
233 template <class _Iter1, class _Iter2>
234 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
235 bool operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
236 {
237     return __x.base() != __y.base();
238 }
239 #endif // _LIBCPP_STD_VER <= 17
240 
241 template <class _Iter1, class _Iter2>
242 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
243 bool operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
244 {
245     return __x.base() < __y.base();
246 }
247 
248 template <class _Iter1, class _Iter2>
249 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
250 bool operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
251 {
252     return __x.base() > __y.base();
253 }
254 
255 template <class _Iter1, class _Iter2>
256 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
257 bool operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
258 {
259     return __x.base() <= __y.base();
260 }
261 
262 template <class _Iter1, class _Iter2>
263 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
264 bool operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
265 {
266     return __x.base() >= __y.base();
267 }
268 
269 #if _LIBCPP_STD_VER > 17
270 template <class _Iter1, three_way_comparable_with<_Iter1> _Iter2>
271 inline _LIBCPP_HIDE_FROM_ABI constexpr
272 auto operator<=>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
273     -> compare_three_way_result_t<_Iter1, _Iter2>
274 {
275     return __x.base() <=> __y.base();
276 }
277 #endif // _LIBCPP_STD_VER > 17
278 
279 #ifndef _LIBCPP_CXX03_LANG
280 template <class _Iter1, class _Iter2>
281 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
282 auto operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
283     -> decltype(__x.base() - __y.base())
284 {
285     return __x.base() - __y.base();
286 }
287 #else
288 template <class _Iter1, class _Iter2>
289 inline _LIBCPP_HIDE_FROM_ABI
290 typename move_iterator<_Iter1>::difference_type
291 operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
292 {
293     return __x.base() - __y.base();
294 }
295 #endif // !_LIBCPP_CXX03_LANG
296 
297 #if _LIBCPP_STD_VER > 17
298 template <class _Iter>
299 inline _LIBCPP_HIDE_FROM_ABI constexpr
300 move_iterator<_Iter> operator+(iter_difference_t<_Iter> __n, const move_iterator<_Iter>& __x)
301     requires requires { { __x.base() + __n } -> same_as<_Iter>; }
302 {
303     return __x + __n;
304 }
305 #else
306 template <class _Iter>
307 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
308 move_iterator<_Iter>
309 operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
310 {
311     return move_iterator<_Iter>(__x.base() + __n);
312 }
313 #endif // _LIBCPP_STD_VER > 17
314 
315 template <class _Iter>
316 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
317 move_iterator<_Iter>
318 make_move_iterator(_Iter __i)
319 {
320     return move_iterator<_Iter>(std::move(__i));
321 }
322 
323 _LIBCPP_END_NAMESPACE_STD
324 
325 #endif // _LIBCPP___ITERATOR_MOVE_ITERATOR_H
326