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 <__assert> // all public C++ headers provide the assertion handler
84 #include <__availability>
86 #include <__memory/allocator.h>
87 #include <__memory/allocator_destructor.h>
88 #include <__memory/allocator_traits.h>
89 #include <__memory/unique_ptr.h>
90 #include <__type_traits/add_const.h>
91 #include <__type_traits/add_pointer.h>
92 #include <__type_traits/aligned_storage.h>
93 #include <__type_traits/alignment_of.h>
94 #include <__type_traits/conditional.h>
95 #include <__type_traits/decay.h>
96 #include <__type_traits/is_constructible.h>
97 #include <__type_traits/is_copy_constructible.h>
98 #include <__type_traits/is_function.h>
99 #include <__type_traits/is_nothrow_move_constructible.h>
100 #include <__type_traits/is_reference.h>
101 #include <__type_traits/is_same.h>
102 #include <__type_traits/remove_cv.h>
103 #include <__type_traits/remove_cvref.h>
104 #include <__type_traits/remove_reference.h>
105 #include <__utility/forward.h>
106 #include <__utility/in_place.h>
107 #include <__utility/move.h>
108 #include <__utility/unreachable.h>
109 #include <__verbose_abort>
110 #include <initializer_list>
114 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
115 # pragma GCC system_header
119 #include <__undef_macros>
122 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_ANY_CAST bad_any_cast : public bad_cast
125 const char* what() const _NOEXCEPT override;
129 _LIBCPP_BEGIN_NAMESPACE_STD
131 #if _LIBCPP_STD_VER >= 17
133 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
134 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
135 void __throw_bad_any_cast()
137 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
138 throw bad_any_cast();
140 _LIBCPP_VERBOSE_ABORT("bad_any_cast was thrown in -fno-exceptions mode");
144 // Forward declarations
145 class _LIBCPP_TEMPLATE_VIS any;
147 template <class _ValueType>
148 _LIBCPP_INLINE_VISIBILITY
149 add_pointer_t<add_const_t<_ValueType>>
150 any_cast(any const *) _NOEXCEPT;
152 template <class _ValueType>
153 _LIBCPP_INLINE_VISIBILITY
154 add_pointer_t<_ValueType> any_cast(any *) _NOEXCEPT;
158 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
159 using _Buffer = aligned_storage_t<3*sizeof(void*), alignment_of<void*>::value>;
160 _LIBCPP_SUPPRESS_DEPRECATED_POP
163 using _IsSmallObject = integral_constant<bool
164 , sizeof(_Tp) <= sizeof(_Buffer)
165 && alignment_of<_Buffer>::value
166 % alignment_of<_Tp>::value == 0
167 && is_nothrow_move_constructible<_Tp>::value
178 template <class _Tp> struct _SmallHandler;
179 template <class _Tp> struct _LargeHandler;
182 struct _LIBCPP_TEMPLATE_VIS __unique_typeinfo { static constexpr int __id = 0; };
183 template <class _Tp> constexpr int __unique_typeinfo<_Tp>::__id;
186 inline _LIBCPP_INLINE_VISIBILITY
187 constexpr const void* __get_fallback_typeid() {
188 return &__unique_typeinfo<remove_cv_t<remove_reference_t<_Tp>>>::__id;
192 inline _LIBCPP_INLINE_VISIBILITY
193 bool __compare_typeid(type_info const* __id, const void* __fallback_id)
195 #if !defined(_LIBCPP_HAS_NO_RTTI)
196 if (__id && *__id == typeid(_Tp))
199 if (!__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>())
205 using _Handler = conditional_t<
206 _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>;
208 } // namespace __any_imp
210 class _LIBCPP_TEMPLATE_VIS any
213 // construct/destruct
214 _LIBCPP_INLINE_VISIBILITY
215 constexpr any() _NOEXCEPT : __h_(nullptr) {}
217 _LIBCPP_INLINE_VISIBILITY
218 any(any const & __other) : __h_(nullptr)
220 if (__other.__h_) __other.__call(_Action::_Copy, this);
223 _LIBCPP_INLINE_VISIBILITY
224 any(any && __other) _NOEXCEPT : __h_(nullptr)
226 if (__other.__h_) __other.__call(_Action::_Move, this);
231 , class _Tp = decay_t<_ValueType>
232 , class = enable_if_t<
233 !is_same<_Tp, any>::value &&
234 !__is_inplace_type<_ValueType>::value &&
235 is_copy_constructible<_Tp>::value>
237 _LIBCPP_INLINE_VISIBILITY
238 any(_ValueType && __value);
240 template <class _ValueType, class ..._Args,
241 class _Tp = decay_t<_ValueType>,
243 is_constructible<_Tp, _Args...>::value &&
244 is_copy_constructible<_Tp>::value
247 _LIBCPP_INLINE_VISIBILITY
248 explicit any(in_place_type_t<_ValueType>, _Args&&... __args);
250 template <class _ValueType, class _Up, class ..._Args,
251 class _Tp = decay_t<_ValueType>,
253 is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
254 is_copy_constructible<_Tp>::value>
256 _LIBCPP_INLINE_VISIBILITY
257 explicit any(in_place_type_t<_ValueType>, initializer_list<_Up>, _Args&&... __args);
259 _LIBCPP_INLINE_VISIBILITY
260 ~any() { this->reset(); }
263 _LIBCPP_INLINE_VISIBILITY
264 any & operator=(any const & __rhs) {
265 any(__rhs).swap(*this);
269 _LIBCPP_INLINE_VISIBILITY
270 any & operator=(any && __rhs) _NOEXCEPT {
271 any(_VSTD::move(__rhs)).swap(*this);
277 , class _Tp = decay_t<_ValueType>
278 , class = enable_if_t<
279 !is_same<_Tp, any>::value
280 && is_copy_constructible<_Tp>::value>
282 _LIBCPP_INLINE_VISIBILITY
283 any & operator=(_ValueType && __rhs);
285 template <class _ValueType, class ..._Args,
286 class _Tp = decay_t<_ValueType>,
288 is_constructible<_Tp, _Args...>::value &&
289 is_copy_constructible<_Tp>::value>
291 _LIBCPP_INLINE_VISIBILITY
292 _Tp& emplace(_Args&&...);
294 template <class _ValueType, class _Up, class ..._Args,
295 class _Tp = decay_t<_ValueType>,
297 is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
298 is_copy_constructible<_Tp>::value>
300 _LIBCPP_INLINE_VISIBILITY
301 _Tp& emplace(initializer_list<_Up>, _Args&&...);
303 // 6.3.3 any modifiers
304 _LIBCPP_INLINE_VISIBILITY
305 void reset() _NOEXCEPT { if (__h_) this->__call(_Action::_Destroy); }
307 _LIBCPP_INLINE_VISIBILITY
308 void swap(any & __rhs) _NOEXCEPT;
310 // 6.3.4 any observers
311 _LIBCPP_INLINE_VISIBILITY
312 bool has_value() const _NOEXCEPT { return __h_ != nullptr; }
314 #if !defined(_LIBCPP_HAS_NO_RTTI)
315 _LIBCPP_INLINE_VISIBILITY
316 const type_info & type() const _NOEXCEPT {
318 return *static_cast<type_info const *>(this->__call(_Action::_TypeInfo));
326 typedef __any_imp::_Action _Action;
327 using _HandleFuncPtr = void* (*)(_Action, any const *, any *, const type_info *,
328 const void* __fallback_info);
331 _LIBCPP_HIDE_FROM_ABI constexpr _Storage() : __ptr(nullptr) {}
333 __any_imp::_Buffer __buf;
336 _LIBCPP_INLINE_VISIBILITY
337 void * __call(_Action __a, any * __other = nullptr,
338 type_info const * __info = nullptr,
339 const void* __fallback_info = nullptr) const
341 return __h_(__a, this, __other, __info, __fallback_info);
344 _LIBCPP_INLINE_VISIBILITY
345 void * __call(_Action __a, any * __other = nullptr,
346 type_info const * __info = nullptr,
347 const void* __fallback_info = nullptr)
349 return __h_(__a, this, __other, __info, __fallback_info);
353 friend struct __any_imp::_SmallHandler;
355 friend struct __any_imp::_LargeHandler;
357 template <class _ValueType>
358 friend add_pointer_t<add_const_t<_ValueType>>
359 any_cast(any const *) _NOEXCEPT;
361 template <class _ValueType>
362 friend add_pointer_t<_ValueType>
363 any_cast(any *) _NOEXCEPT;
365 _HandleFuncPtr __h_ = nullptr;
372 struct _LIBCPP_TEMPLATE_VIS _SmallHandler
374 _LIBCPP_INLINE_VISIBILITY
375 static void* __handle(_Action __act, any const * __this, any * __other,
376 type_info const * __info, const void* __fallback_info)
380 case _Action::_Destroy:
381 __destroy(const_cast<any &>(*__this));
384 __copy(*__this, *__other);
387 __move(const_cast<any &>(*__this), *__other);
390 return __get(const_cast<any &>(*__this), __info, __fallback_info);
391 case _Action::_TypeInfo:
392 return __type_info();
394 __libcpp_unreachable();
397 template <class ..._Args>
398 _LIBCPP_INLINE_VISIBILITY
399 static _Tp& __create(any & __dest, _Args&&... __args) {
400 typedef allocator<_Tp> _Alloc;
401 typedef allocator_traits<_Alloc> _ATraits;
403 _Tp * __ret = static_cast<_Tp*>(static_cast<void*>(&__dest.__s_.__buf));
404 _ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...);
405 __dest.__h_ = &_SmallHandler::__handle;
410 _LIBCPP_INLINE_VISIBILITY
411 static void __destroy(any & __this) {
412 typedef allocator<_Tp> _Alloc;
413 typedef allocator_traits<_Alloc> _ATraits;
415 _Tp * __p = static_cast<_Tp *>(static_cast<void*>(&__this.__s_.__buf));
416 _ATraits::destroy(__a, __p);
417 __this.__h_ = nullptr;
420 _LIBCPP_INLINE_VISIBILITY
421 static void __copy(any const & __this, any & __dest) {
422 _SmallHandler::__create(__dest, *static_cast<_Tp const *>(
423 static_cast<void const *>(&__this.__s_.__buf)));
426 _LIBCPP_INLINE_VISIBILITY
427 static void __move(any & __this, any & __dest) {
428 _SmallHandler::__create(__dest, _VSTD::move(
429 *static_cast<_Tp*>(static_cast<void*>(&__this.__s_.__buf))));
433 _LIBCPP_INLINE_VISIBILITY
434 static void* __get(any & __this,
435 type_info const * __info,
436 const void* __fallback_id)
438 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id))
439 return static_cast<void*>(&__this.__s_.__buf);
443 _LIBCPP_INLINE_VISIBILITY
444 static void* __type_info()
446 #if !defined(_LIBCPP_HAS_NO_RTTI)
447 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
455 struct _LIBCPP_TEMPLATE_VIS _LargeHandler
457 _LIBCPP_INLINE_VISIBILITY
458 static void* __handle(_Action __act, any const * __this,
459 any * __other, type_info const * __info,
460 void const* __fallback_info)
464 case _Action::_Destroy:
465 __destroy(const_cast<any &>(*__this));
468 __copy(*__this, *__other);
471 __move(const_cast<any &>(*__this), *__other);
474 return __get(const_cast<any &>(*__this), __info, __fallback_info);
475 case _Action::_TypeInfo:
476 return __type_info();
478 __libcpp_unreachable();
481 template <class ..._Args>
482 _LIBCPP_INLINE_VISIBILITY
483 static _Tp& __create(any & __dest, _Args&&... __args) {
484 typedef allocator<_Tp> _Alloc;
485 typedef allocator_traits<_Alloc> _ATraits;
486 typedef __allocator_destructor<_Alloc> _Dp;
488 unique_ptr<_Tp, _Dp> __hold(_ATraits::allocate(__a, 1), _Dp(__a, 1));
489 _Tp * __ret = __hold.get();
490 _ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...);
491 __dest.__s_.__ptr = __hold.release();
492 __dest.__h_ = &_LargeHandler::__handle;
498 _LIBCPP_INLINE_VISIBILITY
499 static void __destroy(any & __this){
500 typedef allocator<_Tp> _Alloc;
501 typedef allocator_traits<_Alloc> _ATraits;
503 _Tp * __p = static_cast<_Tp *>(__this.__s_.__ptr);
504 _ATraits::destroy(__a, __p);
505 _ATraits::deallocate(__a, __p, 1);
506 __this.__h_ = nullptr;
509 _LIBCPP_INLINE_VISIBILITY
510 static void __copy(any const & __this, any & __dest) {
511 _LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s_.__ptr));
514 _LIBCPP_INLINE_VISIBILITY
515 static void __move(any & __this, any & __dest) {
516 __dest.__s_.__ptr = __this.__s_.__ptr;
517 __dest.__h_ = &_LargeHandler::__handle;
518 __this.__h_ = nullptr;
521 _LIBCPP_INLINE_VISIBILITY
522 static void* __get(any & __this, type_info const * __info,
523 void const* __fallback_info)
525 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info))
526 return static_cast<void*>(__this.__s_.__ptr);
531 _LIBCPP_INLINE_VISIBILITY
532 static void* __type_info()
534 #if !defined(_LIBCPP_HAS_NO_RTTI)
535 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
542 } // namespace __any_imp
545 template <class _ValueType, class _Tp, class>
546 any::any(_ValueType && __v) : __h_(nullptr)
548 __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_ValueType>(__v));
551 template <class _ValueType, class ..._Args, class _Tp, class>
552 any::any(in_place_type_t<_ValueType>, _Args&&... __args) {
553 __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
556 template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
557 any::any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) {
558 __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
561 template <class _ValueType, class, class>
562 inline _LIBCPP_INLINE_VISIBILITY
563 any & any::operator=(_ValueType && __v)
565 any(_VSTD::forward<_ValueType>(__v)).swap(*this);
569 template <class _ValueType, class ..._Args, class _Tp, class>
570 inline _LIBCPP_INLINE_VISIBILITY
571 _Tp& any::emplace(_Args&&... __args) {
573 return __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
576 template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
577 inline _LIBCPP_INLINE_VISIBILITY
578 _Tp& any::emplace(initializer_list<_Up> __il, _Args&&... __args) {
580 return __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
583 inline _LIBCPP_INLINE_VISIBILITY
584 void any::swap(any & __rhs) _NOEXCEPT
588 if (__h_ && __rhs.__h_) {
590 __rhs.__call(_Action::_Move, &__tmp);
591 this->__call(_Action::_Move, &__rhs);
592 __tmp.__call(_Action::_Move, this);
595 this->__call(_Action::_Move, &__rhs);
597 else if (__rhs.__h_) {
598 __rhs.__call(_Action::_Move, this);
602 // 6.4 Non-member functions
604 inline _LIBCPP_INLINE_VISIBILITY
605 void swap(any & __lhs, any & __rhs) _NOEXCEPT
610 template <class _Tp, class ..._Args>
611 inline _LIBCPP_INLINE_VISIBILITY
612 any make_any(_Args&&... __args) {
613 return any(in_place_type<_Tp>, _VSTD::forward<_Args>(__args)...);
616 template <class _Tp, class _Up, class ..._Args>
617 inline _LIBCPP_INLINE_VISIBILITY
618 any make_any(initializer_list<_Up> __il, _Args&&... __args) {
619 return any(in_place_type<_Tp>, __il, _VSTD::forward<_Args>(__args)...);
622 template <class _ValueType>
623 inline _LIBCPP_INLINE_VISIBILITY
624 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
625 _ValueType any_cast(any const & __v)
627 using _RawValueType = __remove_cvref_t<_ValueType>;
628 static_assert(is_constructible<_ValueType, _RawValueType const &>::value,
629 "ValueType is required to be a const lvalue reference "
630 "or a CopyConstructible type");
631 auto __tmp = _VSTD::any_cast<add_const_t<_RawValueType>>(&__v);
632 if (__tmp == nullptr)
633 __throw_bad_any_cast();
634 return static_cast<_ValueType>(*__tmp);
637 template <class _ValueType>
638 inline _LIBCPP_INLINE_VISIBILITY
639 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
640 _ValueType any_cast(any & __v)
642 using _RawValueType = __remove_cvref_t<_ValueType>;
643 static_assert(is_constructible<_ValueType, _RawValueType &>::value,
644 "ValueType is required to be an lvalue reference "
645 "or a CopyConstructible type");
646 auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
647 if (__tmp == nullptr)
648 __throw_bad_any_cast();
649 return static_cast<_ValueType>(*__tmp);
652 template <class _ValueType>
653 inline _LIBCPP_INLINE_VISIBILITY
654 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
655 _ValueType any_cast(any && __v)
657 using _RawValueType = __remove_cvref_t<_ValueType>;
658 static_assert(is_constructible<_ValueType, _RawValueType>::value,
659 "ValueType is required to be an rvalue reference "
660 "or a CopyConstructible type");
661 auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
662 if (__tmp == nullptr)
663 __throw_bad_any_cast();
664 return static_cast<_ValueType>(_VSTD::move(*__tmp));
667 template <class _ValueType>
668 inline _LIBCPP_INLINE_VISIBILITY
669 add_pointer_t<add_const_t<_ValueType>>
670 any_cast(any const * __any) _NOEXCEPT
672 static_assert(!is_reference<_ValueType>::value,
673 "_ValueType may not be a reference.");
674 return _VSTD::any_cast<_ValueType>(const_cast<any *>(__any));
677 template <class _RetType>
678 inline _LIBCPP_INLINE_VISIBILITY
679 _RetType __pointer_or_func_cast(void* __p, /*IsFunction*/false_type) noexcept {
680 return static_cast<_RetType>(__p);
683 template <class _RetType>
684 inline _LIBCPP_INLINE_VISIBILITY
685 _RetType __pointer_or_func_cast(void*, /*IsFunction*/true_type) noexcept {
689 template <class _ValueType>
690 _LIBCPP_HIDE_FROM_ABI
691 add_pointer_t<_ValueType>
692 any_cast(any * __any) _NOEXCEPT
694 using __any_imp::_Action;
695 static_assert(!is_reference<_ValueType>::value,
696 "_ValueType may not be a reference.");
697 typedef add_pointer_t<_ValueType> _ReturnType;
698 if (__any && __any->__h_) {
699 void *__p = __any->__call(_Action::_Get, nullptr,
700 #if !defined(_LIBCPP_HAS_NO_RTTI)
705 __any_imp::__get_fallback_typeid<_ValueType>());
706 return _VSTD::__pointer_or_func_cast<_ReturnType>(
707 __p, is_function<_ValueType>{});
712 #endif // _LIBCPP_STD_VER >= 17
714 _LIBCPP_END_NAMESPACE_STD
718 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
722 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
729 # include <stdexcept>
730 # include <type_traits>
734 #endif // _LIBCPP_ANY