|
| 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___UTILITY_CONDITIONAL_NO_UNIQUE_ADDRESS_H |
| 10 | +#define _LIBCPP___UTILITY_CONDITIONAL_NO_UNIQUE_ADDRESS_H |
| 11 | + |
| 12 | +#include <__config> |
| 13 | +#include <__type_traits/invoke.h> |
| 14 | +#include <__type_traits/is_swappable.h> |
| 15 | +#include <__utility/forward.h> |
| 16 | +#include <__utility/in_place.h> |
| 17 | + |
| 18 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 19 | +# pragma GCC system_header |
| 20 | +#endif |
| 21 | + |
| 22 | +_LIBCPP_PUSH_MACROS |
| 23 | +#include <__undef_macros> |
| 24 | + |
| 25 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 26 | + |
| 27 | +// If parameter type `_Tp` of `__conditional_no_unique_address` is neither |
| 28 | +// copyable nor movable, a constructor with this tag is provided. For that |
| 29 | +// constructor, the user has to provide a function and arguments. The function |
| 30 | +// must return an object of type `_Tp`. When the function is invoked by the |
| 31 | +// constructor, guaranteed copy elision kicks in and the `_Tp` is constructed |
| 32 | +// in place. |
| 33 | +struct __conditional_no_unique_address_invoke_tag {}; |
| 34 | + |
| 35 | +// This class implements an object with `[[no_unique_address]]` conditionally applied to it, |
| 36 | +// based on the value of `_NoUnique`. |
| 37 | +// |
| 38 | +// A member of this class must always have `[[no_unique_address]]` applied to |
| 39 | +// it. Otherwise, the `[[no_unique_address]]` in the "`_NoUnique == true`" case |
| 40 | +// would not have any effect. In the `false` case, the `__v` is not |
| 41 | +// `[[no_unique_address]]`, so nullifies the effects of the "outer" |
| 42 | +// `[[no_unique_address]]` regarding data layout. |
| 43 | +// |
| 44 | +// If we had a language feature, this class would basically be replaced by `[[no_unique_address(condition)]]`. |
| 45 | +template <bool _NoUnique, class _Tp> |
| 46 | +struct __conditional_no_unique_address; |
| 47 | + |
| 48 | +template <class _Tp> |
| 49 | +struct __conditional_no_unique_address<true, _Tp> { |
| 50 | + template <class... _Args> |
| 51 | + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __conditional_no_unique_address(__in_place_t, _Args&&... __args) |
| 52 | + : __v(std::forward<_Args>(__args)...) {} |
| 53 | + |
| 54 | + template <class _Func, class... _Args> |
| 55 | + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __conditional_no_unique_address( |
| 56 | + __conditional_no_unique_address_invoke_tag, _Func&& __f, _Args&&... __args) |
| 57 | + : __v(std::__invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {} |
| 58 | + |
| 59 | + _LIBCPP_NO_UNIQUE_ADDRESS _Tp __v; |
| 60 | +}; |
| 61 | + |
| 62 | +template <class _Tp> |
| 63 | +struct __conditional_no_unique_address<false, _Tp> { |
| 64 | + template <class... _Args> |
| 65 | + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __conditional_no_unique_address(__in_place_t, _Args&&... __args) |
| 66 | + : __v(std::forward<_Args>(__args)...) {} |
| 67 | + |
| 68 | + template <class _Func, class... _Args> |
| 69 | + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __conditional_no_unique_address( |
| 70 | + __conditional_no_unique_address_invoke_tag, _Func&& __f, _Args&&... __args) |
| 71 | + : __v(std::__invoke(std::forward<_Func>(__f), std::forward<_Args>(__args)...)) {} |
| 72 | + |
| 73 | + _Tp __v; |
| 74 | +}; |
| 75 | + |
| 76 | +template <bool _NoUnique, class _Tp> |
| 77 | +void swap(__conditional_no_unique_address<_NoUnique, _Tp>& __lhs, |
| 78 | + __conditional_no_unique_address<_NoUnique, _Tp>& __rhs) _NOEXCEPT_(__is_swappable_v<_Tp>) { |
| 79 | + using std::swap; |
| 80 | + swap(__lhs.__v, __rhs.__v); |
| 81 | +} |
| 82 | + |
| 83 | +_LIBCPP_END_NAMESPACE_STD |
| 84 | + |
| 85 | +_LIBCPP_POP_MACROS |
| 86 | + |
| 87 | +#endif // _LIBCPP___UTILITY_CONDITIONAL_NO_UNIQUE_ADDRESS_H |
0 commit comments