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_WRAP_ITER_H
11 #define _LIBCPP___ITERATOR_WRAP_ITER_H
12 
13 #include <__config>
14 #include <__debug>
15 #include <__iterator/iterator_traits.h>
16 #include <__memory/addressof.h>
17 #include <__memory/pointer_traits.h>
18 #include <type_traits>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #pragma GCC system_header
22 #endif
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 template <class _Iter>
27 class __wrap_iter
28 {
29 public:
30     typedef _Iter                                                      iterator_type;
31     typedef typename iterator_traits<iterator_type>::value_type        value_type;
32     typedef typename iterator_traits<iterator_type>::difference_type   difference_type;
33     typedef typename iterator_traits<iterator_type>::pointer           pointer;
34     typedef typename iterator_traits<iterator_type>::reference         reference;
35     typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;
36 #if _LIBCPP_STD_VER > 17
37     typedef contiguous_iterator_tag                                    iterator_concept;
38 #endif
39 
40 private:
41     iterator_type __i;
42 public:
43     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 __wrap_iter() _NOEXCEPT
44                 : __i()
45     {
46 #if _LIBCPP_DEBUG_LEVEL == 2
47       if (!__libcpp_is_constant_evaluated())
48         __get_db()->__insert_i(this);
49 #endif
50     }
51     template <class _Up> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
52         __wrap_iter(const __wrap_iter<_Up>& __u,
53             typename enable_if<is_convertible<_Up, iterator_type>::value>::type* = nullptr) _NOEXCEPT
54             : __i(__u.base())
55     {
56 #if _LIBCPP_DEBUG_LEVEL == 2
57       if (!__libcpp_is_constant_evaluated())
58         __get_db()->__iterator_copy(this, _VSTD::addressof(__u));
59 #endif
60     }
61 #if _LIBCPP_DEBUG_LEVEL == 2
62     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
63     __wrap_iter(const __wrap_iter& __x)
64         : __i(__x.base())
65     {
66       if (!__libcpp_is_constant_evaluated())
67         __get_db()->__iterator_copy(this, _VSTD::addressof(__x));
68     }
69     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
70     __wrap_iter& operator=(const __wrap_iter& __x)
71     {
72         if (this != _VSTD::addressof(__x) && !__libcpp_is_constant_evaluated())
73         {
74             __get_db()->__iterator_copy(this, _VSTD::addressof(__x));
75             __i = __x.__i;
76         }
77         return *this;
78     }
79     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
80     ~__wrap_iter()
81     {
82       if (!__libcpp_is_constant_evaluated())
83         __get_db()->__erase_i(this);
84     }
85 #endif
86     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 reference operator*() const _NOEXCEPT
87     {
88 #if _LIBCPP_DEBUG_LEVEL == 2
89       if (!__libcpp_is_constant_evaluated())
90         _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
91                        "Attempted to dereference a non-dereferenceable iterator");
92 #endif
93         return *__i;
94     }
95     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 pointer operator->() const _NOEXCEPT
96     {
97 #if _LIBCPP_DEBUG_LEVEL == 2
98       if (!__libcpp_is_constant_evaluated())
99         _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
100                        "Attempted to dereference a non-dereferenceable iterator");
101 #endif
102         return _VSTD::__to_address(__i);
103     }
104     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 __wrap_iter& operator++() _NOEXCEPT
105     {
106 #if _LIBCPP_DEBUG_LEVEL == 2
107       if (!__libcpp_is_constant_evaluated())
108         _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
109                        "Attempted to increment a non-incrementable iterator");
110 #endif
111         ++__i;
112         return *this;
113     }
114     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 __wrap_iter  operator++(int) _NOEXCEPT
115         {__wrap_iter __tmp(*this); ++(*this); return __tmp;}
116 
117     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 __wrap_iter& operator--() _NOEXCEPT
118     {
119 #if _LIBCPP_DEBUG_LEVEL == 2
120       if (!__libcpp_is_constant_evaluated())
121         _LIBCPP_ASSERT(__get_const_db()->__decrementable(this),
122                        "Attempted to decrement a non-decrementable iterator");
123 #endif
124         --__i;
125         return *this;
126     }
127     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 __wrap_iter  operator--(int) _NOEXCEPT
128         {__wrap_iter __tmp(*this); --(*this); return __tmp;}
129     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 __wrap_iter  operator+ (difference_type __n) const _NOEXCEPT
130         {__wrap_iter __w(*this); __w += __n; return __w;}
131     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 __wrap_iter& operator+=(difference_type __n) _NOEXCEPT
132     {
133 #if _LIBCPP_DEBUG_LEVEL == 2
134       if (!__libcpp_is_constant_evaluated())
135         _LIBCPP_ASSERT(__get_const_db()->__addable(this, __n),
136                    "Attempted to add/subtract an iterator outside its valid range");
137 #endif
138         __i += __n;
139         return *this;
140     }
141     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 __wrap_iter  operator- (difference_type __n) const _NOEXCEPT
142         {return *this + (-__n);}
143     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 __wrap_iter& operator-=(difference_type __n) _NOEXCEPT
144         {*this += -__n; return *this;}
145     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 reference    operator[](difference_type __n) const _NOEXCEPT
146     {
147 #if _LIBCPP_DEBUG_LEVEL == 2
148       if (!__libcpp_is_constant_evaluated())
149         _LIBCPP_ASSERT(__get_const_db()->__subscriptable(this, __n),
150                    "Attempted to subscript an iterator outside its valid range");
151 #endif
152         return __i[__n];
153     }
154 
155     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 iterator_type base() const _NOEXCEPT {return __i;}
156 
157 private:
158 #if _LIBCPP_DEBUG_LEVEL == 2
159     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 __wrap_iter(const void* __p, iterator_type __x) : __i(__x)
160     {
161       if (!__libcpp_is_constant_evaluated())
162         __get_db()->__insert_ic(this, __p);
163     }
164 #else
165     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 __wrap_iter(iterator_type __x) _NOEXCEPT : __i(__x) {}
166 #endif
167 
168     template <class _Up> friend class __wrap_iter;
169     template <class _CharT, class _Traits, class _Alloc> friend class basic_string;
170     template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector;
171     template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span;
172 };
173 
174 template <class _Iter1>
175 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
176 bool operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
177 {
178     return __x.base() == __y.base();
179 }
180 
181 template <class _Iter1, class _Iter2>
182 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
183 bool operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
184 {
185     return __x.base() == __y.base();
186 }
187 
188 template <class _Iter1>
189 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
190 bool operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
191 {
192 #if _LIBCPP_DEBUG_LEVEL == 2
193   if (!__libcpp_is_constant_evaluated())
194     _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(_VSTD::addressof(__x), _VSTD::addressof(__y)),
195                    "Attempted to compare incomparable iterators");
196 #endif
197     return __x.base() < __y.base();
198 }
199 
200 template <class _Iter1, class _Iter2>
201 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
202 bool operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
203 {
204 #if _LIBCPP_DEBUG_LEVEL == 2
205   if (!__libcpp_is_constant_evaluated())
206     _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y),
207                    "Attempted to compare incomparable iterators");
208 #endif
209     return __x.base() < __y.base();
210 }
211 
212 template <class _Iter1>
213 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
214 bool operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
215 {
216     return !(__x == __y);
217 }
218 
219 template <class _Iter1, class _Iter2>
220 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
221 bool operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
222 {
223     return !(__x == __y);
224 }
225 
226 template <class _Iter1>
227 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
228 bool operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
229 {
230     return __y < __x;
231 }
232 
233 template <class _Iter1, class _Iter2>
234 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
235 bool operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
236 {
237     return __y < __x;
238 }
239 
240 template <class _Iter1>
241 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
242 bool operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
243 {
244     return !(__x < __y);
245 }
246 
247 template <class _Iter1, class _Iter2>
248 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
249 bool operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
250 {
251     return !(__x < __y);
252 }
253 
254 template <class _Iter1>
255 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
256 bool operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT
257 {
258     return !(__y < __x);
259 }
260 
261 template <class _Iter1, class _Iter2>
262 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
263 bool operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
264 {
265     return !(__y < __x);
266 }
267 
268 template <class _Iter1, class _Iter2>
269 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
270 #ifndef _LIBCPP_CXX03_LANG
271 auto operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
272     -> decltype(__x.base() - __y.base())
273 #else
274 typename __wrap_iter<_Iter1>::difference_type
275 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
276 #endif // C++03
277 {
278 #if _LIBCPP_DEBUG_LEVEL == 2
279   if (!__libcpp_is_constant_evaluated())
280     _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(_VSTD::addressof(__x), _VSTD::addressof(__y)),
281                    "Attempted to subtract incompatible iterators");
282 #endif
283     return __x.base() - __y.base();
284 }
285 
286 template <class _Iter1>
287 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
288 __wrap_iter<_Iter1> operator+(typename __wrap_iter<_Iter1>::difference_type __n, __wrap_iter<_Iter1> __x) _NOEXCEPT
289 {
290     __x += __n;
291     return __x;
292 }
293 
294 #if _LIBCPP_STD_VER <= 17
295 template <class _It>
296 struct __is_cpp17_contiguous_iterator<__wrap_iter<_It> > : true_type {};
297 #endif
298 
299 template <class _It>
300 struct _LIBCPP_TEMPLATE_VIS pointer_traits<__wrap_iter<_It> >
301 {
302     typedef __wrap_iter<_It> pointer;
303     typedef typename pointer_traits<_It>::element_type element_type;
304     typedef typename pointer_traits<_It>::difference_type difference_type;
305 
306     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
307     static element_type *to_address(pointer __w) _NOEXCEPT {
308         return _VSTD::__to_address(__w.base());
309     }
310 };
311 
312 _LIBCPP_END_NAMESPACE_STD
313 
314 #endif // _LIBCPP___ITERATOR_WRAP_ITER_H
315