-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathany.hpp
604 lines (482 loc) · 13.2 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
#pragma once
#include <array>
#include <memory>
#include <cstring>
#include <type_traits>
#include <typeinfo>
#include <typeindex>
#include <cassert>
#include <sstream>
#include <string>
namespace detail { namespace static_any {
struct move_tag {};
struct copy_tag {};
enum class operation_t { query_type, query_size, copy, move, destroy };
using function_ptr_t = void(*)(operation_t operation, void* this_ptr, void* other_ptr);
}}
template <std::size_t _N>
class static_any
{
public:
template <typename _T>
struct is_static_any : public std::false_type {};
template <std::size_t _M>
struct is_static_any<static_any<_M>> : public std::true_type {};
template <class _T>
static constexpr bool is_static_any_v = is_static_any<_T>::value;
using size_type = std::size_t;
static_any();
~static_any();
template <class _T,
class = std::enable_if_t<!is_static_any_v<std::decay_t<_T>>>>
static_any(_T&&);
static_any(const static_any&);
template <std::size_t _M, class = std::enable_if_t<_M <= _N>>
static_any(const static_any<_M>&);
template <std::size_t _M, class = std::enable_if_t<_M <= _N>>
static_any(static_any<_M>&&);
template <class _T,
class = std::enable_if_t<!is_static_any_v<std::decay_t<_T>>>>
static_any& operator=(_T&& t);
static_any& operator=(const static_any& any)
{
assign_from_any(any);
return *this;
}
template <std::size_t _M, class = std::enable_if_t<_M <= _N>>
static_any& operator=(const static_any<_M>& any)
{
assign_from_any(any);
return *this;
}
template <std::size_t _M, class = std::enable_if_t<_M <= _N>>
static_any& operator=(static_any<_M>&& any)
{
assign_from_any(std::move(any));
return *this;
}
void reset();
template <class _T>
const _T& get() const;
template <class _T>
_T& get();
template <class _T>
bool has() const;
const std::type_info& type() const;
bool empty() const;
size_type size() const;
static constexpr size_type capacity();
template <class _T, class... Args>
void emplace(Args&&... args);
private:
using operation_t = detail::static_any::operation_t;
using function_ptr_t = detail::static_any::function_ptr_t;
template <class _T>
void copy_or_move(_T&& t);
template <class _T>
void assign_from_any(_T&&);
template <std::size_t _M, class CopyOrMoveTag>
void assign_from_any(const static_any<_M>&, CopyOrMoveTag);
const std::type_info& query_type() const;
size_type query_size() const;
void destroy();
template <class _T>
const _T* as() const;
template <class _T>
_T* as();
template <class _RefT>
void call_copy_or_move(void* this_void_ptr, void* other_void_ptr);
void call_operation(const function_ptr_t& function, void* this_void_ptr, void* other_void_ptr, detail::static_any::move_tag);
void call_operation(const function_ptr_t& function, void* this_void_ptr, void* other_void_ptr, detail::static_any::copy_tag);
template <class _T>
void copy_or_move_from_another(_T&&);
std::array<char, _N> __buff;
function_ptr_t __function{};
template <std::size_t _S>
friend class static_any;
template <class _ValueT, std::size_t _S>
friend _ValueT* any_cast(static_any<_S>*);
template <class _ValueT, std::size_t _S>
friend _ValueT& any_cast(static_any<_S>&);
};
namespace detail { namespace static_any {
template <class _T>
static void operation(operation_t operation, void* ptr1, void* ptr2)
{
_T* this_ptr = reinterpret_cast<_T*>(ptr1);
switch(operation)
{
case operation_t::query_type:
{
*reinterpret_cast<const std::type_info**>(ptr1) = &typeid(_T);
break;
}
case operation_t::query_size:
{
*reinterpret_cast<std::size_t*>(ptr1) = sizeof(_T);
break;
}
case operation_t::copy:
{
_T* other_ptr = reinterpret_cast<_T*>(ptr2);
assert(this_ptr);
assert(other_ptr);
new(this_ptr)_T(*other_ptr);
break;
}
case operation_t::move:
{
_T* other_ptr = reinterpret_cast<_T*>(ptr2);
assert(this_ptr);
assert(other_ptr);
new(this_ptr)_T(std::move(*other_ptr));
break;
}
case operation_t::destroy:
{
assert(this_ptr);
this_ptr->~_T();
break;
}
}
}
template <class _T>
static function_ptr_t get_function_for_type()
{
return &static_any::operation<std::remove_cv_t<std::remove_reference_t<_T>>>;
}
}}
template <std::size_t _N>
static_any<_N>::static_any()
{}
template <std::size_t _N>
static_any<_N>::~static_any()
{
destroy();
}
template <std::size_t _N>
template <class _T, class>
static_any<_N>::static_any(_T&& v)
{
copy_or_move(std::forward<_T>(v));
}
template <std::size_t _N>
static_any<_N>::static_any(const static_any<_N>& another)
{
copy_or_move_from_another(another);
}
template <std::size_t _N>
template <std::size_t _M, class>
static_any<_N>::static_any(const static_any<_M>& another)
{
copy_or_move_from_another(another);
}
template <std::size_t _N>
template <std::size_t _M, class>
static_any<_N>::static_any(static_any<_M>&& another)
{
copy_or_move_from_another(std::move(another));
}
template <std::size_t _N>
template <class _T, class>
static_any<_N>& static_any<_N>::operator=(_T&& t)
{
static_assert(capacity() >= sizeof(_T), "_T is too big to be copied to static_any");
using NonConstT = std::remove_cv_t<std::remove_reference_t<_T>>;
NonConstT* non_const_t = const_cast<NonConstT*>(&t);
static_any temp = std::move_if_noexcept(*this);
try
{
destroy();
assert(__function == nullptr);
call_copy_or_move<_T&&>(__buff.data(), non_const_t);
}
catch(...)
{
*this = std::move(temp);
throw;
}
__function = detail::static_any::get_function_for_type<_T>();
return *this;
}
template <std::size_t _N>
void static_any<_N>::reset() { destroy(); }
template <std::size_t _N>
template <class _T>
bool static_any<_N>::has() const
{
if (__function == detail::static_any::get_function_for_type<_T>())
{
return true;
}
else if (__function)
{
// need to try another, possibly more costly way, as we may compare types across DLL boundaries
return std::type_index(typeid(_T)) == std::type_index(query_type());
}
return false;
}
template <std::size_t _N>
const std::type_info& static_any<_N>::type() const
{
if (empty())
return typeid(void);
else
return query_type();
}
template <std::size_t _N>
bool static_any<_N>::empty() const { return __function == nullptr; }
template <std::size_t _N>
typename static_any<_N>::size_type static_any<_N>::size() const
{
if (empty())
return 0;
else
return query_size();
}
template <std::size_t _N>
constexpr typename static_any<_N>::size_type static_any<_N>::capacity()
{
return _N;
}
template <std::size_t _N>
template <class _T, class... Args>
void static_any<_N>::emplace(Args&&... args)
{
destroy();
new(__buff.data()) _T(std::forward<Args>(args)...);
__function = detail::static_any::get_function_for_type<_T>();
}
template <std::size_t _N>
template <class _T>
void static_any<_N>::copy_or_move(_T&& t)
{
static_assert(capacity() >= sizeof(_T), "_T is too big to be copied to static_any");
assert(__function == nullptr);
using NonConstT = std::remove_cv_t<std::remove_reference_t<_T>>;
NonConstT* non_const_t = const_cast<NonConstT*>(&t);
try {
call_copy_or_move<_T&&>(__buff.data(), non_const_t);
}
catch(...) {
throw;
}
__function = detail::static_any::get_function_for_type<_T>();
}
template <std::size_t _N>
template <class _T>
void static_any<_N>::assign_from_any(_T&& t)
{
using CopyOrMoveTag = typename std::conditional<
std::is_rvalue_reference<_T&&>::value,
detail::static_any::move_tag,
detail::static_any::copy_tag
>::type;
assign_from_any(std::forward<_T>(t), CopyOrMoveTag{});
}
template <std::size_t _N>
template <std::size_t _M, class CopyOrMoveTag>
void static_any<_N>::assign_from_any(const static_any<_M>& another, CopyOrMoveTag)
{
if (another.__function == nullptr)
return;
static_any temp = std::move_if_noexcept(*this);
void* other_data = reinterpret_cast<void*>(const_cast<char*>(another.__buff.data()));
try {
destroy();
assert(__function == nullptr);
call_operation(another.__function, __buff.data(), other_data, CopyOrMoveTag{});
}
catch(...) {
*this = std::move(temp);
throw;
}
__function= another.__function;
}
template <std::size_t _N>
const std::type_info& static_any<_N>::query_type() const
{
assert(__function != nullptr);
const std::type_info* ti ;
__function(operation_t::query_type, &ti, nullptr);
return *ti;
}
template <std::size_t _N>
typename static_any<_N>::size_type static_any<_N>::query_size() const
{
assert(__function != nullptr);
std::size_t size;
__function(operation_t::query_size, &size, nullptr);
return size;
}
template <std::size_t _N>
void static_any<_N>::destroy()
{
if (__function)
{
void* not_used = nullptr;
__function(operation_t::destroy, __buff.data(), not_used);
__function = nullptr;
}
}
template <std::size_t _N>
template <class _T>
const _T* static_any<_N>::as() const
{
return reinterpret_cast<const _T*>(__buff.data());
}
template <std::size_t _N>
template <class _T>
_T* static_any<_N>::as()
{
return reinterpret_cast<_T*>(__buff.data());
}
template <std::size_t _N>
template <class _RefT>
void static_any<_N>::call_copy_or_move(void* this_void_ptr, void* other_void_ptr)
{
using Tag = typename std::conditional<std::is_rvalue_reference<_RefT&&>::value,
detail::static_any::move_tag,
detail::static_any::copy_tag>::type;
auto function = detail::static_any::get_function_for_type<_RefT>();
call_operation(function, this_void_ptr, other_void_ptr, Tag{});
}
template <std::size_t _N>
void static_any<_N>::call_operation(const function_ptr_t& function, void* this_void_ptr, void* other_void_ptr, detail::static_any::move_tag)
{
function(operation_t::move, this_void_ptr, other_void_ptr);
}
template <std::size_t _N>
void static_any<_N>::call_operation(const function_ptr_t& function, void* this_void_ptr, void* other_void_ptr, detail::static_any::copy_tag)
{
function(operation_t::copy, this_void_ptr, other_void_ptr);
}
template <std::size_t _N>
template <class _T>
void static_any<_N>::copy_or_move_from_another(_T&& another)
{
assert(__function == nullptr);
if (another.__function == nullptr)
{
return;
}
using Tag = typename std::conditional<std::is_rvalue_reference<_T&&>::value,
detail::static_any::move_tag,
detail::static_any::copy_tag>::type;
void* other_data = reinterpret_cast<void*>(const_cast<char*>(another.__buff.data()));
try {
call_operation(another.__function, __buff.data(), other_data, Tag{});
}
catch(...) {
throw;
}
__function= another.__function;
}
class bad_any_cast : public std::bad_cast
{
public:
explicit bad_any_cast(const std::type_info& from,
const std::type_info& to);
virtual ~bad_any_cast();
const std::type_info& stored_type() const { return __from; }
const std::type_info& target_type() const { return __to; }
const char* what() const noexcept override
{
return __reason.c_str();
}
private:
const std::type_info& __from;
const std::type_info& __to;
std::string __reason;
};
bad_any_cast::bad_any_cast(const std::type_info& from,
const std::type_info& to) :
__from(from),
__to(to)
{
std::ostringstream oss;
oss << "failed conversion using any_cast: stored type "
<< from.name()
<< ", trying to cast to "
<< to.name();
__reason = oss.str();
}
bad_any_cast::~bad_any_cast() {}
template <class _ValueT,
std::size_t _S>
inline _ValueT* any_cast(static_any<_S>* a)
{
if (!a->template has<_ValueT>())
return nullptr;
return a->template as<_ValueT>();
}
template <class _ValueT,
std::size_t _S>
inline const _ValueT* any_cast(const static_any<_S>* a)
{
return any_cast<const _ValueT>(const_cast<static_any<_S>*>(a));
}
template <class _ValueT,
std::size_t _S>
inline _ValueT& any_cast(static_any<_S>& a)
{
if (!a.template has<_ValueT>())
throw bad_any_cast(a.type(), typeid(_ValueT));
return *a.template as<_ValueT>();
}
template <class _ValueT,
std::size_t _S>
inline const _ValueT& any_cast(const static_any<_S>& a)
{
return any_cast<const _ValueT>(const_cast<static_any<_S>&>(a));
}
template <std::size_t _S>
template <class _T>
const _T& static_any<_S>::get() const
{
return any_cast<_T>(*this);
}
template <std::size_t _S>
template <class _T>
_T& static_any<_S>::get()
{
return any_cast<_T>(*this);
}
template <std::size_t _N>
class static_any_t
{
public:
using size_type = std::size_t;
static constexpr size_type capacity() { return _N; }
static_any_t() = default;
static_any_t(const static_any_t&) = default;
template <class _ValueT>
static_any_t(_ValueT&& t)
{
copy(std::forward<_ValueT>(t));
}
template <class _ValueT>
static_any_t& operator=(_ValueT&& t)
{
copy(std::forward<_ValueT>(t));
return *this;
}
template <class _ValueT>
_ValueT& get() { return *reinterpret_cast<_ValueT*>(__buff.data()); }
template <class _ValueT>
const _ValueT& get() const { return *reinterpret_cast<const _ValueT*>(__buff.data()); }
private:
template <class _ValueT>
void copy(_ValueT&& t)
{
using NonConstT = std::remove_cv_t<std::remove_reference_t<_ValueT>>;
#if __GNUG__ && __GNUC__ < 5
static_assert(std::has_trivial_copy_constructor<NonConstT>::value, "_ValueT is not trivially copyable");
#else
static_assert(std::is_trivially_copyable<NonConstT>::value, "_ValueT is not trivially copyable");
#endif
static_assert(capacity() >= sizeof(_ValueT), "_ValueT is too big to be copied to static_any");
std::memcpy(__buff.data(), reinterpret_cast<char*>(&t), sizeof(_ValueT));
}
std::array<char, _N> __buff;
};