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 <__iterator/iterator_traits.h> 15 #include <__memory/addressof.h> 16 #include <__memory/pointer_traits.h> 17 #include <__type_traits/enable_if.h> 18 #include <__type_traits/is_convertible.h> 19 #include <cstddef> 20 21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 22 # pragma GCC system_header 23 #endif 24 25 _LIBCPP_BEGIN_NAMESPACE_STD 26 27 template <class _Iter> 28 class __wrap_iter 29 { 30 public: 31 typedef _Iter iterator_type; 32 typedef typename iterator_traits<iterator_type>::value_type value_type; 33 typedef typename iterator_traits<iterator_type>::difference_type difference_type; 34 typedef typename iterator_traits<iterator_type>::pointer pointer; 35 typedef typename iterator_traits<iterator_type>::reference reference; 36 typedef typename iterator_traits<iterator_type>::iterator_category iterator_category; 37 #if _LIBCPP_STD_VER >= 20 38 typedef contiguous_iterator_tag iterator_concept; 39 #endif 40 41 private: 42 iterator_type __i_; 43 public: __wrap_iter()44 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter() _NOEXCEPT 45 : __i_() 46 { 47 } 48 template <class _Up, __enable_if_t<is_convertible<_Up, iterator_type>::value, int> = 0> __wrap_iter(const __wrap_iter<_Up> & __u)49 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter(const __wrap_iter<_Up>& __u) _NOEXCEPT 50 : __i_(__u.base()) 51 { 52 } 53 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT 54 { 55 return *__i_; 56 } 57 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT 58 { 59 return _VSTD::__to_address(__i_); 60 } 61 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator++() _NOEXCEPT 62 { 63 ++__i_; 64 return *this; 65 } 66 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator++(int) _NOEXCEPT 67 {__wrap_iter __tmp(*this); ++(*this); return __tmp;} 68 69 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator--() _NOEXCEPT 70 { 71 --__i_; 72 return *this; 73 } 74 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator--(int) _NOEXCEPT 75 {__wrap_iter __tmp(*this); --(*this); return __tmp;} 76 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator+ (difference_type __n) const _NOEXCEPT 77 {__wrap_iter __w(*this); __w += __n; return __w;} 78 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator+=(difference_type __n) _NOEXCEPT 79 { 80 __i_ += __n; 81 return *this; 82 } 83 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator- (difference_type __n) const _NOEXCEPT 84 {return *this + (-__n);} 85 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator-=(difference_type __n) _NOEXCEPT 86 {*this += -__n; return *this;} 87 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator[](difference_type __n) const _NOEXCEPT 88 { 89 return __i_[__n]; 90 } 91 base()92 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 iterator_type base() const _NOEXCEPT {return __i_;} 93 94 private: 95 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter(iterator_type __x)96 explicit __wrap_iter(iterator_type __x) _NOEXCEPT : __i_(__x) 97 { 98 } 99 100 template <class _Up> friend class __wrap_iter; 101 template <class _CharT, class _Traits, class _Alloc> friend class basic_string; 102 template <class _Tp, class _Alloc> friend class _LIBCPP_TEMPLATE_VIS vector; 103 template <class _Tp, size_t> friend class _LIBCPP_TEMPLATE_VIS span; 104 }; 105 106 template <class _Iter1> 107 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 108 bool operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT 109 { 110 return __x.base() == __y.base(); 111 } 112 113 template <class _Iter1, class _Iter2> 114 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 115 bool operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT 116 { 117 return __x.base() == __y.base(); 118 } 119 120 template <class _Iter1> 121 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 122 bool operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT 123 { 124 return __x.base() < __y.base(); 125 } 126 127 template <class _Iter1, class _Iter2> 128 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 129 bool operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT 130 { 131 return __x.base() < __y.base(); 132 } 133 134 template <class _Iter1> 135 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 136 bool operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT 137 { 138 return !(__x == __y); 139 } 140 141 template <class _Iter1, class _Iter2> 142 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 143 bool operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT 144 { 145 return !(__x == __y); 146 } 147 148 template <class _Iter1> 149 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 150 bool operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT 151 { 152 return __y < __x; 153 } 154 155 template <class _Iter1, class _Iter2> 156 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 157 bool operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT 158 { 159 return __y < __x; 160 } 161 162 template <class _Iter1> 163 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 164 bool operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT 165 { 166 return !(__x < __y); 167 } 168 169 template <class _Iter1, class _Iter2> 170 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 171 bool operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT 172 { 173 return !(__x < __y); 174 } 175 176 template <class _Iter1> 177 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 178 bool operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT 179 { 180 return !(__y < __x); 181 } 182 183 template <class _Iter1, class _Iter2> 184 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 185 bool operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT 186 { 187 return !(__y < __x); 188 } 189 190 template <class _Iter1, class _Iter2> 191 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 192 #ifndef _LIBCPP_CXX03_LANG 193 auto operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT 194 -> decltype(__x.base() - __y.base()) 195 #else 196 typename __wrap_iter<_Iter1>::difference_type 197 operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT 198 #endif // C++03 199 { 200 return __x.base() - __y.base(); 201 } 202 203 template <class _Iter1> 204 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 205 __wrap_iter<_Iter1> operator+(typename __wrap_iter<_Iter1>::difference_type __n, __wrap_iter<_Iter1> __x) _NOEXCEPT 206 { 207 __x += __n; 208 return __x; 209 } 210 211 #if _LIBCPP_STD_VER <= 17 212 template <class _It> 213 struct __libcpp_is_contiguous_iterator<__wrap_iter<_It> > : true_type {}; 214 #endif 215 216 template <class _It> 217 struct _LIBCPP_TEMPLATE_VIS pointer_traits<__wrap_iter<_It> > 218 { 219 typedef __wrap_iter<_It> pointer; 220 typedef typename pointer_traits<_It>::element_type element_type; 221 typedef typename pointer_traits<_It>::difference_type difference_type; 222 223 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR 224 static element_type *to_address(pointer __w) _NOEXCEPT { 225 return _VSTD::__to_address(__w.base()); 226 } 227 }; 228 229 _LIBCPP_END_NAMESPACE_STD 230 231 #endif // _LIBCPP___ITERATOR_WRAP_ITER_H 232