wrap_iter.h 10 KB

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