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/iterator_operations.h> 13 #include <__algorithm/unwrap_iter.h> 14 #include <__algorithm/unwrap_range.h> 15 #include <__config> 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_copy_constructible.h> 23 #include <__type_traits/is_trivially_assignable.h> 24 #include <__type_traits/is_trivially_copyable.h> 25 #include <__type_traits/is_volatile.h> 26 #include <__utility/move.h> 27 #include <__utility/pair.h> 28 #include <cstddef> 29 30 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 31 # pragma GCC system_header 32 #endif 33 34 _LIBCPP_BEGIN_NAMESPACE_STD 35 36 // Type traits. 37 38 template <class _From, class _To> 39 struct __can_lower_copy_assignment_to_memmove { 40 static const bool value = 41 // If the types are always bitcastable, it's valid to do a bitwise copy between them. 42 __is_always_bitcastable<_From, _To>::value && 43 // Reject conversions that wouldn't be performed by the regular built-in assignment (e.g. between arrays). 44 is_trivially_assignable<_To&, const _From&>::value && 45 // `memmove` doesn't accept `volatile` pointers, make sure the optimization SFINAEs away in that case. 46 !is_volatile<_From>::value && !is_volatile<_To>::value; 47 }; 48 49 template <class _From, class _To> 50 struct __can_lower_move_assignment_to_memmove { 51 static const bool value = 52 __is_always_bitcastable<_From, _To>::value && is_trivially_assignable<_To&, _From&&>::value && 53 !is_volatile<_From>::value && !is_volatile<_To>::value; 54 }; 55 56 // `memmove` algorithms implementation. 57 58 template <class _In, class _Out> 59 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*> 60 __copy_trivial_impl(_In* __first, _In* __last, _Out* __result) { 61 const size_t __n = static_cast<size_t>(__last - __first); 62 63 std::__constexpr_memmove(__result, __first, __element_count(__n)); 64 65 return std::make_pair(__last, __result + __n); 66 } 67 68 template <class _In, class _Out> 69 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*> 70 __copy_backward_trivial_impl(_In* __first, _In* __last, _Out* __result) { 71 const size_t __n = static_cast<size_t>(__last - __first); 72 __result -= __n; 73 74 std::__constexpr_memmove(__result, __first, __element_count(__n)); 75 76 return std::make_pair(__last, __result); 77 } 78 79 // Iterator unwrapping and dispatching to the correct overload. 80 81 template <class _F1, class _F2> 82 struct __overload : _F1, _F2 { 83 using _F1::operator(); 84 using _F2::operator(); 85 }; 86 87 template <class _InIter, class _Sent, class _OutIter, class = void> 88 struct __can_rewrap : false_type {}; 89 90 template <class _InIter, class _Sent, class _OutIter> 91 struct __can_rewrap<_InIter, 92 _Sent, 93 _OutIter, 94 // Note that sentinels are always copy-constructible. 95 __enable_if_t< is_copy_constructible<_InIter>::value && is_copy_constructible<_OutIter>::value > > 96 : true_type {}; 97 98 template <class _Algorithm, 99 class _InIter, 100 class _Sent, 101 class _OutIter, 102 __enable_if_t<__can_rewrap<_InIter, _Sent, _OutIter>::value, int> = 0> 103 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 pair<_InIter, _OutIter> 104 __unwrap_and_dispatch(_InIter __first, _Sent __last, _OutIter __out_first) { 105 auto __range = std::__unwrap_range(__first, std::move(__last)); 106 auto __result = _Algorithm()(std::move(__range.first), std::move(__range.second), std::__unwrap_iter(__out_first)); 107 return std::make_pair(std::__rewrap_range<_Sent>(std::move(__first), std::move(__result.first)), 108 std::__rewrap_iter(std::move(__out_first), std::move(__result.second))); 109 } 110 111 template <class _Algorithm, 112 class _InIter, 113 class _Sent, 114 class _OutIter, 115 __enable_if_t<!__can_rewrap<_InIter, _Sent, _OutIter>::value, int> = 0> 116 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 pair<_InIter, _OutIter> 117 __unwrap_and_dispatch(_InIter __first, _Sent __last, _OutIter __out_first) { 118 return _Algorithm()(std::move(__first), std::move(__last), std::move(__out_first)); 119 } 120 121 template <class _AlgPolicy, 122 class _NaiveAlgorithm, 123 class _OptimizedAlgorithm, 124 class _InIter, 125 class _Sent, 126 class _OutIter> 127 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 pair<_InIter, _OutIter> 128 __dispatch_copy_or_move(_InIter __first, _Sent __last, _OutIter __out_first) { 129 using _Algorithm = __overload<_NaiveAlgorithm, _OptimizedAlgorithm>; 130 return std::__unwrap_and_dispatch<_Algorithm>(std::move(__first), std::move(__last), std::move(__out_first)); 131 } 132 133 _LIBCPP_END_NAMESPACE_STD 134 135 #endif // _LIBCPP___ALGORITHM_COPY_MOVE_COMMON_H 136