|
| 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___MEMORY_USES_ALLOCATOR_CONSTRUCTION_H |
| 10 | +#define _LIBCPP___MEMORY_USES_ALLOCATOR_CONSTRUCTION_H |
| 11 | + |
| 12 | +#include <__config> |
| 13 | +#include <__memory/construct_at.h> |
| 14 | +#include <__memory/uses_allocator.h> |
| 15 | +#include <__type_traits/enable_if.h> |
| 16 | +#include <__type_traits/is_same.h> |
| 17 | +#include <__utility/pair.h> |
| 18 | +#include <tuple> |
| 19 | + |
| 20 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 21 | +# pragma GCC system_header |
| 22 | +#endif |
| 23 | + |
| 24 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 25 | + |
| 26 | +#if _LIBCPP_STD_VER >= 17 |
| 27 | + |
| 28 | +template <class _Type> |
| 29 | +inline constexpr bool __is_std_pair = false; |
| 30 | + |
| 31 | +template <class _Type1, class _Type2> |
| 32 | +inline constexpr bool __is_std_pair<pair<_Type1, _Type2>> = true; |
| 33 | + |
| 34 | +template <class _Type, class _Alloc, class... _Args, __enable_if_t<!__is_std_pair<_Type>, int> = 0> |
| 35 | +_LIBCPP_HIDE_FROM_ABI constexpr auto |
| 36 | +__uses_allocator_construction_args(const _Alloc& __alloc, _Args&&... __args) noexcept { |
| 37 | + if constexpr (!uses_allocator_v<_Type, _Alloc> && is_constructible_v<_Type, _Args...>) { |
| 38 | + return std::forward_as_tuple(std::forward<_Args>(__args)...); |
| 39 | + } else if constexpr (uses_allocator_v<_Type, _Alloc> && |
| 40 | + is_constructible_v<_Type, allocator_arg_t, const _Alloc&, _Args...>) { |
| 41 | + return tuple<allocator_arg_t, const _Alloc&, _Args&&...>(allocator_arg, __alloc, std::forward<_Args>(__args)...); |
| 42 | + } else if constexpr (uses_allocator_v<_Type, _Alloc> && is_constructible_v<_Type, _Args..., const _Alloc&>) { |
| 43 | + return std::forward_as_tuple(std::forward<_Args>(__args)..., __alloc); |
| 44 | + } else { |
| 45 | + static_assert( |
| 46 | + sizeof(_Type) + 1 == 0, "If uses_allocator_v<Type> is true, the type has to be allocator-constructible"); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +template <class _Pair, class _Alloc, class _Tuple1, class _Tuple2, __enable_if_t<__is_std_pair<_Pair>, int> = 0> |
| 51 | +_LIBCPP_HIDE_FROM_ABI constexpr auto __uses_allocator_construction_args( |
| 52 | + const _Alloc& __alloc, piecewise_construct_t, _Tuple1&& __x, _Tuple2&& __y) noexcept { |
| 53 | + return std::make_tuple( |
| 54 | + piecewise_construct, |
| 55 | + std::apply( |
| 56 | + [&__alloc](auto&&... __args1) { |
| 57 | + return std::__uses_allocator_construction_args<typename _Pair::first_type>( |
| 58 | + __alloc, std::forward<decltype(__args1)>(__args1)...); |
| 59 | + }, |
| 60 | + std::forward<_Tuple1>(__x)), |
| 61 | + std::apply( |
| 62 | + [&__alloc](auto&&... __args2) { |
| 63 | + return std::__uses_allocator_construction_args<typename _Pair::second_type>( |
| 64 | + __alloc, std::forward<decltype(__args2)>(__args2)...); |
| 65 | + }, |
| 66 | + std::forward<_Tuple2>(__y))); |
| 67 | +} |
| 68 | + |
| 69 | +template <class _Pair, class _Alloc, __enable_if_t<__is_std_pair<_Pair>, int> = 0> |
| 70 | +_LIBCPP_HIDE_FROM_ABI constexpr auto __uses_allocator_construction_args(const _Alloc& __alloc) noexcept { |
| 71 | + return std::__uses_allocator_construction_args<_Pair>(__alloc, piecewise_construct, tuple<>{}, tuple<>{}); |
| 72 | +} |
| 73 | + |
| 74 | +template <class _Pair, class _Alloc, class _Up, class _Vp, __enable_if_t<__is_std_pair<_Pair>, int> = 0> |
| 75 | +_LIBCPP_HIDE_FROM_ABI constexpr auto |
| 76 | +__uses_allocator_construction_args(const _Alloc& __alloc, _Up&& __u, _Vp&& __v) noexcept { |
| 77 | + return std::__uses_allocator_construction_args<_Pair>( |
| 78 | + __alloc, |
| 79 | + piecewise_construct, |
| 80 | + std::forward_as_tuple(std::forward<_Up>(__u)), |
| 81 | + std::forward_as_tuple(std::forward<_Vp>(__v))); |
| 82 | +} |
| 83 | + |
| 84 | +# if _LIBCPP_STD_VER > 20 |
| 85 | +template <class _Pair, class _Alloc, class _Up, class _Vp, __enable_if_t<__is_std_pair<_Pair>, int> = 0> |
| 86 | +_LIBCPP_HIDE_FROM_ABI constexpr auto |
| 87 | +__uses_allocator_construction_args(const _Alloc& __alloc, pair<_Up, _Vp>& __pair) noexcept { |
| 88 | + return std::__uses_allocator_construction_args<_Pair>( |
| 89 | + __alloc, piecewise_construct, std::forward_as_tuple(__pair.first), std::forward_as_tuple(__pair.second)); |
| 90 | +} |
| 91 | +# endif |
| 92 | + |
| 93 | +template <class _Pair, class _Alloc, class _Up, class _Vp, __enable_if_t<__is_std_pair<_Pair>, int> = 0> |
| 94 | +_LIBCPP_HIDE_FROM_ABI constexpr auto |
| 95 | +__uses_allocator_construction_args(const _Alloc& __alloc, const pair<_Up, _Vp>& __pair) noexcept { |
| 96 | + return std::__uses_allocator_construction_args<_Pair>( |
| 97 | + __alloc, piecewise_construct, std::forward_as_tuple(__pair.first), std::forward_as_tuple(__pair.second)); |
| 98 | +} |
| 99 | + |
| 100 | +template <class _Pair, class _Alloc, class _Up, class _Vp, __enable_if_t<__is_std_pair<_Pair>, int> = 0> |
| 101 | +_LIBCPP_HIDE_FROM_ABI constexpr auto |
| 102 | +__uses_allocator_construction_args(const _Alloc& __alloc, pair<_Up, _Vp>&& __pair) noexcept { |
| 103 | + return std::__uses_allocator_construction_args<_Pair>( |
| 104 | + __alloc, |
| 105 | + piecewise_construct, |
| 106 | + std::forward_as_tuple(std::get<0>(std::move(__pair))), |
| 107 | + std::forward_as_tuple(std::get<1>(std::move(__pair)))); |
| 108 | +} |
| 109 | + |
| 110 | +# if _LIBCPP_STD_VER > 20 |
| 111 | +template <class _Pair, class _Alloc, class _Up, class _Vp, __enable_if_t<__is_std_pair<_Pair>, int> = 0> |
| 112 | +_LIBCPP_HIDE_FROM_ABI constexpr auto |
| 113 | +__uses_allocator_construction_args(const _Alloc& __alloc, const pair<_Up, _Vp>&& __pair) noexcept { |
| 114 | + return std::__uses_allocator_construction_args<_Pair>( |
| 115 | + __alloc, |
| 116 | + piecewise_construct, |
| 117 | + std::forward_as_tuple(std::get<0>(std::move(__pair))), |
| 118 | + std::forward_as_tuple(std::get<1>(std::move(__pair)))); |
| 119 | +} |
| 120 | +# endif |
| 121 | + |
| 122 | +namespace __uses_allocator_detail { |
| 123 | + |
| 124 | +template <class _Ap, class _Bp> |
| 125 | +void __fun(const pair<_Ap, _Bp>&); |
| 126 | + |
| 127 | +template <class _Tp> |
| 128 | +decltype(__uses_allocator_detail::__fun(std::declval<_Tp>()), true_type()) __convertible_to_const_pair_ref_impl(int); |
| 129 | + |
| 130 | +template <class> |
| 131 | +false_type __convertible_to_const_pair_ref_impl(...); |
| 132 | + |
| 133 | +template <class _Tp> |
| 134 | +inline constexpr bool __convertible_to_const_pair_ref = |
| 135 | + decltype(__uses_allocator_detail::__convertible_to_const_pair_ref_impl<_Tp>(0))::value; |
| 136 | + |
| 137 | +} // namespace __uses_allocator_detail |
| 138 | + |
| 139 | +template < |
| 140 | + class _Pair, |
| 141 | + class _Alloc, |
| 142 | + class _Type, |
| 143 | + __enable_if_t<__is_std_pair<_Pair> && !__uses_allocator_detail::__convertible_to_const_pair_ref<_Type>, int> = 0> |
| 144 | +_LIBCPP_HIDE_FROM_ABI constexpr auto |
| 145 | +__uses_allocator_construction_args(const _Alloc& __alloc, _Type&& __value) noexcept; |
| 146 | + |
| 147 | +template <class _Type, class _Alloc, class... _Args> |
| 148 | +_LIBCPP_HIDE_FROM_ABI constexpr _Type __make_obj_using_allocator(const _Alloc& __alloc, _Args&&... __args); |
| 149 | + |
| 150 | +template <class _Pair, |
| 151 | + class _Alloc, |
| 152 | + class _Type, |
| 153 | + __enable_if_t<__is_std_pair<_Pair> && !__uses_allocator_detail::__convertible_to_const_pair_ref<_Type>, int>> |
| 154 | +_LIBCPP_HIDE_FROM_ABI constexpr auto |
| 155 | +__uses_allocator_construction_args(const _Alloc& __alloc, _Type&& __value) noexcept { |
| 156 | + struct __pair_constructor { |
| 157 | + using _PairMutable = remove_cv_t<_Pair>; |
| 158 | + |
| 159 | + _LIBCPP_HIDE_FROM_ABI constexpr auto __do_construct(const _PairMutable& __pair) const { |
| 160 | + return std::__make_obj_using_allocator<_PairMutable>(__alloc_, __pair); |
| 161 | + } |
| 162 | + |
| 163 | + _LIBCPP_HIDE_FROM_ABI constexpr auto __do_construct(_PairMutable&& __pair) const { |
| 164 | + return std::__make_obj_using_allocator<_PairMutable>(__alloc_, std::move(__pair)); |
| 165 | + } |
| 166 | + |
| 167 | + const _Alloc& __alloc_; |
| 168 | + _Type& __value_; |
| 169 | + |
| 170 | + _LIBCPP_HIDE_FROM_ABI constexpr operator _PairMutable() const { |
| 171 | + return __do_construct(std::forward<_Type>(this->__value_)); |
| 172 | + } |
| 173 | + }; |
| 174 | + |
| 175 | + return std::make_tuple(__pair_constructor{__alloc, __value}); |
| 176 | +} |
| 177 | + |
| 178 | +template <class _Type, class _Alloc, class... _Args> |
| 179 | +_LIBCPP_HIDE_FROM_ABI constexpr _Type __make_obj_using_allocator(const _Alloc& __alloc, _Args&&... __args) { |
| 180 | + return std::make_from_tuple<_Type>( |
| 181 | + std::__uses_allocator_construction_args<_Type>(__alloc, std::forward<_Args>(__args)...)); |
| 182 | +} |
| 183 | + |
| 184 | +template <class _Type, class _Alloc, class... _Args> |
| 185 | +_LIBCPP_HIDE_FROM_ABI constexpr _Type* |
| 186 | +__uninitialized_construct_using_allocator(_Type* __ptr, const _Alloc& __alloc, _Args&&... __args) { |
| 187 | + return std::apply( |
| 188 | + [&__ptr](auto&&... __xs) { return std::__construct_at(__ptr, std::forward<decltype(__xs)>(__xs)...); }, |
| 189 | + std::__uses_allocator_construction_args<_Type>(__alloc, std::forward<_Args>(__args)...)); |
| 190 | +} |
| 191 | + |
| 192 | +#endif // _LIBCPP_STD_VER >= 17 |
| 193 | + |
| 194 | +#if _LIBCPP_STD_VER >= 20 |
| 195 | + |
| 196 | +template <class _Type, class _Alloc, class... _Args> |
| 197 | +_LIBCPP_HIDE_FROM_ABI constexpr auto uses_allocator_construction_args(const _Alloc& __alloc, _Args&&... __args) noexcept |
| 198 | + -> decltype(std::__uses_allocator_construction_args<_Type>(__alloc, std::forward<_Args>(__args)...)) { |
| 199 | + return /*--*/ std::__uses_allocator_construction_args<_Type>(__alloc, std::forward<_Args>(__args)...); |
| 200 | +} |
| 201 | + |
| 202 | +template <class _Type, class _Alloc, class... _Args> |
| 203 | +_LIBCPP_HIDE_FROM_ABI constexpr auto make_obj_using_allocator(const _Alloc& __alloc, _Args&&... __args) |
| 204 | + -> decltype(std::__make_obj_using_allocator<_Type>(__alloc, std::forward<_Args>(__args)...)) { |
| 205 | + return /*--*/ std::__make_obj_using_allocator<_Type>(__alloc, std::forward<_Args>(__args)...); |
| 206 | +} |
| 207 | + |
| 208 | +template <class _Type, class _Alloc, class... _Args> |
| 209 | +_LIBCPP_HIDE_FROM_ABI constexpr auto |
| 210 | +uninitialized_construct_using_allocator(_Type* __ptr, const _Alloc& __alloc, _Args&&... __args) |
| 211 | + -> decltype(std::__uninitialized_construct_using_allocator(__ptr, __alloc, std::forward<_Args>(__args)...)) { |
| 212 | + return /*--*/ std::__uninitialized_construct_using_allocator(__ptr, __alloc, std::forward<_Args>(__args)...); |
| 213 | +} |
| 214 | + |
| 215 | +#endif // _LIBCPP_STD_VER >= 20 |
| 216 | + |
| 217 | +_LIBCPP_END_NAMESPACE_STD |
| 218 | + |
| 219 | +#endif // _LIBCPP___MEMORY_USES_ALLOCATOR_CONSTRUCTION_H |
0 commit comments