|
| 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___TYPE_TRAITS_IS_REPLACEABLE_H |
| 10 | +#define _LIBCPP___TYPE_TRAITS_IS_REPLACEABLE_H |
| 11 | + |
| 12 | +#include <__config> |
| 13 | +#include <__type_traits/enable_if.h> |
| 14 | +#include <__type_traits/integral_constant.h> |
| 15 | +#include <__type_traits/is_same.h> |
| 16 | +#include <__type_traits/is_trivially_copyable.h> |
| 17 | + |
| 18 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 19 | +# pragma GCC system_header |
| 20 | +#endif |
| 21 | + |
| 22 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 23 | + |
| 24 | +// A type is replaceable if `x = std::move(y)` is equivalent to: |
| 25 | +// |
| 26 | +// std::destroy_at(&x) |
| 27 | +// std::construct_at(&x, std::move(y)) |
| 28 | +// |
| 29 | +// This allows turning a move-assignment into a sequence of destroy + move-construct, which |
| 30 | +// is often more efficient. This is especially relevant when the move-construct is in fact |
| 31 | +// part of a trivial relocation from somewhere else, in which case there is a huge win. |
| 32 | +// |
| 33 | +// Note that this requires language support in order to be really effective, but we |
| 34 | +// currently emulate the base template with something very conservative. |
| 35 | +template <class _Tp, class = void> |
| 36 | +struct __is_replaceable : is_trivially_copyable<_Tp> {}; |
| 37 | + |
| 38 | +template <class _Tp> |
| 39 | +struct __is_replaceable<_Tp, __enable_if_t<is_same<_Tp, typename _Tp::__replaceable>::value> > : true_type {}; |
| 40 | + |
| 41 | +// Determines whether an allocator member of a container is replaceable. |
| 42 | +// |
| 43 | +// We take into account whether the allocator is propagated on assignments. If the allocator |
| 44 | +// always compares equal, then it doesn't matter whether we propagate it or not on assignments, |
| 45 | +// the result will be the same and we can just as much move-construct it instead. |
| 46 | +// |
| 47 | +// If the allocator does not always compare equal, we check whether it propagates on assignment |
| 48 | +// and it is replaceable. |
| 49 | +template <class _AllocatorTraits> |
| 50 | +struct __container_allocator_is_replaceable |
| 51 | + : integral_constant<bool, |
| 52 | + _AllocatorTraits::is_always_equal::value || |
| 53 | + (_AllocatorTraits::propagate_on_container_move_assignment::value && |
| 54 | + _AllocatorTraits::propagate_on_container_copy_assignment::value && |
| 55 | + __is_replaceable<typename _AllocatorTraits::allocator_type>::value)> {}; |
| 56 | + |
| 57 | +_LIBCPP_END_NAMESPACE_STD |
| 58 | + |
| 59 | +#endif // _LIBCPP___TYPE_TRAITS_IS_REPLACEABLE_H |
0 commit comments