any 19 KB

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