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 <__type_traits/enable_if.h> 19 #include <__type_traits/integral_constant.h> 20 #include <__type_traits/is_constant_evaluated.h> 21 #include <__type_traits/is_copy_constructible.h> 22 #include <__type_traits/is_trivially_assignable.h> 23 #include <__utility/move.h> 24 #include <__utility/pair.h> 25 26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 27 # pragma GCC system_header 28 #endif 29 30 _LIBCPP_BEGIN_NAMESPACE_STD 31 32 // `memmove` algorithms implementation. 33 34 template <class _In, class _Out> 35 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*> 36 __copy_trivial_impl(_In* __first, _In* __last, _Out* __result) { 37 const size_t __n = static_cast<size_t>(__last - __first); 38 ::__builtin_memmove(__result, __first, __n * sizeof(_Out)); 39 40 return std::make_pair(__last, __result + __n); 41 } 42 43 template <class _In, class _Out> 44 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*> 45 __copy_backward_trivial_impl(_In* __first, _In* __last, _Out* __result) { 46 const size_t __n = static_cast<size_t>(__last - __first); 47 __result -= __n; 48 49 ::__builtin_memmove(__result, __first, __n * sizeof(_Out)); 50 51 return std::make_pair(__last, __result); 52 } 53 54 // Iterator unwrapping and dispatching to the correct overload. 55 56 template <class _F1, class _F2> 57 struct __overload : _F1, _F2 { 58 using _F1::operator(); 59 using _F2::operator(); 60 }; 61 62 template <class _InIter, class _Sent, class _OutIter, class = void> 63 struct __can_rewrap : false_type {}; 64 65 template <class _InIter, class _Sent, class _OutIter> 66 struct __can_rewrap<_InIter, 67 _Sent, 68 _OutIter, 69 // Note that sentinels are always copy-constructible. 70 __enable_if_t< is_copy_constructible<_InIter>::value && 71 is_copy_constructible<_OutIter>::value > > : true_type {}; 72 73 template <class _Algorithm, 74 class _InIter, 75 class _Sent, 76 class _OutIter, 77 __enable_if_t<__can_rewrap<_InIter, _Sent, _OutIter>::value, int> = 0> 78 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 pair<_InIter, _OutIter> 79 __unwrap_and_dispatch(_InIter __first, _Sent __last, _OutIter __out_first) { 80 auto __range = std::__unwrap_range(__first, std::move(__last)); 81 auto __result = _Algorithm()(std::move(__range.first), std::move(__range.second), std::__unwrap_iter(__out_first)); 82 return std::make_pair(std::__rewrap_range<_Sent>(std::move(__first), std::move(__result.first)), 83 std::__rewrap_iter(std::move(__out_first), std::move(__result.second))); 84 } 85 86 template <class _Algorithm, 87 class _InIter, 88 class _Sent, 89 class _OutIter, 90 __enable_if_t<!__can_rewrap<_InIter, _Sent, _OutIter>::value, int> = 0> 91 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 pair<_InIter, _OutIter> 92 __unwrap_and_dispatch(_InIter __first, _Sent __last, _OutIter __out_first) { 93 return _Algorithm()(std::move(__first), std::move(__last), std::move(__out_first)); 94 } 95 96 template <class _IterOps, class _InValue, class _OutIter, class = void> 97 struct __can_copy_without_conversion : false_type {}; 98 99 template <class _IterOps, class _InValue, class _OutIter> 100 struct __can_copy_without_conversion< 101 _IterOps, 102 _InValue, 103 _OutIter, 104 __enable_if_t<is_same<_InValue, typename _IterOps::template __value_type<_OutIter> >::value> > : true_type {}; 105 106 template <class _AlgPolicy, 107 class _NaiveAlgorithm, 108 class _OptimizedAlgorithm, 109 class _InIter, 110 class _Sent, 111 class _OutIter> 112 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 pair<_InIter, _OutIter> 113 __dispatch_copy_or_move(_InIter __first, _Sent __last, _OutIter __out_first) { 114 #ifdef _LIBCPP_COMPILER_GCC 115 // GCC doesn't support `__builtin_memmove` during constant evaluation. 116 if (__libcpp_is_constant_evaluated()) { 117 return std::__unwrap_and_dispatch<_NaiveAlgorithm>(std::move(__first), std::move(__last), std::move(__out_first)); 118 } 119 #else 120 // In Clang, `__builtin_memmove` only supports fully trivially copyable types (just having trivial copy assignment is 121 // insufficient). Also, conversions are not supported. 122 if (__libcpp_is_constant_evaluated()) { 123 using _InValue = typename _IterOps<_AlgPolicy>::template __value_type<_InIter>; 124 if (!is_trivially_copyable<_InValue>::value || 125 !__can_copy_without_conversion<_IterOps<_AlgPolicy>, _InValue, _OutIter>::value) { 126 return std::__unwrap_and_dispatch<_NaiveAlgorithm>(std::move(__first), std::move(__last), std::move(__out_first)); 127 } 128 } 129 #endif // _LIBCPP_COMPILER_GCC 130 131 using _Algorithm = __overload<_NaiveAlgorithm, _OptimizedAlgorithm>; 132 return std::__unwrap_and_dispatch<_Algorithm>(std::move(__first), std::move(__last), std::move(__out_first)); 133 } 134 135 _LIBCPP_END_NAMESPACE_STD 136 137 #endif // _LIBCPP___ALGORITHM_COPY_MOVE_COMMON_H 138