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