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