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_COPY_MOVE_COMMON_H 10 #define _LIBCPP___ALGORITHM_COPY_MOVE_COMMON_H 11 12 #include <__algorithm/unwrap_iter.h> 13 #include <__algorithm/unwrap_range.h> 14 #include <__config> 15 #include <__cstddef/size_t.h> 16 #include <__iterator/iterator_traits.h> 17 #include <__memory/pointer_traits.h> 18 #include <__string/constexpr_c_functions.h> 19 #include <__type_traits/enable_if.h> 20 #include <__type_traits/is_always_bitcastable.h> 21 #include <__type_traits/is_constant_evaluated.h> 22 #include <__type_traits/is_constructible.h> 23 #include <__type_traits/is_trivially_assignable.h> 24 #include <__type_traits/is_volatile.h> 25 #include <__utility/move.h> 26 #include <__utility/pair.h> 27 28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 29 # pragma GCC system_header 30 #endif 31 32 _LIBCPP_PUSH_MACROS 33 #include <__undef_macros> 34 35 _LIBCPP_BEGIN_NAMESPACE_STD 36 37 // Type traits. 38 39 template <class _From, class _To> 40 struct __can_lower_copy_assignment_to_memmove { 41 static const bool value = 42 // If the types are always bitcastable, it's valid to do a bitwise copy between them. 43 __is_always_bitcastable<_From, _To>::value && 44 // Reject conversions that wouldn't be performed by the regular built-in assignment (e.g. between arrays). 45 is_trivially_assignable<_To&, const _From&>::value && 46 // `memmove` doesn't accept `volatile` pointers, make sure the optimization SFINAEs away in that case. 47 !is_volatile<_From>::value && !is_volatile<_To>::value; 48 }; 49 50 template <class _From, class _To> 51 struct __can_lower_move_assignment_to_memmove { 52 static const bool value = 53 __is_always_bitcastable<_From, _To>::value && is_trivially_assignable<_To&, _From&&>::value && 54 !is_volatile<_From>::value && !is_volatile<_To>::value; 55 }; 56 57 // `memmove` algorithms implementation. 58 59 template <class _In, class _Out> 60 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*> 61 __copy_trivial_impl(_In* __first, _In* __last, _Out* __result) { 62 const size_t __n = static_cast<size_t>(__last - __first); 63 64 std::__constexpr_memmove(__result, __first, __element_count(__n)); 65 66 return std::make_pair(__last, __result + __n); 67 } 68 69 template <class _In, class _Out> 70 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*> 71 __copy_backward_trivial_impl(_In* __first, _In* __last, _Out* __result) { 72 const size_t __n = static_cast<size_t>(__last - __first); 73 __result -= __n; 74 75 std::__constexpr_memmove(__result, __first, __element_count(__n)); 76 77 return std::make_pair(__last, __result); 78 } 79 80 // Iterator unwrapping and dispatching to the correct overload. 81 82 template <class _InIter, class _OutIter> 83 struct __can_rewrap 84 : integral_constant<bool, is_copy_constructible<_InIter>::value && is_copy_constructible<_OutIter>::value> {}; 85 86 template <class _Algorithm, 87 class _InIter, 88 class _Sent, 89 class _OutIter, 90 __enable_if_t<__can_rewrap<_InIter, _OutIter>::value, int> = 0> 91 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 pair<_InIter, _OutIter> 92 __copy_move_unwrap_iters(_InIter __first, _Sent __last, _OutIter __out_first) { 93 auto __range = std::__unwrap_range(__first, std::move(__last)); 94 auto __result = _Algorithm()(std::move(__range.first), std::move(__range.second), std::__unwrap_iter(__out_first)); 95 return std::make_pair(std::__rewrap_range<_Sent>(std::move(__first), std::move(__result.first)), 96 std::__rewrap_iter(std::move(__out_first), std::move(__result.second))); 97 } 98 99 template <class _Algorithm, 100 class _InIter, 101 class _Sent, 102 class _OutIter, 103 __enable_if_t<!__can_rewrap<_InIter, _OutIter>::value, int> = 0> 104 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 pair<_InIter, _OutIter> 105 __copy_move_unwrap_iters(_InIter __first, _Sent __last, _OutIter __out_first) { 106 return _Algorithm()(std::move(__first), std::move(__last), std::move(__out_first)); 107 } 108 109 _LIBCPP_END_NAMESPACE_STD 110 111 _LIBCPP_POP_MACROS 112 113 #endif // _LIBCPP___ALGORITHM_COPY_MOVE_COMMON_H 114