2021-06-17 23:30:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP___ALGORITHM_UNWRAP_ITER_H
|
|
|
|
#define _LIBCPP___ALGORITHM_UNWRAP_ITER_H
|
|
|
|
|
|
|
|
#include <__config>
|
2022-06-11 01:53:10 +08:00
|
|
|
#include <__iterator/iterator_traits.h>
|
2021-06-17 23:30:11 +08:00
|
|
|
#include <__memory/pointer_traits.h>
|
2022-07-12 21:29:07 +08:00
|
|
|
#include <__utility/move.h>
|
2021-06-17 23:30:11 +08:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-02 09:16:40 +08:00
|
|
|
# pragma GCC system_header
|
2021-06-17 23:30:11 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2022-07-12 21:29:07 +08:00
|
|
|
// TODO: Change the name of __unwrap_iter_impl to something more appropriate
|
|
|
|
// The job of __unwrap_iter is to remove iterator wrappers (like reverse_iterator or __wrap_iter),
|
|
|
|
// to reduce the number of template instantiations and to enable pointer-based optimizations e.g. in std::copy.
|
2021-06-17 23:30:11 +08:00
|
|
|
// In debug mode, we don't do this.
|
|
|
|
//
|
|
|
|
// Some algorithms (e.g. std::copy, but not std::sort) need to convert an
|
2022-07-12 21:29:07 +08:00
|
|
|
// "unwrapped" result back into the original iterator type. Doing that is the job of __rewrap_iter.
|
2021-06-17 23:30:11 +08:00
|
|
|
|
2022-07-12 21:29:07 +08:00
|
|
|
// Default case - we can't unwrap anything
|
2021-06-17 23:30:11 +08:00
|
|
|
template <class _Iter, bool = __is_cpp17_contiguous_iterator<_Iter>::value>
|
|
|
|
struct __unwrap_iter_impl {
|
2022-07-12 21:29:07 +08:00
|
|
|
static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap(_Iter, _Iter __iter) { return __iter; }
|
|
|
|
static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __unwrap(_Iter __i) _NOEXCEPT { return __i; }
|
2021-06-17 23:30:11 +08:00
|
|
|
};
|
|
|
|
|
[libc++] Make the Debug mode a configuration-time only option
The debug mode has been broken pretty much ever since it was shipped
because it was possible to enable the debug mode in user code without
actually enabling it in the dylib, leading to ODR violations that
caused various kinds of failures.
This commit makes the debug mode a knob that is configured when
building the library and which can't be changed afterwards. This is
less flexible for users, however it will actually work as intended
and it will allow us, in the future, to add various kinds of checks
that do not assume the same ABI as the normal library. Furthermore,
this will make the debug mode more robust, which means that vendors
might be more tempted to support it properly, which hasn't been the
case with the current debug mode.
This patch shouldn't break any user code, except folks who are building
against a library that doesn't have the debug mode enabled and who try
to enable the debug mode in their code. Such users will get a compile-time
error explaining that this configuration isn't supported anymore.
In the future, we should further increase the granularity of the debug
mode checks so that we can cherry-pick which checks to enable, like we
do for unspecified behavior randomization.
Differential Revision: https://reviews.llvm.org/D122941
2022-04-02 04:38:30 +08:00
|
|
|
#ifndef _LIBCPP_ENABLE_DEBUG_MODE
|
2021-06-17 23:30:11 +08:00
|
|
|
|
2022-07-12 21:29:07 +08:00
|
|
|
// It's a contiguous iterator, so we can use a raw pointer instead
|
2021-06-17 23:30:11 +08:00
|
|
|
template <class _Iter>
|
|
|
|
struct __unwrap_iter_impl<_Iter, true> {
|
2022-07-12 21:29:07 +08:00
|
|
|
using _ToAddressT = decltype(std::__to_address(std::declval<_Iter>()));
|
2021-06-17 23:30:11 +08:00
|
|
|
|
2022-07-12 21:29:07 +08:00
|
|
|
static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Iter __rewrap(_Iter __orig_iter, _ToAddressT __unwrapped_iter) {
|
|
|
|
return __orig_iter + (__unwrapped_iter - std::__to_address(__orig_iter));
|
2022-06-17 21:59:53 +08:00
|
|
|
}
|
|
|
|
|
2022-07-12 21:29:07 +08:00
|
|
|
static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToAddressT __unwrap(_Iter __i) _NOEXCEPT {
|
|
|
|
return std::__to_address(__i);
|
2022-06-17 21:59:53 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-12 21:29:07 +08:00
|
|
|
#endif // !_LIBCPP_ENABLE_DEBUG_MODE
|
|
|
|
|
|
|
|
template<class _Iter,
|
|
|
|
class _Impl = __unwrap_iter_impl<_Iter>,
|
|
|
|
__enable_if_t<is_copy_constructible<_Iter>::value, int> = 0>
|
2022-08-19 19:08:01 +08:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
|
2022-07-12 21:29:07 +08:00
|
|
|
decltype(_Impl::__unwrap(std::declval<_Iter>())) __unwrap_iter(_Iter __i) _NOEXCEPT {
|
|
|
|
return _Impl::__unwrap(__i);
|
2021-06-17 23:30:11 +08:00
|
|
|
}
|
|
|
|
|
2022-07-12 21:29:07 +08:00
|
|
|
template <class _OrigIter, class _Iter, class _Impl = __unwrap_iter_impl<_OrigIter> >
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _OrigIter __rewrap_iter(_OrigIter __orig_iter, _Iter __iter) _NOEXCEPT {
|
|
|
|
return _Impl::__rewrap(std::move(__orig_iter), std::move(__iter));
|
2021-06-17 23:30:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
|
|
|
#endif // _LIBCPP___ALGORITHM_UNWRAP_ITER_H
|