2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
18 class bad_any_cast : public bad_cast
21 virtual const char* what() const noexcept;
28 // 6.3.1 any construct/destruct
31 any(const any& other);
32 any(any&& other) noexcept;
34 template <class ValueType>
35 any(ValueType&& value);
39 // 6.3.2 any assignments
40 any& operator=(const any& rhs);
41 any& operator=(any&& rhs) noexcept;
43 template <class ValueType>
44 any& operator=(ValueType&& rhs);
46 // 6.3.3 any modifiers
47 template <class ValueType, class... Args>
48 decay_t<ValueType>& emplace(Args&&... args);
49 template <class ValueType, class U, class... Args>
50 decay_t<ValueType>& emplace(initializer_list<U>, Args&&...);
51 void reset() noexcept;
52 void swap(any& rhs) noexcept;
54 // 6.3.4 any observers
55 bool has_value() const noexcept;
56 const type_info& type() const noexcept;
59 // 6.4 Non-member functions
60 void swap(any& x, any& y) noexcept;
62 template <class T, class ...Args>
63 any make_any(Args&& ...args);
64 template <class T, class U, class ...Args>
65 any make_any(initializer_list<U>, Args&& ...args);
67 template<class ValueType>
68 ValueType any_cast(const any& operand);
69 template<class ValueType>
70 ValueType any_cast(any& operand);
71 template<class ValueType>
72 ValueType any_cast(any&& operand);
74 template<class ValueType>
75 const ValueType* any_cast(const any* operand) noexcept;
76 template<class ValueType>
77 ValueType* any_cast(any* operand) noexcept;
83 #include <__cxx03/__config>
84 #include <__cxx03/__memory/allocator.h>
85 #include <__cxx03/__memory/allocator_destructor.h>
86 #include <__cxx03/__memory/allocator_traits.h>
87 #include <__cxx03/__memory/unique_ptr.h>
88 #include <__cxx03/__type_traits/add_const.h>
89 #include <__cxx03/__type_traits/add_pointer.h>
90 #include <__cxx03/__type_traits/aligned_storage.h>
91 #include <__cxx03/__type_traits/conditional.h>
92 #include <__cxx03/__type_traits/decay.h>
93 #include <__cxx03/__type_traits/is_constructible.h>
94 #include <__cxx03/__type_traits/is_function.h>
95 #include <__cxx03/__type_traits/is_nothrow_constructible.h>
96 #include <__cxx03/__type_traits/is_reference.h>
97 #include <__cxx03/__type_traits/is_same.h>
98 #include <__cxx03/__type_traits/is_void.h>
99 #include <__cxx03/__type_traits/remove_cv.h>
100 #include <__cxx03/__type_traits/remove_cvref.h>
101 #include <__cxx03/__type_traits/remove_reference.h>
102 #include <__cxx03/__utility/forward.h>
103 #include <__cxx03/__utility/in_place.h>
104 #include <__cxx03/__utility/move.h>
105 #include <__cxx03/__utility/unreachable.h>
106 #include <__cxx03/__verbose_abort>
107 #include <__cxx03/initializer_list>
108 #include <__cxx03/typeinfo>
109 #include <__cxx03/version>
111 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
112 # pragma GCC system_header
116 #include <__cxx03/__undef_macros>
119 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_ANY_CAST bad_any_cast : public bad_cast {
121 const char* what() const _NOEXCEPT override;
125 _LIBCPP_BEGIN_NAMESPACE_STD
127 #if _LIBCPP_STD_VER >= 17
129 _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST void __throw_bad_any_cast() {
130 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
131 throw bad_any_cast();
133 _LIBCPP_VERBOSE_ABORT("bad_any_cast was thrown in -fno-exceptions mode");
137 // Forward declarations
138 class _LIBCPP_TEMPLATE_VIS any;
140 template <class _ValueType>
141 _LIBCPP_HIDE_FROM_ABI add_pointer_t<add_const_t<_ValueType>> any_cast(any const*) _NOEXCEPT;
143 template <class _ValueType>
144 _LIBCPP_HIDE_FROM_ABI add_pointer_t<_ValueType> any_cast(any*) _NOEXCEPT;
146 namespace __any_imp {
147 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
148 using _Buffer = aligned_storage_t<3 * sizeof(void*), alignof(void*)>;
149 _LIBCPP_SUPPRESS_DEPRECATED_POP
152 using _IsSmallObject =
153 integral_constant<bool,
154 sizeof(_Tp) <= sizeof(_Buffer) && alignof(_Buffer) % alignof(_Tp) == 0 &&
155 is_nothrow_move_constructible<_Tp>::value >;
157 enum class _Action { _Destroy, _Copy, _Move, _Get, _TypeInfo };
160 struct _SmallHandler;
162 struct _LargeHandler;
165 struct _LIBCPP_TEMPLATE_VIS __unique_typeinfo {
166 static constexpr int __id = 0;
169 constexpr int __unique_typeinfo<_Tp>::__id;
172 inline _LIBCPP_HIDE_FROM_ABI constexpr const void* __get_fallback_typeid() {
173 return &__unique_typeinfo<remove_cv_t<remove_reference_t<_Tp>>>::__id;
177 inline _LIBCPP_HIDE_FROM_ABI bool __compare_typeid(type_info const* __id, const void* __fallback_id) {
178 # if !defined(_LIBCPP_HAS_NO_RTTI)
179 if (__id && *__id == typeid(_Tp))
182 return !__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>();
186 using _Handler = conditional_t< _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>;
188 } // namespace __any_imp
190 class _LIBCPP_TEMPLATE_VIS any {
192 // construct/destruct
193 _LIBCPP_HIDE_FROM_ABI constexpr any() _NOEXCEPT : __h_(nullptr) {}
195 _LIBCPP_HIDE_FROM_ABI any(any const& __other) : __h_(nullptr) {
197 __other.__call(_Action::_Copy, this);
200 _LIBCPP_HIDE_FROM_ABI any(any&& __other) _NOEXCEPT : __h_(nullptr) {
202 __other.__call(_Action::_Move, this);
205 template < class _ValueType,
206 class _Tp = decay_t<_ValueType>,
207 class = enable_if_t< !is_same<_Tp, any>::value && !__is_inplace_type<_ValueType>::value &&
208 is_copy_constructible<_Tp>::value> >
209 _LIBCPP_HIDE_FROM_ABI any(_ValueType&& __value);
211 template <class _ValueType,
213 class _Tp = decay_t<_ValueType>,
214 class = enable_if_t< is_constructible<_Tp, _Args...>::value && is_copy_constructible<_Tp>::value > >
215 _LIBCPP_HIDE_FROM_ABI explicit any(in_place_type_t<_ValueType>, _Args&&... __args);
217 template <class _ValueType,
220 class _Tp = decay_t<_ValueType>,
221 class = enable_if_t< is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
222 is_copy_constructible<_Tp>::value> >
223 _LIBCPP_HIDE_FROM_ABI explicit any(in_place_type_t<_ValueType>, initializer_list<_Up>, _Args&&... __args);
225 _LIBCPP_HIDE_FROM_ABI ~any() { this->reset(); }
228 _LIBCPP_HIDE_FROM_ABI any& operator=(any const& __rhs) {
229 any(__rhs).swap(*this);
233 _LIBCPP_HIDE_FROM_ABI any& operator=(any&& __rhs) _NOEXCEPT {
234 any(std::move(__rhs)).swap(*this);
238 template < class _ValueType,
239 class _Tp = decay_t<_ValueType>,
240 class = enable_if_t< !is_same<_Tp, any>::value && is_copy_constructible<_Tp>::value> >
241 _LIBCPP_HIDE_FROM_ABI any& operator=(_ValueType&& __rhs);
243 template <class _ValueType,
245 class _Tp = decay_t<_ValueType>,
246 class = enable_if_t< is_constructible<_Tp, _Args...>::value && is_copy_constructible<_Tp>::value> >
247 _LIBCPP_HIDE_FROM_ABI _Tp& emplace(_Args&&...);
249 template <class _ValueType,
252 class _Tp = decay_t<_ValueType>,
253 class = enable_if_t< is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
254 is_copy_constructible<_Tp>::value> >
255 _LIBCPP_HIDE_FROM_ABI _Tp& emplace(initializer_list<_Up>, _Args&&...);
257 // 6.3.3 any modifiers
258 _LIBCPP_HIDE_FROM_ABI void reset() _NOEXCEPT {
260 this->__call(_Action::_Destroy);
263 _LIBCPP_HIDE_FROM_ABI void swap(any& __rhs) _NOEXCEPT;
265 // 6.3.4 any observers
266 _LIBCPP_HIDE_FROM_ABI bool has_value() const _NOEXCEPT { return __h_ != nullptr; }
268 # if !defined(_LIBCPP_HAS_NO_RTTI)
269 _LIBCPP_HIDE_FROM_ABI const type_info& type() const _NOEXCEPT {
271 return *static_cast<type_info const*>(this->__call(_Action::_TypeInfo));
279 typedef __any_imp::_Action _Action;
280 using _HandleFuncPtr = void* (*)(_Action, any const*, any*, const type_info*, const void* __fallback_info);
283 _LIBCPP_HIDE_FROM_ABI constexpr _Storage() : __ptr(nullptr) {}
285 __any_imp::_Buffer __buf;
288 _LIBCPP_HIDE_FROM_ABI void*
289 __call(_Action __a, any* __other = nullptr, type_info const* __info = nullptr, const void* __fallback_info = nullptr)
291 return __h_(__a, this, __other, __info, __fallback_info);
294 _LIBCPP_HIDE_FROM_ABI void* __call(
295 _Action __a, any* __other = nullptr, type_info const* __info = nullptr, const void* __fallback_info = nullptr) {
296 return __h_(__a, this, __other, __info, __fallback_info);
300 friend struct __any_imp::_SmallHandler;
302 friend struct __any_imp::_LargeHandler;
304 template <class _ValueType>
305 friend add_pointer_t<add_const_t<_ValueType>> any_cast(any const*) _NOEXCEPT;
307 template <class _ValueType>
308 friend add_pointer_t<_ValueType> any_cast(any*) _NOEXCEPT;
310 _HandleFuncPtr __h_ = nullptr;
314 namespace __any_imp {
316 struct _LIBCPP_TEMPLATE_VIS _SmallHandler {
317 _LIBCPP_HIDE_FROM_ABI static void*
318 __handle(_Action __act, any const* __this, any* __other, type_info const* __info, const void* __fallback_info) {
320 case _Action::_Destroy:
321 __destroy(const_cast<any&>(*__this));
324 __copy(*__this, *__other);
327 __move(const_cast<any&>(*__this), *__other);
330 return __get(const_cast<any&>(*__this), __info, __fallback_info);
331 case _Action::_TypeInfo:
332 return __type_info();
334 __libcpp_unreachable();
337 template <class... _Args>
338 _LIBCPP_HIDE_FROM_ABI static _Tp& __create(any& __dest, _Args&&... __args) {
339 typedef allocator<_Tp> _Alloc;
340 typedef allocator_traits<_Alloc> _ATraits;
342 _Tp* __ret = static_cast<_Tp*>(static_cast<void*>(&__dest.__s_.__buf));
343 _ATraits::construct(__a, __ret, std::forward<_Args>(__args)...);
344 __dest.__h_ = &_SmallHandler::__handle;
349 _LIBCPP_HIDE_FROM_ABI static void __destroy(any& __this) {
350 typedef allocator<_Tp> _Alloc;
351 typedef allocator_traits<_Alloc> _ATraits;
353 _Tp* __p = static_cast<_Tp*>(static_cast<void*>(&__this.__s_.__buf));
354 _ATraits::destroy(__a, __p);
355 __this.__h_ = nullptr;
358 _LIBCPP_HIDE_FROM_ABI static void __copy(any const& __this, any& __dest) {
359 _SmallHandler::__create(__dest, *static_cast<_Tp const*>(static_cast<void const*>(&__this.__s_.__buf)));
362 _LIBCPP_HIDE_FROM_ABI static void __move(any& __this, any& __dest) {
363 _SmallHandler::__create(__dest, std::move(*static_cast<_Tp*>(static_cast<void*>(&__this.__s_.__buf))));
367 _LIBCPP_HIDE_FROM_ABI static void* __get(any& __this, type_info const* __info, const void* __fallback_id) {
368 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id))
369 return static_cast<void*>(&__this.__s_.__buf);
373 _LIBCPP_HIDE_FROM_ABI static void* __type_info() {
374 # if !defined(_LIBCPP_HAS_NO_RTTI)
375 return const_cast<void*>(static_cast<void const*>(&typeid(_Tp)));
383 struct _LIBCPP_TEMPLATE_VIS _LargeHandler {
384 _LIBCPP_HIDE_FROM_ABI static void*
385 __handle(_Action __act, any const* __this, any* __other, type_info const* __info, void const* __fallback_info) {
387 case _Action::_Destroy:
388 __destroy(const_cast<any&>(*__this));
391 __copy(*__this, *__other);
394 __move(const_cast<any&>(*__this), *__other);
397 return __get(const_cast<any&>(*__this), __info, __fallback_info);
398 case _Action::_TypeInfo:
399 return __type_info();
401 __libcpp_unreachable();
404 template <class... _Args>
405 _LIBCPP_HIDE_FROM_ABI static _Tp& __create(any& __dest, _Args&&... __args) {
406 typedef allocator<_Tp> _Alloc;
407 typedef allocator_traits<_Alloc> _ATraits;
408 typedef __allocator_destructor<_Alloc> _Dp;
410 unique_ptr<_Tp, _Dp> __hold(_ATraits::allocate(__a, 1), _Dp(__a, 1));
411 _Tp* __ret = __hold.get();
412 _ATraits::construct(__a, __ret, std::forward<_Args>(__args)...);
413 __dest.__s_.__ptr = __hold.release();
414 __dest.__h_ = &_LargeHandler::__handle;
419 _LIBCPP_HIDE_FROM_ABI static void __destroy(any& __this) {
420 typedef allocator<_Tp> _Alloc;
421 typedef allocator_traits<_Alloc> _ATraits;
423 _Tp* __p = static_cast<_Tp*>(__this.__s_.__ptr);
424 _ATraits::destroy(__a, __p);
425 _ATraits::deallocate(__a, __p, 1);
426 __this.__h_ = nullptr;
429 _LIBCPP_HIDE_FROM_ABI static void __copy(any const& __this, any& __dest) {
430 _LargeHandler::__create(__dest, *static_cast<_Tp const*>(__this.__s_.__ptr));
433 _LIBCPP_HIDE_FROM_ABI static void __move(any& __this, any& __dest) {
434 __dest.__s_.__ptr = __this.__s_.__ptr;
435 __dest.__h_ = &_LargeHandler::__handle;
436 __this.__h_ = nullptr;
439 _LIBCPP_HIDE_FROM_ABI static void* __get(any& __this, type_info const* __info, void const* __fallback_info) {
440 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info))
441 return static_cast<void*>(__this.__s_.__ptr);
445 _LIBCPP_HIDE_FROM_ABI static void* __type_info() {
446 # if !defined(_LIBCPP_HAS_NO_RTTI)
447 return const_cast<void*>(static_cast<void const*>(&typeid(_Tp)));
454 } // namespace __any_imp
456 template <class _ValueType, class _Tp, class>
457 any::any(_ValueType&& __v) : __h_(nullptr) {
458 __any_imp::_Handler<_Tp>::__create(*this, std::forward<_ValueType>(__v));
461 template <class _ValueType, class... _Args, class _Tp, class>
462 any::any(in_place_type_t<_ValueType>, _Args&&... __args) {
463 __any_imp::_Handler<_Tp>::__create(*this, std::forward<_Args>(__args)...);
466 template <class _ValueType, class _Up, class... _Args, class _Tp, class>
467 any::any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) {
468 __any_imp::_Handler<_Tp>::__create(*this, __il, std::forward<_Args>(__args)...);
471 template <class _ValueType, class, class>
472 inline _LIBCPP_HIDE_FROM_ABI any& any::operator=(_ValueType&& __v) {
473 any(std::forward<_ValueType>(__v)).swap(*this);
477 template <class _ValueType, class... _Args, class _Tp, class>
478 inline _LIBCPP_HIDE_FROM_ABI _Tp& any::emplace(_Args&&... __args) {
480 return __any_imp::_Handler<_Tp>::__create(*this, std::forward<_Args>(__args)...);
483 template <class _ValueType, class _Up, class... _Args, class _Tp, class>
484 inline _LIBCPP_HIDE_FROM_ABI _Tp& any::emplace(initializer_list<_Up> __il, _Args&&... __args) {
486 return __any_imp::_Handler<_Tp>::__create(*this, __il, std::forward<_Args>(__args)...);
489 inline _LIBCPP_HIDE_FROM_ABI void any::swap(any& __rhs) _NOEXCEPT {
492 if (__h_ && __rhs.__h_) {
494 __rhs.__call(_Action::_Move, &__tmp);
495 this->__call(_Action::_Move, &__rhs);
496 __tmp.__call(_Action::_Move, this);
498 this->__call(_Action::_Move, &__rhs);
499 } else if (__rhs.__h_) {
500 __rhs.__call(_Action::_Move, this);
504 // 6.4 Non-member functions
506 inline _LIBCPP_HIDE_FROM_ABI void swap(any& __lhs, any& __rhs) _NOEXCEPT { __lhs.swap(__rhs); }
508 template <class _Tp, class... _Args>
509 inline _LIBCPP_HIDE_FROM_ABI any make_any(_Args&&... __args) {
510 return any(in_place_type<_Tp>, std::forward<_Args>(__args)...);
513 template <class _Tp, class _Up, class... _Args>
514 inline _LIBCPP_HIDE_FROM_ABI any make_any(initializer_list<_Up> __il, _Args&&... __args) {
515 return any(in_place_type<_Tp>, __il, std::forward<_Args>(__args)...);
518 template <class _ValueType>
519 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST _ValueType any_cast(any const& __v) {
520 using _RawValueType = __remove_cvref_t<_ValueType>;
521 static_assert(is_constructible<_ValueType, _RawValueType const&>::value,
522 "ValueType is required to be a const lvalue reference "
523 "or a CopyConstructible type");
524 auto __tmp = std::any_cast<add_const_t<_RawValueType>>(&__v);
525 if (__tmp == nullptr)
526 __throw_bad_any_cast();
527 return static_cast<_ValueType>(*__tmp);
530 template <class _ValueType>
531 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST _ValueType any_cast(any& __v) {
532 using _RawValueType = __remove_cvref_t<_ValueType>;
533 static_assert(is_constructible<_ValueType, _RawValueType&>::value,
534 "ValueType is required to be an lvalue reference "
535 "or a CopyConstructible type");
536 auto __tmp = std::any_cast<_RawValueType>(&__v);
537 if (__tmp == nullptr)
538 __throw_bad_any_cast();
539 return static_cast<_ValueType>(*__tmp);
542 template <class _ValueType>
543 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST _ValueType any_cast(any&& __v) {
544 using _RawValueType = __remove_cvref_t<_ValueType>;
545 static_assert(is_constructible<_ValueType, _RawValueType>::value,
546 "ValueType is required to be an rvalue reference "
547 "or a CopyConstructible type");
548 auto __tmp = std::any_cast<_RawValueType>(&__v);
549 if (__tmp == nullptr)
550 __throw_bad_any_cast();
551 return static_cast<_ValueType>(std::move(*__tmp));
554 template <class _ValueType>
555 inline _LIBCPP_HIDE_FROM_ABI add_pointer_t<add_const_t<_ValueType>> any_cast(any const* __any) _NOEXCEPT {
556 static_assert(!is_void_v<_ValueType>, "_ValueType may not be void.");
557 static_assert(!is_reference<_ValueType>::value, "_ValueType may not be a reference.");
558 return std::any_cast<_ValueType>(const_cast<any*>(__any));
561 template <class _RetType>
562 inline _LIBCPP_HIDE_FROM_ABI _RetType __pointer_or_func_cast(void* __p, /*IsFunction*/ false_type) noexcept {
563 return static_cast<_RetType>(__p);
566 template <class _RetType>
567 inline _LIBCPP_HIDE_FROM_ABI _RetType __pointer_or_func_cast(void*, /*IsFunction*/ true_type) noexcept {
571 template <class _ValueType>
572 _LIBCPP_HIDE_FROM_ABI add_pointer_t<_ValueType> any_cast(any* __any) _NOEXCEPT {
573 using __any_imp::_Action;
574 static_assert(!is_void_v<_ValueType>, "_ValueType may not be void.");
575 static_assert(!is_reference<_ValueType>::value, "_ValueType may not be a reference.");
576 typedef add_pointer_t<_ValueType> _ReturnType;
577 if (__any && __any->__h_) {
578 void* __p = __any->__call(
581 # if !defined(_LIBCPP_HAS_NO_RTTI)
586 __any_imp::__get_fallback_typeid<_ValueType>());
587 return std::__pointer_or_func_cast<_ReturnType>(__p, is_function<_ValueType>{});
592 #endif // _LIBCPP_STD_VER >= 17
594 _LIBCPP_END_NAMESPACE_STD
598 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
599 # include <__cxx03/chrono>
602 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
603 # include <__cxx03/atomic>
604 # include <__cxx03/concepts>
605 # include <__cxx03/cstdlib>
606 # include <__cxx03/iosfwd>
607 # include <__cxx03/iterator>
608 # include <__cxx03/memory>
609 # include <__cxx03/stdexcept>
610 # include <__cxx03/type_traits>
611 # include <__cxx03/variant>
614 #endif // _LIBCPP_ANY