|
| 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_DISENGAGED_TRAITS_H |
| 10 | +#define _LIBCPP___TYPE_TRAITS_DISENGAGED_TRAITS_H |
| 11 | + |
| 12 | +#include <__config> |
| 13 | +#include <__memory/construct_at.h> |
| 14 | +#include <__type_traits/datasizeof.h> |
| 15 | +#include <__type_traits/enable_if.h> |
| 16 | +#include <__type_traits/is_constant_evaluated.h> |
| 17 | +#include <__type_traits/is_fundamental.h> |
| 18 | +#include <__type_traits/is_trivially_destructible.h> |
| 19 | +#include <__type_traits/void_t.h> |
| 20 | +#include <__utility/forward_like.h> |
| 21 | +#include <__utility/in_place.h> |
| 22 | +#include <__utility/piecewise_construct.h> |
| 23 | +#include <__utility/pointer_int_pair.h> |
| 24 | + |
| 25 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 26 | +# pragma GCC system_header |
| 27 | +#endif |
| 28 | + |
| 29 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 30 | + |
| 31 | +template <class> |
| 32 | +struct __tombstone_memory_layout; |
| 33 | + |
| 34 | +// bools have always exactly one bit set. If there is more than one set it's disengaged. |
| 35 | +template <> |
| 36 | +struct __tombstone_memory_layout<bool> { |
| 37 | + static constexpr uint8_t __disengaged_value_ = 3; |
| 38 | + static constexpr size_t __is_disengaged_offset_ = 0; |
| 39 | +}; |
| 40 | + |
| 41 | +struct __tombstone_pointer_layout { |
| 42 | + static constexpr uint8_t __disengaged_value_ = 1; |
| 43 | +#ifdef _LIBCPP_LITTLE_ENDIAN |
| 44 | + static constexpr size_t __is_disengaged_offset_ = 0; |
| 45 | +#else |
| 46 | + static constexpr size_t __is_disengaged_offset_ = sizeof(void*) - 1; |
| 47 | +#endif |
| 48 | +}; |
| 49 | + |
| 50 | +// TODO: Look into |
| 51 | +// - filesystem::directory_iterator |
| 52 | +// - vector<T> with alignof(T) == 1 |
| 53 | + |
| 54 | +template <class _Tp> |
| 55 | +struct __tombstone_memory_layout<__enable_specialization_if<is_fundamental_v<_Tp> && alignof(_Tp) >= 2, _Tp*>> |
| 56 | + : __tombstone_pointer_layout {}; |
| 57 | + |
| 58 | +template <class _Tp> |
| 59 | +struct __tombstone_memory_layout<_Tp**> : __tombstone_pointer_layout {}; |
| 60 | + |
| 61 | +inline constexpr struct __init_engaged_t { |
| 62 | +} __init_engaged; |
| 63 | +inline constexpr struct __init_disengaged_t { |
| 64 | +} __init_disengaged; |
| 65 | + |
| 66 | +template <class _Tp, class _Payload> |
| 67 | +struct __tombstone_data { |
| 68 | + using _TombstoneLayout = __tombstone_memory_layout<_Tp>; |
| 69 | + using _IsDisengagedT = remove_cv_t<decltype(_TombstoneLayout::__disengaged_value_)>; |
| 70 | + |
| 71 | + _LIBCPP_NO_UNIQUE_ADDRESS _Payload __payload_; |
| 72 | + char __padding_[_TombstoneLayout::__is_disengaged_offset_ - __datasizeof_v<_Payload>]; |
| 73 | + _IsDisengagedT __is_disengaged_; |
| 74 | + |
| 75 | + template <class... _Args> |
| 76 | + _LIBCPP_HIDE_FROM_ABI constexpr __tombstone_data(_Args&&... __args) |
| 77 | + : __payload_(std::forward<_Args>(__args)...), __is_disengaged_(_TombstoneLayout::__disengaged_value_) {} |
| 78 | +}; |
| 79 | + |
| 80 | +template <class _Tp, class _Payload> |
| 81 | + requires(__tombstone_memory_layout<_Tp>::__is_disengaged_offset_ == 0) |
| 82 | +struct __tombstone_data<_Tp, _Payload> { |
| 83 | + using _TombstoneLayout = __tombstone_memory_layout<_Tp>; |
| 84 | + using _IsDisengagedT = remove_cv_t<decltype(_TombstoneLayout::__disengaged_value_)>; |
| 85 | + |
| 86 | + _IsDisengagedT __is_disengaged_; |
| 87 | + _LIBCPP_NO_UNIQUE_ADDRESS _Payload __payload_; |
| 88 | + |
| 89 | + template <class... _Args> |
| 90 | + _LIBCPP_HIDE_FROM_ABI constexpr __tombstone_data(_Args&&... __args) |
| 91 | + : __is_disengaged_(_TombstoneLayout::__disengaged_value_), __payload_(std::forward<_Args>(__args)...) {} |
| 92 | +}; |
| 93 | + |
| 94 | +template <class _Tp, class _Payload> |
| 95 | +struct __tombstone_traits { |
| 96 | + using _TombstoneLayout = __tombstone_memory_layout<_Tp>; |
| 97 | + using _TombstoneData = __tombstone_data<_Tp, _Payload>; |
| 98 | + |
| 99 | + union { |
| 100 | + _Tp __value_; |
| 101 | + _TombstoneData __tombstone_; |
| 102 | + }; |
| 103 | + |
| 104 | + static_assert(sizeof(__tombstone_data<_Tp, _Payload>) <= sizeof(_Tp)); |
| 105 | + static_assert(is_integral_v<decltype(_TombstoneLayout::__disengaged_value_)>); |
| 106 | + static_assert(offsetof(_TombstoneData, __is_disengaged_) == _TombstoneLayout::__is_disengaged_offset_); |
| 107 | + |
| 108 | + template <class... _Args> |
| 109 | + _LIBCPP_HIDE_FROM_ABI constexpr __tombstone_traits(__init_disengaged_t, _Args&&... __args) |
| 110 | + : __tombstone_(std::forward<_Args>(__args)...) {} |
| 111 | + |
| 112 | + template <class... _Args> |
| 113 | + _LIBCPP_HIDE_FROM_ABI constexpr __tombstone_traits(__init_engaged_t, _Args&&... __args) |
| 114 | + : __value_(std::forward<_Args>(__args)...) {} |
| 115 | + |
| 116 | + template <class _Class> |
| 117 | + _LIBCPP_HIDE_FROM_ABI constexpr auto&& __get_value(this _Class&& __self) noexcept { |
| 118 | + return std::forward<_Class>(__self).__value_; |
| 119 | + } |
| 120 | + |
| 121 | + template <class _Class> |
| 122 | + _LIBCPP_HIDE_FROM_ABI constexpr auto&& __get_payload(this _Class&& __self) noexcept { |
| 123 | + return std::forward<_Class>(__self).__tombstone_.__payload_; |
| 124 | + } |
| 125 | + |
| 126 | + _LIBCPP_HIDE_FROM_ABI constexpr bool __is_engaged() const noexcept { |
| 127 | + if (__libcpp_is_constant_evaluated()) |
| 128 | + return !__builtin_constant_p(__tombstone_.__is_disengaged_ == _TombstoneLayout::__disengaged_value_); |
| 129 | + return __tombstone_.__is_disengaged_ != _TombstoneLayout::__disengaged_value_; |
| 130 | + } |
| 131 | + |
| 132 | + template <class _Class, class... _Args> |
| 133 | + _LIBCPP_HIDE_FROM_ABI constexpr void __engage(this _Class& __self, piecewise_construct_t, _Args&&... __args) { |
| 134 | + std::destroy_at(&__self.__tombstone_); |
| 135 | + std::construct_at(&__self.__value_, std::forward<_Args>(__args)...); |
| 136 | + } |
| 137 | + |
| 138 | + template <class _Class, class... _Args> |
| 139 | + _LIBCPP_HIDE_FROM_ABI constexpr void __disengage(this _Class& __self, piecewise_construct_t, _Args&&... __args) { |
| 140 | + std::destroy_at(&__self.__data_.__value_); |
| 141 | + std::construct_at(&__self.__data_.__payload_, std::forward<_Args>(__args)...); |
| 142 | + __self.__data_.__is_disengaged_ = _TombstoneLayout::__disengaged_value_; |
| 143 | + } |
| 144 | + |
| 145 | + __tombstone_traits(const __tombstone_traits&) = default; |
| 146 | + __tombstone_traits(__tombstone_traits&&) = default; |
| 147 | + __tombstone_traits& operator=(const __tombstone_traits&) = default; |
| 148 | + __tombstone_traits& operator=(__tombstone_traits&&) = default; |
| 149 | + |
| 150 | + _LIBCPP_HIDE_FROM_ABI constexpr ~__tombstone_traits() { |
| 151 | + if (__is_engaged()) { |
| 152 | + std::destroy_at(&__value_); |
| 153 | + } else { |
| 154 | + std::destroy_at(&__tombstone_); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + ~__tombstone_traits() |
| 159 | + requires is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Payload> |
| 160 | + = default; |
| 161 | +}; |
| 162 | + |
| 163 | +template <class _Tp, class = void> |
| 164 | +inline constexpr bool __has_tombstone_v = false; |
| 165 | + |
| 166 | +template <class _Tp> |
| 167 | +inline constexpr bool __has_tombstone_v<_Tp, void_t<decltype(sizeof(__tombstone_memory_layout<_Tp>))>> = true; |
| 168 | + |
| 169 | +_LIBCPP_END_NAMESPACE_STD |
| 170 | + |
| 171 | +#endif // _LIBCPP___TYPE_TRAITS_DISENGAGED_TRAITS_H |
0 commit comments