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