forked from jbcoe/value_types
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polymorphic.h
381 lines (325 loc) · 12.9 KB
/
polymorphic.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* Copyright (c) 2016 The Value Types Authors. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==============================================================================*/
#ifndef XYZ_POLYMORPHIC_H_
#define XYZ_POLYMORPHIC_H_
#include <cassert>
#include <concepts>
#include <initializer_list>
#include <memory>
#include <utility>
#ifndef XYZ_POLYMORPHIC_HAS_EXTENDED_CONSTRUCTORS
#define XYZ_POLYMORPHIC_HAS_EXTENDED_CONSTRUCTORS 1
#endif // XYZ_POLYMORPHIC_HAS_EXTENDED_CONSTRUCTORS
namespace xyz {
#ifndef XYZ_UNREACHABLE_DEFINED
#define XYZ_UNREACHABLE_DEFINED
[[noreturn]] inline void unreachable() { // LCOV_EXCL_LINE
#if (__cpp_lib_unreachable >= 202202L)
std::unreachable(); // LCOV_EXCL_LINE
#elif defined(_MSC_VER)
__assume(false); // LCOV_EXCL_LINE
#else
__builtin_unreachable(); // LCOV_EXCL_LINE
#endif
}
#endif // XYZ_UNREACHABLE_DEFINED
namespace detail {
template <class T, class A>
struct control_block {
using allocator_traits = std::allocator_traits<A>;
typename allocator_traits::pointer p_;
virtual constexpr ~control_block() = default;
virtual constexpr void destroy(A& alloc) = 0;
virtual constexpr control_block<T, A>* clone(const A& alloc) = 0;
virtual constexpr control_block<T, A>* move(const A& alloc) = 0;
};
template <class T, class U, class A>
class direct_control_block final : public control_block<T, A> {
union uninitialized_storage {
U u_;
constexpr uninitialized_storage() {}
constexpr ~uninitialized_storage() {}
} storage_;
using cb_allocator = typename std::allocator_traits<A>::template rebind_alloc<
direct_control_block<T, U, A>>;
using cb_alloc_traits = std::allocator_traits<cb_allocator>;
public:
template <class... Ts>
constexpr direct_control_block(const A& alloc, Ts&&... ts) {
cb_allocator cb_alloc(alloc);
cb_alloc_traits::construct(cb_alloc, std::addressof(storage_.u_),
std::forward<Ts>(ts)...);
control_block<T, A>::p_ = std::addressof(storage_.u_);
}
constexpr control_block<T, A>* clone(const A& alloc) override {
cb_allocator cb_alloc(alloc);
auto mem = cb_alloc_traits::allocate(cb_alloc, 1);
try {
cb_alloc_traits::construct(cb_alloc, mem, alloc, storage_.u_);
return mem;
} catch (...) {
cb_alloc_traits::deallocate(cb_alloc, mem, 1);
throw;
}
}
constexpr control_block<T, A>* move(const A& alloc) override {
cb_allocator cb_alloc(alloc);
auto mem = cb_alloc_traits::allocate(cb_alloc, 1);
try {
cb_alloc_traits::construct(cb_alloc, mem, alloc, std::move(storage_.u_));
return mem;
} catch (...) {
cb_alloc_traits::deallocate(cb_alloc, mem, 1);
throw;
}
}
constexpr void destroy(A& alloc) override {
cb_allocator cb_alloc(alloc);
cb_alloc_traits::destroy(cb_alloc, std::addressof(storage_.u_));
cb_alloc_traits::deallocate(cb_alloc, this, 1);
}
};
} // namespace detail
template <class T, class A = std::allocator<T>>
class polymorphic {
using cblock_t = detail::control_block<T, A>;
cblock_t* cb_;
#if defined(_MSC_VER)
// https://devblogs.microsoft.com/cppblog/msvc-cpp20-and-the-std-cpp20-switch/#msvc-extensions-and-abi
[[msvc::no_unique_address]] A alloc_;
#else
[[no_unique_address]] A alloc_;
#endif
using allocator_traits = std::allocator_traits<A>;
template <class U, class... Ts>
[[nodiscard]] constexpr cblock_t* create_control_block(Ts&&... ts) const {
using cb_allocator = typename std::allocator_traits<
A>::template rebind_alloc<detail::direct_control_block<T, U, A>>;
cb_allocator cb_alloc(alloc_);
using cb_alloc_traits = std::allocator_traits<cb_allocator>;
auto mem = cb_alloc_traits::allocate(cb_alloc, 1);
try {
cb_alloc_traits::construct(cb_alloc, mem, alloc_,
std::forward<Ts>(ts)...);
return mem;
} catch (...) {
cb_alloc_traits::deallocate(cb_alloc, mem, 1);
throw;
}
}
public:
using value_type = T;
using allocator_type = A;
using pointer = typename allocator_traits::pointer;
using const_pointer = typename allocator_traits::const_pointer;
// The template type TT defers the constraint evaluation until the constructor
// is instantiated.
template <typename TT = T>
explicit constexpr polymorphic(std::allocator_arg_t, const A& alloc)
requires(std::is_default_constructible_v<TT> &&
std::is_copy_constructible_v<TT>)
: alloc_(alloc) {
cb_ = create_control_block<T>();
}
// The template type TT defers the constraint evaluation until the constructor
// is instantiated.
template <typename TT = T>
explicit constexpr polymorphic()
requires(std::is_default_constructible_v<TT> &&
std::is_copy_constructible_v<TT>)
: polymorphic(std::allocator_arg_t{}, A{}) {}
template <class U>
constexpr explicit polymorphic(std::allocator_arg_t, const A& alloc, U&& u)
requires(not std::same_as<polymorphic, std::remove_cvref_t<U>>) &&
std::copy_constructible<std::remove_cvref_t<U>> &&
std::derived_from<std::remove_cvref_t<U>, T>
: alloc_(alloc) {
cb_ = create_control_block<std::remove_cvref_t<U>>(std::forward<U>(u));
}
template <class U>
constexpr explicit polymorphic(U&& u)
requires(not std::same_as<polymorphic, std::remove_cvref_t<U>>) &&
std::copy_constructible<std::remove_cvref_t<U>> &&
std::derived_from<std::remove_cvref_t<U>, T>
: polymorphic(std::allocator_arg_t{}, A{}, std::forward<U>(u)) {}
template <class U, class... Ts>
explicit constexpr polymorphic(std::allocator_arg_t, const A& alloc,
std::in_place_type_t<U>, Ts&&... ts)
requires std::constructible_from<U, Ts&&...> &&
std::copy_constructible<U> && std::derived_from<U, T>
: alloc_(alloc) {
cb_ = create_control_block<U>(std::forward<Ts>(ts)...);
}
template <class U, class... Ts>
explicit constexpr polymorphic(std::in_place_type_t<U>, Ts&&... ts)
requires std::constructible_from<U, Ts&&...> &&
std::copy_constructible<U> && std::derived_from<U, T>
: polymorphic(std::allocator_arg_t{}, A{}, std::in_place_type<U>,
std::forward<Ts>(ts)...) {}
template <class U, class I, class... Ts>
explicit constexpr polymorphic(std::allocator_arg_t, const A& alloc,
std::in_place_type_t<U>,
std::initializer_list<I> ilist, Ts&&... ts)
requires std::constructible_from<U, std::initializer_list<I>, Ts&&...> &&
std::copy_constructible<U> && std::derived_from<U, T>
: alloc_(alloc) {
cb_ = create_control_block<U>(ilist, std::forward<Ts>(ts)...);
}
template <class U, class I, class... Ts>
explicit constexpr polymorphic(std::in_place_type_t<U>,
std::initializer_list<I> ilist, Ts&&... ts)
requires std::constructible_from<U, std::initializer_list<I>, Ts&&...> &&
std::copy_constructible<U> && std::derived_from<U, T>
: polymorphic(std::allocator_arg_t{}, A{}, std::in_place_type<U>, ilist,
std::forward<Ts>(ts)...) {}
constexpr polymorphic(std::allocator_arg_t, const A& alloc,
const polymorphic& other)
: alloc_(alloc) {
if (!other.valueless_after_move()) {
cb_ = other.cb_->clone(alloc_);
} else {
cb_ = nullptr;
}
}
constexpr polymorphic(const polymorphic& other)
: polymorphic(std::allocator_arg_t{},
allocator_traits::select_on_container_copy_construction(
other.alloc_),
other) {}
constexpr polymorphic(
std::allocator_arg_t, const A& alloc,
polymorphic&& other) noexcept(allocator_traits::is_always_equal::value)
: alloc_(alloc) {
if constexpr (allocator_traits::is_always_equal::value) {
cb_ = std::exchange(other.cb_, nullptr);
} else {
if (alloc_ == other.alloc_) {
cb_ = std::exchange(other.cb_, nullptr);
} else {
if (!other.valueless_after_move()) {
cb_ = other.cb_->move(alloc_);
} else {
cb_ = nullptr;
}
}
}
}
constexpr polymorphic(polymorphic&& other) noexcept(
allocator_traits::is_always_equal::value)
: polymorphic(std::allocator_arg_t{}, other.alloc_, std::move(other)) {}
constexpr ~polymorphic() { reset(); }
constexpr polymorphic& operator=(const polymorphic& other) {
if (this == &other) return *this;
// Check to see if the allocators need to be updated.
// We defer actually updating the allocator until later because it may be
// needed to delete the current control block.
bool update_alloc =
allocator_traits::propagate_on_container_copy_assignment::value;
if (other.valueless_after_move()) {
reset();
} else {
// Constructing a new control block could throw so we need to defer
// resetting or updating allocators until this is done.
// Inlining the allocator into the construct_from call confuses LCOV.
auto tmp = other.cb_->clone(update_alloc ? other.alloc_ : alloc_);
reset();
cb_ = tmp;
}
if (update_alloc) {
alloc_ = other.alloc_;
}
return *this;
}
constexpr polymorphic& operator=(polymorphic&& other) noexcept(
allocator_traits::propagate_on_container_move_assignment::value ||
allocator_traits::is_always_equal::value) {
if (this == &other) return *this;
// Check to see if the allocators need to be updated.
// We defer actually updating the allocator until later because it may be
// needed to delete the current control block.
bool update_alloc =
allocator_traits::propagate_on_container_move_assignment::value;
if (other.valueless_after_move()) {
reset();
} else {
if (alloc_ == other.alloc_) {
std::swap(cb_, other.cb_);
other.reset();
} else {
// Constructing a new control block could throw so we need to defer
// resetting or updating allocators until this is done.
// Inlining the allocator into the construct_from call confuses LCOV.
auto tmp = other.cb_->move(update_alloc ? other.alloc_ : alloc_);
reset();
cb_ = tmp;
}
}
if (update_alloc) {
alloc_ = other.alloc_;
}
return *this;
}
[[nodiscard]] constexpr pointer operator->() noexcept {
assert(!valueless_after_move()); // LCOV_EXCL_LINE
return cb_->p_;
}
[[nodiscard]] constexpr const_pointer operator->() const noexcept {
assert(!valueless_after_move()); // LCOV_EXCL_LINE
return cb_->p_;
}
[[nodiscard]] constexpr T& operator*() noexcept {
assert(!valueless_after_move()); // LCOV_EXCL_LINE
return *cb_->p_;
}
[[nodiscard]] constexpr const T& operator*() const noexcept {
assert(!valueless_after_move()); // LCOV_EXCL_LINE
return *cb_->p_;
}
[[nodiscard]] constexpr bool valueless_after_move() const noexcept {
return cb_ == nullptr;
}
constexpr allocator_type get_allocator() const noexcept { return alloc_; }
constexpr void swap(polymorphic& other) noexcept(
std::allocator_traits<A>::propagate_on_container_swap::value ||
std::allocator_traits<A>::is_always_equal::value) {
if constexpr (allocator_traits::propagate_on_container_swap::value) {
// If allocators move with their allocated objects, we can swap both.
std::swap(alloc_, other.alloc_);
std::swap(cb_, other.cb_);
return;
} else /* constexpr */ {
if (alloc_ == other.alloc_) {
std::swap(cb_, other.cb_);
} else {
unreachable(); // LCOV_EXCL_LINE
}
}
}
friend constexpr void swap(polymorphic& lhs, polymorphic& rhs) noexcept(
noexcept(lhs.swap(rhs))) {
lhs.swap(rhs);
}
private:
constexpr void reset() noexcept {
if (cb_ != nullptr) {
cb_->destroy(alloc_);
cb_ = nullptr;
}
}
}; // namespace xyz
} // namespace xyz
#endif // XYZ_POLYMORPHIC_H_