113 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___CXX03___ALGORITHM_COPY_MOVE_COMMON_H10#define _LIBCPP___CXX03___ALGORITHM_COPY_MOVE_COMMON_H11 12#include <__cxx03/__algorithm/iterator_operations.h>13#include <__cxx03/__algorithm/unwrap_iter.h>14#include <__cxx03/__algorithm/unwrap_range.h>15#include <__cxx03/__config>16#include <__cxx03/__iterator/iterator_traits.h>17#include <__cxx03/__memory/pointer_traits.h>18#include <__cxx03/__string/constexpr_c_functions.h>19#include <__cxx03/__type_traits/enable_if.h>20#include <__cxx03/__type_traits/is_always_bitcastable.h>21#include <__cxx03/__type_traits/is_constant_evaluated.h>22#include <__cxx03/__type_traits/is_constructible.h>23#include <__cxx03/__type_traits/is_trivially_assignable.h>24#include <__cxx03/__type_traits/is_volatile.h>25#include <__cxx03/__utility/move.h>26#include <__cxx03/__utility/pair.h>27#include <__cxx03/cstddef>28 29#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)30# pragma GCC system_header31#endif32 33_LIBCPP_PUSH_MACROS34#include <__cxx03/__undef_macros>35 36_LIBCPP_BEGIN_NAMESPACE_STD37 38// Type traits.39 40template <class _From, class _To>41struct __can_lower_copy_assignment_to_memmove {42 static const bool value =43 // If the types are always bitcastable, it's valid to do a bitwise copy between them.44 __is_always_bitcastable<_From, _To>::value &&45 // Reject conversions that wouldn't be performed by the regular built-in assignment (e.g. between arrays).46 is_trivially_assignable<_To&, const _From&>::value &&47 // `memmove` doesn't accept `volatile` pointers, make sure the optimization SFINAEs away in that case.48 !is_volatile<_From>::value && !is_volatile<_To>::value;49};50 51template <class _From, class _To>52struct __can_lower_move_assignment_to_memmove {53 static const bool value =54 __is_always_bitcastable<_From, _To>::value && is_trivially_assignable<_To&, _From&&>::value &&55 !is_volatile<_From>::value && !is_volatile<_To>::value;56};57 58// `memmove` algorithms implementation.59 60template <class _In, class _Out>61_LIBCPP_HIDE_FROM_ABI pair<_In*, _Out*> __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 69template <class _In, class _Out>70_LIBCPP_HIDE_FROM_ABI pair<_In*, _Out*> __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 81template <class _InIter, class _OutIter>82struct __can_rewrap83 : integral_constant<bool, is_copy_constructible<_InIter>::value && is_copy_constructible<_OutIter>::value> {};84 85template <class _Algorithm,86 class _InIter,87 class _Sent,88 class _OutIter,89 __enable_if_t<__can_rewrap<_InIter, _OutIter>::value, int> = 0>90_LIBCPP_HIDE_FROM_ABI pair<_InIter, _OutIter>91__copy_move_unwrap_iters(_InIter __first, _Sent __last, _OutIter __out_first) {92 auto __range = std::__unwrap_range(__first, std::move(__last));93 auto __result = _Algorithm()(std::move(__range.first), std::move(__range.second), std::__unwrap_iter(__out_first));94 return std::make_pair(std::__rewrap_range<_Sent>(std::move(__first), std::move(__result.first)),95 std::__rewrap_iter(std::move(__out_first), std::move(__result.second)));96}97 98template <class _Algorithm,99 class _InIter,100 class _Sent,101 class _OutIter,102 __enable_if_t<!__can_rewrap<_InIter, _OutIter>::value, int> = 0>103_LIBCPP_HIDE_FROM_ABI pair<_InIter, _OutIter>104__copy_move_unwrap_iters(_InIter __first, _Sent __last, _OutIter __out_first) {105 return _Algorithm()(std::move(__first), std::move(__last), std::move(__out_first));106}107 108_LIBCPP_END_NAMESPACE_STD109 110_LIBCPP_POP_MACROS111 112#endif // _LIBCPP___CXX03___ALGORITHM_COPY_MOVE_COMMON_H113