any 21 KB

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