1fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 2fe6060f1SDimitry Andric // 3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6fe6060f1SDimitry Andric // 7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 8fe6060f1SDimitry Andric 9fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_UNWRAP_ITER_H 10fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_UNWRAP_ITER_H 11fe6060f1SDimitry Andric 12fe6060f1SDimitry Andric #include <__config> 1381ad6265SDimitry Andric #include <__iterator/iterator_traits.h> 14fe6060f1SDimitry Andric #include <__memory/pointer_traits.h> 15bdd1243dSDimitry Andric #include <__type_traits/enable_if.h> 16*0fca6ea1SDimitry Andric #include <__type_traits/is_constructible.h> 17bdd1243dSDimitry Andric #include <__utility/declval.h> 18fcaf7f86SDimitry Andric #include <__utility/move.h> 19fe6060f1SDimitry Andric 20fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 21fe6060f1SDimitry Andric # pragma GCC system_header 22fe6060f1SDimitry Andric #endif 23fe6060f1SDimitry Andric 2406c3fb27SDimitry Andric _LIBCPP_PUSH_MACROS 2506c3fb27SDimitry Andric #include <__undef_macros> 2606c3fb27SDimitry Andric 27fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 28fe6060f1SDimitry Andric 29fcaf7f86SDimitry Andric // TODO: Change the name of __unwrap_iter_impl to something more appropriate 30fcaf7f86SDimitry Andric // The job of __unwrap_iter is to remove iterator wrappers (like reverse_iterator or __wrap_iter), 31fcaf7f86SDimitry Andric // to reduce the number of template instantiations and to enable pointer-based optimizations e.g. in std::copy. 32fe6060f1SDimitry Andric // 33fe6060f1SDimitry Andric // Some algorithms (e.g. std::copy, but not std::sort) need to convert an 34fcaf7f86SDimitry Andric // "unwrapped" result back into the original iterator type. Doing that is the job of __rewrap_iter. 35fe6060f1SDimitry Andric 36fcaf7f86SDimitry Andric // Default case - we can't unwrap anything 3706c3fb27SDimitry Andric template <class _Iter, bool = __libcpp_is_contiguous_iterator<_Iter>::value> 38fe6060f1SDimitry Andric struct __unwrap_iter_impl { __rewrap__unwrap_iter_impl39fcaf7f86SDimitry Andric static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap(_Iter, _Iter __iter) { return __iter; } __unwrap__unwrap_iter_impl40fcaf7f86SDimitry Andric static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __unwrap(_Iter __i) _NOEXCEPT { return __i; } 41fe6060f1SDimitry Andric }; 42fe6060f1SDimitry Andric 4306c3fb27SDimitry Andric // TODO(hardening): make sure that the following unwrapping doesn't unexpectedly turn hardened iterators into raw 4406c3fb27SDimitry Andric // pointers. 45fe6060f1SDimitry Andric 46fcaf7f86SDimitry Andric // It's a contiguous iterator, so we can use a raw pointer instead 47fe6060f1SDimitry Andric template <class _Iter> 48fe6060f1SDimitry Andric struct __unwrap_iter_impl<_Iter, true> { 49fcaf7f86SDimitry Andric using _ToAddressT = decltype(std::__to_address(std::declval<_Iter>())); 50fcaf7f86SDimitry Andric 51fcaf7f86SDimitry Andric static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap(_Iter __orig_iter, _ToAddressT __unwrapped_iter) { 52fcaf7f86SDimitry Andric return __orig_iter + (__unwrapped_iter - std::__to_address(__orig_iter)); 53fcaf7f86SDimitry Andric } 54fcaf7f86SDimitry Andric 55fcaf7f86SDimitry Andric static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToAddressT __unwrap(_Iter __i) _NOEXCEPT { 56fcaf7f86SDimitry Andric return std::__to_address(__i); 57fe6060f1SDimitry Andric } 58fe6060f1SDimitry Andric }; 59fe6060f1SDimitry Andric 60fcaf7f86SDimitry Andric template <class _Iter, 61fcaf7f86SDimitry Andric class _Impl = __unwrap_iter_impl<_Iter>, 62fcaf7f86SDimitry Andric __enable_if_t<is_copy_constructible<_Iter>::value, int> = 0> 63cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 decltype(_Impl::__unwrap(std::declval<_Iter>())) 64cb14a3feSDimitry Andric __unwrap_iter(_Iter __i) _NOEXCEPT { 65fcaf7f86SDimitry Andric return _Impl::__unwrap(__i); 66fe6060f1SDimitry Andric } 67fe6060f1SDimitry Andric 6806c3fb27SDimitry Andric // Allow input_iterators to be passed to __unwrap_iter (but not __rewrap_iter) 6906c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20 7006c3fb27SDimitry Andric template <class _Iter, __enable_if_t<!is_copy_constructible<_Iter>::value, int> = 0> 7106c3fb27SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI constexpr _Iter __unwrap_iter(_Iter __i) noexcept { 7206c3fb27SDimitry Andric return __i; 7306c3fb27SDimitry Andric } 7406c3fb27SDimitry Andric #endif 7506c3fb27SDimitry Andric 76fcaf7f86SDimitry Andric template <class _OrigIter, class _Iter, class _Impl = __unwrap_iter_impl<_OrigIter> > 77fcaf7f86SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _OrigIter __rewrap_iter(_OrigIter __orig_iter, _Iter __iter) _NOEXCEPT { 78fcaf7f86SDimitry Andric return _Impl::__rewrap(std::move(__orig_iter), std::move(__iter)); 79fe6060f1SDimitry Andric } 80fe6060f1SDimitry Andric 81fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD 82fe6060f1SDimitry Andric 83b3edf446SDimitry Andric _LIBCPP_POP_MACROS 8406c3fb27SDimitry Andric 85fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_UNWRAP_ITER_H 86