any 20 KB

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