Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:12

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___ALGORITHM_UNWRAP_ITER_H
0010 #define _LIBCPP___ALGORITHM_UNWRAP_ITER_H
0011 
0012 #include <__config>
0013 #include <__iterator/iterator_traits.h>
0014 #include <__memory/pointer_traits.h>
0015 #include <__type_traits/enable_if.h>
0016 #include <__type_traits/is_constructible.h>
0017 #include <__utility/declval.h>
0018 #include <__utility/move.h>
0019 
0020 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0021 #  pragma GCC system_header
0022 #endif
0023 
0024 _LIBCPP_PUSH_MACROS
0025 #include <__undef_macros>
0026 
0027 _LIBCPP_BEGIN_NAMESPACE_STD
0028 
0029 // TODO: Change the name of __unwrap_iter_impl to something more appropriate
0030 // The job of __unwrap_iter is to remove iterator wrappers (like reverse_iterator or __wrap_iter),
0031 // to reduce the number of template instantiations and to enable pointer-based optimizations e.g. in std::copy.
0032 //
0033 // Some algorithms (e.g. std::copy, but not std::sort) need to convert an
0034 // "unwrapped" result back into the original iterator type. Doing that is the job of __rewrap_iter.
0035 
0036 // Default case - we can't unwrap anything
0037 template <class _Iter, bool = __libcpp_is_contiguous_iterator<_Iter>::value>
0038 struct __unwrap_iter_impl {
0039   static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap(_Iter, _Iter __iter) { return __iter; }
0040   static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __unwrap(_Iter __i) _NOEXCEPT { return __i; }
0041 };
0042 
0043 // TODO(hardening): make sure that the following unwrapping doesn't unexpectedly turn hardened iterators into raw
0044 // pointers.
0045 
0046 // It's a contiguous iterator, so we can use a raw pointer instead
0047 template <class _Iter>
0048 struct __unwrap_iter_impl<_Iter, true> {
0049   using _ToAddressT _LIBCPP_NODEBUG = decltype(std::__to_address(std::declval<_Iter>()));
0050 
0051   static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap(_Iter __orig_iter, _ToAddressT __unwrapped_iter) {
0052     return __orig_iter + (__unwrapped_iter - std::__to_address(__orig_iter));
0053   }
0054 
0055   static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToAddressT __unwrap(_Iter __i) _NOEXCEPT {
0056     return std::__to_address(__i);
0057   }
0058 };
0059 
0060 template <class _Iter,
0061           class _Impl                                             = __unwrap_iter_impl<_Iter>,
0062           __enable_if_t<is_copy_constructible<_Iter>::value, int> = 0>
0063 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 decltype(_Impl::__unwrap(std::declval<_Iter>()))
0064 __unwrap_iter(_Iter __i) _NOEXCEPT {
0065   return _Impl::__unwrap(__i);
0066 }
0067 
0068 // Allow input_iterators to be passed to __unwrap_iter (but not __rewrap_iter)
0069 #if _LIBCPP_STD_VER >= 20
0070 template <class _Iter, __enable_if_t<!is_copy_constructible<_Iter>::value, int> = 0>
0071 inline _LIBCPP_HIDE_FROM_ABI constexpr _Iter __unwrap_iter(_Iter __i) noexcept {
0072   return __i;
0073 }
0074 #endif
0075 
0076 template <class _OrigIter, class _Iter, class _Impl = __unwrap_iter_impl<_OrigIter> >
0077 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _OrigIter __rewrap_iter(_OrigIter __orig_iter, _Iter __iter) _NOEXCEPT {
0078   return _Impl::__rewrap(std::move(__orig_iter), std::move(__iter));
0079 }
0080 
0081 _LIBCPP_END_NAMESPACE_STD
0082 
0083 _LIBCPP_POP_MACROS
0084 
0085 #endif // _LIBCPP___ALGORITHM_UNWRAP_ITER_H