-
Notifications
You must be signed in to change notification settings - Fork 0
/
uses_allocator.h
245 lines (214 loc) · 10.4 KB
/
uses_allocator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* uses_allocator.h -*-C++-*-
*
* Copyright (C) 2016 Pablo Halpern <phalpern@halpernwightsoftware.com>
* Distributed under the Boost Software License - Version 1.0
*/
#ifndef INCLUDED_USES_ALLOCATOR_DOT_H
#define INCLUDED_USES_ALLOCATOR_DOT_H
#include <make_from_tuple.h>
#include <memory>
namespace std {
inline namespace Cpp20 {
////////////////////////////////////////////////////////////////////////
// Forward declaration
template <class T, class Alloc, class... Args>
auto uses_allocator_construction_args(const Alloc& a, Args&&... args);
namespace internal {
template <class T, class A>
struct has_allocator : std::uses_allocator<T, A> { };
// Specialization of `has_allocator` for `std::pair`
template <class T1, class T2, class A>
struct has_allocator<pair<T1, T2>, A>
: integral_constant<bool, has_allocator<T1, A>::value ||
has_allocator<T2, A>::value>
{
};
template <bool V> using boolean_constant = integral_constant<bool, V>;
template <class T> struct is_pair : false_type { };
template <class T1, class T2>
struct is_pair<std::pair<T1, T2>> : true_type { };
// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload is handles types for which `has_allocator<T, Alloc>` is false.
template <class T, class Unused1, class Unused2, class Alloc, class... Args>
auto uses_allocator_args_imp(Unused1 /* is_pair */,
false_type /* has_allocator */,
Unused2 /* uses prefix allocator arg */,
const Alloc& /* ignored */,
Args&&... args)
{
// Allocator is ignored
return std::forward_as_tuple(std::forward<Args>(args)...);
}
// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload handles non-pair `T` for which `has_allocator<T, Alloc>` is
// true and constructor `T(allocator_arg_t, a, args...)` is valid.
template <class T, class Alloc, class... Args>
auto uses_allocator_args_imp(false_type /* is_pair */,
true_type /* has_allocator */,
true_type /* uses prefix allocator arg */,
const Alloc& a,
Args&&... args)
{
// Allocator added to front of argument list, after `allocator_arg`.
return tuple<allocator_arg_t, const Alloc&,
Args&&...>(allocator_arg, a, std::forward<Args>(args)...);
}
// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload handles non-pair `T` for which `has_allocator<T, Alloc>` is
// true and constructor `T(allocator_arg_t, a, args...)` NOT valid.
// This function will produce invalid results unless `T(args..., a)` is valid.
template <class T1, class Alloc, class... Args>
auto uses_allocator_args_imp(false_type /* is_pair */,
true_type /* has_allocator */,
false_type /* prefix allocator arg */,
const Alloc& a,
Args&&... args)
{
// Allocator added to end of argument list
return std::forward_as_tuple(std::forward<Args>(args)..., a);
}
// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload handles specializations of `T` = `std::pair` for which
// `has_allocator<T, Alloc>` is true for either or both of the elements and
// piecewise_construct arguments are passed in.
template <class T, class Alloc, class Tuple1, class Tuple2>
auto uses_allocator_args_imp(true_type /* is_pair */,
true_type /* has_allocator */,
false_type /* prefix allocator arg */,
const Alloc& a,
piecewise_construct_t,
Tuple1&& x, Tuple2&& y)
{
using T1 = typename T::first_type;
using T2 = typename T::second_type;
return make_tuple(piecewise_construct,
apply([&a](auto&&... args1) -> auto {
return uses_allocator_construction_args<T1>(a,
std::forward<decltype(args1)>(args1)...);
}, std::forward<Tuple1>(x)),
apply([&a](auto&&... args2) -> auto {
return uses_allocator_construction_args<T2>(a,
std::forward<decltype(args2)>(args2)...);
}, std::forward<Tuple2>(y))
);
}
// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload handles specializations of `T` = `std::pair` for which
// `has_allocator<T, Alloc>` is true for either or both of the elements and
// no other constructor arguments are passed in.
template <class T, class Alloc>
auto uses_allocator_args_imp(true_type /* is_pair */,
true_type /* has_allocator */,
false_type /* prefix allocator arg */,
const Alloc& a)
{
// using T1 = typename T::first_type;
// using T2 = typename T::second_type;
// return std::make_tuple(
// piecewise_construct,
// uses_allocator_construction_args<T1>(a),
// uses_allocator_construction_args<T2>(a));
return uses_allocator_construction_args<T>(a, piecewise_construct,
tuple<>{}, tuple<>{});
}
// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload handles specializations of `T` = `std::pair` for which
// `has_allocator<T, Alloc>` is true for either or both of the elements and
// a single argument of type const-lvalue-of-pair is passed in.
template <class T, class Alloc, class U1, class U2>
auto uses_allocator_args_imp(true_type /* is_pair */,
true_type /* has_allocator */,
false_type /* prefix allocator arg */,
const Alloc& a,
const pair<U1, U2>& arg)
{
// using T1 = typename T::first_type;
// using T2 = typename T::second_type;
// return std::make_tuple(
// piecewise_construct,
// uses_allocator_construction_args<T1>(a, arg.first),
// uses_allocator_construction_args<T2>(a, arg.second));
return uses_allocator_construction_args<T>(a, piecewise_construct,
forward_as_tuple(arg.first),
forward_as_tuple(arg.second));
}
// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload handles specializations of `T` = `std::pair` for which
// `has_allocator<T, Alloc>` is true for either or both of the elements and
// a single argument of type rvalue-of-pair is passed in.
template <class T, class Alloc, class U1, class U2>
auto uses_allocator_args_imp(true_type /* is_pair */,
true_type /* has_allocator */,
false_type /* prefix allocator arg */,
const Alloc& a,
pair<U1, U2>&& arg)
{
// using T1 = typename T::first_type;
// using T2 = typename T::second_type;
// return std::make_tuple(
// piecewise_construct,
// uses_allocator_construction_args<T1>(a, forward<U1>(arg.first)),
// uses_allocator_construction_args<T2>(a, forward<U2>(arg.second)));
return uses_allocator_construction_args<T>(a, piecewise_construct,
forward_as_tuple(forward<U1>(arg.first)),
forward_as_tuple(forward<U2>(arg.second)));
}
// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload handles specializations of `T` = `std::pair` for which
// `has_allocator<T, Alloc>` is true for either or both of the elements and
// two additional constructor arguments are passed in.
template <class T, class Alloc, class U1, class U2>
auto uses_allocator_args_imp(true_type /* is_pair */,
true_type /* has_allocator */,
false_type /* prefix allocator arg */,
const Alloc& a,
U1&& arg1, U2&& arg2)
{
// using T1 = typename T::first_type;
// using T2 = typename T::second_type;
// return std::make_tuple(
// piecewise_construct,
// uses_allocator_construction_args<T1>(a, forward<U1>(arg1)),
// uses_allocator_construction_args<T2>(a, forward<U2>(arg2)));
return uses_allocator_construction_args<T>(a, piecewise_construct,
forward_as_tuple(forward<U1>(arg1)),
forward_as_tuple(forward<U2>(arg2)));
}
} // close namespace internal
template <class T, class Alloc, class... Args>
auto uses_allocator_construction_args(const Alloc& a, Args&&... args)
{
using namespace internal;
return uses_allocator_args_imp<T>(is_pair<T>(),
has_allocator<T, Alloc>(),
is_constructible<T, allocator_arg_t,
Alloc, Args...>(),
a, std::forward<Args>(args)...);
}
template <class T, class Alloc, class... Args>
T make_obj_using_allocator(const Alloc& a, Args&&... args)
{
return make_from_tuple<T>(
uses_allocator_construction_args<T>(a, forward<Args>(args)...));
}
template <class T, class Alloc, class... Args>
T* uninitialized_construct_using_allocator(T* p,
const Alloc& a,
Args&&... args)
{
return apply([p](auto&&... args2){
return ::new(static_cast<void*>(p))
T(forward<decltype(args2)>(args2)...);
}, uses_allocator_construction_args<T>(a, forward<Args>(args)...));
}
} // close namespace Cpp20
} // close namespace std
#endif // ! defined(INCLUDED_USES_ALLOCATOR_DOT_H)