10b57cec5SDimitry Andric// -*- C++ -*- 20b57cec5SDimitry Andric//===------------------------------ any -----------------------------------===// 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 830b57cec5SDimitry Andric#include <experimental/__config> 84*e8d8bef9SDimitry Andric#include <__availability> 850b57cec5SDimitry Andric#include <memory> 860b57cec5SDimitry Andric#include <typeinfo> 870b57cec5SDimitry Andric#include <type_traits> 880b57cec5SDimitry Andric#include <cstdlib> 890b57cec5SDimitry Andric#include <version> 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 920b57cec5SDimitry Andric#pragma GCC system_header 930b57cec5SDimitry Andric#endif 940b57cec5SDimitry Andric 950b57cec5SDimitry Andricnamespace std { 960b57cec5SDimitry Andricclass _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_ANY_CAST bad_any_cast : public bad_cast 970b57cec5SDimitry Andric{ 980b57cec5SDimitry Andricpublic: 990b57cec5SDimitry Andric virtual const char* what() const _NOEXCEPT; 1000b57cec5SDimitry Andric}; 1010b57cec5SDimitry Andric} // namespace std 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY 1080b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST 1090b57cec5SDimitry Andricvoid __throw_bad_any_cast() 1100b57cec5SDimitry Andric{ 1110b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS 1120b57cec5SDimitry Andric throw bad_any_cast(); 1130b57cec5SDimitry Andric#else 1140b57cec5SDimitry Andric _VSTD::abort(); 1150b57cec5SDimitry Andric#endif 1160b57cec5SDimitry Andric} 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric// Forward declarations 1190b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS any; 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andrictemplate <class _ValueType> 1220b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY 1230b57cec5SDimitry Andricadd_pointer_t<add_const_t<_ValueType>> 1240b57cec5SDimitry Andricany_cast(any const *) _NOEXCEPT; 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andrictemplate <class _ValueType> 1270b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY 1280b57cec5SDimitry Andricadd_pointer_t<_ValueType> any_cast(any *) _NOEXCEPT; 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andricnamespace __any_imp 1310b57cec5SDimitry Andric{ 1320b57cec5SDimitry Andric using _Buffer = aligned_storage_t<3*sizeof(void*), alignment_of<void*>::value>; 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric template <class _Tp> 1350b57cec5SDimitry Andric using _IsSmallObject = integral_constant<bool 1360b57cec5SDimitry Andric , sizeof(_Tp) <= sizeof(_Buffer) 1370b57cec5SDimitry Andric && alignment_of<_Buffer>::value 1380b57cec5SDimitry Andric % alignment_of<_Tp>::value == 0 1390b57cec5SDimitry Andric && is_nothrow_move_constructible<_Tp>::value 1400b57cec5SDimitry Andric >; 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric enum class _Action { 1430b57cec5SDimitry Andric _Destroy, 1440b57cec5SDimitry Andric _Copy, 1450b57cec5SDimitry Andric _Move, 1460b57cec5SDimitry Andric _Get, 1470b57cec5SDimitry Andric _TypeInfo 1480b57cec5SDimitry Andric }; 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric template <class _Tp> struct _SmallHandler; 1510b57cec5SDimitry Andric template <class _Tp> struct _LargeHandler; 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric template <class _Tp> 1540b57cec5SDimitry Andric struct _LIBCPP_TEMPLATE_VIS __unique_typeinfo { static constexpr int __id = 0; }; 1550b57cec5SDimitry Andric template <class _Tp> constexpr int __unique_typeinfo<_Tp>::__id; 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric template <class _Tp> 1580b57cec5SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY 1590b57cec5SDimitry Andric constexpr const void* __get_fallback_typeid() { 160*e8d8bef9SDimitry Andric return &__unique_typeinfo<remove_cv_t<remove_reference_t<_Tp>>>::__id; 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric template <class _Tp> 1640b57cec5SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY 1650b57cec5SDimitry Andric bool __compare_typeid(type_info const* __id, const void* __fallback_id) 1660b57cec5SDimitry Andric { 1670b57cec5SDimitry Andric#if !defined(_LIBCPP_NO_RTTI) 1680b57cec5SDimitry Andric if (__id && *__id == typeid(_Tp)) 1690b57cec5SDimitry Andric return true; 1700b57cec5SDimitry Andric#endif 1710b57cec5SDimitry Andric if (!__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>()) 1720b57cec5SDimitry Andric return true; 1730b57cec5SDimitry Andric return false; 1740b57cec5SDimitry Andric } 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric template <class _Tp> 1770b57cec5SDimitry Andric using _Handler = conditional_t< 1780b57cec5SDimitry Andric _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>; 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric} // namespace __any_imp 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS any 1830b57cec5SDimitry Andric{ 1840b57cec5SDimitry Andricpublic: 1850b57cec5SDimitry Andric // construct/destruct 1860b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1870b57cec5SDimitry Andric constexpr any() _NOEXCEPT : __h(nullptr) {} 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1900b57cec5SDimitry Andric any(any const & __other) : __h(nullptr) 1910b57cec5SDimitry Andric { 1920b57cec5SDimitry Andric if (__other.__h) __other.__call(_Action::_Copy, this); 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 1960b57cec5SDimitry Andric any(any && __other) _NOEXCEPT : __h(nullptr) 1970b57cec5SDimitry Andric { 1980b57cec5SDimitry Andric if (__other.__h) __other.__call(_Action::_Move, this); 1990b57cec5SDimitry Andric } 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric template < 2020b57cec5SDimitry Andric class _ValueType 2030b57cec5SDimitry Andric , class _Tp = decay_t<_ValueType> 2040b57cec5SDimitry Andric , class = enable_if_t< 2050b57cec5SDimitry Andric !is_same<_Tp, any>::value && 2060b57cec5SDimitry Andric !__is_inplace_type<_ValueType>::value && 2070b57cec5SDimitry Andric is_copy_constructible<_Tp>::value> 2080b57cec5SDimitry Andric > 2090b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2100b57cec5SDimitry Andric any(_ValueType && __value); 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric template <class _ValueType, class ..._Args, 2130b57cec5SDimitry Andric class _Tp = decay_t<_ValueType>, 2140b57cec5SDimitry Andric class = enable_if_t< 2150b57cec5SDimitry Andric is_constructible<_Tp, _Args...>::value && 2160b57cec5SDimitry Andric is_copy_constructible<_Tp>::value 2170b57cec5SDimitry Andric > 2180b57cec5SDimitry Andric > 2190b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2200b57cec5SDimitry Andric explicit any(in_place_type_t<_ValueType>, _Args&&... __args); 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric template <class _ValueType, class _Up, class ..._Args, 2230b57cec5SDimitry Andric class _Tp = decay_t<_ValueType>, 2240b57cec5SDimitry Andric class = enable_if_t< 2250b57cec5SDimitry Andric is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value && 2260b57cec5SDimitry Andric is_copy_constructible<_Tp>::value> 2270b57cec5SDimitry Andric > 2280b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2290b57cec5SDimitry Andric explicit any(in_place_type_t<_ValueType>, initializer_list<_Up>, _Args&&... __args); 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2320b57cec5SDimitry Andric ~any() { this->reset(); } 2330b57cec5SDimitry Andric 2340b57cec5SDimitry Andric // assignments 2350b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2360b57cec5SDimitry Andric any & operator=(any const & __rhs) { 2370b57cec5SDimitry Andric any(__rhs).swap(*this); 2380b57cec5SDimitry Andric return *this; 2390b57cec5SDimitry Andric } 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2420b57cec5SDimitry Andric any & operator=(any && __rhs) _NOEXCEPT { 2430b57cec5SDimitry Andric any(_VSTD::move(__rhs)).swap(*this); 2440b57cec5SDimitry Andric return *this; 2450b57cec5SDimitry Andric } 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric template < 2480b57cec5SDimitry Andric class _ValueType 2490b57cec5SDimitry Andric , class _Tp = decay_t<_ValueType> 2500b57cec5SDimitry Andric , class = enable_if_t< 2510b57cec5SDimitry Andric !is_same<_Tp, any>::value 2520b57cec5SDimitry Andric && is_copy_constructible<_Tp>::value> 2530b57cec5SDimitry Andric > 2540b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2550b57cec5SDimitry Andric any & operator=(_ValueType && __rhs); 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric template <class _ValueType, class ..._Args, 2580b57cec5SDimitry Andric class _Tp = decay_t<_ValueType>, 2590b57cec5SDimitry Andric class = enable_if_t< 2600b57cec5SDimitry Andric is_constructible<_Tp, _Args...>::value && 2610b57cec5SDimitry Andric is_copy_constructible<_Tp>::value> 2620b57cec5SDimitry Andric > 2630b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2640b57cec5SDimitry Andric _Tp& emplace(_Args&&... args); 2650b57cec5SDimitry Andric 2660b57cec5SDimitry Andric template <class _ValueType, class _Up, class ..._Args, 2670b57cec5SDimitry Andric class _Tp = decay_t<_ValueType>, 2680b57cec5SDimitry Andric class = enable_if_t< 2690b57cec5SDimitry Andric is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value && 2700b57cec5SDimitry Andric is_copy_constructible<_Tp>::value> 2710b57cec5SDimitry Andric > 2720b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2730b57cec5SDimitry Andric _Tp& emplace(initializer_list<_Up>, _Args&&...); 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric // 6.3.3 any modifiers 2760b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2770b57cec5SDimitry Andric void reset() _NOEXCEPT { if (__h) this->__call(_Action::_Destroy); } 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2800b57cec5SDimitry Andric void swap(any & __rhs) _NOEXCEPT; 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric // 6.3.4 any observers 2830b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2840b57cec5SDimitry Andric bool has_value() const _NOEXCEPT { return __h != nullptr; } 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric#if !defined(_LIBCPP_NO_RTTI) 2870b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 2880b57cec5SDimitry Andric const type_info & type() const _NOEXCEPT { 2890b57cec5SDimitry Andric if (__h) { 2900b57cec5SDimitry Andric return *static_cast<type_info const *>(this->__call(_Action::_TypeInfo)); 2910b57cec5SDimitry Andric } else { 2920b57cec5SDimitry Andric return typeid(void); 2930b57cec5SDimitry Andric } 2940b57cec5SDimitry Andric } 2950b57cec5SDimitry Andric#endif 2960b57cec5SDimitry Andric 2970b57cec5SDimitry Andricprivate: 2980b57cec5SDimitry Andric typedef __any_imp::_Action _Action; 2990b57cec5SDimitry Andric using _HandleFuncPtr = void* (*)(_Action, any const *, any *, const type_info *, 3000b57cec5SDimitry Andric const void* __fallback_info); 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric union _Storage { 3030b57cec5SDimitry Andric constexpr _Storage() : __ptr(nullptr) {} 3040b57cec5SDimitry Andric void * __ptr; 3050b57cec5SDimitry Andric __any_imp::_Buffer __buf; 3060b57cec5SDimitry Andric }; 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3090b57cec5SDimitry Andric void * __call(_Action __a, any * __other = nullptr, 3100b57cec5SDimitry Andric type_info const * __info = nullptr, 3110b57cec5SDimitry Andric const void* __fallback_info = nullptr) const 3120b57cec5SDimitry Andric { 3130b57cec5SDimitry Andric return __h(__a, this, __other, __info, __fallback_info); 3140b57cec5SDimitry Andric } 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3170b57cec5SDimitry Andric void * __call(_Action __a, any * __other = nullptr, 3180b57cec5SDimitry Andric type_info const * __info = nullptr, 3190b57cec5SDimitry Andric const void* __fallback_info = nullptr) 3200b57cec5SDimitry Andric { 3210b57cec5SDimitry Andric return __h(__a, this, __other, __info, __fallback_info); 3220b57cec5SDimitry Andric } 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric template <class> 3250b57cec5SDimitry Andric friend struct __any_imp::_SmallHandler; 3260b57cec5SDimitry Andric template <class> 3270b57cec5SDimitry Andric friend struct __any_imp::_LargeHandler; 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric template <class _ValueType> 3300b57cec5SDimitry Andric friend add_pointer_t<add_const_t<_ValueType>> 3310b57cec5SDimitry Andric any_cast(any const *) _NOEXCEPT; 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric template <class _ValueType> 3340b57cec5SDimitry Andric friend add_pointer_t<_ValueType> 3350b57cec5SDimitry Andric any_cast(any *) _NOEXCEPT; 3360b57cec5SDimitry Andric 3370b57cec5SDimitry Andric _HandleFuncPtr __h = nullptr; 3380b57cec5SDimitry Andric _Storage __s; 3390b57cec5SDimitry Andric}; 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andricnamespace __any_imp 3420b57cec5SDimitry Andric{ 3430b57cec5SDimitry Andric template <class _Tp> 3440b57cec5SDimitry Andric struct _LIBCPP_TEMPLATE_VIS _SmallHandler 3450b57cec5SDimitry Andric { 3460b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3470b57cec5SDimitry Andric static void* __handle(_Action __act, any const * __this, any * __other, 3480b57cec5SDimitry Andric type_info const * __info, const void* __fallback_info) 3490b57cec5SDimitry Andric { 3500b57cec5SDimitry Andric switch (__act) 3510b57cec5SDimitry Andric { 3520b57cec5SDimitry Andric case _Action::_Destroy: 3530b57cec5SDimitry Andric __destroy(const_cast<any &>(*__this)); 3540b57cec5SDimitry Andric return nullptr; 3550b57cec5SDimitry Andric case _Action::_Copy: 3560b57cec5SDimitry Andric __copy(*__this, *__other); 3570b57cec5SDimitry Andric return nullptr; 3580b57cec5SDimitry Andric case _Action::_Move: 3590b57cec5SDimitry Andric __move(const_cast<any &>(*__this), *__other); 3600b57cec5SDimitry Andric return nullptr; 3610b57cec5SDimitry Andric case _Action::_Get: 3620b57cec5SDimitry Andric return __get(const_cast<any &>(*__this), __info, __fallback_info); 3630b57cec5SDimitry Andric case _Action::_TypeInfo: 3640b57cec5SDimitry Andric return __type_info(); 3650b57cec5SDimitry Andric } 3660b57cec5SDimitry Andric } 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric template <class ..._Args> 3690b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3700b57cec5SDimitry Andric static _Tp& __create(any & __dest, _Args&&... __args) { 371*e8d8bef9SDimitry Andric typedef allocator<_Tp> _Alloc; 372*e8d8bef9SDimitry Andric typedef allocator_traits<_Alloc> _ATraits; 373*e8d8bef9SDimitry Andric _Alloc __a; 374*e8d8bef9SDimitry Andric _Tp * __ret = static_cast<_Tp*>(static_cast<void*>(&__dest.__s.__buf)); 375*e8d8bef9SDimitry Andric _ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...); 3760b57cec5SDimitry Andric __dest.__h = &_SmallHandler::__handle; 3770b57cec5SDimitry Andric return *__ret; 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andric private: 3810b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3820b57cec5SDimitry Andric static void __destroy(any & __this) { 383*e8d8bef9SDimitry Andric typedef allocator<_Tp> _Alloc; 384*e8d8bef9SDimitry Andric typedef allocator_traits<_Alloc> _ATraits; 385*e8d8bef9SDimitry Andric _Alloc __a; 386*e8d8bef9SDimitry Andric _Tp * __p = static_cast<_Tp *>(static_cast<void*>(&__this.__s.__buf)); 387*e8d8bef9SDimitry Andric _ATraits::destroy(__a, __p); 3880b57cec5SDimitry Andric __this.__h = nullptr; 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric 3910b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3920b57cec5SDimitry Andric static void __copy(any const & __this, any & __dest) { 3930b57cec5SDimitry Andric _SmallHandler::__create(__dest, *static_cast<_Tp const *>( 3940b57cec5SDimitry Andric static_cast<void const *>(&__this.__s.__buf))); 3950b57cec5SDimitry Andric } 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 3980b57cec5SDimitry Andric static void __move(any & __this, any & __dest) { 3990b57cec5SDimitry Andric _SmallHandler::__create(__dest, _VSTD::move( 4000b57cec5SDimitry Andric *static_cast<_Tp*>(static_cast<void*>(&__this.__s.__buf)))); 4010b57cec5SDimitry Andric __destroy(__this); 4020b57cec5SDimitry Andric } 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4050b57cec5SDimitry Andric static void* __get(any & __this, 4060b57cec5SDimitry Andric type_info const * __info, 4070b57cec5SDimitry Andric const void* __fallback_id) 4080b57cec5SDimitry Andric { 4090b57cec5SDimitry Andric if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id)) 4100b57cec5SDimitry Andric return static_cast<void*>(&__this.__s.__buf); 4110b57cec5SDimitry Andric return nullptr; 4120b57cec5SDimitry Andric } 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4150b57cec5SDimitry Andric static void* __type_info() 4160b57cec5SDimitry Andric { 4170b57cec5SDimitry Andric#if !defined(_LIBCPP_NO_RTTI) 4180b57cec5SDimitry Andric return const_cast<void*>(static_cast<void const *>(&typeid(_Tp))); 4190b57cec5SDimitry Andric#else 4200b57cec5SDimitry Andric return nullptr; 4210b57cec5SDimitry Andric#endif 4220b57cec5SDimitry Andric } 4230b57cec5SDimitry Andric }; 4240b57cec5SDimitry Andric 4250b57cec5SDimitry Andric template <class _Tp> 4260b57cec5SDimitry Andric struct _LIBCPP_TEMPLATE_VIS _LargeHandler 4270b57cec5SDimitry Andric { 4280b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4290b57cec5SDimitry Andric static void* __handle(_Action __act, any const * __this, 4300b57cec5SDimitry Andric any * __other, type_info const * __info, 4310b57cec5SDimitry Andric void const* __fallback_info) 4320b57cec5SDimitry Andric { 4330b57cec5SDimitry Andric switch (__act) 4340b57cec5SDimitry Andric { 4350b57cec5SDimitry Andric case _Action::_Destroy: 4360b57cec5SDimitry Andric __destroy(const_cast<any &>(*__this)); 4370b57cec5SDimitry Andric return nullptr; 4380b57cec5SDimitry Andric case _Action::_Copy: 4390b57cec5SDimitry Andric __copy(*__this, *__other); 4400b57cec5SDimitry Andric return nullptr; 4410b57cec5SDimitry Andric case _Action::_Move: 4420b57cec5SDimitry Andric __move(const_cast<any &>(*__this), *__other); 4430b57cec5SDimitry Andric return nullptr; 4440b57cec5SDimitry Andric case _Action::_Get: 4450b57cec5SDimitry Andric return __get(const_cast<any &>(*__this), __info, __fallback_info); 4460b57cec5SDimitry Andric case _Action::_TypeInfo: 4470b57cec5SDimitry Andric return __type_info(); 4480b57cec5SDimitry Andric } 4490b57cec5SDimitry Andric } 4500b57cec5SDimitry Andric 4510b57cec5SDimitry Andric template <class ..._Args> 4520b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4530b57cec5SDimitry Andric static _Tp& __create(any & __dest, _Args&&... __args) { 4540b57cec5SDimitry Andric typedef allocator<_Tp> _Alloc; 455*e8d8bef9SDimitry Andric typedef allocator_traits<_Alloc> _ATraits; 4560b57cec5SDimitry Andric typedef __allocator_destructor<_Alloc> _Dp; 4570b57cec5SDimitry Andric _Alloc __a; 458*e8d8bef9SDimitry Andric unique_ptr<_Tp, _Dp> __hold(_ATraits::allocate(__a, 1), _Dp(__a, 1)); 459*e8d8bef9SDimitry Andric _Tp * __ret = __hold.get(); 460*e8d8bef9SDimitry Andric _ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...); 4610b57cec5SDimitry Andric __dest.__s.__ptr = __hold.release(); 4620b57cec5SDimitry Andric __dest.__h = &_LargeHandler::__handle; 4630b57cec5SDimitry Andric return *__ret; 4640b57cec5SDimitry Andric } 4650b57cec5SDimitry Andric 4660b57cec5SDimitry Andric private: 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4690b57cec5SDimitry Andric static void __destroy(any & __this){ 470*e8d8bef9SDimitry Andric typedef allocator<_Tp> _Alloc; 471*e8d8bef9SDimitry Andric typedef allocator_traits<_Alloc> _ATraits; 472*e8d8bef9SDimitry Andric _Alloc __a; 473*e8d8bef9SDimitry Andric _Tp * __p = static_cast<_Tp *>(__this.__s.__ptr); 474*e8d8bef9SDimitry Andric _ATraits::destroy(__a, __p); 475*e8d8bef9SDimitry Andric _ATraits::deallocate(__a, __p, 1); 4760b57cec5SDimitry Andric __this.__h = nullptr; 4770b57cec5SDimitry Andric } 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4800b57cec5SDimitry Andric static void __copy(any const & __this, any & __dest) { 4810b57cec5SDimitry Andric _LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s.__ptr)); 4820b57cec5SDimitry Andric } 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4850b57cec5SDimitry Andric static void __move(any & __this, any & __dest) { 4860b57cec5SDimitry Andric __dest.__s.__ptr = __this.__s.__ptr; 4870b57cec5SDimitry Andric __dest.__h = &_LargeHandler::__handle; 4880b57cec5SDimitry Andric __this.__h = nullptr; 4890b57cec5SDimitry Andric } 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 4920b57cec5SDimitry Andric static void* __get(any & __this, type_info const * __info, 4930b57cec5SDimitry Andric void const* __fallback_info) 4940b57cec5SDimitry Andric { 4950b57cec5SDimitry Andric if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info)) 4960b57cec5SDimitry Andric return static_cast<void*>(__this.__s.__ptr); 4970b57cec5SDimitry Andric return nullptr; 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric } 5000b57cec5SDimitry Andric 5010b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 5020b57cec5SDimitry Andric static void* __type_info() 5030b57cec5SDimitry Andric { 5040b57cec5SDimitry Andric#if !defined(_LIBCPP_NO_RTTI) 5050b57cec5SDimitry Andric return const_cast<void*>(static_cast<void const *>(&typeid(_Tp))); 5060b57cec5SDimitry Andric#else 5070b57cec5SDimitry Andric return nullptr; 5080b57cec5SDimitry Andric#endif 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric }; 5110b57cec5SDimitry Andric 5120b57cec5SDimitry Andric} // namespace __any_imp 5130b57cec5SDimitry Andric 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andrictemplate <class _ValueType, class _Tp, class> 5160b57cec5SDimitry Andricany::any(_ValueType && __v) : __h(nullptr) 5170b57cec5SDimitry Andric{ 5180b57cec5SDimitry Andric __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_ValueType>(__v)); 5190b57cec5SDimitry Andric} 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andrictemplate <class _ValueType, class ..._Args, class _Tp, class> 5220b57cec5SDimitry Andricany::any(in_place_type_t<_ValueType>, _Args&&... __args) { 5230b57cec5SDimitry Andric __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...); 5240b57cec5SDimitry Andric} 5250b57cec5SDimitry Andric 5260b57cec5SDimitry Andrictemplate <class _ValueType, class _Up, class ..._Args, class _Tp, class> 5270b57cec5SDimitry Andricany::any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) { 5280b57cec5SDimitry Andric __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...); 5290b57cec5SDimitry Andric} 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andrictemplate <class _ValueType, class, class> 5320b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5330b57cec5SDimitry Andricany & any::operator=(_ValueType && __v) 5340b57cec5SDimitry Andric{ 5350b57cec5SDimitry Andric any(_VSTD::forward<_ValueType>(__v)).swap(*this); 5360b57cec5SDimitry Andric return *this; 5370b57cec5SDimitry Andric} 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andrictemplate <class _ValueType, class ..._Args, class _Tp, class> 5400b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5410b57cec5SDimitry Andric_Tp& any::emplace(_Args&&... __args) { 5420b57cec5SDimitry Andric reset(); 5430b57cec5SDimitry Andric return __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...); 5440b57cec5SDimitry Andric} 5450b57cec5SDimitry Andric 5460b57cec5SDimitry Andrictemplate <class _ValueType, class _Up, class ..._Args, class _Tp, class> 5470b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5480b57cec5SDimitry Andric_Tp& any::emplace(initializer_list<_Up> __il, _Args&&... __args) { 5490b57cec5SDimitry Andric reset(); 5500b57cec5SDimitry Andric return __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...); 5510b57cec5SDimitry Andric} 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5540b57cec5SDimitry Andricvoid any::swap(any & __rhs) _NOEXCEPT 5550b57cec5SDimitry Andric{ 5560b57cec5SDimitry Andric if (this == &__rhs) 5570b57cec5SDimitry Andric return; 5580b57cec5SDimitry Andric if (__h && __rhs.__h) { 5590b57cec5SDimitry Andric any __tmp; 5600b57cec5SDimitry Andric __rhs.__call(_Action::_Move, &__tmp); 5610b57cec5SDimitry Andric this->__call(_Action::_Move, &__rhs); 5620b57cec5SDimitry Andric __tmp.__call(_Action::_Move, this); 5630b57cec5SDimitry Andric } 5640b57cec5SDimitry Andric else if (__h) { 5650b57cec5SDimitry Andric this->__call(_Action::_Move, &__rhs); 5660b57cec5SDimitry Andric } 5670b57cec5SDimitry Andric else if (__rhs.__h) { 5680b57cec5SDimitry Andric __rhs.__call(_Action::_Move, this); 5690b57cec5SDimitry Andric } 5700b57cec5SDimitry Andric} 5710b57cec5SDimitry Andric 5720b57cec5SDimitry Andric// 6.4 Non-member functions 5730b57cec5SDimitry Andric 5740b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5750b57cec5SDimitry Andricvoid swap(any & __lhs, any & __rhs) _NOEXCEPT 5760b57cec5SDimitry Andric{ 5770b57cec5SDimitry Andric __lhs.swap(__rhs); 5780b57cec5SDimitry Andric} 5790b57cec5SDimitry Andric 5800b57cec5SDimitry Andrictemplate <class _Tp, class ..._Args> 5810b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5820b57cec5SDimitry Andricany make_any(_Args&&... __args) { 5830b57cec5SDimitry Andric return any(in_place_type<_Tp>, _VSTD::forward<_Args>(__args)...); 5840b57cec5SDimitry Andric} 5850b57cec5SDimitry Andric 5860b57cec5SDimitry Andrictemplate <class _Tp, class _Up, class ..._Args> 5870b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5880b57cec5SDimitry Andricany make_any(initializer_list<_Up> __il, _Args&&... __args) { 5890b57cec5SDimitry Andric return any(in_place_type<_Tp>, __il, _VSTD::forward<_Args>(__args)...); 5900b57cec5SDimitry Andric} 5910b57cec5SDimitry Andric 5920b57cec5SDimitry Andrictemplate <class _ValueType> 5930b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 5940b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST 5950b57cec5SDimitry Andric_ValueType any_cast(any const & __v) 5960b57cec5SDimitry Andric{ 5970b57cec5SDimitry Andric using _RawValueType = __uncvref_t<_ValueType>; 5980b57cec5SDimitry Andric static_assert(is_constructible<_ValueType, _RawValueType const &>::value, 5990b57cec5SDimitry Andric "ValueType is required to be a const lvalue reference " 6000b57cec5SDimitry Andric "or a CopyConstructible type"); 6010b57cec5SDimitry Andric auto __tmp = _VSTD::any_cast<add_const_t<_RawValueType>>(&__v); 6020b57cec5SDimitry Andric if (__tmp == nullptr) 6030b57cec5SDimitry Andric __throw_bad_any_cast(); 6040b57cec5SDimitry Andric return static_cast<_ValueType>(*__tmp); 6050b57cec5SDimitry Andric} 6060b57cec5SDimitry Andric 6070b57cec5SDimitry Andrictemplate <class _ValueType> 6080b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 6090b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST 6100b57cec5SDimitry Andric_ValueType any_cast(any & __v) 6110b57cec5SDimitry Andric{ 6120b57cec5SDimitry Andric using _RawValueType = __uncvref_t<_ValueType>; 6130b57cec5SDimitry Andric static_assert(is_constructible<_ValueType, _RawValueType &>::value, 6140b57cec5SDimitry Andric "ValueType is required to be an lvalue reference " 6150b57cec5SDimitry Andric "or a CopyConstructible type"); 6160b57cec5SDimitry Andric auto __tmp = _VSTD::any_cast<_RawValueType>(&__v); 6170b57cec5SDimitry Andric if (__tmp == nullptr) 6180b57cec5SDimitry Andric __throw_bad_any_cast(); 6190b57cec5SDimitry Andric return static_cast<_ValueType>(*__tmp); 6200b57cec5SDimitry Andric} 6210b57cec5SDimitry Andric 6220b57cec5SDimitry Andrictemplate <class _ValueType> 6230b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 6240b57cec5SDimitry Andric_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST 6250b57cec5SDimitry Andric_ValueType any_cast(any && __v) 6260b57cec5SDimitry Andric{ 6270b57cec5SDimitry Andric using _RawValueType = __uncvref_t<_ValueType>; 6280b57cec5SDimitry Andric static_assert(is_constructible<_ValueType, _RawValueType>::value, 6290b57cec5SDimitry Andric "ValueType is required to be an rvalue reference " 6300b57cec5SDimitry Andric "or a CopyConstructible type"); 6310b57cec5SDimitry Andric auto __tmp = _VSTD::any_cast<_RawValueType>(&__v); 6320b57cec5SDimitry Andric if (__tmp == nullptr) 6330b57cec5SDimitry Andric __throw_bad_any_cast(); 6340b57cec5SDimitry Andric return static_cast<_ValueType>(_VSTD::move(*__tmp)); 6350b57cec5SDimitry Andric} 6360b57cec5SDimitry Andric 6370b57cec5SDimitry Andrictemplate <class _ValueType> 6380b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 6390b57cec5SDimitry Andricadd_pointer_t<add_const_t<_ValueType>> 6400b57cec5SDimitry Andricany_cast(any const * __any) _NOEXCEPT 6410b57cec5SDimitry Andric{ 6420b57cec5SDimitry Andric static_assert(!is_reference<_ValueType>::value, 6430b57cec5SDimitry Andric "_ValueType may not be a reference."); 6440b57cec5SDimitry Andric return _VSTD::any_cast<_ValueType>(const_cast<any *>(__any)); 6450b57cec5SDimitry Andric} 6460b57cec5SDimitry Andric 6470b57cec5SDimitry Andrictemplate <class _RetType> 6480b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 6490b57cec5SDimitry Andric_RetType __pointer_or_func_cast(void* __p, /*IsFunction*/false_type) noexcept { 6500b57cec5SDimitry Andric return static_cast<_RetType>(__p); 6510b57cec5SDimitry Andric} 6520b57cec5SDimitry Andric 6530b57cec5SDimitry Andrictemplate <class _RetType> 6540b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 6550b57cec5SDimitry Andric_RetType __pointer_or_func_cast(void*, /*IsFunction*/true_type) noexcept { 6560b57cec5SDimitry Andric return nullptr; 6570b57cec5SDimitry Andric} 6580b57cec5SDimitry Andric 6590b57cec5SDimitry Andrictemplate <class _ValueType> 6600b57cec5SDimitry Andricadd_pointer_t<_ValueType> 6610b57cec5SDimitry Andricany_cast(any * __any) _NOEXCEPT 6620b57cec5SDimitry Andric{ 6630b57cec5SDimitry Andric using __any_imp::_Action; 6640b57cec5SDimitry Andric static_assert(!is_reference<_ValueType>::value, 6650b57cec5SDimitry Andric "_ValueType may not be a reference."); 6660b57cec5SDimitry Andric typedef typename add_pointer<_ValueType>::type _ReturnType; 6670b57cec5SDimitry Andric if (__any && __any->__h) { 6680b57cec5SDimitry Andric void *__p = __any->__call(_Action::_Get, nullptr, 6690b57cec5SDimitry Andric#if !defined(_LIBCPP_NO_RTTI) 6700b57cec5SDimitry Andric &typeid(_ValueType), 6710b57cec5SDimitry Andric#else 6720b57cec5SDimitry Andric nullptr, 6730b57cec5SDimitry Andric#endif 6740b57cec5SDimitry Andric __any_imp::__get_fallback_typeid<_ValueType>()); 6750b57cec5SDimitry Andric return _VSTD::__pointer_or_func_cast<_ReturnType>( 6760b57cec5SDimitry Andric __p, is_function<_ValueType>{}); 6770b57cec5SDimitry Andric } 6780b57cec5SDimitry Andric return nullptr; 6790b57cec5SDimitry Andric} 6800b57cec5SDimitry Andric 6810b57cec5SDimitry Andric#endif // _LIBCPP_STD_VER > 14 6820b57cec5SDimitry Andric 6830b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD 6840b57cec5SDimitry Andric 6850b57cec5SDimitry Andric#endif // _LIBCPP_ANY 686