wrap_iter.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. 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 >= 20
  32. typedef contiguous_iterator_tag iterator_concept;
  33. #endif
  34. private:
  35. iterator_type __i_;
  36. public:
  37. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter() _NOEXCEPT : __i_() {}
  38. template <class _Up, __enable_if_t<is_convertible<_Up, iterator_type>::value, int> = 0>
  39. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter(const __wrap_iter<_Up>& __u) _NOEXCEPT
  40. : __i_(__u.base()) {}
  41. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT { return *__i_; }
  42. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT {
  43. return std::__to_address(__i_);
  44. }
  45. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator++() _NOEXCEPT {
  46. ++__i_;
  47. return *this;
  48. }
  49. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator++(int) _NOEXCEPT {
  50. __wrap_iter __tmp(*this);
  51. ++(*this);
  52. return __tmp;
  53. }
  54. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator--() _NOEXCEPT {
  55. --__i_;
  56. return *this;
  57. }
  58. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator--(int) _NOEXCEPT {
  59. __wrap_iter __tmp(*this);
  60. --(*this);
  61. return __tmp;
  62. }
  63. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator+(difference_type __n) const _NOEXCEPT {
  64. __wrap_iter __w(*this);
  65. __w += __n;
  66. return __w;
  67. }
  68. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator+=(difference_type __n) _NOEXCEPT {
  69. __i_ += __n;
  70. return *this;
  71. }
  72. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator-(difference_type __n) const _NOEXCEPT {
  73. return *this + (-__n);
  74. }
  75. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator-=(difference_type __n) _NOEXCEPT {
  76. *this += -__n;
  77. return *this;
  78. }
  79. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator[](difference_type __n) const _NOEXCEPT {
  80. return __i_[__n];
  81. }
  82. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 iterator_type base() const _NOEXCEPT { return __i_; }
  83. private:
  84. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __wrap_iter(iterator_type __x) _NOEXCEPT : __i_(__x) {}
  85. template <class _Up>
  86. friend class __wrap_iter;
  87. template <class _CharT, class _Traits, class _Alloc>
  88. friend class basic_string;
  89. template <class _CharT, class _Traits>
  90. friend class basic_string_view;
  91. template <class _Tp, class _Alloc>
  92. friend class _LIBCPP_TEMPLATE_VIS vector;
  93. template <class _Tp, size_t>
  94. friend class _LIBCPP_TEMPLATE_VIS span;
  95. template <class _Tp, size_t _Size>
  96. friend struct array;
  97. };
  98. template <class _Iter1>
  99. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  100. operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT {
  101. return __x.base() == __y.base();
  102. }
  103. template <class _Iter1, class _Iter2>
  104. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  105. operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
  106. return __x.base() == __y.base();
  107. }
  108. template <class _Iter1>
  109. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  110. operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT {
  111. return __x.base() < __y.base();
  112. }
  113. template <class _Iter1, class _Iter2>
  114. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  115. operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
  116. return __x.base() < __y.base();
  117. }
  118. template <class _Iter1>
  119. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  120. operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT {
  121. return !(__x == __y);
  122. }
  123. template <class _Iter1, class _Iter2>
  124. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  125. operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
  126. return !(__x == __y);
  127. }
  128. template <class _Iter1>
  129. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  130. operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT {
  131. return __y < __x;
  132. }
  133. template <class _Iter1, class _Iter2>
  134. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  135. operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
  136. return __y < __x;
  137. }
  138. template <class _Iter1>
  139. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  140. operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT {
  141. return !(__x < __y);
  142. }
  143. template <class _Iter1, class _Iter2>
  144. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  145. operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
  146. return !(__x < __y);
  147. }
  148. template <class _Iter1>
  149. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  150. operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT {
  151. return !(__y < __x);
  152. }
  153. template <class _Iter1, class _Iter2>
  154. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  155. operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
  156. return !(__y < __x);
  157. }
  158. template <class _Iter1, class _Iter2>
  159. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  160. #ifndef _LIBCPP_CXX03_LANG
  161. auto
  162. operator-(const __wrap_iter<_Iter1>& __x,
  163. const __wrap_iter<_Iter2>& __y) _NOEXCEPT->decltype(__x.base() - __y.base())
  164. #else
  165. typename __wrap_iter<_Iter1>::difference_type
  166. operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
  167. #endif // C++03
  168. {
  169. return __x.base() - __y.base();
  170. }
  171. template <class _Iter1>
  172. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter<_Iter1>
  173. operator+(typename __wrap_iter<_Iter1>::difference_type __n, __wrap_iter<_Iter1> __x) _NOEXCEPT {
  174. __x += __n;
  175. return __x;
  176. }
  177. #if _LIBCPP_STD_VER <= 17
  178. template <class _It>
  179. struct __libcpp_is_contiguous_iterator<__wrap_iter<_It> > : true_type {};
  180. #endif
  181. template <class _It>
  182. struct _LIBCPP_TEMPLATE_VIS pointer_traits<__wrap_iter<_It> > {
  183. typedef __wrap_iter<_It> pointer;
  184. typedef typename pointer_traits<_It>::element_type element_type;
  185. typedef typename pointer_traits<_It>::difference_type difference_type;
  186. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static element_type* to_address(pointer __w) _NOEXCEPT {
  187. return std::__to_address(__w.base());
  188. }
  189. };
  190. _LIBCPP_END_NAMESPACE_STD
  191. #endif // _LIBCPP___ITERATOR_WRAP_ITER_H