comparison libcxx/include/__algorithm/unwrap_iter.h @ 252:1f2b6ac9f198 llvm-original

LLVM16-1
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 18 Aug 2023 09:04:13 +0900
parents c4bab56944e8
children
comparison
equal deleted inserted replaced
237:c80f45b162ad 252:1f2b6ac9f198
10 #define _LIBCPP___ALGORITHM_UNWRAP_ITER_H 10 #define _LIBCPP___ALGORITHM_UNWRAP_ITER_H
11 11
12 #include <__config> 12 #include <__config>
13 #include <__iterator/iterator_traits.h> 13 #include <__iterator/iterator_traits.h>
14 #include <__memory/pointer_traits.h> 14 #include <__memory/pointer_traits.h>
15 #include <__type_traits/enable_if.h>
16 #include <__type_traits/is_copy_constructible.h>
17 #include <__utility/declval.h>
15 #include <__utility/move.h> 18 #include <__utility/move.h>
16 #include <type_traits>
17 19
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 # pragma GCC system_header 21 # pragma GCC system_header
20 #endif 22 #endif
23
24 _LIBCPP_PUSH_MACROS
25 #include <__undef_macros>
21 26
22 _LIBCPP_BEGIN_NAMESPACE_STD 27 _LIBCPP_BEGIN_NAMESPACE_STD
23 28
24 // TODO: Change the name of __unwrap_iter_impl to something more appropriate 29 // TODO: Change the name of __unwrap_iter_impl to something more appropriate
25 // The job of __unwrap_iter is to remove iterator wrappers (like reverse_iterator or __wrap_iter), 30 // The job of __unwrap_iter is to remove iterator wrappers (like reverse_iterator or __wrap_iter),
26 // to reduce the number of template instantiations and to enable pointer-based optimizations e.g. in std::copy. 31 // to reduce the number of template instantiations and to enable pointer-based optimizations e.g. in std::copy.
27 // In debug mode, we don't do this.
28 // 32 //
29 // Some algorithms (e.g. std::copy, but not std::sort) need to convert an 33 // Some algorithms (e.g. std::copy, but not std::sort) need to convert an
30 // "unwrapped" result back into the original iterator type. Doing that is the job of __rewrap_iter. 34 // "unwrapped" result back into the original iterator type. Doing that is the job of __rewrap_iter.
31 35
32 // Default case - we can't unwrap anything 36 // Default case - we can't unwrap anything
33 template <class _Iter, bool = __is_cpp17_contiguous_iterator<_Iter>::value> 37 template <class _Iter, bool = __libcpp_is_contiguous_iterator<_Iter>::value>
34 struct __unwrap_iter_impl { 38 struct __unwrap_iter_impl {
35 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap(_Iter, _Iter __iter) { return __iter; } 39 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap(_Iter, _Iter __iter) { return __iter; }
36 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __unwrap(_Iter __i) _NOEXCEPT { return __i; } 40 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __unwrap(_Iter __i) _NOEXCEPT { return __i; }
37 }; 41 };
38 42
39 #ifndef _LIBCPP_ENABLE_DEBUG_MODE 43 // TODO(hardening): make sure that the following unwrapping doesn't unexpectedly turn hardened iterators into raw
44 // pointers.
40 45
41 // It's a contiguous iterator, so we can use a raw pointer instead 46 // It's a contiguous iterator, so we can use a raw pointer instead
42 template <class _Iter> 47 template <class _Iter>
43 struct __unwrap_iter_impl<_Iter, true> { 48 struct __unwrap_iter_impl<_Iter, true> {
44 using _ToAddressT = decltype(std::__to_address(std::declval<_Iter>())); 49 using _ToAddressT = decltype(std::__to_address(std::declval<_Iter>()));
50 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToAddressT __unwrap(_Iter __i) _NOEXCEPT { 55 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToAddressT __unwrap(_Iter __i) _NOEXCEPT {
51 return std::__to_address(__i); 56 return std::__to_address(__i);
52 } 57 }
53 }; 58 };
54 59
55 #endif // !_LIBCPP_ENABLE_DEBUG_MODE
56
57 template<class _Iter, 60 template<class _Iter,
58 class _Impl = __unwrap_iter_impl<_Iter>, 61 class _Impl = __unwrap_iter_impl<_Iter>,
59 __enable_if_t<is_copy_constructible<_Iter>::value, int> = 0> 62 __enable_if_t<is_copy_constructible<_Iter>::value, int> = 0>
60 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 63 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
61 decltype(_Impl::__unwrap(std::declval<_Iter>())) __unwrap_iter(_Iter __i) _NOEXCEPT { 64 decltype(_Impl::__unwrap(std::declval<_Iter>())) __unwrap_iter(_Iter __i) _NOEXCEPT {
62 return _Impl::__unwrap(__i); 65 return _Impl::__unwrap(__i);
63 } 66 }
64 67
68 // Allow input_iterators to be passed to __unwrap_iter (but not __rewrap_iter)
69 #if _LIBCPP_STD_VER >= 20
70 template <class _Iter, __enable_if_t<!is_copy_constructible<_Iter>::value, int> = 0>
71 inline _LIBCPP_HIDE_FROM_ABI constexpr _Iter __unwrap_iter(_Iter __i) noexcept {
72 return __i;
73 }
74 #endif
75
65 template <class _OrigIter, class _Iter, class _Impl = __unwrap_iter_impl<_OrigIter> > 76 template <class _OrigIter, class _Iter, class _Impl = __unwrap_iter_impl<_OrigIter> >
66 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _OrigIter __rewrap_iter(_OrigIter __orig_iter, _Iter __iter) _NOEXCEPT { 77 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _OrigIter __rewrap_iter(_OrigIter __orig_iter, _Iter __iter) _NOEXCEPT {
67 return _Impl::__rewrap(std::move(__orig_iter), std::move(__iter)); 78 return _Impl::__rewrap(std::move(__orig_iter), std::move(__iter));
68 } 79 }
69 80
70 _LIBCPP_END_NAMESPACE_STD 81 _LIBCPP_END_NAMESPACE_STD
71 82
83 _LIBCPP_PUSH_MACROS
84
72 #endif // _LIBCPP___ALGORITHM_UNWRAP_ITER_H 85 #endif // _LIBCPP___ALGORITHM_UNWRAP_ITER_H