wrap_iter.h 7.6 KB

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