10b57cec5SDimitry Andric// -*- C++ -*- 2349cc55cSDimitry Andric//===----------------------------------------------------------------------===// 30b57cec5SDimitry Andric// 40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information. 60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 70b57cec5SDimitry Andric// 80b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 90b57cec5SDimitry Andric 100b57cec5SDimitry Andric#ifndef _LIBCPP_ANY 110b57cec5SDimitry Andric#define _LIBCPP_ANY 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric/* 140b57cec5SDimitry Andric any synopsis 150b57cec5SDimitry Andric 160b57cec5SDimitry Andricnamespace std { 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric class bad_any_cast : public bad_cast 190b57cec5SDimitry Andric { 200b57cec5SDimitry Andric public: 210b57cec5SDimitry Andric virtual const char* what() const noexcept; 220b57cec5SDimitry Andric }; 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric class any 250b57cec5SDimitry Andric { 260b57cec5SDimitry Andric public: 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric // 6.3.1 any construct/destruct 290b57cec5SDimitry Andric any() noexcept; 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric any(const any& other); 320b57cec5SDimitry Andric any(any&& other) noexcept; 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric template <class ValueType> 350b57cec5SDimitry Andric any(ValueType&& value); 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric ~any(); 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric // 6.3.2 any assignments 400b57cec5SDimitry Andric any& operator=(const any& rhs); 410b57cec5SDimitry Andric any& operator=(any&& rhs) noexcept; 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric template <class ValueType> 440b57cec5SDimitry Andric any& operator=(ValueType&& rhs); 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric // 6.3.3 any modifiers 470b57cec5SDimitry Andric template <class ValueType, class... Args> 480b57cec5SDimitry Andric decay_t<ValueType>& emplace(Args&&... args); 490b57cec5SDimitry Andric template <class ValueType, class U, class... Args> 500b57cec5SDimitry Andric decay_t<ValueType>& emplace(initializer_list<U>, Args&&...); 510b57cec5SDimitry Andric void reset() noexcept; 520b57cec5SDimitry Andric void swap(any& rhs) noexcept; 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric // 6.3.4 any observers 550b57cec5SDimitry Andric bool has_value() const noexcept; 560b57cec5SDimitry Andric const type_info& type() const noexcept; 570b57cec5SDimitry Andric }; 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric // 6.4 Non-member functions 600b57cec5SDimitry Andric void swap(any& x, any& y) noexcept; 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric template <class T, class ...Args> 630b57cec5SDimitry Andric any make_any(Args&& ...args); 640b57cec5SDimitry Andric template <class T, class U, class ...Args> 650b57cec5SDimitry Andric any make_any(initializer_list<U>, Args&& ...args); 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric template<class ValueType> 680b57cec5SDimitry Andric ValueType any_cast(const any& operand); 690b57cec5SDimitry Andric template<class ValueType> 700b57cec5SDimitry Andric ValueType any_cast(any& operand); 710b57cec5SDimitry Andric template<class ValueType> 720b57cec5SDimitry Andric ValueType any_cast(any&& operand); 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric template<class ValueType> 750b57cec5SDimitry Andric const ValueType* any_cast(const any* operand) noexcept; 760b57cec5SDimitry Andric template<class ValueType> 770b57cec5SDimitry Andric ValueType* any_cast(any* operand) noexcept; 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric} // namespace std 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric*/ 820b57cec5SDimitry Andric 83*81ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler 84e8d8bef9SDimitry Andric#include <__availability> 85fe6060f1SDimitry Andric#include <__config> 86fe6060f1SDimitry Andric#include <__utility/forward.h> 87*81ad6265SDimitry Andric#include <__utility/in_place.h> 88*81ad6265SDimitry Andric#include <__utility/move.h> 89*81ad6265SDimitry Andric#include <__utility/unreachable.h> 900b57cec5SDimitry Andric#include <cstdlib> 91*81ad6265SDimitry Andric#include <initializer_list> 92fe6060f1SDimitry Andric#include <memory> 93fe6060f1SDimitry Andric#include <type_traits> 94fe6060f1SDimitry Andric#include <typeinfo> 950b57cec5SDimitry Andric#include <version> 960b57cec5SDimitry Andric 97*81ad6265SDimitry Andric#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES 98*81ad6265SDimitry Andric# include <chrono> 99*81ad6265SDimitry Andric#endif 100*81ad6265SDimitry Andric 1010b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 1020b57cec5SDimitry Andric# pragma GCC system_header 1030b57cec5SDimitry Andric#endif 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andricnamespace std { 1060b57cec5SDimitry Andricclass _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_ANY_CAST bad_any_cast : public bad_cast 1070b57cec5SDimitry Andric{ 1080b57cec5SDimitry Andricpublic: 1090b57cec5SDimitry Andric virtual const char* what() const _NOEXCEPT; 1100b57cec5SDimitry Andric}; 1110b57cec5SDimitry Andric} // namespace std 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY 1180b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST 1190b57cec5SDimitry Andricvoid __throw_bad_any_cast() 1200b57cec5SDimitry Andric{ 1210b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS 1220b57cec5SDimitry Andric throw bad_any_cast(); 1230b57cec5SDimitry Andric#else 1240b57cec5SDimitry Andric _VSTD::abort(); 1250b57cec5SDimitry Andric#endif 1260b57cec5SDimitry Andric} 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric// Forward declarations 1290b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS any; 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andrictemplate <class _ValueType> 1320b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY 1330b57cec5SDimitry Andricadd_pointer_t<add_const_t<_ValueType>> 1340b57cec5SDimitry Andricany_cast(any const *) _NOEXCEPT; 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andrictemplate <class _ValueType> 1370b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY 1380b57cec5SDimitry Andricadd_pointer_t<_ValueType> any_cast(any *) _NOEXCEPT; 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andricnamespace __any_imp 1410b57cec5SDimitry Andric{ 1420b57cec5SDimitry Andric using _Buffer = aligned_storage_t<3*sizeof(void*), alignment_of<void*>::value>; 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric template <class _Tp> 1450b57cec5SDimitry Andric using _IsSmallObject = integral_constant<bool 1460b57cec5SDimitry Andric , sizeof(_Tp) <= sizeof(_Buffer) 1470b57cec5SDimitry Andric && alignment_of<_Buffer>::value 1480b57cec5SDimitry Andric % alignment_of<_Tp>::value == 0 1490b57cec5SDimitry Andric && is_nothrow_move_constructible<_Tp>::value 1500b57cec5SDimitry Andric >; 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric enum class _Action { 1530b57cec5SDimitry Andric _Destroy, 1540b57cec5SDimitry Andric _Copy, 1550b57cec5SDimitry Andric _Move, 1560b57cec5SDimitry Andric _Get, 1570b57cec5SDimitry Andric _TypeInfo 1580b57cec5SDimitry Andric }; 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric template <class _Tp> struct _SmallHandler; 1610b57cec5SDimitry Andric template <class _Tp> struct _LargeHandler; 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric template <class _Tp> 1640b57cec5SDimitry Andric struct _LIBCPP_TEMPLATE_VIS __unique_typeinfo { static constexpr int __id = 0; }; 1650b57cec5SDimitry Andric template <class _Tp> constexpr int __unique_typeinfo<_Tp>::__id; 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric template <class _Tp> 1680b57cec5SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY 1690b57cec5SDimitry Andric constexpr const void* __get_fallback_typeid() { 170e8d8bef9SDimitry Andric return &__unique_typeinfo<remove_cv_t<remove_reference_t<_Tp>>>::__id; 1710b57cec5SDimitry Andric } 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric template <class _Tp> 1740b57cec5SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY 1750b57cec5SDimitry Andric bool __compare_typeid(type_info const* __id, const void* __fallback_id) 1760b57cec5SDimitry Andric { 1770b57cec5SDimitry Andric#if !defined(_LIBCPP_NO_RTTI) 1780b57cec5SDimitry Andric if (__id && *__id == typeid(_Tp)) 1790b57cec5SDimitry Andric return true; 1800b57cec5SDimitry Andric#endif 1810b57cec5SDimitry Andric if (!__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>()) 1820b57cec5SDimitry Andric return true; 1830b57cec5SDimitry Andric return false; 1840b57cec5SDimitry Andric } 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric template <class _Tp> 1870b57cec5SDimitry Andric using _Handler = conditional_t< 1880b57cec5SDimitry Andric _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>; 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric} // namespace __any_imp 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS any 1930b57cec5SDimitry Andric{ 1940b57cec5SDimitry Andricpublic: 1950b57cec5SDimitry Andric // construct/destruct 1960b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1970b57cec5SDimitry Andric constexpr any() _NOEXCEPT : __h(nullptr) {} 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2000b57cec5SDimitry Andric any(any const & __other) : __h(nullptr) 2010b57cec5SDimitry Andric { 2020b57cec5SDimitry Andric if (__other.__h) __other.__call(_Action::_Copy, this); 2030b57cec5SDimitry Andric } 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2060b57cec5SDimitry Andric any(any && __other) _NOEXCEPT : __h(nullptr) 2070b57cec5SDimitry Andric { 2080b57cec5SDimitry Andric if (__other.__h) __other.__call(_Action::_Move, this); 2090b57cec5SDimitry Andric } 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric template < 2120b57cec5SDimitry Andric class _ValueType 2130b57cec5SDimitry Andric , class _Tp = decay_t<_ValueType> 2140b57cec5SDimitry Andric , class = enable_if_t< 2150b57cec5SDimitry Andric !is_same<_Tp, any>::value && 2160b57cec5SDimitry Andric !__is_inplace_type<_ValueType>::value && 2170b57cec5SDimitry Andric is_copy_constructible<_Tp>::value> 2180b57cec5SDimitry Andric > 2190b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2200b57cec5SDimitry Andric any(_ValueType && __value); 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric template <class _ValueType, class ..._Args, 2230b57cec5SDimitry Andric class _Tp = decay_t<_ValueType>, 2240b57cec5SDimitry Andric class = enable_if_t< 2250b57cec5SDimitry Andric is_constructible<_Tp, _Args...>::value && 2260b57cec5SDimitry Andric is_copy_constructible<_Tp>::value 2270b57cec5SDimitry Andric > 2280b57cec5SDimitry Andric > 2290b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2300b57cec5SDimitry Andric explicit any(in_place_type_t<_ValueType>, _Args&&... __args); 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric template <class _ValueType, class _Up, class ..._Args, 2330b57cec5SDimitry Andric class _Tp = decay_t<_ValueType>, 2340b57cec5SDimitry Andric class = enable_if_t< 2350b57cec5SDimitry Andric is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value && 2360b57cec5SDimitry Andric is_copy_constructible<_Tp>::value> 2370b57cec5SDimitry Andric > 2380b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2390b57cec5SDimitry Andric explicit any(in_place_type_t<_ValueType>, initializer_list<_Up>, _Args&&... __args); 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2420b57cec5SDimitry Andric ~any() { this->reset(); } 2430b57cec5SDimitry Andric 2440b57cec5SDimitry Andric // assignments 2450b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2460b57cec5SDimitry Andric any & operator=(any const & __rhs) { 2470b57cec5SDimitry Andric any(__rhs).swap(*this); 2480b57cec5SDimitry Andric return *this; 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2520b57cec5SDimitry Andric any & operator=(any && __rhs) _NOEXCEPT { 2530b57cec5SDimitry Andric any(_VSTD::move(__rhs)).swap(*this); 2540b57cec5SDimitry Andric return *this; 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric template < 2580b57cec5SDimitry Andric class _ValueType 2590b57cec5SDimitry Andric , class _Tp = decay_t<_ValueType> 2600b57cec5SDimitry Andric , class = enable_if_t< 2610b57cec5SDimitry Andric !is_same<_Tp, any>::value 2620b57cec5SDimitry Andric && is_copy_constructible<_Tp>::value> 2630b57cec5SDimitry Andric > 2640b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2650b57cec5SDimitry Andric any & operator=(_ValueType && __rhs); 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric template <class _ValueType, class ..._Args, 2680b57cec5SDimitry Andric class _Tp = decay_t<_ValueType>, 2690b57cec5SDimitry Andric class = enable_if_t< 2700b57cec5SDimitry Andric is_constructible<_Tp, _Args...>::value && 2710b57cec5SDimitry Andric is_copy_constructible<_Tp>::value> 2720b57cec5SDimitry Andric > 2730b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2740b57cec5SDimitry Andric _Tp& emplace(_Args&&... args); 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric template <class _ValueType, class _Up, class ..._Args, 2770b57cec5SDimitry Andric class _Tp = decay_t<_ValueType>, 2780b57cec5SDimitry Andric class = enable_if_t< 2790b57cec5SDimitry Andric is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value && 2800b57cec5SDimitry Andric is_copy_constructible<_Tp>::value> 2810b57cec5SDimitry Andric > 2820b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2830b57cec5SDimitry Andric _Tp& emplace(initializer_list<_Up>, _Args&&...); 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric // 6.3.3 any modifiers 2860b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2870b57cec5SDimitry Andric void reset() _NOEXCEPT { if (__h) this->__call(_Action::_Destroy); } 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2900b57cec5SDimitry Andric void swap(any & __rhs) _NOEXCEPT; 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric // 6.3.4 any observers 2930b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2940b57cec5SDimitry Andric bool has_value() const _NOEXCEPT { return __h != nullptr; } 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric#if !defined(_LIBCPP_NO_RTTI) 2970b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2980b57cec5SDimitry Andric const type_info & type() const _NOEXCEPT { 2990b57cec5SDimitry Andric if (__h) { 3000b57cec5SDimitry Andric return *static_cast<type_info const *>(this->__call(_Action::_TypeInfo)); 3010b57cec5SDimitry Andric } else { 3020b57cec5SDimitry Andric return typeid(void); 3030b57cec5SDimitry Andric } 3040b57cec5SDimitry Andric } 3050b57cec5SDimitry Andric#endif 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andricprivate: 3080b57cec5SDimitry Andric typedef __any_imp::_Action _Action; 3090b57cec5SDimitry Andric using _HandleFuncPtr = void* (*)(_Action, any const *, any *, const type_info *, 3100b57cec5SDimitry Andric const void* __fallback_info); 3110b57cec5SDimitry Andric 3120b57cec5SDimitry Andric union _Storage { 3130b57cec5SDimitry Andric constexpr _Storage() : __ptr(nullptr) {} 3140b57cec5SDimitry Andric void * __ptr; 3150b57cec5SDimitry Andric __any_imp::_Buffer __buf; 3160b57cec5SDimitry Andric }; 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3190b57cec5SDimitry Andric void * __call(_Action __a, any * __other = nullptr, 3200b57cec5SDimitry Andric type_info const * __info = nullptr, 3210b57cec5SDimitry Andric const void* __fallback_info = nullptr) const 3220b57cec5SDimitry Andric { 3230b57cec5SDimitry Andric return __h(__a, this, __other, __info, __fallback_info); 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3270b57cec5SDimitry Andric void * __call(_Action __a, any * __other = nullptr, 3280b57cec5SDimitry Andric type_info const * __info = nullptr, 3290b57cec5SDimitry Andric const void* __fallback_info = nullptr) 3300b57cec5SDimitry Andric { 3310b57cec5SDimitry Andric return __h(__a, this, __other, __info, __fallback_info); 3320b57cec5SDimitry Andric } 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric template <class> 3350b57cec5SDimitry Andric friend struct __any_imp::_SmallHandler; 3360b57cec5SDimitry Andric template <class> 3370b57cec5SDimitry Andric friend struct __any_imp::_LargeHandler; 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andric template <class _ValueType> 3400b57cec5SDimitry Andric friend add_pointer_t<add_const_t<_ValueType>> 3410b57cec5SDimitry Andric any_cast(any const *) _NOEXCEPT; 3420b57cec5SDimitry Andric 3430b57cec5SDimitry Andric template <class _ValueType> 3440b57cec5SDimitry Andric friend add_pointer_t<_ValueType> 3450b57cec5SDimitry Andric any_cast(any *) _NOEXCEPT; 3460b57cec5SDimitry Andric 3470b57cec5SDimitry Andric _HandleFuncPtr __h = nullptr; 3480b57cec5SDimitry Andric _Storage __s; 3490b57cec5SDimitry Andric}; 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andricnamespace __any_imp 3520b57cec5SDimitry Andric{ 3530b57cec5SDimitry Andric template <class _Tp> 3540b57cec5SDimitry Andric struct _LIBCPP_TEMPLATE_VIS _SmallHandler 3550b57cec5SDimitry Andric { 3560b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3570b57cec5SDimitry Andric static void* __handle(_Action __act, any const * __this, any * __other, 3580b57cec5SDimitry Andric type_info const * __info, const void* __fallback_info) 3590b57cec5SDimitry Andric { 3600b57cec5SDimitry Andric switch (__act) 3610b57cec5SDimitry Andric { 3620b57cec5SDimitry Andric case _Action::_Destroy: 3630b57cec5SDimitry Andric __destroy(const_cast<any &>(*__this)); 3640b57cec5SDimitry Andric return nullptr; 3650b57cec5SDimitry Andric case _Action::_Copy: 3660b57cec5SDimitry Andric __copy(*__this, *__other); 3670b57cec5SDimitry Andric return nullptr; 3680b57cec5SDimitry Andric case _Action::_Move: 3690b57cec5SDimitry Andric __move(const_cast<any &>(*__this), *__other); 3700b57cec5SDimitry Andric return nullptr; 3710b57cec5SDimitry Andric case _Action::_Get: 3720b57cec5SDimitry Andric return __get(const_cast<any &>(*__this), __info, __fallback_info); 3730b57cec5SDimitry Andric case _Action::_TypeInfo: 3740b57cec5SDimitry Andric return __type_info(); 3750b57cec5SDimitry Andric } 376*81ad6265SDimitry Andric __libcpp_unreachable(); 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric 3790b57cec5SDimitry Andric template <class ..._Args> 3800b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3810b57cec5SDimitry Andric static _Tp& __create(any & __dest, _Args&&... __args) { 382e8d8bef9SDimitry Andric typedef allocator<_Tp> _Alloc; 383e8d8bef9SDimitry Andric typedef allocator_traits<_Alloc> _ATraits; 384e8d8bef9SDimitry Andric _Alloc __a; 385e8d8bef9SDimitry Andric _Tp * __ret = static_cast<_Tp*>(static_cast<void*>(&__dest.__s.__buf)); 386e8d8bef9SDimitry Andric _ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...); 3870b57cec5SDimitry Andric __dest.__h = &_SmallHandler::__handle; 3880b57cec5SDimitry Andric return *__ret; 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric 3910b57cec5SDimitry Andric private: 3920b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3930b57cec5SDimitry Andric static void __destroy(any & __this) { 394e8d8bef9SDimitry Andric typedef allocator<_Tp> _Alloc; 395e8d8bef9SDimitry Andric typedef allocator_traits<_Alloc> _ATraits; 396e8d8bef9SDimitry Andric _Alloc __a; 397e8d8bef9SDimitry Andric _Tp * __p = static_cast<_Tp *>(static_cast<void*>(&__this.__s.__buf)); 398e8d8bef9SDimitry Andric _ATraits::destroy(__a, __p); 3990b57cec5SDimitry Andric __this.__h = nullptr; 4000b57cec5SDimitry Andric } 4010b57cec5SDimitry Andric 4020b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4030b57cec5SDimitry Andric static void __copy(any const & __this, any & __dest) { 4040b57cec5SDimitry Andric _SmallHandler::__create(__dest, *static_cast<_Tp const *>( 4050b57cec5SDimitry Andric static_cast<void const *>(&__this.__s.__buf))); 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric 4080b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4090b57cec5SDimitry Andric static void __move(any & __this, any & __dest) { 4100b57cec5SDimitry Andric _SmallHandler::__create(__dest, _VSTD::move( 4110b57cec5SDimitry Andric *static_cast<_Tp*>(static_cast<void*>(&__this.__s.__buf)))); 4120b57cec5SDimitry Andric __destroy(__this); 4130b57cec5SDimitry Andric } 4140b57cec5SDimitry Andric 4150b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4160b57cec5SDimitry Andric static void* __get(any & __this, 4170b57cec5SDimitry Andric type_info const * __info, 4180b57cec5SDimitry Andric const void* __fallback_id) 4190b57cec5SDimitry Andric { 4200b57cec5SDimitry Andric if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id)) 4210b57cec5SDimitry Andric return static_cast<void*>(&__this.__s.__buf); 4220b57cec5SDimitry Andric return nullptr; 4230b57cec5SDimitry Andric } 4240b57cec5SDimitry Andric 4250b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4260b57cec5SDimitry Andric static void* __type_info() 4270b57cec5SDimitry Andric { 4280b57cec5SDimitry Andric#if !defined(_LIBCPP_NO_RTTI) 4290b57cec5SDimitry Andric return const_cast<void*>(static_cast<void const *>(&typeid(_Tp))); 4300b57cec5SDimitry Andric#else 4310b57cec5SDimitry Andric return nullptr; 4320b57cec5SDimitry Andric#endif 4330b57cec5SDimitry Andric } 4340b57cec5SDimitry Andric }; 4350b57cec5SDimitry Andric 4360b57cec5SDimitry Andric template <class _Tp> 4370b57cec5SDimitry Andric struct _LIBCPP_TEMPLATE_VIS _LargeHandler 4380b57cec5SDimitry Andric { 4390b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4400b57cec5SDimitry Andric static void* __handle(_Action __act, any const * __this, 4410b57cec5SDimitry Andric any * __other, type_info const * __info, 4420b57cec5SDimitry Andric void const* __fallback_info) 4430b57cec5SDimitry Andric { 4440b57cec5SDimitry Andric switch (__act) 4450b57cec5SDimitry Andric { 4460b57cec5SDimitry Andric case _Action::_Destroy: 4470b57cec5SDimitry Andric __destroy(const_cast<any &>(*__this)); 4480b57cec5SDimitry Andric return nullptr; 4490b57cec5SDimitry Andric case _Action::_Copy: 4500b57cec5SDimitry Andric __copy(*__this, *__other); 4510b57cec5SDimitry Andric return nullptr; 4520b57cec5SDimitry Andric case _Action::_Move: 4530b57cec5SDimitry Andric __move(const_cast<any &>(*__this), *__other); 4540b57cec5SDimitry Andric return nullptr; 4550b57cec5SDimitry Andric case _Action::_Get: 4560b57cec5SDimitry Andric return __get(const_cast<any &>(*__this), __info, __fallback_info); 4570b57cec5SDimitry Andric case _Action::_TypeInfo: 4580b57cec5SDimitry Andric return __type_info(); 4590b57cec5SDimitry Andric } 460*81ad6265SDimitry Andric __libcpp_unreachable(); 4610b57cec5SDimitry Andric } 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric template <class ..._Args> 4640b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4650b57cec5SDimitry Andric static _Tp& __create(any & __dest, _Args&&... __args) { 4660b57cec5SDimitry Andric typedef allocator<_Tp> _Alloc; 467e8d8bef9SDimitry Andric typedef allocator_traits<_Alloc> _ATraits; 4680b57cec5SDimitry Andric typedef __allocator_destructor<_Alloc> _Dp; 4690b57cec5SDimitry Andric _Alloc __a; 470e8d8bef9SDimitry Andric unique_ptr<_Tp, _Dp> __hold(_ATraits::allocate(__a, 1), _Dp(__a, 1)); 471e8d8bef9SDimitry Andric _Tp * __ret = __hold.get(); 472e8d8bef9SDimitry Andric _ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...); 4730b57cec5SDimitry Andric __dest.__s.__ptr = __hold.release(); 4740b57cec5SDimitry Andric __dest.__h = &_LargeHandler::__handle; 4750b57cec5SDimitry Andric return *__ret; 4760b57cec5SDimitry Andric } 4770b57cec5SDimitry Andric 4780b57cec5SDimitry Andric private: 4790b57cec5SDimitry Andric 4800b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4810b57cec5SDimitry Andric static void __destroy(any & __this){ 482e8d8bef9SDimitry Andric typedef allocator<_Tp> _Alloc; 483e8d8bef9SDimitry Andric typedef allocator_traits<_Alloc> _ATraits; 484e8d8bef9SDimitry Andric _Alloc __a; 485e8d8bef9SDimitry Andric _Tp * __p = static_cast<_Tp *>(__this.__s.__ptr); 486e8d8bef9SDimitry Andric _ATraits::destroy(__a, __p); 487e8d8bef9SDimitry Andric _ATraits::deallocate(__a, __p, 1); 4880b57cec5SDimitry Andric __this.__h = nullptr; 4890b57cec5SDimitry Andric } 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4920b57cec5SDimitry Andric static void __copy(any const & __this, any & __dest) { 4930b57cec5SDimitry Andric _LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s.__ptr)); 4940b57cec5SDimitry Andric } 4950b57cec5SDimitry Andric 4960b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4970b57cec5SDimitry Andric static void __move(any & __this, any & __dest) { 4980b57cec5SDimitry Andric __dest.__s.__ptr = __this.__s.__ptr; 4990b57cec5SDimitry Andric __dest.__h = &_LargeHandler::__handle; 5000b57cec5SDimitry Andric __this.__h = nullptr; 5010b57cec5SDimitry Andric } 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 5040b57cec5SDimitry Andric static void* __get(any & __this, type_info const * __info, 5050b57cec5SDimitry Andric void const* __fallback_info) 5060b57cec5SDimitry Andric { 5070b57cec5SDimitry Andric if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info)) 5080b57cec5SDimitry Andric return static_cast<void*>(__this.__s.__ptr); 5090b57cec5SDimitry Andric return nullptr; 5100b57cec5SDimitry Andric 5110b57cec5SDimitry Andric } 5120b57cec5SDimitry Andric 5130b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 5140b57cec5SDimitry Andric static void* __type_info() 5150b57cec5SDimitry Andric { 5160b57cec5SDimitry Andric#if !defined(_LIBCPP_NO_RTTI) 5170b57cec5SDimitry Andric return const_cast<void*>(static_cast<void const *>(&typeid(_Tp))); 5180b57cec5SDimitry Andric#else 5190b57cec5SDimitry Andric return nullptr; 5200b57cec5SDimitry Andric#endif 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric }; 5230b57cec5SDimitry Andric 5240b57cec5SDimitry Andric} // namespace __any_imp 5250b57cec5SDimitry Andric 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andrictemplate <class _ValueType, class _Tp, class> 5280b57cec5SDimitry Andricany::any(_ValueType && __v) : __h(nullptr) 5290b57cec5SDimitry Andric{ 5300b57cec5SDimitry Andric __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_ValueType>(__v)); 5310b57cec5SDimitry Andric} 5320b57cec5SDimitry Andric 5330b57cec5SDimitry Andrictemplate <class _ValueType, class ..._Args, class _Tp, class> 5340b57cec5SDimitry Andricany::any(in_place_type_t<_ValueType>, _Args&&... __args) { 5350b57cec5SDimitry Andric __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...); 5360b57cec5SDimitry Andric} 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andrictemplate <class _ValueType, class _Up, class ..._Args, class _Tp, class> 5390b57cec5SDimitry Andricany::any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) { 5400b57cec5SDimitry Andric __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...); 5410b57cec5SDimitry Andric} 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andrictemplate <class _ValueType, class, class> 5440b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5450b57cec5SDimitry Andricany & any::operator=(_ValueType && __v) 5460b57cec5SDimitry Andric{ 5470b57cec5SDimitry Andric any(_VSTD::forward<_ValueType>(__v)).swap(*this); 5480b57cec5SDimitry Andric return *this; 5490b57cec5SDimitry Andric} 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andrictemplate <class _ValueType, class ..._Args, class _Tp, class> 5520b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5530b57cec5SDimitry Andric_Tp& any::emplace(_Args&&... __args) { 5540b57cec5SDimitry Andric reset(); 5550b57cec5SDimitry Andric return __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...); 5560b57cec5SDimitry Andric} 5570b57cec5SDimitry Andric 5580b57cec5SDimitry Andrictemplate <class _ValueType, class _Up, class ..._Args, class _Tp, class> 5590b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5600b57cec5SDimitry Andric_Tp& any::emplace(initializer_list<_Up> __il, _Args&&... __args) { 5610b57cec5SDimitry Andric reset(); 5620b57cec5SDimitry Andric return __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...); 5630b57cec5SDimitry Andric} 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5660b57cec5SDimitry Andricvoid any::swap(any & __rhs) _NOEXCEPT 5670b57cec5SDimitry Andric{ 5680b57cec5SDimitry Andric if (this == &__rhs) 5690b57cec5SDimitry Andric return; 5700b57cec5SDimitry Andric if (__h && __rhs.__h) { 5710b57cec5SDimitry Andric any __tmp; 5720b57cec5SDimitry Andric __rhs.__call(_Action::_Move, &__tmp); 5730b57cec5SDimitry Andric this->__call(_Action::_Move, &__rhs); 5740b57cec5SDimitry Andric __tmp.__call(_Action::_Move, this); 5750b57cec5SDimitry Andric } 5760b57cec5SDimitry Andric else if (__h) { 5770b57cec5SDimitry Andric this->__call(_Action::_Move, &__rhs); 5780b57cec5SDimitry Andric } 5790b57cec5SDimitry Andric else if (__rhs.__h) { 5800b57cec5SDimitry Andric __rhs.__call(_Action::_Move, this); 5810b57cec5SDimitry Andric } 5820b57cec5SDimitry Andric} 5830b57cec5SDimitry Andric 5840b57cec5SDimitry Andric// 6.4 Non-member functions 5850b57cec5SDimitry Andric 5860b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5870b57cec5SDimitry Andricvoid swap(any & __lhs, any & __rhs) _NOEXCEPT 5880b57cec5SDimitry Andric{ 5890b57cec5SDimitry Andric __lhs.swap(__rhs); 5900b57cec5SDimitry Andric} 5910b57cec5SDimitry Andric 5920b57cec5SDimitry Andrictemplate <class _Tp, class ..._Args> 5930b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5940b57cec5SDimitry Andricany make_any(_Args&&... __args) { 5950b57cec5SDimitry Andric return any(in_place_type<_Tp>, _VSTD::forward<_Args>(__args)...); 5960b57cec5SDimitry Andric} 5970b57cec5SDimitry Andric 5980b57cec5SDimitry Andrictemplate <class _Tp, class _Up, class ..._Args> 5990b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 6000b57cec5SDimitry Andricany make_any(initializer_list<_Up> __il, _Args&&... __args) { 6010b57cec5SDimitry Andric return any(in_place_type<_Tp>, __il, _VSTD::forward<_Args>(__args)...); 6020b57cec5SDimitry Andric} 6030b57cec5SDimitry Andric 6040b57cec5SDimitry Andrictemplate <class _ValueType> 6050b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 6060b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST 6070b57cec5SDimitry Andric_ValueType any_cast(any const & __v) 6080b57cec5SDimitry Andric{ 6090b57cec5SDimitry Andric using _RawValueType = __uncvref_t<_ValueType>; 6100b57cec5SDimitry Andric static_assert(is_constructible<_ValueType, _RawValueType const &>::value, 6110b57cec5SDimitry Andric "ValueType is required to be a const lvalue reference " 6120b57cec5SDimitry Andric "or a CopyConstructible type"); 6130b57cec5SDimitry Andric auto __tmp = _VSTD::any_cast<add_const_t<_RawValueType>>(&__v); 6140b57cec5SDimitry Andric if (__tmp == nullptr) 6150b57cec5SDimitry Andric __throw_bad_any_cast(); 6160b57cec5SDimitry Andric return static_cast<_ValueType>(*__tmp); 6170b57cec5SDimitry Andric} 6180b57cec5SDimitry Andric 6190b57cec5SDimitry Andrictemplate <class _ValueType> 6200b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 6210b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST 6220b57cec5SDimitry Andric_ValueType any_cast(any & __v) 6230b57cec5SDimitry Andric{ 6240b57cec5SDimitry Andric using _RawValueType = __uncvref_t<_ValueType>; 6250b57cec5SDimitry Andric static_assert(is_constructible<_ValueType, _RawValueType &>::value, 6260b57cec5SDimitry Andric "ValueType is required to be an lvalue reference " 6270b57cec5SDimitry Andric "or a CopyConstructible type"); 6280b57cec5SDimitry Andric auto __tmp = _VSTD::any_cast<_RawValueType>(&__v); 6290b57cec5SDimitry Andric if (__tmp == nullptr) 6300b57cec5SDimitry Andric __throw_bad_any_cast(); 6310b57cec5SDimitry Andric return static_cast<_ValueType>(*__tmp); 6320b57cec5SDimitry Andric} 6330b57cec5SDimitry Andric 6340b57cec5SDimitry Andrictemplate <class _ValueType> 6350b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 6360b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST 6370b57cec5SDimitry Andric_ValueType any_cast(any && __v) 6380b57cec5SDimitry Andric{ 6390b57cec5SDimitry Andric using _RawValueType = __uncvref_t<_ValueType>; 6400b57cec5SDimitry Andric static_assert(is_constructible<_ValueType, _RawValueType>::value, 6410b57cec5SDimitry Andric "ValueType is required to be an rvalue reference " 6420b57cec5SDimitry Andric "or a CopyConstructible type"); 6430b57cec5SDimitry Andric auto __tmp = _VSTD::any_cast<_RawValueType>(&__v); 6440b57cec5SDimitry Andric if (__tmp == nullptr) 6450b57cec5SDimitry Andric __throw_bad_any_cast(); 6460b57cec5SDimitry Andric return static_cast<_ValueType>(_VSTD::move(*__tmp)); 6470b57cec5SDimitry Andric} 6480b57cec5SDimitry Andric 6490b57cec5SDimitry Andrictemplate <class _ValueType> 6500b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 6510b57cec5SDimitry Andricadd_pointer_t<add_const_t<_ValueType>> 6520b57cec5SDimitry Andricany_cast(any const * __any) _NOEXCEPT 6530b57cec5SDimitry Andric{ 6540b57cec5SDimitry Andric static_assert(!is_reference<_ValueType>::value, 6550b57cec5SDimitry Andric "_ValueType may not be a reference."); 6560b57cec5SDimitry Andric return _VSTD::any_cast<_ValueType>(const_cast<any *>(__any)); 6570b57cec5SDimitry Andric} 6580b57cec5SDimitry Andric 6590b57cec5SDimitry Andrictemplate <class _RetType> 6600b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 6610b57cec5SDimitry Andric_RetType __pointer_or_func_cast(void* __p, /*IsFunction*/false_type) noexcept { 6620b57cec5SDimitry Andric return static_cast<_RetType>(__p); 6630b57cec5SDimitry Andric} 6640b57cec5SDimitry Andric 6650b57cec5SDimitry Andrictemplate <class _RetType> 6660b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 6670b57cec5SDimitry Andric_RetType __pointer_or_func_cast(void*, /*IsFunction*/true_type) noexcept { 6680b57cec5SDimitry Andric return nullptr; 6690b57cec5SDimitry Andric} 6700b57cec5SDimitry Andric 6710b57cec5SDimitry Andrictemplate <class _ValueType> 672*81ad6265SDimitry Andric_LIBCPP_HIDE_FROM_ABI 6730b57cec5SDimitry Andricadd_pointer_t<_ValueType> 6740b57cec5SDimitry Andricany_cast(any * __any) _NOEXCEPT 6750b57cec5SDimitry Andric{ 6760b57cec5SDimitry Andric using __any_imp::_Action; 6770b57cec5SDimitry Andric static_assert(!is_reference<_ValueType>::value, 6780b57cec5SDimitry Andric "_ValueType may not be a reference."); 6790b57cec5SDimitry Andric typedef typename add_pointer<_ValueType>::type _ReturnType; 6800b57cec5SDimitry Andric if (__any && __any->__h) { 6810b57cec5SDimitry Andric void *__p = __any->__call(_Action::_Get, nullptr, 6820b57cec5SDimitry Andric#if !defined(_LIBCPP_NO_RTTI) 6830b57cec5SDimitry Andric &typeid(_ValueType), 6840b57cec5SDimitry Andric#else 6850b57cec5SDimitry Andric nullptr, 6860b57cec5SDimitry Andric#endif 6870b57cec5SDimitry Andric __any_imp::__get_fallback_typeid<_ValueType>()); 6880b57cec5SDimitry Andric return _VSTD::__pointer_or_func_cast<_ReturnType>( 6890b57cec5SDimitry Andric __p, is_function<_ValueType>{}); 6900b57cec5SDimitry Andric } 6910b57cec5SDimitry Andric return nullptr; 6920b57cec5SDimitry Andric} 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andric#endif // _LIBCPP_STD_VER > 14 6950b57cec5SDimitry Andric 6960b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD 6970b57cec5SDimitry Andric 6980b57cec5SDimitry Andric#endif // _LIBCPP_ANY 699