-
Notifications
You must be signed in to change notification settings - Fork 7
/
any.hpp
683 lines (599 loc) · 20.9 KB
/
any.hpp
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// solid/utility/any.hpp
//
// Copyright (c) 2016,2020 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#pragma once
#include <algorithm>
#include <cstddef>
#include <typeindex>
#include <typeinfo>
#include <utility>
#include "solid/system/exception.hpp"
#include "solid/system/log.hpp"
#include "solid/utility/common.hpp"
#include "solid/utility/typetraits.hpp"
namespace solid {
inline constexpr size_t any_default_data_size = 3 * sizeof(void*);
inline constexpr size_t any_size_from_sizeof(const size_t _sizeof)
{
return _sizeof - sizeof(void*);
}
template <class T>
inline constexpr const T& any_max(const T& a, const T& b)
{
return (a < b) ? b : a;
}
template <size_t DataSize = any_default_data_size>
class Any;
template <class T>
struct is_any;
template <size_t V>
struct is_any<Any<V>> : std::true_type {
};
template <class T>
struct is_any : std::false_type {
};
namespace any_impl {
enum struct RepresentationE : uintptr_t {
None = 0,
Small,
Big,
};
constexpr uintptr_t representation_mask = 3;
constexpr uintptr_t representation_and_flags_mask = representation_mask;
struct SmallRTTI;
struct BigRTTI {
using DestroyFncT = void(void*);
using CopyFncT = RepresentationE(const void*, void*, const size_t, const SmallRTTI*&, void*&, const BigRTTI*&);
using MoveFncT = RepresentationE(void*, void*, const size_t, const SmallRTTI*&, void*&, const BigRTTI*&);
using GetIfFncT = const void*(const std::type_index&, const void*);
template <class T>
static void destroy(void* const _what) noexcept
{
//::delete static_cast<T*>(_what);
std::destroy_at(std::launder(static_cast<T*>(_what)));
}
template <class T>
static void* move(void* const _from)
{
return ::new T(std::move(*static_cast<T*>(_from)));
}
DestroyFncT* pdestroy_fnc_;
CopyFncT* pcopy_fnc_;
MoveFncT* pmove_fnc_;
GetIfFncT* pget_if_fnc_;
const bool is_copyable_;
const bool is_movable_;
const bool is_tuple_;
};
struct SmallRTTI {
using DestroyFncT = void(void*) noexcept;
using CopyFncT = RepresentationE(const void*, void*, const size_t, const SmallRTTI*&, void*&, const BigRTTI*&);
using MoveFncT = RepresentationE(void*, void*, const size_t, const SmallRTTI*&, void*&, const BigRTTI*&);
using GetIfFncT = const void*(const std::type_index&, const void*);
template <class T>
static void destroy(void* const _what) noexcept
{
static_cast<T*>(_what)->~T();
}
static void dummy_destroy(void* const _what) noexcept
{
}
DestroyFncT* pdestroy_fnc_;
CopyFncT* pcopy_fnc_;
MoveFncT* pmove_fnc_;
GetIfFncT* pget_if_fnc_;
const bool is_copyable_;
const bool is_movable_;
const bool is_tuple_;
};
template <class T>
RepresentationE do_copy(
const void* _pfrom,
void* _pto_small, const size_t _small_cap, const SmallRTTI*& _rpsmall_rtti,
void*& _rpto_big, const BigRTTI*& _rpbig_rtti);
template <class T>
RepresentationE do_move(
void* _pfrom,
void* _pto_small, const size_t _small_cap, const SmallRTTI*& _rpsmall_rtti,
void*& _rpto_big, const BigRTTI*& _rpbig_rtti);
template <class T>
RepresentationE do_move_big(
void* _pfrom,
void* _pto_small, const size_t _small_cap, const SmallRTTI*& _rpsmall_rtti,
void*& _rpto_big, const BigRTTI*& _rpbig_rtti);
template <class T>
const void* do_get_if(const std::type_index& _type_index, const void* _pdata);
template <class T>
inline constexpr BigRTTI big_rtti = {
&BigRTTI::destroy<T>, &do_copy<T>, &do_move_big<T>, &do_get_if<T>, std::is_copy_constructible_v<T>, std::is_move_constructible_v<T>, is_specialization_v<T, std::tuple>};
template <class T>
inline constexpr SmallRTTI small_rtti = {
&SmallRTTI::destroy<T>, &do_copy<T>, &do_move<T>, &do_get_if<T>, std::is_copy_constructible_v<T>, std::is_move_constructible_v<T>, is_specialization_v<T, std::tuple>};
inline RepresentationE do_dummy_copy(
const void* _pfrom,
void* _pto_small, const size_t _small_cap, const SmallRTTI*& _rpsmall_rtti,
void*& _rpto_big, const BigRTTI*& _rpbig_rtti)
{
return RepresentationE::None;
}
inline RepresentationE do_dummy_move(
void* _pfrom,
void* _pto_small, const size_t _small_cap, const SmallRTTI*& _rpsmall_rtti,
void*& _rpto_big, const BigRTTI*& _rpbig_rtti)
{
return RepresentationE::None;
}
inline const void* do_dummy_get_if(const std::type_index& _type_index, const void* _pdata)
{
return nullptr;
}
inline constexpr SmallRTTI dummy_small_rtti = {
&SmallRTTI::dummy_destroy, &do_dummy_copy, &do_dummy_move, &do_dummy_get_if, false, false, false};
template <class T>
RepresentationE do_copy(
const void* _pfrom,
void* _pto_small, const size_t _small_cap, const SmallRTTI*& _rpsmall_rtti,
void*& _rpto_big, const BigRTTI*& _rpbig_rtti)
{
if constexpr (alignof(T) <= alignof(max_align_t) && std::is_copy_constructible_v<T>) {
if (sizeof(T) <= _small_cap) {
T& rdst = *static_cast<T*>(_pto_small);
const T& rsrc = *static_cast<const T*>(_pfrom);
::new (const_cast<void*>(static_cast<const volatile void*>(std::addressof(rdst)))) T(rsrc);
_rpsmall_rtti = &small_rtti<T>;
return RepresentationE::Small;
} else {
_rpto_big = ::new T(*static_cast<const T*>(_pfrom));
_rpbig_rtti = &big_rtti<T>;
return RepresentationE::Big;
}
} else if constexpr (std::is_trivially_constructible_v<T> || std::is_copy_constructible_v<T>) {
_rpto_big = ::new T(*static_cast<const T*>(_pfrom));
_rpbig_rtti = &big_rtti<T>;
return RepresentationE::Big;
} else {
solid_throw("Any: contained value not copyable");
return RepresentationE::None;
}
}
template <class T>
RepresentationE do_move(
void* _pfrom,
void* _pto_small, const size_t _small_cap, const SmallRTTI*& _rpsmall_rtti,
void*& _rpto_big, const BigRTTI*& _rpbig_rtti)
{
if constexpr (alignof(T) <= alignof(max_align_t) && std::is_move_constructible_v<T>) {
if (sizeof(T) <= _small_cap) {
T& rdst = *static_cast<T*>(_pto_small);
T& rsrc = *static_cast<T*>(_pfrom);
::new (const_cast<void*>(static_cast<const volatile void*>(std::addressof(rdst)))) T{std::move(rsrc)};
_rpsmall_rtti = &small_rtti<T>;
return RepresentationE::Small;
} else {
_rpto_big = ::new T{std::move(*static_cast<T*>(_pfrom))};
_rpbig_rtti = &big_rtti<T>;
return RepresentationE::Big;
}
} else if constexpr (std::is_move_constructible_v<T>) {
_rpto_big = ::new T{std::move(*static_cast<T*>(_pfrom))};
_rpbig_rtti = &big_rtti<T>;
return RepresentationE::Big;
} else {
solid_throw("Any: contained value not movable");
return RepresentationE::None;
}
}
template <class T>
RepresentationE do_move_big(
void* _pfrom,
void* _pto_small, const size_t _small_cap, const SmallRTTI*& _rpsmall_rtti,
void*& _rpto_big, const BigRTTI*& _rpbig_rtti)
{
if constexpr (alignof(T) <= alignof(max_align_t) && std::is_move_constructible_v<T>) {
if (sizeof(T) <= _small_cap) {
T& rdst = *static_cast<T*>(_pto_small);
T& rsrc = *static_cast<T*>(_pfrom);
::new (const_cast<void*>(static_cast<const volatile void*>(std::addressof(rdst)))) T{std::move(rsrc)};
_rpsmall_rtti = &small_rtti<T>;
return RepresentationE::Small;
} else {
_rpto_big = static_cast<T*>(_pfrom); //::new T{ std::move(*static_cast<T*>(_pfrom)) };
_rpbig_rtti = &big_rtti<T>;
return RepresentationE::Big;
}
} else {
_rpto_big = static_cast<T*>(_pfrom); //::new T{ std::move(*static_cast<T*>(_pfrom)) };
_rpbig_rtti = &big_rtti<T>;
return RepresentationE::Big;
}
}
template <typename Tuple, size_t Index = 0>
const void* tuple_get_if_helper(const Tuple& _rtuple, const std::type_index& _type_index)
{
if constexpr (Index < std::tuple_size_v<Tuple>) {
if (_type_index == std::type_index(typeid(std::tuple_element_t<Index, Tuple>))) {
return &std::get<Index>(_rtuple);
}
return tuple_get_if_helper<Tuple, Index + 1>(_rtuple, _type_index);
}
return nullptr;
}
template <class T>
const void* do_get_if(const std::type_index& _type_index, const void* _pdata)
{
if constexpr (is_specialization_v<T, std::tuple>) {
return tuple_get_if_helper<>(*static_cast<const T*>(_pdata), _type_index);
} else if (std::type_index(typeid(T)) == _type_index) {
return _pdata;
}
return nullptr;
}
constexpr size_t compute_small_capacity(const size_t _req_capacity)
{
const size_t end_capacity = sizeof(uintptr_t) + sizeof(void*);
const size_t req_capacity = any_max(_req_capacity, any_max(end_capacity, sizeof(max_align_t)) - end_capacity);
const size_t tot_capacity = padded_size(req_capacity + sizeof(uintptr_t) + sizeof(void*), alignof(max_align_t));
return tot_capacity - end_capacity;
}
} // namespace any_impl
template <size_t DataSize>
class Any {
static constexpr size_t small_capacity = any_impl::compute_small_capacity(any_max(sizeof(void*) * 3, DataSize));
static constexpr size_t big_padding = small_capacity - sizeof(void*);
struct Small {
unsigned char data_[small_capacity];
const any_impl::SmallRTTI* prtti_;
};
struct Big {
unsigned char padding_[big_padding];
void* ptr_;
const any_impl::BigRTTI* prtti_;
};
struct Storage {
union {
Small small_;
Big big_;
};
uintptr_t type_data_;
};
union {
Storage storage_{};
max_align_t dummy_;
};
template <size_t S>
friend class Any;
private:
any_impl::RepresentationE representation() const noexcept
{
return static_cast<any_impl::RepresentationE>(storage_.type_data_ & any_impl::representation_and_flags_mask);
}
void representation(const any_impl::RepresentationE _repr) noexcept
{
storage_.type_data_ &= (~any_impl::representation_and_flags_mask);
storage_.type_data_ |= static_cast<uintptr_t>(_repr);
}
const std::type_info* typeInfo() const noexcept
{
return reinterpret_cast<const std::type_info*>(storage_.type_data_ & ~any_impl::representation_and_flags_mask);
}
public:
using ThisT = Any<DataSize>;
static constexpr size_t smallCapacity()
{
return small_capacity;
}
template <class T>
static constexpr bool is_small_type()
{
return alignof(T) <= alignof(max_align_t) && sizeof(T) <= small_capacity;
}
constexpr Any() noexcept
{
storage_.type_data_ = 0;
}
Any(const ThisT& _other)
{
doCopyFrom(_other);
}
template <size_t Sz>
Any(const Any<Sz>& _other)
{
doCopyFrom(_other);
}
Any(ThisT&& _other) noexcept
{
doMoveFrom(_other);
}
template <size_t Sz>
Any(Any<Sz>&& _other) noexcept
{
doMoveFrom(_other);
}
template <class T, std::enable_if_t<std::conjunction_v<std::negation<is_any<std::decay_t<T>>>, std::negation<is_specialization<std::decay_t<T>, std::in_place_type_t>> /*,
std::is_copy_constructible<std::decay_t<T>>*/
>,
int>
= 0>
Any(T&& _rvalue)
{
doEmplace<std::decay_t<T>>(std::forward<T>(_rvalue));
}
template <class T, class... Args,
std::enable_if_t<
std::conjunction_v<std::is_constructible<std::decay_t<T>, Args...> /*, std::is_copy_constructible<std::decay_t<T>>*/>,
int>
= 0>
explicit Any(std::in_place_type_t<T>, Args&&... _args)
{
doEmplace<std::decay_t<T>>(std::forward<Args>(_args)...);
}
template <class T, class E, class... Args,
std::enable_if_t<std::conjunction_v<std::is_constructible<std::decay_t<T>, std::initializer_list<E>&, Args...>,
std::is_copy_constructible<std::decay_t<T>>>,
int>
= 0>
explicit Any(std::in_place_type_t<T>, std::initializer_list<E> _ilist, Args&&... _args)
{
doEmplace<std::decay_t<T>>(_ilist, std::forward<Args>(_args)...);
}
~Any() noexcept
{
reset();
}
ThisT& operator=(const ThisT& _other)
{
*this = ThisT{_other};
return *this;
}
ThisT& operator=(ThisT&& _other) noexcept
{
reset();
doMoveFrom(_other);
return *this;
}
template <size_t Sz>
ThisT& operator=(const Any<Sz>& _other)
{
*this = ThisT{_other};
return *this;
}
template <size_t Sz>
ThisT& operator=(Any<Sz>&& _other) noexcept
{
reset();
doMoveFrom(_other);
return *this;
}
template <class T, std::enable_if_t<std::conjunction_v<std::negation<is_any<std::decay_t<T>>>, std::is_copy_constructible<std::decay_t<T>>>, int> = 0>
ThisT& operator=(T&& _rvalue)
{
*this = ThisT{std::forward<T>(_rvalue)};
return *this;
}
template <class T, class... Args,
std::enable_if_t<
std::conjunction_v<std::is_constructible<std::decay_t<T>, Args...>>,
int>
= 0>
std::decay_t<T>& emplace(Args&&... _args)
{
reset();
return doEmplace<std::decay_t<T>>(std::forward<Args>(_args)...);
}
template <class T, class E, class... Args,
std::enable_if_t<std::conjunction_v<std::is_constructible<std::decay_t<T>, std::initializer_list<E>&, Args...> /*,
is_copy_constructible<decay_t<T>>*/
>,
int>
= 0>
std::decay_t<T>& emplace(std::initializer_list<E> _ilist, Args&&... _args)
{
reset();
return doEmplace<std::decay_t<T>>(_ilist, std::forward<Args>(_args)...);
}
void reset() noexcept
{
switch (representation()) {
case any_impl::RepresentationE::Small:
storage_.small_.prtti_->pdestroy_fnc_(&storage_.small_.data_);
break;
case any_impl::RepresentationE::Big:
storage_.big_.prtti_->pdestroy_fnc_(storage_.big_.ptr_);
break;
case any_impl::RepresentationE::None:
default:
break;
}
storage_.type_data_ = 0;
}
template <size_t Sz>
void swap(Any<Sz>& _other) noexcept
{
_other = std::exchange(*this, std::move(_other));
}
bool has_value() const noexcept
{
return storage_.type_data_ != 0;
}
explicit operator bool() const noexcept
{
return has_value();
}
bool empty() const noexcept
{
return !has_value();
}
const std::type_info& type() const noexcept
{
const std::type_info* const pinfo = typeInfo();
return pinfo ? *pinfo : typeid(void);
}
template <class T>
const T* cast() const noexcept
{
const std::type_info* const pinfo = typeInfo();
if (!pinfo || std::type_index(*pinfo) != std::type_index(typeid(T))) {
return nullptr;
}
if constexpr (is_small_type<T>()) {
return reinterpret_cast<const T*>(&storage_.small_.data_);
} else {
return static_cast<const T*>(storage_.big_.ptr_);
}
}
template <class T>
T* cast() noexcept
{
return const_cast<T*>(static_cast<const ThisT*>(this)->cast<T>());
}
template <class T>
const T* get_if() const noexcept
{
switch (representation()) {
case any_impl::RepresentationE::Small:
return static_cast<const T*>(storage_.small_.prtti_->pget_if_fnc_(std::type_index(typeid(T)), &storage_.small_.data_));
case any_impl::RepresentationE::Big:
return static_cast<const T*>(storage_.big_.prtti_->pget_if_fnc_(std::type_index(typeid(T)), storage_.big_.ptr_));
default:
return nullptr;
}
}
template <class T>
T* get_if() noexcept
{
return const_cast<T*>(static_cast<const ThisT*>(this)->get_if<T>());
}
bool is_movable() const
{
if (is_small()) {
return storage_.small_.prtti_->is_movable_;
} else if (is_big()) {
return storage_.big_.prtti_->is_movable_;
}
return true;
}
bool is_copyable() const
{
if (is_small()) {
return storage_.small_.prtti_->is_copyable_;
} else if (is_big()) {
return storage_.big_.prtti_->is_copyable_;
}
return true;
}
bool is_tuple() const
{
if (is_small()) {
return storage_.small_.prtti_->is_tuple_;
} else if (is_big()) {
return storage_.big_.prtti_->is_tuple_;
}
return false;
}
bool is_small() const
{
return representation() == any_impl::RepresentationE::Small;
}
bool is_big() const
{
return representation() == any_impl::RepresentationE::Big;
}
private:
template <size_t Sz>
void doMoveFrom(Any<Sz>& _other)
{
storage_.type_data_ = _other.storage_.type_data_;
representation(any_impl::RepresentationE::None);
switch (_other.representation()) {
case any_impl::RepresentationE::Small: {
const auto repr = _other.storage_.small_.prtti_->pmove_fnc_(
_other.storage_.small_.data_,
storage_.small_.data_, small_capacity, storage_.small_.prtti_,
storage_.big_.ptr_, storage_.big_.prtti_);
representation(repr);
_other.reset();
} break;
case any_impl::RepresentationE::Big: {
const auto repr = _other.storage_.big_.prtti_->pmove_fnc_(
_other.storage_.big_.ptr_,
storage_.small_.data_, small_capacity, storage_.small_.prtti_,
storage_.big_.ptr_, storage_.big_.prtti_);
representation(repr);
if (repr == any_impl::RepresentationE::Big) {
_other.storage_.type_data_ = 0;
} else {
_other.reset();
}
} break;
default:
break;
}
}
template <size_t Sz>
void doCopyFrom(const Any<Sz>& _other)
{
storage_.type_data_ = _other.storage_.type_data_;
representation(any_impl::RepresentationE::None);
switch (_other.representation()) {
case any_impl::RepresentationE::Small: {
const auto repr = _other.storage_.small_.prtti_->pcopy_fnc_(
_other.storage_.small_.data_,
storage_.small_.data_, small_capacity, storage_.small_.prtti_,
storage_.big_.ptr_, storage_.big_.prtti_);
representation(repr);
} break;
case any_impl::RepresentationE::Big: {
const auto repr = _other.storage_.big_.prtti_->pcopy_fnc_(
_other.storage_.big_.ptr_,
storage_.small_.data_, small_capacity, storage_.small_.prtti_,
storage_.big_.ptr_, storage_.big_.prtti_);
representation(repr);
} break;
default:
break;
}
}
template <class T, class... Args>
T& doEmplace(Args&&... _args)
{
if constexpr (is_small_type<T>()) {
auto& rval = reinterpret_cast<T&>(storage_.small_.data_);
::new (const_cast<void*>(static_cast<const volatile void*>(std::addressof(rval)))) T{std::forward<Args>(_args)...};
storage_.small_.prtti_ = &any_impl::small_rtti<T>;
storage_.type_data_ = reinterpret_cast<uintptr_t>(&typeid(T));
representation(any_impl::RepresentationE::Small);
return rval;
} else {
T* const ptr = ::new T(std::forward<Args>(_args)...);
storage_.big_.ptr_ = ptr;
storage_.big_.prtti_ = &any_impl::big_rtti<T>;
storage_.type_data_ = reinterpret_cast<uintptr_t>(&typeid(T));
representation(any_impl::RepresentationE::Big);
return *ptr;
}
}
};
//-----------------------------------------------------------------------------
template <size_t S1, size_t S2>
inline void swap(Any<S1>& _a1, Any<S2>& _a2) noexcept
{
_a1.swap(_a2);
}
template <class T, size_t Size = any_default_data_size, class... Args>
Any<Size> make_any(Args&&... _args)
{
return Any<Size>{std::in_place_type<T>, std::forward<Args>(_args)...};
}
template <class T, size_t Size = any_default_data_size, class E, class... Args>
Any<Size> make_any(std::initializer_list<E> _ilist, Args&&... _args)
{
return Any<Size>{std::in_place_type<T>, _ilist, std::forward<Args>(_args)...};
}
//-----------------------------------------------------------------------------
} // namespace solid