forked from jbcoe/value_types
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polymorphic_cxx14.h
392 lines (343 loc) · 13 KB
/
polymorphic_cxx14.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
382
383
384
385
386
387
388
389
390
391
392
/* 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 <memory>
#include <type_traits>
#include <utility>
#ifndef XYZ_IN_PLACE_TYPE_DEFINED
#define XYZ_IN_PLACE_TYPE_DEFINED
namespace xyz {
template <class T>
struct in_place_type_t {};
} // namespace xyz
#endif // XYZ_IN_PLACE_TYPE_DEFINED
#ifndef XYZ_UNREACHABLE_DEFINED
#define XYZ_UNREACHABLE_DEFINED
namespace xyz {
[[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
}
} // namespace xyz
#endif // XYZ_UNREACHABLE_DEFINED
#ifndef XYZ_EMPTY_BASE_DEFINED
#define XYZ_EMPTY_BASE_DEFINED
// This is a helper class to allow empty base class optimization.
// This implementation is duplicated in compatibility/in_place_type_cxx14.h.
// These implementations must be kept in sync.
// We duplicate implementations to allow this header to work as a single
// include. https://godbolt.org needs single-file includes.
// TODO: Add tests to keep implementations in sync.
namespace xyz {
namespace detail {
template <class T, bool CanBeEmptyBaseClass =
std::is_empty<T>::value && !std::is_final<T>::value>
class empty_base_optimization {
protected:
empty_base_optimization() = default;
empty_base_optimization(const T& t) : t_(t) {}
empty_base_optimization(T&& t) : t_(std::move(t)) {}
T& get() noexcept { return t_; }
const T& get() const noexcept { return t_; }
T t_;
};
template <class T>
class empty_base_optimization<T, true> : private T {
protected:
empty_base_optimization() = default;
empty_base_optimization(const T& t) : T(t) {}
empty_base_optimization(T&& t) : T(std::move(t)) {}
T& get() noexcept { return *this; }
const T& get() const noexcept { return *this; }
};
} // namespace detail
} // namespace xyz
#endif // XYZ_EMPTY_BASE_DEFINED
namespace xyz {
namespace detail {
template <class T, class A>
struct control_block {
using allocator_traits = std::allocator_traits<A>;
typename allocator_traits::pointer p_;
virtual ~control_block() = default;
virtual void destroy(A& alloc) = 0;
virtual control_block<T, A>* clone(const A& alloc) = 0;
virtual 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> {
U u_;
public:
~direct_control_block() override = default;
template <class... Ts>
direct_control_block(Ts&&... ts) : u_(std::forward<Ts>(ts)...) {
control_block<T, A>::p_ = &u_;
}
control_block<T, A>* clone(const A& alloc) override {
using cb_allocator = typename std::allocator_traits<
A>::template rebind_alloc<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, u_);
return mem;
} catch (...) {
cb_alloc_traits::deallocate(cb_alloc, mem, 1);
throw;
}
}
control_block<T, A>* move(const A& alloc) override {
using cb_allocator = typename std::allocator_traits<
A>::template rebind_alloc<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, std::move(u_));
return mem;
} catch (...) {
cb_alloc_traits::deallocate(cb_alloc, mem, 1);
throw;
}
}
void destroy(A& alloc) override {
using cb_allocator = typename std::allocator_traits<
A>::template rebind_alloc<direct_control_block<T, U, A>>;
cb_allocator cb_alloc(alloc);
using cb_alloc_traits = std::allocator_traits<cb_allocator>;
cb_alloc_traits::destroy(cb_alloc, this);
cb_alloc_traits::deallocate(cb_alloc, this, 1);
}
};
} // namespace detail
template <class T, class A = std::allocator<T>>
class polymorphic : private detail::empty_base_optimization<A> {
using cblock_t = detail::control_block<T, A>;
cblock_t* cb_;
using allocator_traits = std::allocator_traits<A>;
using alloc_base = detail::empty_base_optimization<A>;
public:
using value_type = T;
using allocator_type = A;
using pointer = typename allocator_traits::pointer;
using const_pointer = typename allocator_traits::const_pointer;
template <typename TT = T,
typename std::enable_if<std::is_default_constructible<TT>::value,
int>::type = 0>
polymorphic(std::allocator_arg_t, const A& alloc) : alloc_base(alloc) {
using cb_allocator = typename std::allocator_traits<
A>::template rebind_alloc<detail::direct_control_block<T, T, A>>;
using cb_traits = std::allocator_traits<cb_allocator>;
cb_allocator cb_alloc(alloc_base::get());
auto mem = cb_traits::allocate(cb_alloc, 1);
try {
cb_traits::construct(cb_alloc, mem);
cb_ = mem;
} catch (...) {
cb_traits::deallocate(cb_alloc, mem, 1);
throw;
}
}
template <typename TT = T,
typename std::enable_if<std::is_default_constructible<TT>::value,
int>::type = 0,
typename AA = A,
typename std::enable_if<std::is_default_constructible<AA>::value,
int>::type = 0>
polymorphic() : polymorphic(std::allocator_arg, A()) {}
template <
class U, class... Ts,
typename std::enable_if<std::is_constructible<U, Ts&&...>::value,
int>::type = 0,
typename std::enable_if<std::is_copy_constructible<U>::value, int>::type =
0,
typename std::enable_if<std::is_base_of<T, U>::value, int>::type = 0>
polymorphic(std::allocator_arg_t, const A& alloc, in_place_type_t<U>,
Ts&&... ts)
: alloc_base(alloc) {
using cb_allocator = typename std::allocator_traits<
A>::template rebind_alloc<detail::direct_control_block<T, U, A>>;
using cb_traits = std::allocator_traits<cb_allocator>;
cb_allocator cb_alloc(alloc_base::get());
auto mem = cb_traits::allocate(cb_alloc, 1);
try {
cb_traits::construct(cb_alloc, mem, std::forward<Ts>(ts)...);
cb_ = mem;
} catch (...) {
cb_traits::deallocate(cb_alloc, mem, 1);
throw;
}
}
template <
class U, class... Ts,
typename std::enable_if<std::is_constructible<U, Ts&&...>::value,
int>::type = 0,
typename std::enable_if<std::is_copy_constructible<U>::value, int>::type =
0,
typename std::enable_if<std::is_base_of<T, U>::value, int>::type = 0,
typename AA = A,
typename std::enable_if<std::is_default_constructible<AA>::value,
int>::type = 0>
explicit polymorphic(in_place_type_t<U>, Ts&&... ts)
: polymorphic(std::allocator_arg, A(), in_place_type_t<U>{},
std::forward<Ts>(ts)...) {}
polymorphic(std::allocator_arg_t, const A& alloc, const polymorphic& other)
: alloc_base(alloc) {
if (!other.valueless_after_move()) {
cb_ = other.cb_->clone(alloc_base::get());
} else {
cb_ = nullptr;
}
}
polymorphic(const polymorphic& other)
: polymorphic(std::allocator_arg,
allocator_traits::select_on_container_copy_construction(
other.get_allocator()),
other) {}
polymorphic(
std::allocator_arg_t, const A& alloc,
polymorphic&& other) noexcept(allocator_traits::is_always_equal::value)
: alloc_base(alloc) {
if (allocator_traits::propagate_on_container_copy_assignment::value) {
cb_ = other.cb_;
other.cb_ = nullptr;
} else {
if (get_allocator() == other.get_allocator()) {
cb_ = other.cb_;
other.cb_ = nullptr;
} else {
if (!other.valueless_after_move()) {
cb_ = other.cb_->move(alloc_base::get());
} else {
cb_ = nullptr;
}
}
}
}
polymorphic(polymorphic&& other) noexcept
: polymorphic(std::allocator_arg, other.get_allocator(),
std::move(other)) {}
~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 &&
alloc_base::get() != other.alloc_base::get();
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.
auto tmp = other.cb_->clone(update_alloc ? other.alloc_base::get()
: alloc_base::get());
reset();
cb_ = tmp;
}
if (update_alloc) {
alloc_base::get() = other.alloc_base::get();
}
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 &&
alloc_base::get() != other.alloc_base::get();
if (other.valueless_after_move()) {
reset();
} else {
if (alloc_base::get() == other.alloc_base::get()) {
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.
auto tmp = other.cb_->move(update_alloc ? other.alloc_base::get()
: alloc_base::get());
reset();
cb_ = tmp;
}
}
if (update_alloc) {
alloc_base::get() = other.alloc_base::get();
}
return *this;
}
pointer operator->() noexcept {
assert(!valueless_after_move()); // LCOV_EXCL_LINE
return cb_->p_;
}
const_pointer operator->() const noexcept {
assert(!valueless_after_move()); // LCOV_EXCL_LINE
return cb_->p_;
}
T& operator*() noexcept {
assert(!valueless_after_move()); // LCOV_EXCL_LINE
return *cb_->p_;
}
const T& operator*() const noexcept {
assert(!valueless_after_move()); // LCOV_EXCL_LINE
return *cb_->p_;
}
bool valueless_after_move() const noexcept { return cb_ == nullptr; }
allocator_type get_allocator() const noexcept { return alloc_base::get(); }
void swap(polymorphic& other) noexcept(
std::allocator_traits<A>::propagate_on_container_swap::value ||
std::allocator_traits<A>::is_always_equal::value) {
if (allocator_traits::propagate_on_container_swap::value) {
// If allocators move with their allocated objects we can swap both.
std::swap(alloc_base::get(), other.alloc_base::get());
std::swap(cb_, other.cb_);
return;
} else /* */ {
if (alloc_base::get() == other.alloc_base::get()) {
std::swap(cb_, other.cb_);
} else {
unreachable(); // LCOV_EXCL_LINE
}
}
}
friend void swap(polymorphic& lhs,
polymorphic& rhs) noexcept(noexcept(lhs.swap(rhs))) {
lhs.swap(rhs);
}
private:
void reset() noexcept {
if (cb_ != nullptr) {
cb_->destroy(alloc_base::get());
cb_ = nullptr;
}
}
};
} // namespace xyz
#endif // XYZ_POLYMORPHIC_H_