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___CXX03___ITERATOR_MOVE_ITERATOR_H
11 #define _LIBCPP___CXX03___ITERATOR_MOVE_ITERATOR_H
12
13 #include <__cxx03/__config>
14 #include <__cxx03/__iterator/iterator_traits.h>
15 #include <__cxx03/__type_traits/conditional.h>
16 #include <__cxx03/__type_traits/enable_if.h>
17 #include <__cxx03/__type_traits/is_assignable.h>
18 #include <__cxx03/__type_traits/is_constructible.h>
19 #include <__cxx03/__type_traits/is_convertible.h>
20 #include <__cxx03/__type_traits/is_reference.h>
21 #include <__cxx03/__type_traits/is_same.h>
22 #include <__cxx03/__type_traits/remove_reference.h>
23 #include <__cxx03/__utility/declval.h>
24 #include <__cxx03/__utility/move.h>
25
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 # pragma GCC system_header
28 #endif
29
30 _LIBCPP_PUSH_MACROS
31 #include <__cxx03/__undef_macros>
32
33 _LIBCPP_BEGIN_NAMESPACE_STD
34
35 template <class _Iter>
36 class _LIBCPP_TEMPLATE_VIS move_iterator {
37 public:
38 typedef _Iter iterator_type;
39 typedef _If< __has_random_access_iterator_category<_Iter>::value,
40 random_access_iterator_tag,
41 typename iterator_traits<_Iter>::iterator_category >
42 iterator_category;
43 typedef typename iterator_traits<iterator_type>::value_type value_type;
44 typedef typename iterator_traits<iterator_type>::difference_type difference_type;
45 typedef iterator_type pointer;
46
47 typedef typename iterator_traits<iterator_type>::reference __reference;
48 typedef __conditional_t<is_reference<__reference>::value, __libcpp_remove_reference_t<__reference>&&, __reference>
49 reference;
50
move_iterator(_Iter __i)51 _LIBCPP_HIDE_FROM_ABI explicit move_iterator(_Iter __i) : __current_(std::move(__i)) {}
52
53 _LIBCPP_HIDE_FROM_ABI move_iterator& operator++() {
54 ++__current_;
55 return *this;
56 }
57
58 _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return __current_; }
59
move_iterator()60 _LIBCPP_HIDE_FROM_ABI move_iterator() : __current_() {}
61
62 template <class _Up, __enable_if_t< !is_same<_Up, _Iter>::value && is_convertible<const _Up&, _Iter>::value, int> = 0>
move_iterator(const move_iterator<_Up> & __u)63 _LIBCPP_HIDE_FROM_ABI move_iterator(const move_iterator<_Up>& __u) : __current_(__u.base()) {}
64
65 template <class _Up,
66 __enable_if_t< !is_same<_Up, _Iter>::value && is_convertible<const _Up&, _Iter>::value &&
67 is_assignable<_Iter&, const _Up&>::value,
68 int> = 0>
69 _LIBCPP_HIDE_FROM_ABI move_iterator& operator=(const move_iterator<_Up>& __u) {
70 __current_ = __u.base();
71 return *this;
72 }
73
base()74 _LIBCPP_HIDE_FROM_ABI _Iter base() const { return __current_; }
75
76 _LIBCPP_HIDE_FROM_ABI reference operator*() const { return static_cast<reference>(*__current_); }
77 _LIBCPP_HIDE_FROM_ABI reference operator[](difference_type __n) const {
78 return static_cast<reference>(__current_[__n]);
79 }
80
81 _LIBCPP_HIDE_FROM_ABI move_iterator operator++(int) {
82 move_iterator __tmp(*this);
83 ++__current_;
84 return __tmp;
85 }
86
87 _LIBCPP_HIDE_FROM_ABI move_iterator& operator--() {
88 --__current_;
89 return *this;
90 }
91 _LIBCPP_HIDE_FROM_ABI move_iterator operator--(int) {
92 move_iterator __tmp(*this);
93 --__current_;
94 return __tmp;
95 }
96 _LIBCPP_HIDE_FROM_ABI move_iterator operator+(difference_type __n) const { return move_iterator(__current_ + __n); }
97 _LIBCPP_HIDE_FROM_ABI move_iterator& operator+=(difference_type __n) {
98 __current_ += __n;
99 return *this;
100 }
101 _LIBCPP_HIDE_FROM_ABI move_iterator operator-(difference_type __n) const { return move_iterator(__current_ - __n); }
102 _LIBCPP_HIDE_FROM_ABI move_iterator& operator-=(difference_type __n) {
103 __current_ -= __n;
104 return *this;
105 }
106
107 private:
108 template <class _It2>
109 friend class move_iterator;
110
111 _Iter __current_;
112 };
113 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(move_iterator);
114
115 template <class _Iter1, class _Iter2>
116 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y) {
117 return __x.base() == __y.base();
118 }
119
120 template <class _Iter1, class _Iter2>
121 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y) {
122 return __x.base() != __y.base();
123 }
124
125 template <class _Iter1, class _Iter2>
126 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y) {
127 return __x.base() < __y.base();
128 }
129
130 template <class _Iter1, class _Iter2>
131 inline _LIBCPP_HIDE_FROM_ABI bool operator>(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y) {
132 return __x.base() > __y.base();
133 }
134
135 template <class _Iter1, class _Iter2>
136 inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y) {
137 return __x.base() <= __y.base();
138 }
139
140 template <class _Iter1, class _Iter2>
141 inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y) {
142 return __x.base() >= __y.base();
143 }
144
145 template <class _Iter1, class _Iter2>
146 inline _LIBCPP_HIDE_FROM_ABI typename move_iterator<_Iter1>::difference_type
147 operator-(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y) {
148 return __x.base() - __y.base();
149 }
150
151 template <class _Iter>
152 inline _LIBCPP_HIDE_FROM_ABI move_iterator<_Iter>
153 operator+(typename move_iterator<_Iter>::difference_type __n, const move_iterator<_Iter>& __x) {
154 return move_iterator<_Iter>(__x.base() + __n);
155 }
156
157 template <class _Iter>
make_move_iterator(_Iter __i)158 inline _LIBCPP_HIDE_FROM_ABI move_iterator<_Iter> make_move_iterator(_Iter __i) {
159 return move_iterator<_Iter>(std::move(__i));
160 }
161
162 _LIBCPP_END_NAMESPACE_STD
163
164 _LIBCPP_POP_MACROS
165
166 #endif // _LIBCPP___CXX03___ITERATOR_MOVE_ITERATOR_H
167