unwrap_iter.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #ifndef _LIBCPP___ALGORITHM_UNWRAP_ITER_H
  9. #define _LIBCPP___ALGORITHM_UNWRAP_ITER_H
  10. #include <__config>
  11. #include <__iterator/iterator_traits.h>
  12. #include <__memory/pointer_traits.h>
  13. #include <__type_traits/enable_if.h>
  14. #include <__type_traits/is_copy_constructible.h>
  15. #include <__utility/declval.h>
  16. #include <__utility/move.h>
  17. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  18. # pragma GCC system_header
  19. #endif
  20. _LIBCPP_PUSH_MACROS
  21. #include <__undef_macros>
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. // TODO: Change the name of __unwrap_iter_impl to something more appropriate
  24. // The job of __unwrap_iter is to remove iterator wrappers (like reverse_iterator or __wrap_iter),
  25. // to reduce the number of template instantiations and to enable pointer-based optimizations e.g. in std::copy.
  26. //
  27. // Some algorithms (e.g. std::copy, but not std::sort) need to convert an
  28. // "unwrapped" result back into the original iterator type. Doing that is the job of __rewrap_iter.
  29. // Default case - we can't unwrap anything
  30. template <class _Iter, bool = __libcpp_is_contiguous_iterator<_Iter>::value>
  31. struct __unwrap_iter_impl {
  32. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap(_Iter, _Iter __iter) { return __iter; }
  33. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __unwrap(_Iter __i) _NOEXCEPT { return __i; }
  34. };
  35. // TODO(hardening): make sure that the following unwrapping doesn't unexpectedly turn hardened iterators into raw
  36. // pointers.
  37. // It's a contiguous iterator, so we can use a raw pointer instead
  38. template <class _Iter>
  39. struct __unwrap_iter_impl<_Iter, true> {
  40. using _ToAddressT = decltype(std::__to_address(std::declval<_Iter>()));
  41. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap(_Iter __orig_iter, _ToAddressT __unwrapped_iter) {
  42. return __orig_iter + (__unwrapped_iter - std::__to_address(__orig_iter));
  43. }
  44. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToAddressT __unwrap(_Iter __i) _NOEXCEPT {
  45. return std::__to_address(__i);
  46. }
  47. };
  48. template <class _Iter,
  49. class _Impl = __unwrap_iter_impl<_Iter>,
  50. __enable_if_t<is_copy_constructible<_Iter>::value, int> = 0>
  51. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 decltype(_Impl::__unwrap(std::declval<_Iter>()))
  52. __unwrap_iter(_Iter __i) _NOEXCEPT {
  53. return _Impl::__unwrap(__i);
  54. }
  55. // Allow input_iterators to be passed to __unwrap_iter (but not __rewrap_iter)
  56. #if _LIBCPP_STD_VER >= 20
  57. template <class _Iter, __enable_if_t<!is_copy_constructible<_Iter>::value, int> = 0>
  58. inline _LIBCPP_HIDE_FROM_ABI constexpr _Iter __unwrap_iter(_Iter __i) noexcept {
  59. return __i;
  60. }
  61. #endif
  62. template <class _OrigIter, class _Iter, class _Impl = __unwrap_iter_impl<_OrigIter> >
  63. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _OrigIter __rewrap_iter(_OrigIter __orig_iter, _Iter __iter) _NOEXCEPT {
  64. return _Impl::__rewrap(std::move(__orig_iter), std::move(__iter));
  65. }
  66. _LIBCPP_END_NAMESPACE_STD
  67. _LIBCPP_POP_MACROS
  68. #endif // _LIBCPP___ALGORITHM_UNWRAP_ITER_H