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