1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5// See https://llvm.org/LICENSE.txt for license information. 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_ANY 11#define _LIBCPP_ANY 12 13/* 14 any synopsis 15 16namespace std { 17 18 class bad_any_cast : public bad_cast 19 { 20 public: 21 virtual const char* what() const noexcept; 22 }; 23 24 class any 25 { 26 public: 27 28 // 6.3.1 any construct/destruct 29 any() noexcept; 30 31 any(const any& other); 32 any(any&& other) noexcept; 33 34 template <class ValueType> 35 any(ValueType&& value); 36 37 ~any(); 38 39 // 6.3.2 any assignments 40 any& operator=(const any& rhs); 41 any& operator=(any&& rhs) noexcept; 42 43 template <class ValueType> 44 any& operator=(ValueType&& rhs); 45 46 // 6.3.3 any modifiers 47 template <class ValueType, class... Args> 48 decay_t<ValueType>& emplace(Args&&... args); 49 template <class ValueType, class U, class... Args> 50 decay_t<ValueType>& emplace(initializer_list<U>, Args&&...); 51 void reset() noexcept; 52 void swap(any& rhs) noexcept; 53 54 // 6.3.4 any observers 55 bool has_value() const noexcept; 56 const type_info& type() const noexcept; 57 }; 58 59 // 6.4 Non-member functions 60 void swap(any& x, any& y) noexcept; 61 62 template <class T, class ...Args> 63 any make_any(Args&& ...args); 64 template <class T, class U, class ...Args> 65 any make_any(initializer_list<U>, Args&& ...args); 66 67 template<class ValueType> 68 ValueType any_cast(const any& operand); 69 template<class ValueType> 70 ValueType any_cast(any& operand); 71 template<class ValueType> 72 ValueType any_cast(any&& operand); 73 74 template<class ValueType> 75 const ValueType* any_cast(const any* operand) noexcept; 76 template<class ValueType> 77 ValueType* any_cast(any* operand) noexcept; 78 79} // namespace std 80 81*/ 82 83#include <__assert> // all public C++ headers provide the assertion handler 84#include <__availability> 85#include <__config> 86#include <__memory/allocator.h> 87#include <__memory/allocator_destructor.h> 88#include <__memory/allocator_traits.h> 89#include <__memory/unique_ptr.h> 90#include <__type_traits/add_const.h> 91#include <__type_traits/add_pointer.h> 92#include <__type_traits/aligned_storage.h> 93#include <__type_traits/alignment_of.h> 94#include <__type_traits/conditional.h> 95#include <__type_traits/decay.h> 96#include <__type_traits/is_constructible.h> 97#include <__type_traits/is_copy_constructible.h> 98#include <__type_traits/is_function.h> 99#include <__type_traits/is_nothrow_move_constructible.h> 100#include <__type_traits/is_reference.h> 101#include <__type_traits/is_same.h> 102#include <__type_traits/remove_cv.h> 103#include <__type_traits/remove_cvref.h> 104#include <__type_traits/remove_reference.h> 105#include <__utility/forward.h> 106#include <__utility/in_place.h> 107#include <__utility/move.h> 108#include <__utility/unreachable.h> 109#include <__verbose_abort> 110#include <initializer_list> 111#include <typeinfo> 112#include <version> 113 114#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 115# pragma GCC system_header 116#endif 117 118_LIBCPP_PUSH_MACROS 119#include <__undef_macros> 120 121namespace std { 122class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_ANY_CAST bad_any_cast : public bad_cast 123{ 124public: 125 const char* what() const _NOEXCEPT override; 126}; 127} // namespace std 128 129_LIBCPP_BEGIN_NAMESPACE_STD 130 131#if _LIBCPP_STD_VER >= 17 132 133_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY 134_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST 135void __throw_bad_any_cast() 136{ 137#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 138 throw bad_any_cast(); 139#else 140 _LIBCPP_VERBOSE_ABORT("bad_any_cast was thrown in -fno-exceptions mode"); 141#endif 142} 143 144// Forward declarations 145class _LIBCPP_TEMPLATE_VIS any; 146 147template <class _ValueType> 148_LIBCPP_INLINE_VISIBILITY 149add_pointer_t<add_const_t<_ValueType>> 150any_cast(any const *) _NOEXCEPT; 151 152template <class _ValueType> 153_LIBCPP_INLINE_VISIBILITY 154add_pointer_t<_ValueType> any_cast(any *) _NOEXCEPT; 155 156namespace __any_imp 157{ 158 _LIBCPP_SUPPRESS_DEPRECATED_PUSH 159 using _Buffer = aligned_storage_t<3*sizeof(void*), alignment_of<void*>::value>; 160 _LIBCPP_SUPPRESS_DEPRECATED_POP 161 162 template <class _Tp> 163 using _IsSmallObject = integral_constant<bool 164 , sizeof(_Tp) <= sizeof(_Buffer) 165 && alignment_of<_Buffer>::value 166 % alignment_of<_Tp>::value == 0 167 && is_nothrow_move_constructible<_Tp>::value 168 >; 169 170 enum class _Action { 171 _Destroy, 172 _Copy, 173 _Move, 174 _Get, 175 _TypeInfo 176 }; 177 178 template <class _Tp> struct _SmallHandler; 179 template <class _Tp> struct _LargeHandler; 180 181 template <class _Tp> 182 struct _LIBCPP_TEMPLATE_VIS __unique_typeinfo { static constexpr int __id = 0; }; 183 template <class _Tp> constexpr int __unique_typeinfo<_Tp>::__id; 184 185 template <class _Tp> 186 inline _LIBCPP_INLINE_VISIBILITY 187 constexpr const void* __get_fallback_typeid() { 188 return &__unique_typeinfo<remove_cv_t<remove_reference_t<_Tp>>>::__id; 189 } 190 191 template <class _Tp> 192 inline _LIBCPP_INLINE_VISIBILITY 193 bool __compare_typeid(type_info const* __id, const void* __fallback_id) 194 { 195#if !defined(_LIBCPP_HAS_NO_RTTI) 196 if (__id && *__id == typeid(_Tp)) 197 return true; 198#endif 199 if (!__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>()) 200 return true; 201 return false; 202 } 203 204 template <class _Tp> 205 using _Handler = conditional_t< 206 _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>; 207 208} // namespace __any_imp 209 210class _LIBCPP_TEMPLATE_VIS any 211{ 212public: 213 // construct/destruct 214 _LIBCPP_INLINE_VISIBILITY 215 constexpr any() _NOEXCEPT : __h_(nullptr) {} 216 217 _LIBCPP_INLINE_VISIBILITY 218 any(any const & __other) : __h_(nullptr) 219 { 220 if (__other.__h_) __other.__call(_Action::_Copy, this); 221 } 222 223 _LIBCPP_INLINE_VISIBILITY 224 any(any && __other) _NOEXCEPT : __h_(nullptr) 225 { 226 if (__other.__h_) __other.__call(_Action::_Move, this); 227 } 228 229 template < 230 class _ValueType 231 , class _Tp = decay_t<_ValueType> 232 , class = enable_if_t< 233 !is_same<_Tp, any>::value && 234 !__is_inplace_type<_ValueType>::value && 235 is_copy_constructible<_Tp>::value> 236 > 237 _LIBCPP_INLINE_VISIBILITY 238 any(_ValueType && __value); 239 240 template <class _ValueType, class ..._Args, 241 class _Tp = decay_t<_ValueType>, 242 class = enable_if_t< 243 is_constructible<_Tp, _Args...>::value && 244 is_copy_constructible<_Tp>::value 245 > 246 > 247 _LIBCPP_INLINE_VISIBILITY 248 explicit any(in_place_type_t<_ValueType>, _Args&&... __args); 249 250 template <class _ValueType, class _Up, class ..._Args, 251 class _Tp = decay_t<_ValueType>, 252 class = enable_if_t< 253 is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value && 254 is_copy_constructible<_Tp>::value> 255 > 256 _LIBCPP_INLINE_VISIBILITY 257 explicit any(in_place_type_t<_ValueType>, initializer_list<_Up>, _Args&&... __args); 258 259 _LIBCPP_INLINE_VISIBILITY 260 ~any() { this->reset(); } 261 262 // assignments 263 _LIBCPP_INLINE_VISIBILITY 264 any & operator=(any const & __rhs) { 265 any(__rhs).swap(*this); 266 return *this; 267 } 268 269 _LIBCPP_INLINE_VISIBILITY 270 any & operator=(any && __rhs) _NOEXCEPT { 271 any(_VSTD::move(__rhs)).swap(*this); 272 return *this; 273 } 274 275 template < 276 class _ValueType 277 , class _Tp = decay_t<_ValueType> 278 , class = enable_if_t< 279 !is_same<_Tp, any>::value 280 && is_copy_constructible<_Tp>::value> 281 > 282 _LIBCPP_INLINE_VISIBILITY 283 any & operator=(_ValueType && __rhs); 284 285 template <class _ValueType, class ..._Args, 286 class _Tp = decay_t<_ValueType>, 287 class = enable_if_t< 288 is_constructible<_Tp, _Args...>::value && 289 is_copy_constructible<_Tp>::value> 290 > 291 _LIBCPP_INLINE_VISIBILITY 292 _Tp& emplace(_Args&&...); 293 294 template <class _ValueType, class _Up, class ..._Args, 295 class _Tp = decay_t<_ValueType>, 296 class = enable_if_t< 297 is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value && 298 is_copy_constructible<_Tp>::value> 299 > 300 _LIBCPP_INLINE_VISIBILITY 301 _Tp& emplace(initializer_list<_Up>, _Args&&...); 302 303 // 6.3.3 any modifiers 304 _LIBCPP_INLINE_VISIBILITY 305 void reset() _NOEXCEPT { if (__h_) this->__call(_Action::_Destroy); } 306 307 _LIBCPP_INLINE_VISIBILITY 308 void swap(any & __rhs) _NOEXCEPT; 309 310 // 6.3.4 any observers 311 _LIBCPP_INLINE_VISIBILITY 312 bool has_value() const _NOEXCEPT { return __h_ != nullptr; } 313 314#if !defined(_LIBCPP_HAS_NO_RTTI) 315 _LIBCPP_INLINE_VISIBILITY 316 const type_info & type() const _NOEXCEPT { 317 if (__h_) { 318 return *static_cast<type_info const *>(this->__call(_Action::_TypeInfo)); 319 } else { 320 return typeid(void); 321 } 322 } 323#endif 324 325private: 326 typedef __any_imp::_Action _Action; 327 using _HandleFuncPtr = void* (*)(_Action, any const *, any *, const type_info *, 328 const void* __fallback_info); 329 330 union _Storage { 331 _LIBCPP_HIDE_FROM_ABI constexpr _Storage() : __ptr(nullptr) {} 332 void * __ptr; 333 __any_imp::_Buffer __buf; 334 }; 335 336 _LIBCPP_INLINE_VISIBILITY 337 void * __call(_Action __a, any * __other = nullptr, 338 type_info const * __info = nullptr, 339 const void* __fallback_info = nullptr) const 340 { 341 return __h_(__a, this, __other, __info, __fallback_info); 342 } 343 344 _LIBCPP_INLINE_VISIBILITY 345 void * __call(_Action __a, any * __other = nullptr, 346 type_info const * __info = nullptr, 347 const void* __fallback_info = nullptr) 348 { 349 return __h_(__a, this, __other, __info, __fallback_info); 350 } 351 352 template <class> 353 friend struct __any_imp::_SmallHandler; 354 template <class> 355 friend struct __any_imp::_LargeHandler; 356 357 template <class _ValueType> 358 friend add_pointer_t<add_const_t<_ValueType>> 359 any_cast(any const *) _NOEXCEPT; 360 361 template <class _ValueType> 362 friend add_pointer_t<_ValueType> 363 any_cast(any *) _NOEXCEPT; 364 365 _HandleFuncPtr __h_ = nullptr; 366 _Storage __s_; 367}; 368 369namespace __any_imp 370{ 371 template <class _Tp> 372 struct _LIBCPP_TEMPLATE_VIS _SmallHandler 373 { 374 _LIBCPP_INLINE_VISIBILITY 375 static void* __handle(_Action __act, any const * __this, any * __other, 376 type_info const * __info, const void* __fallback_info) 377 { 378 switch (__act) 379 { 380 case _Action::_Destroy: 381 __destroy(const_cast<any &>(*__this)); 382 return nullptr; 383 case _Action::_Copy: 384 __copy(*__this, *__other); 385 return nullptr; 386 case _Action::_Move: 387 __move(const_cast<any &>(*__this), *__other); 388 return nullptr; 389 case _Action::_Get: 390 return __get(const_cast<any &>(*__this), __info, __fallback_info); 391 case _Action::_TypeInfo: 392 return __type_info(); 393 } 394 __libcpp_unreachable(); 395 } 396 397 template <class ..._Args> 398 _LIBCPP_INLINE_VISIBILITY 399 static _Tp& __create(any & __dest, _Args&&... __args) { 400 typedef allocator<_Tp> _Alloc; 401 typedef allocator_traits<_Alloc> _ATraits; 402 _Alloc __a; 403 _Tp * __ret = static_cast<_Tp*>(static_cast<void*>(&__dest.__s_.__buf)); 404 _ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...); 405 __dest.__h_ = &_SmallHandler::__handle; 406 return *__ret; 407 } 408 409 private: 410 _LIBCPP_INLINE_VISIBILITY 411 static void __destroy(any & __this) { 412 typedef allocator<_Tp> _Alloc; 413 typedef allocator_traits<_Alloc> _ATraits; 414 _Alloc __a; 415 _Tp * __p = static_cast<_Tp *>(static_cast<void*>(&__this.__s_.__buf)); 416 _ATraits::destroy(__a, __p); 417 __this.__h_ = nullptr; 418 } 419 420 _LIBCPP_INLINE_VISIBILITY 421 static void __copy(any const & __this, any & __dest) { 422 _SmallHandler::__create(__dest, *static_cast<_Tp const *>( 423 static_cast<void const *>(&__this.__s_.__buf))); 424 } 425 426 _LIBCPP_INLINE_VISIBILITY 427 static void __move(any & __this, any & __dest) { 428 _SmallHandler::__create(__dest, _VSTD::move( 429 *static_cast<_Tp*>(static_cast<void*>(&__this.__s_.__buf)))); 430 __destroy(__this); 431 } 432 433 _LIBCPP_INLINE_VISIBILITY 434 static void* __get(any & __this, 435 type_info const * __info, 436 const void* __fallback_id) 437 { 438 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id)) 439 return static_cast<void*>(&__this.__s_.__buf); 440 return nullptr; 441 } 442 443 _LIBCPP_INLINE_VISIBILITY 444 static void* __type_info() 445 { 446#if !defined(_LIBCPP_HAS_NO_RTTI) 447 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp))); 448#else 449 return nullptr; 450#endif 451 } 452 }; 453 454 template <class _Tp> 455 struct _LIBCPP_TEMPLATE_VIS _LargeHandler 456 { 457 _LIBCPP_INLINE_VISIBILITY 458 static void* __handle(_Action __act, any const * __this, 459 any * __other, type_info const * __info, 460 void const* __fallback_info) 461 { 462 switch (__act) 463 { 464 case _Action::_Destroy: 465 __destroy(const_cast<any &>(*__this)); 466 return nullptr; 467 case _Action::_Copy: 468 __copy(*__this, *__other); 469 return nullptr; 470 case _Action::_Move: 471 __move(const_cast<any &>(*__this), *__other); 472 return nullptr; 473 case _Action::_Get: 474 return __get(const_cast<any &>(*__this), __info, __fallback_info); 475 case _Action::_TypeInfo: 476 return __type_info(); 477 } 478 __libcpp_unreachable(); 479 } 480 481 template <class ..._Args> 482 _LIBCPP_INLINE_VISIBILITY 483 static _Tp& __create(any & __dest, _Args&&... __args) { 484 typedef allocator<_Tp> _Alloc; 485 typedef allocator_traits<_Alloc> _ATraits; 486 typedef __allocator_destructor<_Alloc> _Dp; 487 _Alloc __a; 488 unique_ptr<_Tp, _Dp> __hold(_ATraits::allocate(__a, 1), _Dp(__a, 1)); 489 _Tp * __ret = __hold.get(); 490 _ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...); 491 __dest.__s_.__ptr = __hold.release(); 492 __dest.__h_ = &_LargeHandler::__handle; 493 return *__ret; 494 } 495 496 private: 497 498 _LIBCPP_INLINE_VISIBILITY 499 static void __destroy(any & __this){ 500 typedef allocator<_Tp> _Alloc; 501 typedef allocator_traits<_Alloc> _ATraits; 502 _Alloc __a; 503 _Tp * __p = static_cast<_Tp *>(__this.__s_.__ptr); 504 _ATraits::destroy(__a, __p); 505 _ATraits::deallocate(__a, __p, 1); 506 __this.__h_ = nullptr; 507 } 508 509 _LIBCPP_INLINE_VISIBILITY 510 static void __copy(any const & __this, any & __dest) { 511 _LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s_.__ptr)); 512 } 513 514 _LIBCPP_INLINE_VISIBILITY 515 static void __move(any & __this, any & __dest) { 516 __dest.__s_.__ptr = __this.__s_.__ptr; 517 __dest.__h_ = &_LargeHandler::__handle; 518 __this.__h_ = nullptr; 519 } 520 521 _LIBCPP_INLINE_VISIBILITY 522 static void* __get(any & __this, type_info const * __info, 523 void const* __fallback_info) 524 { 525 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info)) 526 return static_cast<void*>(__this.__s_.__ptr); 527 return nullptr; 528 529 } 530 531 _LIBCPP_INLINE_VISIBILITY 532 static void* __type_info() 533 { 534#if !defined(_LIBCPP_HAS_NO_RTTI) 535 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp))); 536#else 537 return nullptr; 538#endif 539 } 540 }; 541 542} // namespace __any_imp 543 544 545template <class _ValueType, class _Tp, class> 546any::any(_ValueType && __v) : __h_(nullptr) 547{ 548 __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_ValueType>(__v)); 549} 550 551template <class _ValueType, class ..._Args, class _Tp, class> 552any::any(in_place_type_t<_ValueType>, _Args&&... __args) { 553 __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...); 554} 555 556template <class _ValueType, class _Up, class ..._Args, class _Tp, class> 557any::any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) { 558 __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...); 559} 560 561template <class _ValueType, class, class> 562inline _LIBCPP_INLINE_VISIBILITY 563any & any::operator=(_ValueType && __v) 564{ 565 any(_VSTD::forward<_ValueType>(__v)).swap(*this); 566 return *this; 567} 568 569template <class _ValueType, class ..._Args, class _Tp, class> 570inline _LIBCPP_INLINE_VISIBILITY 571_Tp& any::emplace(_Args&&... __args) { 572 reset(); 573 return __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...); 574} 575 576template <class _ValueType, class _Up, class ..._Args, class _Tp, class> 577inline _LIBCPP_INLINE_VISIBILITY 578_Tp& any::emplace(initializer_list<_Up> __il, _Args&&... __args) { 579 reset(); 580 return __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...); 581} 582 583inline _LIBCPP_INLINE_VISIBILITY 584void any::swap(any & __rhs) _NOEXCEPT 585{ 586 if (this == &__rhs) 587 return; 588 if (__h_ && __rhs.__h_) { 589 any __tmp; 590 __rhs.__call(_Action::_Move, &__tmp); 591 this->__call(_Action::_Move, &__rhs); 592 __tmp.__call(_Action::_Move, this); 593 } 594 else if (__h_) { 595 this->__call(_Action::_Move, &__rhs); 596 } 597 else if (__rhs.__h_) { 598 __rhs.__call(_Action::_Move, this); 599 } 600} 601 602// 6.4 Non-member functions 603 604inline _LIBCPP_INLINE_VISIBILITY 605void swap(any & __lhs, any & __rhs) _NOEXCEPT 606{ 607 __lhs.swap(__rhs); 608} 609 610template <class _Tp, class ..._Args> 611inline _LIBCPP_INLINE_VISIBILITY 612any make_any(_Args&&... __args) { 613 return any(in_place_type<_Tp>, _VSTD::forward<_Args>(__args)...); 614} 615 616template <class _Tp, class _Up, class ..._Args> 617inline _LIBCPP_INLINE_VISIBILITY 618any make_any(initializer_list<_Up> __il, _Args&&... __args) { 619 return any(in_place_type<_Tp>, __il, _VSTD::forward<_Args>(__args)...); 620} 621 622template <class _ValueType> 623inline _LIBCPP_INLINE_VISIBILITY 624_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST 625_ValueType any_cast(any const & __v) 626{ 627 using _RawValueType = __remove_cvref_t<_ValueType>; 628 static_assert(is_constructible<_ValueType, _RawValueType const &>::value, 629 "ValueType is required to be a const lvalue reference " 630 "or a CopyConstructible type"); 631 auto __tmp = _VSTD::any_cast<add_const_t<_RawValueType>>(&__v); 632 if (__tmp == nullptr) 633 __throw_bad_any_cast(); 634 return static_cast<_ValueType>(*__tmp); 635} 636 637template <class _ValueType> 638inline _LIBCPP_INLINE_VISIBILITY 639_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST 640_ValueType any_cast(any & __v) 641{ 642 using _RawValueType = __remove_cvref_t<_ValueType>; 643 static_assert(is_constructible<_ValueType, _RawValueType &>::value, 644 "ValueType is required to be an lvalue reference " 645 "or a CopyConstructible type"); 646 auto __tmp = _VSTD::any_cast<_RawValueType>(&__v); 647 if (__tmp == nullptr) 648 __throw_bad_any_cast(); 649 return static_cast<_ValueType>(*__tmp); 650} 651 652template <class _ValueType> 653inline _LIBCPP_INLINE_VISIBILITY 654_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST 655_ValueType any_cast(any && __v) 656{ 657 using _RawValueType = __remove_cvref_t<_ValueType>; 658 static_assert(is_constructible<_ValueType, _RawValueType>::value, 659 "ValueType is required to be an rvalue reference " 660 "or a CopyConstructible type"); 661 auto __tmp = _VSTD::any_cast<_RawValueType>(&__v); 662 if (__tmp == nullptr) 663 __throw_bad_any_cast(); 664 return static_cast<_ValueType>(_VSTD::move(*__tmp)); 665} 666 667template <class _ValueType> 668inline _LIBCPP_INLINE_VISIBILITY 669add_pointer_t<add_const_t<_ValueType>> 670any_cast(any const * __any) _NOEXCEPT 671{ 672 static_assert(!is_reference<_ValueType>::value, 673 "_ValueType may not be a reference."); 674 return _VSTD::any_cast<_ValueType>(const_cast<any *>(__any)); 675} 676 677template <class _RetType> 678inline _LIBCPP_INLINE_VISIBILITY 679_RetType __pointer_or_func_cast(void* __p, /*IsFunction*/false_type) noexcept { 680 return static_cast<_RetType>(__p); 681} 682 683template <class _RetType> 684inline _LIBCPP_INLINE_VISIBILITY 685_RetType __pointer_or_func_cast(void*, /*IsFunction*/true_type) noexcept { 686 return nullptr; 687} 688 689template <class _ValueType> 690_LIBCPP_HIDE_FROM_ABI 691add_pointer_t<_ValueType> 692any_cast(any * __any) _NOEXCEPT 693{ 694 using __any_imp::_Action; 695 static_assert(!is_reference<_ValueType>::value, 696 "_ValueType may not be a reference."); 697 typedef add_pointer_t<_ValueType> _ReturnType; 698 if (__any && __any->__h_) { 699 void *__p = __any->__call(_Action::_Get, nullptr, 700#if !defined(_LIBCPP_HAS_NO_RTTI) 701 &typeid(_ValueType), 702#else 703 nullptr, 704#endif 705 __any_imp::__get_fallback_typeid<_ValueType>()); 706 return _VSTD::__pointer_or_func_cast<_ReturnType>( 707 __p, is_function<_ValueType>{}); 708 } 709 return nullptr; 710} 711 712#endif // _LIBCPP_STD_VER >= 17 713 714_LIBCPP_END_NAMESPACE_STD 715 716_LIBCPP_POP_MACROS 717 718#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17 719# include <chrono> 720#endif 721 722#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 723# include <atomic> 724# include <concepts> 725# include <cstdlib> 726# include <iosfwd> 727# include <iterator> 728# include <memory> 729# include <stdexcept> 730# include <type_traits> 731# include <variant> 732#endif 733 734#endif // _LIBCPP_ANY 735