any 20 KB

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