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, class _Iter2> 168 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG 169 bool operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __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 #if _LIBCPP_DEBUG_LEVEL == 2 179 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y), 180 "Attempted to compare incomparable iterators"); 181 #endif 182 return __x.base() < __y.base(); 183 } 184 185 template <class _Iter1, class _Iter2> 186 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG 187 bool operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT 188 { 189 return !(__x == __y); 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 return __y < __x; 197 } 198 199 template <class _Iter1, class _Iter2> 200 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG 201 bool operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT 202 { 203 return !(__x < __y); 204 } 205 206 template <class _Iter1, class _Iter2> 207 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG 208 bool operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT 209 { 210 return !(__y < __x); 211 } 212 213 template <class _Iter1, class _Iter2> 214 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG 215 #ifndef _LIBCPP_CXX03_LANG 216 auto operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT 217 -> decltype(__x.base() - __y.base()) 218 #else 219 typename __wrap_iter<_Iter1>::difference_type 220 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT 221 #endif // C++03 222 { 223 #if _LIBCPP_DEBUG_LEVEL == 2 224 _LIBCPP_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y), 225 "Attempted to subtract incompatible iterators"); 226 #endif 227 return __x.base() - __y.base(); 228 } 229 230 template <class _Iter1> 231 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_IF_NODEBUG 232 __wrap_iter<_Iter1> operator+(typename __wrap_iter<_Iter1>::difference_type __n, __wrap_iter<_Iter1> __x) _NOEXCEPT 233 { 234 __x += __n; 235 return __x; 236 } 237 238 #if _LIBCPP_STD_VER <= 17 239 template <class _It> 240 struct __is_cpp17_contiguous_iterator<__wrap_iter<_It> > : true_type {}; 241 #endif 242 243 template <class _Iter> 244 _LIBCPP_CONSTEXPR 245 decltype(_VSTD::__to_address(declval<_Iter>())) 246 __to_address(__wrap_iter<_Iter> __w) _NOEXCEPT { 247 return _VSTD::__to_address(__w.base()); 248 } 249 250 _LIBCPP_END_NAMESPACE_STD 251 252 _LIBCPP_POP_MACROS 253 254 #endif // _LIBCPP___ITERATOR_WRAP_ITER_H 255