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
|
// -*- C++ -*-
//===------------------------------ any -----------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP_ANY
#define _LIBCPP_ANY
/*
any synopsis
namespace std {
class bad_any_cast : public bad_cast
{
public:
virtual const char* what() const noexcept;
};
class any
{
public:
// 6.3.1 any construct/destruct
any() noexcept;
any(const any& other);
any(any&& other) noexcept;
template <class ValueType>
any(ValueType&& value);
~any();
// 6.3.2 any assignments
any& operator=(const any& rhs);
any& operator=(any&& rhs) noexcept;
template <class ValueType>
any& operator=(ValueType&& rhs);
// 6.3.3 any modifiers
template <class ValueType, class... Args>
decay_t<ValueType>& emplace(Args&&... args);
template <class ValueType, class U, class... Args>
decay_t<ValueType>& emplace(initializer_list<U>, Args&&...);
void reset() noexcept;
void swap(any& rhs) noexcept;
// 6.3.4 any observers
bool has_value() const noexcept;
const type_info& type() const noexcept;
};
// 6.4 Non-member functions
void swap(any& x, any& y) noexcept;
template <class T, class ...Args>
any make_any(Args&& ...args);
template <class T, class U, class ...Args>
any make_any(initializer_list<U>, Args&& ...args);
template<class ValueType>
ValueType any_cast(const any& operand);
template<class ValueType>
ValueType any_cast(any& operand);
template<class ValueType>
ValueType any_cast(any&& operand);
template<class ValueType>
const ValueType* any_cast(const any* operand) noexcept;
template<class ValueType>
ValueType* any_cast(any* operand) noexcept;
} // namespace std
*/
#include <experimental/__config>
#include <memory>
#include <new>
#include <typeinfo>
#include <type_traits>
#include <cstdlib>
#include <version>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif
namespace std {
class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_ANY_CAST bad_any_cast : public bad_cast
{
public:
virtual const char* what() const _NOEXCEPT;
};
} // namespace std
_LIBCPP_BEGIN_NAMESPACE_STD
#if _LIBCPP_STD_VER > 14
_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
void __throw_bad_any_cast()
{
#ifndef _LIBCPP_NO_EXCEPTIONS
throw bad_any_cast();
#else
_VSTD::abort();
#endif
}
// Forward declarations
class _LIBCPP_TEMPLATE_VIS any;
template <class _ValueType>
_LIBCPP_INLINE_VISIBILITY
add_pointer_t<add_const_t<_ValueType>>
any_cast(any const *) _NOEXCEPT;
template <class _ValueType>
_LIBCPP_INLINE_VISIBILITY
add_pointer_t<_ValueType> any_cast(any *) _NOEXCEPT;
namespace __any_imp
{
using _Buffer = aligned_storage_t<3*sizeof(void*), alignment_of<void*>::value>;
template <class _Tp>
using _IsSmallObject = integral_constant<bool
, sizeof(_Tp) <= sizeof(_Buffer)
&& alignment_of<_Buffer>::value
% alignment_of<_Tp>::value == 0
&& is_nothrow_move_constructible<_Tp>::value
>;
enum class _Action {
_Destroy,
_Copy,
_Move,
_Get,
_TypeInfo
};
template <class _Tp> struct _SmallHandler;
template <class _Tp> struct _LargeHandler;
template <class _Tp>
struct _LIBCPP_TEMPLATE_VIS __unique_typeinfo { static constexpr int __id = 0; };
template <class _Tp> constexpr int __unique_typeinfo<_Tp>::__id;
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
constexpr const void* __get_fallback_typeid() {
return &__unique_typeinfo<decay_t<_Tp>>::__id;
}
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
bool __compare_typeid(type_info const* __id, const void* __fallback_id)
{
#if !defined(_LIBCPP_NO_RTTI)
if (__id && *__id == typeid(_Tp))
return true;
#endif
if (!__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>())
return true;
return false;
}
template <class _Tp>
using _Handler = conditional_t<
_IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>;
} // namespace __any_imp
class _LIBCPP_TEMPLATE_VIS any
{
public:
// construct/destruct
_LIBCPP_INLINE_VISIBILITY
constexpr any() _NOEXCEPT : __h(nullptr) {}
_LIBCPP_INLINE_VISIBILITY
any(any const & __other) : __h(nullptr)
{
if (__other.__h) __other.__call(_Action::_Copy, this);
}
_LIBCPP_INLINE_VISIBILITY
any(any && __other) _NOEXCEPT : __h(nullptr)
{
if (__other.__h) __other.__call(_Action::_Move, this);
}
template <
class _ValueType
, class _Tp = decay_t<_ValueType>
, class = enable_if_t<
!is_same<_Tp, any>::value &&
!__is_inplace_type<_ValueType>::value &&
is_copy_constructible<_Tp>::value>
>
_LIBCPP_INLINE_VISIBILITY
any(_ValueType && __value);
template <class _ValueType, class ..._Args,
class _Tp = decay_t<_ValueType>,
class = enable_if_t<
is_constructible<_Tp, _Args...>::value &&
is_copy_constructible<_Tp>::value
>
>
_LIBCPP_INLINE_VISIBILITY
explicit any(in_place_type_t<_ValueType>, _Args&&... __args);
template <class _ValueType, class _Up, class ..._Args,
class _Tp = decay_t<_ValueType>,
class = enable_if_t<
is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
is_copy_constructible<_Tp>::value>
>
_LIBCPP_INLINE_VISIBILITY
explicit any(in_place_type_t<_ValueType>, initializer_list<_Up>, _Args&&... __args);
_LIBCPP_INLINE_VISIBILITY
~any() { this->reset(); }
// assignments
_LIBCPP_INLINE_VISIBILITY
any & operator=(any const & __rhs) {
any(__rhs).swap(*this);
return *this;
}
_LIBCPP_INLINE_VISIBILITY
any & operator=(any && __rhs) _NOEXCEPT {
any(_VSTD::move(__rhs)).swap(*this);
return *this;
}
template <
class _ValueType
, class _Tp = decay_t<_ValueType>
, class = enable_if_t<
!is_same<_Tp, any>::value
&& is_copy_constructible<_Tp>::value>
>
_LIBCPP_INLINE_VISIBILITY
any & operator=(_ValueType && __rhs);
template <class _ValueType, class ..._Args,
class _Tp = decay_t<_ValueType>,
class = enable_if_t<
is_constructible<_Tp, _Args...>::value &&
is_copy_constructible<_Tp>::value>
>
_LIBCPP_INLINE_VISIBILITY
_Tp& emplace(_Args&&... args);
template <class _ValueType, class _Up, class ..._Args,
class _Tp = decay_t<_ValueType>,
class = enable_if_t<
is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
is_copy_constructible<_Tp>::value>
>
_LIBCPP_INLINE_VISIBILITY
_Tp& emplace(initializer_list<_Up>, _Args&&...);
// 6.3.3 any modifiers
_LIBCPP_INLINE_VISIBILITY
void reset() _NOEXCEPT { if (__h) this->__call(_Action::_Destroy); }
_LIBCPP_INLINE_VISIBILITY
void swap(any & __rhs) _NOEXCEPT;
// 6.3.4 any observers
_LIBCPP_INLINE_VISIBILITY
bool has_value() const _NOEXCEPT { return __h != nullptr; }
#if !defined(_LIBCPP_NO_RTTI)
_LIBCPP_INLINE_VISIBILITY
const type_info & type() const _NOEXCEPT {
if (__h) {
return *static_cast<type_info const *>(this->__call(_Action::_TypeInfo));
} else {
return typeid(void);
}
}
#endif
private:
typedef __any_imp::_Action _Action;
using _HandleFuncPtr = void* (*)(_Action, any const *, any *, const type_info *,
const void* __fallback_info);
union _Storage {
constexpr _Storage() : __ptr(nullptr) {}
void * __ptr;
__any_imp::_Buffer __buf;
};
_LIBCPP_INLINE_VISIBILITY
void * __call(_Action __a, any * __other = nullptr,
type_info const * __info = nullptr,
const void* __fallback_info = nullptr) const
{
return __h(__a, this, __other, __info, __fallback_info);
}
_LIBCPP_INLINE_VISIBILITY
void * __call(_Action __a, any * __other = nullptr,
type_info const * __info = nullptr,
const void* __fallback_info = nullptr)
{
return __h(__a, this, __other, __info, __fallback_info);
}
template <class>
friend struct __any_imp::_SmallHandler;
template <class>
friend struct __any_imp::_LargeHandler;
template <class _ValueType>
friend add_pointer_t<add_const_t<_ValueType>>
any_cast(any const *) _NOEXCEPT;
template <class _ValueType>
friend add_pointer_t<_ValueType>
any_cast(any *) _NOEXCEPT;
_HandleFuncPtr __h = nullptr;
_Storage __s;
};
namespace __any_imp
{
template <class _Tp>
struct _LIBCPP_TEMPLATE_VIS _SmallHandler
{
_LIBCPP_INLINE_VISIBILITY
static void* __handle(_Action __act, any const * __this, any * __other,
type_info const * __info, const void* __fallback_info)
{
switch (__act)
{
case _Action::_Destroy:
__destroy(const_cast<any &>(*__this));
return nullptr;
case _Action::_Copy:
__copy(*__this, *__other);
return nullptr;
case _Action::_Move:
__move(const_cast<any &>(*__this), *__other);
return nullptr;
case _Action::_Get:
return __get(const_cast<any &>(*__this), __info, __fallback_info);
case _Action::_TypeInfo:
return __type_info();
}
}
template <class ..._Args>
_LIBCPP_INLINE_VISIBILITY
static _Tp& __create(any & __dest, _Args&&... __args) {
_Tp* __ret = ::new (static_cast<void*>(&__dest.__s.__buf)) _Tp(_VSTD::forward<_Args>(__args)...);
__dest.__h = &_SmallHandler::__handle;
return *__ret;
}
private:
_LIBCPP_INLINE_VISIBILITY
static void __destroy(any & __this) {
_Tp & __value = *static_cast<_Tp *>(static_cast<void*>(&__this.__s.__buf));
__value.~_Tp();
__this.__h = nullptr;
}
_LIBCPP_INLINE_VISIBILITY
static void __copy(any const & __this, any & __dest) {
_SmallHandler::__create(__dest, *static_cast<_Tp const *>(
static_cast<void const *>(&__this.__s.__buf)));
}
_LIBCPP_INLINE_VISIBILITY
static void __move(any & __this, any & __dest) {
_SmallHandler::__create(__dest, _VSTD::move(
*static_cast<_Tp*>(static_cast<void*>(&__this.__s.__buf))));
__destroy(__this);
}
_LIBCPP_INLINE_VISIBILITY
static void* __get(any & __this,
type_info const * __info,
const void* __fallback_id)
{
if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id))
return static_cast<void*>(&__this.__s.__buf);
return nullptr;
}
_LIBCPP_INLINE_VISIBILITY
static void* __type_info()
{
#if !defined(_LIBCPP_NO_RTTI)
return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
#else
return nullptr;
#endif
}
};
template <class _Tp>
struct _LIBCPP_TEMPLATE_VIS _LargeHandler
{
_LIBCPP_INLINE_VISIBILITY
static void* __handle(_Action __act, any const * __this,
any * __other, type_info const * __info,
void const* __fallback_info)
{
switch (__act)
{
case _Action::_Destroy:
__destroy(const_cast<any &>(*__this));
return nullptr;
case _Action::_Copy:
__copy(*__this, *__other);
return nullptr;
case _Action::_Move:
__move(const_cast<any &>(*__this), *__other);
return nullptr;
case _Action::_Get:
return __get(const_cast<any &>(*__this), __info, __fallback_info);
case _Action::_TypeInfo:
return __type_info();
}
}
template <class ..._Args>
_LIBCPP_INLINE_VISIBILITY
static _Tp& __create(any & __dest, _Args&&... __args) {
typedef allocator<_Tp> _Alloc;
typedef __allocator_destructor<_Alloc> _Dp;
_Alloc __a;
unique_ptr<_Tp, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
_Tp* __ret = ::new ((void*)__hold.get()) _Tp(_VSTD::forward<_Args>(__args)...);
__dest.__s.__ptr = __hold.release();
__dest.__h = &_LargeHandler::__handle;
return *__ret;
}
private:
_LIBCPP_INLINE_VISIBILITY
static void __destroy(any & __this){
delete static_cast<_Tp*>(__this.__s.__ptr);
__this.__h = nullptr;
}
_LIBCPP_INLINE_VISIBILITY
static void __copy(any const & __this, any & __dest) {
_LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s.__ptr));
}
_LIBCPP_INLINE_VISIBILITY
static void __move(any & __this, any & __dest) {
__dest.__s.__ptr = __this.__s.__ptr;
__dest.__h = &_LargeHandler::__handle;
__this.__h = nullptr;
}
_LIBCPP_INLINE_VISIBILITY
static void* __get(any & __this, type_info const * __info,
void const* __fallback_info)
{
if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info))
return static_cast<void*>(__this.__s.__ptr);
return nullptr;
}
_LIBCPP_INLINE_VISIBILITY
static void* __type_info()
{
#if !defined(_LIBCPP_NO_RTTI)
return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
#else
return nullptr;
#endif
}
};
} // namespace __any_imp
template <class _ValueType, class _Tp, class>
any::any(_ValueType && __v) : __h(nullptr)
{
__any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_ValueType>(__v));
}
template <class _ValueType, class ..._Args, class _Tp, class>
any::any(in_place_type_t<_ValueType>, _Args&&... __args) {
__any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
};
template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
any::any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) {
__any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
}
template <class _ValueType, class, class>
inline _LIBCPP_INLINE_VISIBILITY
any & any::operator=(_ValueType && __v)
{
any(_VSTD::forward<_ValueType>(__v)).swap(*this);
return *this;
}
template <class _ValueType, class ..._Args, class _Tp, class>
inline _LIBCPP_INLINE_VISIBILITY
_Tp& any::emplace(_Args&&... __args) {
reset();
return __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
}
template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
inline _LIBCPP_INLINE_VISIBILITY
_Tp& any::emplace(initializer_list<_Up> __il, _Args&&... __args) {
reset();
return __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
}
inline _LIBCPP_INLINE_VISIBILITY
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
inline _LIBCPP_INLINE_VISIBILITY
void swap(any & __lhs, any & __rhs) _NOEXCEPT
{
__lhs.swap(__rhs);
}
template <class _Tp, class ..._Args>
inline _LIBCPP_INLINE_VISIBILITY
any make_any(_Args&&... __args) {
return any(in_place_type<_Tp>, _VSTD::forward<_Args>(__args)...);
}
template <class _Tp, class _Up, class ..._Args>
inline _LIBCPP_INLINE_VISIBILITY
any make_any(initializer_list<_Up> __il, _Args&&... __args) {
return any(in_place_type<_Tp>, __il, _VSTD::forward<_Args>(__args)...);
}
template <class _ValueType>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
_ValueType any_cast(any const & __v)
{
using _RawValueType = __uncvref_t<_ValueType>;
static_assert(is_constructible<_ValueType, _RawValueType const &>::value,
"ValueType is required to be a const lvalue reference "
"or a CopyConstructible type");
auto __tmp = _VSTD::any_cast<add_const_t<_RawValueType>>(&__v);
if (__tmp == nullptr)
__throw_bad_any_cast();
return static_cast<_ValueType>(*__tmp);
}
template <class _ValueType>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
_ValueType any_cast(any & __v)
{
using _RawValueType = __uncvref_t<_ValueType>;
static_assert(is_constructible<_ValueType, _RawValueType &>::value,
"ValueType is required to be an lvalue reference "
"or a CopyConstructible type");
auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
if (__tmp == nullptr)
__throw_bad_any_cast();
return static_cast<_ValueType>(*__tmp);
}
template <class _ValueType>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
_ValueType any_cast(any && __v)
{
using _RawValueType = __uncvref_t<_ValueType>;
static_assert(is_constructible<_ValueType, _RawValueType>::value,
"ValueType is required to be an rvalue reference "
"or a CopyConstructible type");
auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
if (__tmp == nullptr)
__throw_bad_any_cast();
return static_cast<_ValueType>(_VSTD::move(*__tmp));
}
template <class _ValueType>
inline _LIBCPP_INLINE_VISIBILITY
add_pointer_t<add_const_t<_ValueType>>
any_cast(any const * __any) _NOEXCEPT
{
static_assert(!is_reference<_ValueType>::value,
"_ValueType may not be a reference.");
return _VSTD::any_cast<_ValueType>(const_cast<any *>(__any));
}
template <class _RetType>
inline _LIBCPP_INLINE_VISIBILITY
_RetType __pointer_or_func_cast(void* __p, /*IsFunction*/false_type) noexcept {
return static_cast<_RetType>(__p);
}
template <class _RetType>
inline _LIBCPP_INLINE_VISIBILITY
_RetType __pointer_or_func_cast(void*, /*IsFunction*/true_type) noexcept {
return nullptr;
}
template <class _ValueType>
add_pointer_t<_ValueType>
any_cast(any * __any) _NOEXCEPT
{
using __any_imp::_Action;
static_assert(!is_reference<_ValueType>::value,
"_ValueType may not be a reference.");
typedef typename add_pointer<_ValueType>::type _ReturnType;
if (__any && __any->__h) {
void *__p = __any->__call(_Action::_Get, nullptr,
#if !defined(_LIBCPP_NO_RTTI)
&typeid(_ValueType),
#else
nullptr,
#endif
__any_imp::__get_fallback_typeid<_ValueType>());
return _VSTD::__pointer_or_func_cast<_ReturnType>(
__p, is_function<_ValueType>{});
}
return nullptr;
}
#endif // _LIBCPP_STD_VER > 14
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP_ANY
|