any 20 KB

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