any 19 KB

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