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