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     _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
97     pointer operator->() const { return __current_; }
98 
99 #if _LIBCPP_STD_VER > 17
100     _LIBCPP_HIDE_FROM_ABI constexpr
101     move_iterator() requires is_constructible_v<_Iter> : __current_() {}
102 
103     template <class _Up>
104         requires (!_IsSame<_Up, _Iter>::value) && convertible_to<const _Up&, _Iter>
105     _LIBCPP_HIDE_FROM_ABI constexpr
106     move_iterator(const move_iterator<_Up>& __u) : __current_(__u.base()) {}
107 
108     template <class _Up>
109         requires (!_IsSame<_Up, _Iter>::value) &&
110                  convertible_to<const _Up&, _Iter> &&
111                  assignable_from<_Iter&, const _Up&>
112     _LIBCPP_HIDE_FROM_ABI constexpr
113     move_iterator& operator=(const move_iterator<_Up>& __u) {
114         __current_ = __u.base();
115         return *this;
116     }
117 
118     _LIBCPP_HIDE_FROM_ABI constexpr const _Iter& base() const & noexcept { return __current_; }
119     _LIBCPP_HIDE_FROM_ABI constexpr _Iter base() && { return std::move(__current_); }
120 
121     _LIBCPP_HIDE_FROM_ABI constexpr
122     reference operator*() const { return ranges::iter_move(__current_); }
123     _LIBCPP_HIDE_FROM_ABI constexpr
124     reference operator[](difference_type __n) const { return ranges::iter_move(__current_ + __n); }
125 
126     _LIBCPP_HIDE_FROM_ABI constexpr
127     auto operator++(int)
128         requires forward_iterator<_Iter>
129     {
130         move_iterator __tmp(*this); ++__current_; return __tmp;
131     }
132 
133     _LIBCPP_HIDE_FROM_ABI constexpr
134     void operator++(int) { ++__current_; }
135 #else
136     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
137     move_iterator() : __current_() {}
138 
139     template <class _Up, class = __enable_if_t<
140         !is_same<_Up, _Iter>::value && is_convertible<const _Up&, _Iter>::value
141     > >
142     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
143     move_iterator(const move_iterator<_Up>& __u) : __current_(__u.base()) {}
144 
145     template <class _Up, class = __enable_if_t<
146         !is_same<_Up, _Iter>::value &&
147         is_convertible<const _Up&, _Iter>::value &&
148         is_assignable<_Iter&, const _Up&>::value
149     > >
150     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
151     move_iterator& operator=(const move_iterator<_Up>& __u) {
152         __current_ = __u.base();
153         return *this;
154     }
155 
156     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
157     _Iter base() const { return __current_; }
158 
159     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
160     reference operator*() const { return static_cast<reference>(*__current_); }
161     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
162     reference operator[](difference_type __n) const { return static_cast<reference>(__current_[__n]); }
163 
164     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
165     move_iterator operator++(int) { move_iterator __tmp(*this); ++__current_; return __tmp; }
166 #endif // _LIBCPP_STD_VER > 17
167 
168     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
169     move_iterator& operator--() { --__current_; return *this; }
170     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
171     move_iterator operator--(int) { move_iterator __tmp(*this); --__current_; return __tmp; }
172     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
173     move_iterator operator+(difference_type __n) const { return move_iterator(__current_ + __n); }
174     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
175     move_iterator& operator+=(difference_type __n) { __current_ += __n; return *this; }
176     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
177     move_iterator operator-(difference_type __n) const { return move_iterator(__current_ - __n); }
178     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
179     move_iterator& operator-=(difference_type __n) { __current_ -= __n; return *this; }
180 
181 #if _LIBCPP_STD_VER > 17
182     template<sentinel_for<_Iter> _Sent>
183     friend _LIBCPP_HIDE_FROM_ABI constexpr
184     bool operator==(const move_iterator& __x, const move_sentinel<_Sent>& __y)
185         requires __move_iter_comparable<_Iter, _Sent>
186     {
187         return __x.base() == __y.base();
188     }
189 
190     template<sized_sentinel_for<_Iter> _Sent>
191     friend _LIBCPP_HIDE_FROM_ABI constexpr
192     iter_difference_t<_Iter> operator-(const move_sentinel<_Sent>& __x, const move_iterator& __y)
193     {
194         return __x.base() - __y.base();
195     }
196 
197     template<sized_sentinel_for<_Iter> _Sent>
198     friend _LIBCPP_HIDE_FROM_ABI constexpr
199     iter_difference_t<_Iter> operator-(const move_iterator& __x, const move_sentinel<_Sent>& __y)
200     {
201         return __x.base() - __y.base();
202     }
203 
204     friend _LIBCPP_HIDE_FROM_ABI constexpr
205     iter_rvalue_reference_t<_Iter> iter_move(const move_iterator& __i)
206         noexcept(noexcept(ranges::iter_move(__i.__current_)))
207     {
208         return ranges::iter_move(__i.__current_);
209     }
210 
211     template<indirectly_swappable<_Iter> _It2>
212     friend _LIBCPP_HIDE_FROM_ABI constexpr
213     void iter_swap(const move_iterator& __x, const move_iterator<_It2>& __y)
214         noexcept(noexcept(ranges::iter_swap(__x.__current_, __y.__current_)))
215     {
216         return ranges::iter_swap(__x.__current_, __y.__current_);
217     }
218 #endif // _LIBCPP_STD_VER > 17
219 
220 private:
221     template<class _It2> friend class move_iterator;
222 
223     _Iter __current_;
224 };
225 
226 template <class _Iter1, class _Iter2>
227 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
228 bool operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
229 {
230     return __x.base() == __y.base();
231 }
232 
233 #if _LIBCPP_STD_VER <= 17
234 template <class _Iter1, class _Iter2>
235 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
236 bool operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
237 {
238     return __x.base() != __y.base();
239 }
240 #endif // _LIBCPP_STD_VER <= 17
241 
242 template <class _Iter1, class _Iter2>
243 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
244 bool operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
245 {
246     return __x.base() < __y.base();
247 }
248 
249 template <class _Iter1, class _Iter2>
250 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
251 bool operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
252 {
253     return __x.base() > __y.base();
254 }
255 
256 template <class _Iter1, class _Iter2>
257 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
258 bool operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
259 {
260     return __x.base() <= __y.base();
261 }
262 
263 template <class _Iter1, class _Iter2>
264 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
265 bool operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
266 {
267     return __x.base() >= __y.base();
268 }
269 
270 #if _LIBCPP_STD_VER > 17
271 template <class _Iter1, three_way_comparable_with<_Iter1> _Iter2>
272 inline _LIBCPP_HIDE_FROM_ABI constexpr
273 auto operator<=>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
274     -> compare_three_way_result_t<_Iter1, _Iter2>
275 {
276     return __x.base() <=> __y.base();
277 }
278 #endif // _LIBCPP_STD_VER > 17
279 
280 #ifndef _LIBCPP_CXX03_LANG
281 template <class _Iter1, class _Iter2>
282 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
283 auto operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
284     -> decltype(__x.base() - __y.base())
285 {
286     return __x.base() - __y.base();
287 }
288 #else
289 template <class _Iter1, class _Iter2>
290 inline _LIBCPP_HIDE_FROM_ABI
291 typename move_iterator<_Iter1>::difference_type
292 operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
293 {
294     return __x.base() - __y.base();
295 }
296 #endif // !_LIBCPP_CXX03_LANG
297 
298 #if _LIBCPP_STD_VER > 17
299 template <class _Iter>
300 inline _LIBCPP_HIDE_FROM_ABI constexpr
301 move_iterator<_Iter> operator+(iter_difference_t<_Iter> __n, const move_iterator<_Iter>& __x)
302     requires requires { { __x.base() + __n } -> same_as<_Iter>; }
303 {
304     return __x + __n;
305 }
306 #else
307 template <class _Iter>
308 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
309 move_iterator<_Iter>
310 operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x)
311 {
312     return move_iterator<_Iter>(__x.base() + __n);
313 }
314 #endif // _LIBCPP_STD_VER > 17
315 
316 template <class _Iter>
317 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
318 move_iterator<_Iter>
319 make_move_iterator(_Iter __i)
320 {
321     return move_iterator<_Iter>(std::move(__i));
322 }
323 
324 _LIBCPP_END_NAMESPACE_STD
325 
326 #endif // _LIBCPP___ITERATOR_MOVE_ITERATOR_H
327