// -*- C++ -*- //===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef _LIBCPP_ANY #define _LIBCPP_ANY /* any synopsis namespace std { class bad_any_cast : public bad_cast { public: virtual const char* what() const noexcept; }; class any { public: // 6.3.1 any construct/destruct any() noexcept; any(const any& other); any(any&& other) noexcept; template any(ValueType&& value); ~any(); // 6.3.2 any assignments any& operator=(const any& rhs); any& operator=(any&& rhs) noexcept; template any& operator=(ValueType&& rhs); // 6.3.3 any modifiers template decay_t& emplace(Args&&... args); template decay_t& emplace(initializer_list, Args&&...); void reset() noexcept; void swap(any& rhs) noexcept; // 6.3.4 any observers bool has_value() const noexcept; const type_info& type() const noexcept; }; // 6.4 Non-member functions void swap(any& x, any& y) noexcept; template any make_any(Args&& ...args); template any make_any(initializer_list, Args&& ...args); template ValueType any_cast(const any& operand); template ValueType any_cast(any& operand); template ValueType any_cast(any&& operand); template const ValueType* any_cast(const any* operand) noexcept; template ValueType* any_cast(any* operand) noexcept; } // namespace std */ #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS) # include <__cxx03/__config> #else # include <__config> # include <__memory/construct_at.h> # include <__new/allocate.h> # include <__type_traits/add_cv_quals.h> # include <__type_traits/add_pointer.h> # include <__type_traits/conditional.h> # include <__type_traits/conjunction.h> # include <__type_traits/decay.h> # include <__type_traits/enable_if.h> # include <__type_traits/is_constructible.h> # include <__type_traits/is_function.h> # include <__type_traits/is_nothrow_constructible.h> # include <__type_traits/is_reference.h> # include <__type_traits/is_same.h> # include <__type_traits/is_void.h> # include <__type_traits/negation.h> # include <__type_traits/remove_cv.h> # include <__type_traits/remove_cvref.h> # include <__type_traits/remove_reference.h> # include <__utility/exception_guard.h> # include <__utility/forward.h> # include <__utility/in_place.h> # include <__utility/move.h> # include <__utility/unreachable.h> # include <__verbose_abort> # include # include # include # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header # endif _LIBCPP_PUSH_MACROS # include <__undef_macros> _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD class _LIBCPP_EXPORTED_FROM_ABI bad_any_cast : public bad_cast { public: const char* what() const _NOEXCEPT override; }; _LIBCPP_END_UNVERSIONED_NAMESPACE_STD _LIBCPP_BEGIN_NAMESPACE_STD # if _LIBCPP_STD_VER >= 17 [[noreturn]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_any_cast() { # if _LIBCPP_HAS_EXCEPTIONS throw bad_any_cast(); # else _LIBCPP_VERBOSE_ABORT("bad_any_cast was thrown in -fno-exceptions mode"); # endif } // Forward declarations class any; template _LIBCPP_HIDE_FROM_ABI add_pointer_t> any_cast(any const*) noexcept; template _LIBCPP_HIDE_FROM_ABI add_pointer_t<_ValueType> any_cast(any*) noexcept; namespace __any_imp { inline constexpr size_t __small_buffer_size = 3 * sizeof(void*); inline constexpr size_t __small_buffer_alignment = alignof(void*); template using _IsSmallObject _LIBCPP_NODEBUG = integral_constant>; enum class _Action { _Destroy, _Copy, _Move, _Get, _TypeInfo }; template struct _SmallHandler; template struct _LargeHandler; template struct __unique_typeinfo { static constexpr int __id = 0; }; template inline _LIBCPP_HIDE_FROM_ABI constexpr const void* __get_fallback_typeid() { return &__unique_typeinfo>>::__id; } template inline _LIBCPP_HIDE_FROM_ABI bool __compare_typeid(type_info const* __id, const void* __fallback_id) { # if _LIBCPP_HAS_RTTI if (__id && *__id == typeid(_Tp)) return true; # endif return !__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>(); } template using _Handler _LIBCPP_NODEBUG = conditional_t< _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>; } // namespace __any_imp class any { public: // construct/destruct _LIBCPP_HIDE_FROM_ABI constexpr any() noexcept : __h_(nullptr) {} _LIBCPP_HIDE_FROM_ABI any(any const& __other) : __h_(nullptr) { if (__other.__h_) __other.__call(_Action::_Copy, this); } _LIBCPP_HIDE_FROM_ABI any(any&& __other) noexcept : __h_(nullptr) { if (__other.__h_) __other.__call(_Action::_Move, this); } template < class _ValueType, class _Tp = decay_t<_ValueType>, enable_if_t<_And<_Not>, _Not<__is_inplace_type<_ValueType>>, is_copy_constructible<_Tp>>::value, int> = 0> _LIBCPP_HIDE_FROM_ABI any(_ValueType&& __value) : __h_(nullptr) { __any_imp::_Handler<_Tp>::__create(*this, std::forward<_ValueType>(__value)); } template , enable_if_t && is_copy_constructible_v<_Tp>, int> = 0> _LIBCPP_HIDE_FROM_ABI explicit any(in_place_type_t<_ValueType>, _Args&&... __args) { __any_imp::_Handler<_Tp>::__create(*this, std::forward<_Args>(__args)...); } template < class _ValueType, class _Up, class... _Args, class _Tp = decay_t<_ValueType>, enable_if_t&, _Args...> && is_copy_constructible_v<_Tp>, int> = 0> _LIBCPP_HIDE_FROM_ABI explicit any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) { __any_imp::_Handler<_Tp>::__create(*this, __il, std::forward<_Args>(__args)...); } _LIBCPP_HIDE_FROM_ABI ~any() { this->reset(); } // assignments _LIBCPP_HIDE_FROM_ABI any& operator=(any const& __rhs) { any(__rhs).swap(*this); return *this; } _LIBCPP_HIDE_FROM_ABI any& operator=(any&& __rhs) noexcept { any(std::move(__rhs)).swap(*this); return *this; } template , enable_if_t && is_copy_constructible_v<_Tp>, int> = 0> _LIBCPP_HIDE_FROM_ABI any& operator=(_ValueType&& __rhs) { any(std::forward<_ValueType>(__rhs)).swap(*this); return *this; } template , enable_if_t && is_copy_constructible_v<_Tp>, int> = 0> _LIBCPP_HIDE_FROM_ABI _Tp& emplace(_Args&&... __args) { reset(); return __any_imp::_Handler<_Tp>::__create(*this, std::forward<_Args>(__args)...); } template < class _ValueType, class _Up, class... _Args, class _Tp = decay_t<_ValueType>, enable_if_t&, _Args...> && is_copy_constructible_v<_Tp>, int> = 0> _LIBCPP_HIDE_FROM_ABI _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) { reset(); return __any_imp::_Handler<_Tp>::__create(*this, __il, std::forward<_Args>(__args)...); } // 6.3.3 any modifiers _LIBCPP_HIDE_FROM_ABI void reset() noexcept { if (__h_) this->__call(_Action::_Destroy); } _LIBCPP_HIDE_FROM_ABI void swap(any& __rhs) noexcept { if (this == &__rhs) return; if (__h_ && __rhs.__h_) { any __tmp; __rhs.__call(_Action::_Move, &__tmp); this->__call(_Action::_Move, &__rhs); __tmp.__call(_Action::_Move, this); } else if (__h_) { this->__call(_Action::_Move, &__rhs); } else if (__rhs.__h_) { __rhs.__call(_Action::_Move, this); } } // 6.3.4 any observers [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool has_value() const noexcept { return __h_ != nullptr; } # if _LIBCPP_HAS_RTTI [[nodiscard]] _LIBCPP_HIDE_FROM_ABI const type_info& type() const noexcept { if (__h_) { return *static_cast(this->__call(_Action::_TypeInfo)); } else { return typeid(void); } } # endif private: using _Action _LIBCPP_NODEBUG = __any_imp::_Action; using _HandleFuncPtr _LIBCPP_NODEBUG = void* (*)(_Action, any const*, any*, const type_info*, const void* __fallback_info); union _Storage { _LIBCPP_HIDE_FROM_ABI constexpr _Storage() : __ptr(nullptr) {} void* __ptr; alignas(__any_imp::__small_buffer_alignment) char __buf[__any_imp::__small_buffer_size]; }; _LIBCPP_HIDE_FROM_ABI void* __call(_Action __a, any* __other = nullptr, type_info const* __info = nullptr, const void* __fallback_info = nullptr) const { return __h_(__a, this, __other, __info, __fallback_info); } _LIBCPP_HIDE_FROM_ABI void* __call( _Action __a, any* __other = nullptr, type_info const* __info = nullptr, const void* __fallback_info = nullptr) { return __h_(__a, this, __other, __info, __fallback_info); } template friend struct __any_imp::_SmallHandler; template friend struct __any_imp::_LargeHandler; template friend add_pointer_t> any_cast(any const*) noexcept; template friend add_pointer_t<_ValueType> any_cast(any*) noexcept; _HandleFuncPtr __h_ = nullptr; _Storage __s_; }; namespace __any_imp { template struct _SmallHandler { _LIBCPP_HIDE_FROM_ABI static void* __handle(_Action __act, any const* __this, any* __other, type_info const* __info, const void* __fallback_info) { switch (__act) { case _Action::_Destroy: __destroy(const_cast(*__this)); return nullptr; case _Action::_Copy: __copy(*__this, *__other); return nullptr; case _Action::_Move: __move(const_cast(*__this), *__other); return nullptr; case _Action::_Get: return __get(const_cast(*__this), __info, __fallback_info); case _Action::_TypeInfo: return __type_info(); } __libcpp_unreachable(); } template _LIBCPP_HIDE_FROM_ABI static _Tp& __create(any& __dest, _Args&&... __args) { auto __ret = std::__construct_at(reinterpret_cast<_Tp*>(&__dest.__s_.__buf), std::forward<_Args>(__args)...); __dest.__h_ = &_SmallHandler::__handle; return *__ret; } private: _LIBCPP_HIDE_FROM_ABI static void __destroy(any& __this) { std::__destroy_at(reinterpret_cast<_Tp*>(&__this.__s_.__buf)); __this.__h_ = nullptr; } _LIBCPP_HIDE_FROM_ABI static void __copy(any const& __this, any& __dest) { _SmallHandler::__create(__dest, *static_cast<_Tp const*>(static_cast(&__this.__s_.__buf))); } _LIBCPP_HIDE_FROM_ABI static void __move(any& __this, any& __dest) { _SmallHandler::__create(__dest, std::move(*static_cast<_Tp*>(static_cast(&__this.__s_.__buf)))); __destroy(__this); } _LIBCPP_HIDE_FROM_ABI static void* __get(any& __this, type_info const* __info, const void* __fallback_id) { if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id)) return static_cast(&__this.__s_.__buf); return nullptr; } _LIBCPP_HIDE_FROM_ABI static void* __type_info() { # if _LIBCPP_HAS_RTTI return const_cast(static_cast(&typeid(_Tp))); # else return nullptr; # endif } }; template struct _LargeHandler { _LIBCPP_HIDE_FROM_ABI static void* __handle(_Action __act, any const* __this, any* __other, type_info const* __info, void const* __fallback_info) { switch (__act) { case _Action::_Destroy: __destroy(const_cast(*__this)); return nullptr; case _Action::_Copy: __copy(*__this, *__other); return nullptr; case _Action::_Move: __move(const_cast(*__this), *__other); return nullptr; case _Action::_Get: return __get(const_cast(*__this), __info, __fallback_info); case _Action::_TypeInfo: return __type_info(); } __libcpp_unreachable(); } template _LIBCPP_HIDE_FROM_ABI static _Tp& __create(any& __dest, _Args&&... __args) { _Tp* __ptr = static_cast<_Tp*>(std::__libcpp_allocate<_Tp>(__element_count(1))); std::__exception_guard __guard([&] { std::__libcpp_deallocate<_Tp>(__ptr, __element_count(1)); }); std::__construct_at(__ptr, std::forward<_Args>(__args)...); __guard.__complete(); __dest.__s_.__ptr = __ptr; __dest.__h_ = &_LargeHandler::__handle; return *__ptr; } private: _LIBCPP_HIDE_FROM_ABI static void __destroy(any& __this) { _Tp* __p = static_cast<_Tp*>(__this.__s_.__ptr); std::__destroy_at(__p); std::__libcpp_deallocate<_Tp>(__p, __element_count(1)); __this.__h_ = nullptr; } _LIBCPP_HIDE_FROM_ABI static void __copy(any const& __this, any& __dest) { _LargeHandler::__create(__dest, *static_cast<_Tp const*>(__this.__s_.__ptr)); } _LIBCPP_HIDE_FROM_ABI static void __move(any& __this, any& __dest) { __dest.__s_.__ptr = __this.__s_.__ptr; __dest.__h_ = &_LargeHandler::__handle; __this.__h_ = nullptr; } _LIBCPP_HIDE_FROM_ABI static void* __get(any& __this, type_info const* __info, void const* __fallback_info) { if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info)) return static_cast(__this.__s_.__ptr); return nullptr; } _LIBCPP_HIDE_FROM_ABI static void* __type_info() { # if _LIBCPP_HAS_RTTI return const_cast(static_cast(&typeid(_Tp))); # else return nullptr; # endif } }; } // namespace __any_imp // 6.4 Non-member functions inline _LIBCPP_HIDE_FROM_ABI void swap(any& __lhs, any& __rhs) noexcept { __lhs.swap(__rhs); } template [[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI any make_any(_Args&&... __args) { return any(in_place_type<_Tp>, std::forward<_Args>(__args)...); } template [[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI any make_any(initializer_list<_Up> __il, _Args&&... __args) { return any(in_place_type<_Tp>, __il, std::forward<_Args>(__args)...); } template [[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any const& __v) { using _RawValueType = __remove_cvref_t<_ValueType>; static_assert(is_constructible_v<_ValueType, _RawValueType const&>, "ValueType is required to be a const lvalue reference " "or a CopyConstructible type"); auto __tmp = std::any_cast>(&__v); if (__tmp == nullptr) std::__throw_bad_any_cast(); return static_cast<_ValueType>(*__tmp); } template [[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any& __v) { using _RawValueType = __remove_cvref_t<_ValueType>; static_assert(is_constructible_v<_ValueType, _RawValueType&>, "ValueType is required to be an lvalue reference " "or a CopyConstructible type"); auto __tmp = std::any_cast<_RawValueType>(&__v); if (__tmp == nullptr) std::__throw_bad_any_cast(); return static_cast<_ValueType>(*__tmp); } template [[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any&& __v) { using _RawValueType = __remove_cvref_t<_ValueType>; static_assert(is_constructible_v<_ValueType, _RawValueType>, "ValueType is required to be an rvalue reference " "or a CopyConstructible type"); auto __tmp = std::any_cast<_RawValueType>(&__v); if (__tmp == nullptr) std::__throw_bad_any_cast(); return static_cast<_ValueType>(std::move(*__tmp)); } template [[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI add_pointer_t> any_cast(any const* __any) noexcept { static_assert(!is_void_v<_ValueType>, "_ValueType may not be void."); static_assert(!is_reference_v<_ValueType>, "_ValueType may not be a reference."); return std::any_cast<_ValueType>(const_cast(__any)); } template [[nodiscard]] _LIBCPP_HIDE_FROM_ABI add_pointer_t<_ValueType> any_cast(any* __any) noexcept { using __any_imp::_Action; static_assert(!is_void_v<_ValueType>, "_ValueType may not be void."); static_assert(!is_reference_v<_ValueType>, "_ValueType may not be a reference."); if constexpr (!is_function_v<_ValueType>) { using _ReturnType = add_pointer_t<_ValueType>; if (__any && __any->__h_) { void* __p = __any->__call( _Action::_Get, nullptr, # if _LIBCPP_HAS_RTTI &typeid(_ValueType), # else nullptr, # endif __any_imp::__get_fallback_typeid<_ValueType>()); return static_cast<_ReturnType>(__p); } } return nullptr; } # endif // _LIBCPP_STD_VER >= 17 _LIBCPP_END_NAMESPACE_STD _LIBCPP_POP_MACROS # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17 # include # endif # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 # include # include # include # include # include # include # include # include # include # endif # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 23 # include # include # endif #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS) #endif // _LIBCPP_ANY