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