Path: blob/main/contrib/llvm-project/libcxx/include/__iterator/wrap_iter.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___ITERATOR_WRAP_ITER_H10#define _LIBCPP___ITERATOR_WRAP_ITER_H1112#include <__compare/ordering.h>13#include <__compare/three_way_comparable.h>14#include <__config>15#include <__iterator/iterator_traits.h>16#include <__memory/addressof.h>17#include <__memory/pointer_traits.h>18#include <__type_traits/enable_if.h>19#include <__type_traits/is_convertible.h>20#include <cstddef>2122#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)23# pragma GCC system_header24#endif2526_LIBCPP_BEGIN_NAMESPACE_STD2728template <class _Iter>29class __wrap_iter {30public:31typedef _Iter iterator_type;32typedef typename iterator_traits<iterator_type>::value_type value_type;33typedef typename iterator_traits<iterator_type>::difference_type difference_type;34typedef typename iterator_traits<iterator_type>::pointer pointer;35typedef typename iterator_traits<iterator_type>::reference reference;36typedef typename iterator_traits<iterator_type>::iterator_category iterator_category;37#if _LIBCPP_STD_VER >= 2038typedef contiguous_iterator_tag iterator_concept;39#endif4041private:42iterator_type __i_;4344public:45_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter() _NOEXCEPT : __i_() {}46template <class _Up, __enable_if_t<is_convertible<_Up, iterator_type>::value, int> = 0>47_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter(const __wrap_iter<_Up>& __u) _NOEXCEPT48: __i_(__u.base()) {}49_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT { return *__i_; }50_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT {51return std::__to_address(__i_);52}53_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator++() _NOEXCEPT {54++__i_;55return *this;56}57_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator++(int) _NOEXCEPT {58__wrap_iter __tmp(*this);59++(*this);60return __tmp;61}6263_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator--() _NOEXCEPT {64--__i_;65return *this;66}67_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator--(int) _NOEXCEPT {68__wrap_iter __tmp(*this);69--(*this);70return __tmp;71}72_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator+(difference_type __n) const _NOEXCEPT {73__wrap_iter __w(*this);74__w += __n;75return __w;76}77_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator+=(difference_type __n) _NOEXCEPT {78__i_ += __n;79return *this;80}81_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator-(difference_type __n) const _NOEXCEPT {82return *this + (-__n);83}84_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator-=(difference_type __n) _NOEXCEPT {85*this += -__n;86return *this;87}88_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator[](difference_type __n) const _NOEXCEPT {89return __i_[__n];90}9192_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 iterator_type base() const _NOEXCEPT { return __i_; }9394private:95_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __wrap_iter(iterator_type __x) _NOEXCEPT : __i_(__x) {}9697template <class _Up>98friend class __wrap_iter;99template <class _CharT, class _Traits, class _Alloc>100friend class basic_string;101template <class _CharT, class _Traits>102friend class basic_string_view;103template <class _Tp, class _Alloc>104friend class _LIBCPP_TEMPLATE_VIS vector;105template <class _Tp, size_t>106friend class _LIBCPP_TEMPLATE_VIS span;107template <class _Tp, size_t _Size>108friend struct array;109};110111template <class _Iter1>112_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool113operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT {114return __x.base() == __y.base();115}116117template <class _Iter1, class _Iter2>118_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool119operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {120return __x.base() == __y.base();121}122123template <class _Iter1>124_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool125operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT {126return __x.base() < __y.base();127}128129template <class _Iter1, class _Iter2>130_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool131operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {132return __x.base() < __y.base();133}134135#if _LIBCPP_STD_VER <= 17136template <class _Iter1>137_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool138operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT {139return !(__x == __y);140}141142template <class _Iter1, class _Iter2>143_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool144operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {145return !(__x == __y);146}147#endif148149// TODO(mordante) disable these overloads in the LLVM 20 release.150template <class _Iter1>151_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool152operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT {153return __y < __x;154}155156template <class _Iter1, class _Iter2>157_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool158operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {159return __y < __x;160}161162template <class _Iter1>163_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool164operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT {165return !(__x < __y);166}167168template <class _Iter1, class _Iter2>169_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool170operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {171return !(__x < __y);172}173174template <class _Iter1>175_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool176operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) _NOEXCEPT {177return !(__y < __x);178}179180template <class _Iter1, class _Iter2>181_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool182operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {183return !(__y < __x);184}185186#if _LIBCPP_STD_VER >= 20187template <class _Iter1, class _Iter2>188_LIBCPP_HIDE_FROM_ABI constexpr strong_ordering189operator<=>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) noexcept {190if constexpr (three_way_comparable_with<_Iter1, _Iter2, strong_ordering>) {191return __x.base() <=> __y.base();192} else {193if (__x.base() < __y.base())194return strong_ordering::less;195196if (__x.base() == __y.base())197return strong_ordering::equal;198199return strong_ordering::greater;200}201}202#endif // _LIBCPP_STD_VER >= 20203204template <class _Iter1, class _Iter2>205_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14206#ifndef _LIBCPP_CXX03_LANG207auto208operator-(const __wrap_iter<_Iter1>& __x,209const __wrap_iter<_Iter2>& __y) _NOEXCEPT->decltype(__x.base() - __y.base())210#else211typename __wrap_iter<_Iter1>::difference_type212operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT213#endif // C++03214{215return __x.base() - __y.base();216}217218template <class _Iter1>219_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter<_Iter1>220operator+(typename __wrap_iter<_Iter1>::difference_type __n, __wrap_iter<_Iter1> __x) _NOEXCEPT {221__x += __n;222return __x;223}224225#if _LIBCPP_STD_VER <= 17226template <class _It>227struct __libcpp_is_contiguous_iterator<__wrap_iter<_It> > : true_type {};228#endif229230template <class _It>231struct _LIBCPP_TEMPLATE_VIS pointer_traits<__wrap_iter<_It> > {232typedef __wrap_iter<_It> pointer;233typedef typename pointer_traits<_It>::element_type element_type;234typedef typename pointer_traits<_It>::difference_type difference_type;235236_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static element_type* to_address(pointer __w) _NOEXCEPT {237return std::__to_address(__w.base());238}239};240241_LIBCPP_END_NAMESPACE_STD242243#endif // _LIBCPP___ITERATOR_WRAP_ITER_H244245246