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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___ALGORITHM_UNWRAP_ITER_H
10 #define _LIBCPP___ALGORITHM_UNWRAP_ITER_H
11 
12 #include <__config>
13 #include <__iterator/iterator_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>
18 #include <__utility/move.h>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 // TODO: Change the name of __unwrap_iter_impl to something more appropriate
27 // The job of __unwrap_iter is to remove iterator wrappers (like reverse_iterator or __wrap_iter),
28 // to reduce the number of template instantiations and to enable pointer-based optimizations e.g. in std::copy.
29 // In debug mode, we don't do this.
30 //
31 // Some algorithms (e.g. std::copy, but not std::sort) need to convert an
32 // "unwrapped" result back into the original iterator type. Doing that is the job of __rewrap_iter.
33 
34 // Default case - we can't unwrap anything
35 template <class _Iter, bool = __is_cpp17_contiguous_iterator<_Iter>::value>
36 struct __unwrap_iter_impl {
37   static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap(_Iter, _Iter __iter) { return __iter; }
38   static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __unwrap(_Iter __i) _NOEXCEPT { return __i; }
39 };
40 
41 #ifndef _LIBCPP_ENABLE_DEBUG_MODE
42 
43 // It's a contiguous iterator, so we can use a raw pointer instead
44 template <class _Iter>
45 struct __unwrap_iter_impl<_Iter, true> {
46   using _ToAddressT = decltype(std::__to_address(std::declval<_Iter>()));
47 
48   static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap(_Iter __orig_iter, _ToAddressT __unwrapped_iter) {
49     return __orig_iter + (__unwrapped_iter - std::__to_address(__orig_iter));
50   }
51 
52   static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToAddressT __unwrap(_Iter __i) _NOEXCEPT {
53     return std::__to_address(__i);
54   }
55 };
56 
57 #endif // !_LIBCPP_ENABLE_DEBUG_MODE
58 
59 template<class _Iter,
60          class _Impl = __unwrap_iter_impl<_Iter>,
61          __enable_if_t<is_copy_constructible<_Iter>::value, int> = 0>
62 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
63 decltype(_Impl::__unwrap(std::declval<_Iter>())) __unwrap_iter(_Iter __i) _NOEXCEPT {
64   return _Impl::__unwrap(__i);
65 }
66 
67 template <class _OrigIter, class _Iter, class _Impl = __unwrap_iter_impl<_OrigIter> >
68 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _OrigIter __rewrap_iter(_OrigIter __orig_iter, _Iter __iter) _NOEXCEPT {
69   return _Impl::__rewrap(std::move(__orig_iter), std::move(__iter));
70 }
71 
72 _LIBCPP_END_NAMESPACE_STD
73 
74 #endif // _LIBCPP___ALGORITHM_UNWRAP_ITER_H
75