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