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/conditional.h>
94 #include <__type_traits/decay.h>
95 #include <__type_traits/is_constructible.h>
96 #include <__type_traits/is_copy_constructible.h>
97 #include <__type_traits/is_function.h>
98 #include <__type_traits/is_nothrow_move_constructible.h>
99 #include <__type_traits/is_reference.h>
100 #include <__type_traits/is_same.h>
101 #include <__type_traits/remove_cv.h>
102 #include <__type_traits/remove_cvref.h>
103 #include <__type_traits/remove_reference.h>
104 #include <__utility/forward.h>
105 #include <__utility/in_place.h>
106 #include <__utility/move.h>
107 #include <__utility/unreachable.h>
108 #include <__verbose_abort>
109 #include <initializer_list>
113 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
114 # pragma GCC system_header
118 #include <__undef_macros>
121 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_AVAILABILITY_BAD_ANY_CAST bad_any_cast : public bad_cast
124 const char* what() const _NOEXCEPT override;
128 _LIBCPP_BEGIN_NAMESPACE_STD
130 #if _LIBCPP_STD_VER >= 17
132 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
133 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
134 void __throw_bad_any_cast()
136 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
137 throw bad_any_cast();
139 _LIBCPP_VERBOSE_ABORT("bad_any_cast was thrown in -fno-exceptions mode");
143 // Forward declarations
144 class _LIBCPP_TEMPLATE_VIS any;
146 template <class _ValueType>
147 _LIBCPP_INLINE_VISIBILITY
148 add_pointer_t<add_const_t<_ValueType>>
149 any_cast(any const *) _NOEXCEPT;
151 template <class _ValueType>
152 _LIBCPP_INLINE_VISIBILITY
153 add_pointer_t<_ValueType> any_cast(any *) _NOEXCEPT;
157 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
158 using _Buffer = aligned_storage_t<3*sizeof(void*), alignof(void*)>;
159 _LIBCPP_SUPPRESS_DEPRECATED_POP
162 using _IsSmallObject = integral_constant<bool
163 , sizeof(_Tp) <= sizeof(_Buffer)
164 && alignof(_Buffer) % alignof(_Tp) == 0
165 && is_nothrow_move_constructible<_Tp>::value
176 template <class _Tp> struct _SmallHandler;
177 template <class _Tp> struct _LargeHandler;
180 struct _LIBCPP_TEMPLATE_VIS __unique_typeinfo { static constexpr int __id = 0; };
181 template <class _Tp> constexpr int __unique_typeinfo<_Tp>::__id;
184 inline _LIBCPP_INLINE_VISIBILITY
185 constexpr const void* __get_fallback_typeid() {
186 return &__unique_typeinfo<remove_cv_t<remove_reference_t<_Tp>>>::__id;
190 inline _LIBCPP_INLINE_VISIBILITY
191 bool __compare_typeid(type_info const* __id, const void* __fallback_id)
193 #if !defined(_LIBCPP_HAS_NO_RTTI)
194 if (__id && *__id == typeid(_Tp))
197 if (!__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>())
203 using _Handler = conditional_t<
204 _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>;
206 } // namespace __any_imp
208 class _LIBCPP_TEMPLATE_VIS any
211 // construct/destruct
212 _LIBCPP_INLINE_VISIBILITY
213 constexpr any() _NOEXCEPT : __h_(nullptr) {}
215 _LIBCPP_INLINE_VISIBILITY
216 any(any const & __other) : __h_(nullptr)
218 if (__other.__h_) __other.__call(_Action::_Copy, this);
221 _LIBCPP_INLINE_VISIBILITY
222 any(any && __other) _NOEXCEPT : __h_(nullptr)
224 if (__other.__h_) __other.__call(_Action::_Move, this);
229 , class _Tp = decay_t<_ValueType>
230 , class = enable_if_t<
231 !is_same<_Tp, any>::value &&
232 !__is_inplace_type<_ValueType>::value &&
233 is_copy_constructible<_Tp>::value>
235 _LIBCPP_INLINE_VISIBILITY
236 any(_ValueType && __value);
238 template <class _ValueType, class ..._Args,
239 class _Tp = decay_t<_ValueType>,
241 is_constructible<_Tp, _Args...>::value &&
242 is_copy_constructible<_Tp>::value
245 _LIBCPP_INLINE_VISIBILITY
246 explicit any(in_place_type_t<_ValueType>, _Args&&... __args);
248 template <class _ValueType, class _Up, class ..._Args,
249 class _Tp = decay_t<_ValueType>,
251 is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
252 is_copy_constructible<_Tp>::value>
254 _LIBCPP_INLINE_VISIBILITY
255 explicit any(in_place_type_t<_ValueType>, initializer_list<_Up>, _Args&&... __args);
257 _LIBCPP_INLINE_VISIBILITY
258 ~any() { this->reset(); }
261 _LIBCPP_INLINE_VISIBILITY
262 any & operator=(any const & __rhs) {
263 any(__rhs).swap(*this);
267 _LIBCPP_INLINE_VISIBILITY
268 any & operator=(any && __rhs) _NOEXCEPT {
269 any(_VSTD::move(__rhs)).swap(*this);
275 , class _Tp = decay_t<_ValueType>
276 , class = enable_if_t<
277 !is_same<_Tp, any>::value
278 && is_copy_constructible<_Tp>::value>
280 _LIBCPP_INLINE_VISIBILITY
281 any & operator=(_ValueType && __rhs);
283 template <class _ValueType, class ..._Args,
284 class _Tp = decay_t<_ValueType>,
286 is_constructible<_Tp, _Args...>::value &&
287 is_copy_constructible<_Tp>::value>
289 _LIBCPP_INLINE_VISIBILITY
290 _Tp& emplace(_Args&&...);
292 template <class _ValueType, class _Up, class ..._Args,
293 class _Tp = decay_t<_ValueType>,
295 is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
296 is_copy_constructible<_Tp>::value>
298 _LIBCPP_INLINE_VISIBILITY
299 _Tp& emplace(initializer_list<_Up>, _Args&&...);
301 // 6.3.3 any modifiers
302 _LIBCPP_INLINE_VISIBILITY
303 void reset() _NOEXCEPT { if (__h_) this->__call(_Action::_Destroy); }
305 _LIBCPP_INLINE_VISIBILITY
306 void swap(any & __rhs) _NOEXCEPT;
308 // 6.3.4 any observers
309 _LIBCPP_INLINE_VISIBILITY
310 bool has_value() const _NOEXCEPT { return __h_ != nullptr; }
312 #if !defined(_LIBCPP_HAS_NO_RTTI)
313 _LIBCPP_INLINE_VISIBILITY
314 const type_info & type() const _NOEXCEPT {
316 return *static_cast<type_info const *>(this->__call(_Action::_TypeInfo));
324 typedef __any_imp::_Action _Action;
325 using _HandleFuncPtr = void* (*)(_Action, any const *, any *, const type_info *,
326 const void* __fallback_info);
329 _LIBCPP_HIDE_FROM_ABI constexpr _Storage() : __ptr(nullptr) {}
331 __any_imp::_Buffer __buf;
334 _LIBCPP_INLINE_VISIBILITY
335 void * __call(_Action __a, any * __other = nullptr,
336 type_info const * __info = nullptr,
337 const void* __fallback_info = nullptr) const
339 return __h_(__a, this, __other, __info, __fallback_info);
342 _LIBCPP_INLINE_VISIBILITY
343 void * __call(_Action __a, any * __other = nullptr,
344 type_info const * __info = nullptr,
345 const void* __fallback_info = nullptr)
347 return __h_(__a, this, __other, __info, __fallback_info);
351 friend struct __any_imp::_SmallHandler;
353 friend struct __any_imp::_LargeHandler;
355 template <class _ValueType>
356 friend add_pointer_t<add_const_t<_ValueType>>
357 any_cast(any const *) _NOEXCEPT;
359 template <class _ValueType>
360 friend add_pointer_t<_ValueType>
361 any_cast(any *) _NOEXCEPT;
363 _HandleFuncPtr __h_ = nullptr;
370 struct _LIBCPP_TEMPLATE_VIS _SmallHandler
372 _LIBCPP_INLINE_VISIBILITY
373 static void* __handle(_Action __act, any const * __this, any * __other,
374 type_info const * __info, const void* __fallback_info)
378 case _Action::_Destroy:
379 __destroy(const_cast<any &>(*__this));
382 __copy(*__this, *__other);
385 __move(const_cast<any &>(*__this), *__other);
388 return __get(const_cast<any &>(*__this), __info, __fallback_info);
389 case _Action::_TypeInfo:
390 return __type_info();
392 __libcpp_unreachable();
395 template <class ..._Args>
396 _LIBCPP_INLINE_VISIBILITY
397 static _Tp& __create(any & __dest, _Args&&... __args) {
398 typedef allocator<_Tp> _Alloc;
399 typedef allocator_traits<_Alloc> _ATraits;
401 _Tp * __ret = static_cast<_Tp*>(static_cast<void*>(&__dest.__s_.__buf));
402 _ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...);
403 __dest.__h_ = &_SmallHandler::__handle;
408 _LIBCPP_INLINE_VISIBILITY
409 static void __destroy(any & __this) {
410 typedef allocator<_Tp> _Alloc;
411 typedef allocator_traits<_Alloc> _ATraits;
413 _Tp * __p = static_cast<_Tp *>(static_cast<void*>(&__this.__s_.__buf));
414 _ATraits::destroy(__a, __p);
415 __this.__h_ = nullptr;
418 _LIBCPP_INLINE_VISIBILITY
419 static void __copy(any const & __this, any & __dest) {
420 _SmallHandler::__create(__dest, *static_cast<_Tp const *>(
421 static_cast<void const *>(&__this.__s_.__buf)));
424 _LIBCPP_INLINE_VISIBILITY
425 static void __move(any & __this, any & __dest) {
426 _SmallHandler::__create(__dest, _VSTD::move(
427 *static_cast<_Tp*>(static_cast<void*>(&__this.__s_.__buf))));
431 _LIBCPP_INLINE_VISIBILITY
432 static void* __get(any & __this,
433 type_info const * __info,
434 const void* __fallback_id)
436 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id))
437 return static_cast<void*>(&__this.__s_.__buf);
441 _LIBCPP_INLINE_VISIBILITY
442 static void* __type_info()
444 #if !defined(_LIBCPP_HAS_NO_RTTI)
445 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
453 struct _LIBCPP_TEMPLATE_VIS _LargeHandler
455 _LIBCPP_INLINE_VISIBILITY
456 static void* __handle(_Action __act, any const * __this,
457 any * __other, type_info const * __info,
458 void const* __fallback_info)
462 case _Action::_Destroy:
463 __destroy(const_cast<any &>(*__this));
466 __copy(*__this, *__other);
469 __move(const_cast<any &>(*__this), *__other);
472 return __get(const_cast<any &>(*__this), __info, __fallback_info);
473 case _Action::_TypeInfo:
474 return __type_info();
476 __libcpp_unreachable();
479 template <class ..._Args>
480 _LIBCPP_INLINE_VISIBILITY
481 static _Tp& __create(any & __dest, _Args&&... __args) {
482 typedef allocator<_Tp> _Alloc;
483 typedef allocator_traits<_Alloc> _ATraits;
484 typedef __allocator_destructor<_Alloc> _Dp;
486 unique_ptr<_Tp, _Dp> __hold(_ATraits::allocate(__a, 1), _Dp(__a, 1));
487 _Tp * __ret = __hold.get();
488 _ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...);
489 __dest.__s_.__ptr = __hold.release();
490 __dest.__h_ = &_LargeHandler::__handle;
496 _LIBCPP_INLINE_VISIBILITY
497 static void __destroy(any & __this){
498 typedef allocator<_Tp> _Alloc;
499 typedef allocator_traits<_Alloc> _ATraits;
501 _Tp * __p = static_cast<_Tp *>(__this.__s_.__ptr);
502 _ATraits::destroy(__a, __p);
503 _ATraits::deallocate(__a, __p, 1);
504 __this.__h_ = nullptr;
507 _LIBCPP_INLINE_VISIBILITY
508 static void __copy(any const & __this, any & __dest) {
509 _LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s_.__ptr));
512 _LIBCPP_INLINE_VISIBILITY
513 static void __move(any & __this, any & __dest) {
514 __dest.__s_.__ptr = __this.__s_.__ptr;
515 __dest.__h_ = &_LargeHandler::__handle;
516 __this.__h_ = nullptr;
519 _LIBCPP_INLINE_VISIBILITY
520 static void* __get(any & __this, type_info const * __info,
521 void const* __fallback_info)
523 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info))
524 return static_cast<void*>(__this.__s_.__ptr);
529 _LIBCPP_INLINE_VISIBILITY
530 static void* __type_info()
532 #if !defined(_LIBCPP_HAS_NO_RTTI)
533 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
540 } // namespace __any_imp
543 template <class _ValueType, class _Tp, class>
544 any::any(_ValueType && __v) : __h_(nullptr)
546 __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_ValueType>(__v));
549 template <class _ValueType, class ..._Args, class _Tp, class>
550 any::any(in_place_type_t<_ValueType>, _Args&&... __args) {
551 __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
554 template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
555 any::any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) {
556 __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
559 template <class _ValueType, class, class>
560 inline _LIBCPP_INLINE_VISIBILITY
561 any & any::operator=(_ValueType && __v)
563 any(_VSTD::forward<_ValueType>(__v)).swap(*this);
567 template <class _ValueType, class ..._Args, class _Tp, class>
568 inline _LIBCPP_INLINE_VISIBILITY
569 _Tp& any::emplace(_Args&&... __args) {
571 return __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
574 template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
575 inline _LIBCPP_INLINE_VISIBILITY
576 _Tp& any::emplace(initializer_list<_Up> __il, _Args&&... __args) {
578 return __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
581 inline _LIBCPP_INLINE_VISIBILITY
582 void any::swap(any & __rhs) _NOEXCEPT
586 if (__h_ && __rhs.__h_) {
588 __rhs.__call(_Action::_Move, &__tmp);
589 this->__call(_Action::_Move, &__rhs);
590 __tmp.__call(_Action::_Move, this);
593 this->__call(_Action::_Move, &__rhs);
595 else if (__rhs.__h_) {
596 __rhs.__call(_Action::_Move, this);
600 // 6.4 Non-member functions
602 inline _LIBCPP_INLINE_VISIBILITY
603 void swap(any & __lhs, any & __rhs) _NOEXCEPT
608 template <class _Tp, class ..._Args>
609 inline _LIBCPP_INLINE_VISIBILITY
610 any make_any(_Args&&... __args) {
611 return any(in_place_type<_Tp>, _VSTD::forward<_Args>(__args)...);
614 template <class _Tp, class _Up, class ..._Args>
615 inline _LIBCPP_INLINE_VISIBILITY
616 any make_any(initializer_list<_Up> __il, _Args&&... __args) {
617 return any(in_place_type<_Tp>, __il, _VSTD::forward<_Args>(__args)...);
620 template <class _ValueType>
621 inline _LIBCPP_INLINE_VISIBILITY
622 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
623 _ValueType any_cast(any const & __v)
625 using _RawValueType = __remove_cvref_t<_ValueType>;
626 static_assert(is_constructible<_ValueType, _RawValueType const &>::value,
627 "ValueType is required to be a const lvalue reference "
628 "or a CopyConstructible type");
629 auto __tmp = _VSTD::any_cast<add_const_t<_RawValueType>>(&__v);
630 if (__tmp == nullptr)
631 __throw_bad_any_cast();
632 return static_cast<_ValueType>(*__tmp);
635 template <class _ValueType>
636 inline _LIBCPP_INLINE_VISIBILITY
637 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
638 _ValueType any_cast(any & __v)
640 using _RawValueType = __remove_cvref_t<_ValueType>;
641 static_assert(is_constructible<_ValueType, _RawValueType &>::value,
642 "ValueType is required to be an lvalue reference "
643 "or a CopyConstructible type");
644 auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
645 if (__tmp == nullptr)
646 __throw_bad_any_cast();
647 return static_cast<_ValueType>(*__tmp);
650 template <class _ValueType>
651 inline _LIBCPP_INLINE_VISIBILITY
652 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
653 _ValueType any_cast(any && __v)
655 using _RawValueType = __remove_cvref_t<_ValueType>;
656 static_assert(is_constructible<_ValueType, _RawValueType>::value,
657 "ValueType is required to be an rvalue reference "
658 "or a CopyConstructible type");
659 auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
660 if (__tmp == nullptr)
661 __throw_bad_any_cast();
662 return static_cast<_ValueType>(_VSTD::move(*__tmp));
665 template <class _ValueType>
666 inline _LIBCPP_INLINE_VISIBILITY
667 add_pointer_t<add_const_t<_ValueType>>
668 any_cast(any const * __any) _NOEXCEPT
670 static_assert(!is_reference<_ValueType>::value,
671 "_ValueType may not be a reference.");
672 return _VSTD::any_cast<_ValueType>(const_cast<any *>(__any));
675 template <class _RetType>
676 inline _LIBCPP_INLINE_VISIBILITY
677 _RetType __pointer_or_func_cast(void* __p, /*IsFunction*/false_type) noexcept {
678 return static_cast<_RetType>(__p);
681 template <class _RetType>
682 inline _LIBCPP_INLINE_VISIBILITY
683 _RetType __pointer_or_func_cast(void*, /*IsFunction*/true_type) noexcept {
687 template <class _ValueType>
688 _LIBCPP_HIDE_FROM_ABI
689 add_pointer_t<_ValueType>
690 any_cast(any * __any) _NOEXCEPT
692 using __any_imp::_Action;
693 static_assert(!is_reference<_ValueType>::value,
694 "_ValueType may not be a reference.");
695 typedef add_pointer_t<_ValueType> _ReturnType;
696 if (__any && __any->__h_) {
697 void *__p = __any->__call(_Action::_Get, nullptr,
698 #if !defined(_LIBCPP_HAS_NO_RTTI)
703 __any_imp::__get_fallback_typeid<_ValueType>());
704 return _VSTD::__pointer_or_func_cast<_ReturnType>(
705 __p, is_function<_ValueType>{});
710 #endif // _LIBCPP_STD_VER >= 17
712 _LIBCPP_END_NAMESPACE_STD
716 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
720 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
727 # include <stdexcept>
728 # include <type_traits>
732 #endif // _LIBCPP_ANY