-
Notifications
You must be signed in to change notification settings - Fork 3
/
xllvm-any.h
594 lines (509 loc) · 15.9 KB
/
xllvm-any.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//
// xllvm-any.h
//
// A lightly-edited rewrite of the std::any class from the
// LLVM Compiler project, version 11.0.0
// https://github.com/kocienda/Any
//
//===------------------------------ any -----------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LLVM-LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef XLLVM_ANY
#define XLLVM_ANY
#include <iostream>
#include <memory>
#include <new>
#include <typeinfo>
#include <type_traits>
#include <cstdlib>
namespace XLLVM {
class bad_any_cast : public std::bad_cast {};
#ifndef XLLVM_ALWAYS_INLINE
#ifdef NDEBUG
#define XLLVM_ALWAYS_INLINE inline __attribute__ ((__visibility__("hidden"), __always_inline__))
#else
#define XLLVM_ALWAYS_INLINE inline
#endif
#endif
#ifdef __cpp_rtti
#define XLLVM_USE_TYPEINFO 1
#else
#define XLLVM_USE_TYPEINFO 0
#endif
#ifdef __cpp_exceptions
#define XLLVM_USE_EXCEPTIONS 1
#else
#define XLLVM_USE_EXCEPTIONS 0
#endif
#define XLLVM_USE(FEATURE) (defined XLLVM_USE_##FEATURE && XLLVM_USE_##FEATURE)
XLLVM_ALWAYS_INLINE
void throw_bad_any_cast()
{
#if XLLVM_USE(EXCEPTIONS)
throw bad_any_cast();
#else
abort();
#endif
}
template <class T> struct IsInPlaceType_ : std::false_type {};
template <> struct IsInPlaceType_<std::in_place_t> : std::true_type {};
template <class T> struct IsInPlaceType_<std::in_place_type_t<T>> : std::true_type {};
template <size_t S> struct IsInPlaceType_<std::in_place_index_t<S>> : std::true_type {};
template <class T>
constexpr bool IsInPlaceType = IsInPlaceType_<T>::value;
class Any;
template <class V>
XLLVM_ALWAYS_INLINE
std::add_pointer_t<std::add_const_t<V>>
any_cast(Any const *) noexcept;
template <class V>
XLLVM_ALWAYS_INLINE
std::add_pointer_t<V> any_cast(Any *) noexcept;
namespace AnyImp
{
using Buffer = std::aligned_storage_t<3 * sizeof(void *), std::alignment_of<void *>::value>;
template <class T>
using IsSmallObject = std::integral_constant<bool, sizeof(T) <= sizeof(Buffer) &&
std::alignment_of<Buffer>::value % std::alignment_of<T>::value == 0 &&
std::is_nothrow_move_constructible<T>::value>;
enum class Action {
Destroy,
Copy,
Move,
Get,
TypeInfo
};
template <class T> struct SmallHandler;
template <class T> struct LargeHandler;
template <class T>
struct unique_typeinfo { static constexpr int id = 0; };
template <class T> constexpr int unique_typeinfo<T>::id;
template <class T>
XLLVM_ALWAYS_INLINE
constexpr const void *get_fallback_typeid() {
return &unique_typeinfo<std::decay_t<T>>::id;
}
template <class T>
XLLVM_ALWAYS_INLINE
static bool compare_typeid(std::type_info const *id, const void *fallback_id) {
#if XLLVM_USE(TYPEINFO)
if (id && *id == typeid(T)) {
return true;
}
#endif
if (!id && fallback_id == AnyImp::get_fallback_typeid<T>()) {
return true;
}
return false;
}
template <class T>
using Handler = std::conditional_t<IsSmallObject<T>::value,
SmallHandler<T>, LargeHandler<T>>;
} // namespace AnyImp
class Any
{
public:
// construct/destruct
XLLVM_ALWAYS_INLINE
constexpr Any() noexcept : h(nullptr) {}
XLLVM_ALWAYS_INLINE
Any(Any const & other) : h(nullptr) {
if (other.h) {
other.call(Action::Copy, this);
}
}
XLLVM_ALWAYS_INLINE
Any(Any && other) noexcept : h(nullptr) {
if (other.h) {
other.call(Action::Move, this);
}
}
template <class V , class T = std::decay_t<V> , class = std::enable_if_t<
!std::is_same<T, Any>::value && !IsInPlaceType<V> &&
std::is_copy_constructible<T>::value>>
XLLVM_ALWAYS_INLINE
Any(V && value);
template <class V, class ...Args, class T = std::decay_t<V>, class = std::enable_if_t<
std::is_constructible<T, Args...>::value &&
std::is_copy_constructible<T>::value>>
XLLVM_ALWAYS_INLINE
explicit Any(std::in_place_type_t<V>, Args &&... args);
template <class V, class U, class ...Args,
class T = std::decay_t<V>,
class = std::enable_if_t<
std::is_constructible<T, std::initializer_list<U>&, Args...>::value &&
std::is_copy_constructible<T>::value>>
XLLVM_ALWAYS_INLINE
explicit Any(std::in_place_type_t<V>, std::initializer_list<U>, Args &&... args);
XLLVM_ALWAYS_INLINE
~Any() { this->reset(); }
// assignments
XLLVM_ALWAYS_INLINE
Any &operator=(Any const &rhs) {
Any(rhs).swap(*this);
return *this;
}
XLLVM_ALWAYS_INLINE
Any &operator=(Any &&rhs) noexcept {
Any(std::move(rhs)).swap(*this);
return *this;
}
template <class V, class T = std::decay_t<V>,
class = std::enable_if_t<!std::is_same<T, Any>::value &&
std::is_copy_constructible<T>::value>>
XLLVM_ALWAYS_INLINE
Any &operator=(V &&rhs);
template <class V, class ...Args, class T = std::decay_t<V>,
class = std::enable_if_t<std::is_constructible<T, Args...>::value &&
std::is_copy_constructible<T>::value>>
XLLVM_ALWAYS_INLINE
T &emplace(Args &&... args);
template <class V, class U, class ...Args,
class T = std::decay_t<V>,
class = std::enable_if_t<
std::is_constructible<T, std::initializer_list<U>&, Args...>::value &&
std::is_copy_constructible<T>::value>>
XLLVM_ALWAYS_INLINE
T &emplace(std::initializer_list<U>, Args &&...);
// 6.3.3 any modifiers
XLLVM_ALWAYS_INLINE
void reset() noexcept {
if (h) {
this->call(Action::Destroy);
}
}
XLLVM_ALWAYS_INLINE
void swap(Any &rhs) noexcept;
// 6.3.4 any observers
XLLVM_ALWAYS_INLINE
bool has_value() const noexcept { return h != nullptr; }
#if XLLVM_USE(TYPEINFO)
XLLVM_ALWAYS_INLINE
const std::type_info &type() const noexcept {
if (h) {
return *static_cast<std::type_info const *>(this->call(Action::TypeInfo));
}
else {
return typeid(void);
}
}
#endif
private:
typedef AnyImp::Action Action;
using HandleFuncPtr = void *(*)(Action, Any const *, Any *, const std::type_info *,
const void *fallback_id);
union Storage {
constexpr Storage() : ptr(nullptr) {}
void *ptr;
AnyImp::Buffer buf;
};
XLLVM_ALWAYS_INLINE
void *call(Action a, Any *other_any = nullptr, std::type_info const * info = nullptr,
const void *fallback_id = nullptr) const {
return h(a, this, other_any, info, fallback_id);
}
XLLVM_ALWAYS_INLINE
void *call(Action a, Any * other_any = nullptr, std::type_info const * info = nullptr,
const void *fallback_id = nullptr) {
return h(a, this, other_any, info, fallback_id);
}
template <class>
friend struct AnyImp::SmallHandler;
template <class>
friend struct AnyImp::LargeHandler;
template <class V>
friend std::add_pointer_t<std::add_const_t<V>>
any_cast(Any const *) noexcept;
template <class V>
friend std::add_pointer_t<V>
any_cast(Any *) noexcept;
HandleFuncPtr h = nullptr;
Storage s;
};
namespace AnyImp
{
template <class T>
struct SmallHandler
{
XLLVM_ALWAYS_INLINE
static void *handle(Action action, Any const *this_any, Any *other_any,
std::type_info const *info, const void *fallback_id) {
switch (action) {
case Action::Destroy:
destroy(const_cast<Any &>(*this_any));
return nullptr;
case Action::Copy:
copy(*this_any, *other_any);
return nullptr;
case Action::Move:
move(const_cast<Any &>(*this_any), *other_any);
return nullptr;
case Action::Get:
return get(const_cast<Any &>(*this_any), info, fallback_id);
case Action::TypeInfo:
return get_type_info();
}
return nullptr;
}
template <class ...Args>
XLLVM_ALWAYS_INLINE
static T &create(Any &dst, Args &&... args) {
T *ret = ::new (static_cast<void *>(&dst.s.buf)) T(std::forward<Args>(args)...);
dst.h = &SmallHandler::handle;
return *ret;
}
private:
XLLVM_ALWAYS_INLINE
static void destroy(Any &this_any) {
T &value = *static_cast<T *>(static_cast<void *>(&this_any.s.buf));
value.~T();
this_any.h = nullptr;
}
XLLVM_ALWAYS_INLINE
static void copy(Any const &this_any, Any &dst) {
SmallHandler::create(dst, *static_cast<T const *>(static_cast<void const *>(&this_any.s.buf)));
}
XLLVM_ALWAYS_INLINE
static void move(Any &this_any, Any &dst) {
SmallHandler::create(dst, std::move(*static_cast<T*>(static_cast<void *>(&this_any.s.buf))));
destroy(this_any);
}
XLLVM_ALWAYS_INLINE
static void *get(Any &this_any, std::type_info const *info, const void *fallback_id) {
if (AnyImp::compare_typeid<T>(info, fallback_id)) {
return static_cast<void *>(&this_any.s.buf);
}
return nullptr;
}
XLLVM_ALWAYS_INLINE
static void *get_type_info() {
#if XLLVM_USE(TYPEINFO)
return const_cast<void *>(static_cast<void const *>(&typeid(T)));
#else
return nullptr;
#endif
}
};
template <class T>
struct LargeHandler
{
XLLVM_ALWAYS_INLINE
static void *handle(Action action, Any const * this_any, Any *other_any,
std::type_info const *info, void const *fallback_id) {
switch (action) {
case Action::Destroy:
destroy(const_cast<Any &>(*this_any));
return nullptr;
case Action::Copy:
copy(*this_any, *other_any);
return nullptr;
case Action::Move:
move(const_cast<Any &>(*this_any), *other_any);
return nullptr;
case Action::Get:
return get(const_cast<Any &>(*this_any), info, fallback_id);
case Action::TypeInfo:
return get_type_info();
}
return nullptr;
}
template <class ...Args>
XLLVM_ALWAYS_INLINE
static T &create(Any & dst, Args &&... args) {
#if defined(__clang__)
typedef std::allocator<T> _Alloc;
typedef std::__allocator_destructor<_Alloc> D;
_Alloc a;
std::unique_ptr<T, D> hold(a.allocate(1), D(a, 1));
#else
typedef std::allocator<T> _Alloc;
_Alloc a;
std::unique_ptr<T> hold(a.allocate(1));
#endif
T* ret = ::new ((void *)hold.get()) T(std::forward<Args>(args)...);
dst.s.ptr = hold.release();
dst.h = &LargeHandler::handle;
return *ret;
}
private:
XLLVM_ALWAYS_INLINE
static void destroy(Any &this_any){
delete static_cast<T*>(this_any.s.ptr);
this_any.h = nullptr;
}
XLLVM_ALWAYS_INLINE
static void copy(Any const &this_any, Any &dst) {
LargeHandler::create(dst, *static_cast<T const *>(this_any.s.ptr));
}
XLLVM_ALWAYS_INLINE
static void move(Any &this_any, Any &dst) {
dst.s.ptr = this_any.s.ptr;
dst.h = &LargeHandler::handle;
this_any.h = nullptr;
}
XLLVM_ALWAYS_INLINE
static void *get(Any &this_any, std::type_info const *info, const void *fallback_id) {
if (AnyImp::compare_typeid<T>(info, fallback_id)) {
return static_cast<void *>(this_any.s.ptr);
}
return nullptr;
}
XLLVM_ALWAYS_INLINE
static void *get_type_info() {
#if XLLVM_USE(TYPEINFO)
return const_cast<void *>(static_cast<void const *>(&typeid(T)));
#else
return nullptr;
#endif
}
};
} // namespace AnyImp
template <class V, class T, class>
Any::Any(V && v) : h(nullptr) {
AnyImp::Handler<T>::create(*this, std::forward<V>(v));
}
template <class V, class ...Args, class T, class>
Any::Any(std::in_place_type_t<V>, Args&&... args) {
AnyImp::Handler<T>::create(*this, std::forward<Args>(args)...);
};
template <class V, class U, class ...Args, class T, class>
Any::Any(std::in_place_type_t<V>, std::initializer_list<U> il, Args&&... args) {
AnyImp::Handler<T>::create(*this, il, std::forward<Args>(args)...);
}
template <class V, class, class>
XLLVM_ALWAYS_INLINE
Any & Any::operator=(V && v) {
Any(std::forward<V>(v)).swap(*this);
return *this;
}
template <class V, class ...Args, class T, class>
XLLVM_ALWAYS_INLINE
T& Any::emplace(Args&&... args) {
reset();
return AnyImp::Handler<T>::create(*this, std::forward<Args>(args)...);
}
template <class V, class U, class ...Args, class T, class>
XLLVM_ALWAYS_INLINE
T& Any::emplace(std::initializer_list<U> il, Args&&... args) {
reset();
return AnyImp::Handler<T>::create(*this, il, std::forward<Args>(args)...);
}
XLLVM_ALWAYS_INLINE
void Any::swap(Any & rhs) noexcept {
if (this == &rhs) {
return;
}
if (h && rhs.h) {
Any tmp;
rhs.call(Action::Move, &tmp);
this->call(Action::Move, &rhs);
tmp.call(Action::Move, this);
}
else if (h) {
this->call(Action::Move, &rhs);
}
else if (rhs.h) {
rhs.call(Action::Move, this);
}
}
// 6.4 Non-member functions
XLLVM_ALWAYS_INLINE
void swap(Any &lhs, Any &rhs) noexcept {
lhs.swap(rhs);
}
template <class T, class ...Args>
XLLVM_ALWAYS_INLINE
Any make_any(Args&&... args) {
return Any(std::in_place_type<T>, std::forward<Args>(args)...);
}
template <class T, class U, class ...Args>
XLLVM_ALWAYS_INLINE
Any make_any(std::initializer_list<U> il, Args&&... args) {
return Any(std::in_place_type<T>, il, std::forward<Args>(args)...);
}
template <class V>
XLLVM_ALWAYS_INLINE
V any_cast(Any const & v)
{
using T = std::remove_cv_t<std::remove_reference_t<V>>;
static_assert(std::is_constructible<V, T const &>::value,
"V is required to be a const lvalue reference "
"or a CopyConstructible type");
auto tmp = any_cast<std::add_const_t<T>>(&v);
if (tmp == nullptr) {
throw_bad_any_cast();
}
return static_cast<V>(*tmp);
}
template <class V>
XLLVM_ALWAYS_INLINE
V any_cast(Any & v)
{
using T = std::remove_cv_t<std::remove_reference_t<V>>;
static_assert(std::is_constructible<V, T &>::value,
"V is required to be an lvalue reference "
"or a CopyConstructible type");
auto tmp = any_cast<T>(&v);
if (tmp == nullptr) {
throw_bad_any_cast();
}
return static_cast<V>(*tmp);
}
template <class V>
XLLVM_ALWAYS_INLINE
V any_cast(Any && v)
{
using T = std::remove_cv_t<std::remove_reference_t<V>>;
static_assert(std::is_constructible<V, T>::value,
"V is required to be an rvalue reference "
"or a CopyConstructible type");
auto tmp = any_cast<T>(&v);
if (tmp == nullptr) {
throw_bad_any_cast();
}
return static_cast<V>(std::move(*tmp));
}
template <class V>
XLLVM_ALWAYS_INLINE
std::add_pointer_t<std::add_const_t<V>>
any_cast(Any const *any) noexcept
{
static_assert(!std::is_reference<V>::value, "V may not be a reference.");
return any_cast<V>(const_cast<Any *>(any));
}
template <class R>
XLLVM_ALWAYS_INLINE
R pointer_or_func_cast(void *ptr, /*IsFunction*/std::false_type) noexcept {
return static_cast<R>(ptr);
}
template <class R>
XLLVM_ALWAYS_INLINE
R pointer_or_func_cast(void *, /*IsFunction*/std::true_type) noexcept {
return nullptr;
}
template <class V>
std::add_pointer_t<V>
any_cast(Any *any) noexcept
{
using AnyImp::Action;
static_assert(!std::is_reference<V>::value, "V may not be a reference.");
typedef typename std::add_pointer<V>::type R;
if (any && any->h) {
void *ptr = any->call(Action::Get, nullptr,
#if XLLVM_USE(TYPEINFO)
&typeid(V),
#else
nullptr,
#endif
AnyImp::get_fallback_typeid<V>());
return pointer_or_func_cast<R>(ptr, std::is_function<V>{});
}
return nullptr;
}
} // namespace XLLVM
#endif // XLLVM_ANY