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 <__utility/forward.h>
87 #include <__utility/in_place.h>
88 #include <__utility/move.h>
89 #include <__utility/unreachable.h>
91 #include <initializer_list>
93 #include <type_traits>
97 #ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
101 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
102 # pragma GCC system_header
106 class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_ANY_CAST bad_any_cast : public bad_cast
109 virtual const char* what() const _NOEXCEPT;
113 _LIBCPP_BEGIN_NAMESPACE_STD
115 #if _LIBCPP_STD_VER > 14
117 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
118 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
119 void __throw_bad_any_cast()
121 #ifndef _LIBCPP_NO_EXCEPTIONS
122 throw bad_any_cast();
128 // Forward declarations
129 class _LIBCPP_TEMPLATE_VIS any;
131 template <class _ValueType>
132 _LIBCPP_INLINE_VISIBILITY
133 add_pointer_t<add_const_t<_ValueType>>
134 any_cast(any const *) _NOEXCEPT;
136 template <class _ValueType>
137 _LIBCPP_INLINE_VISIBILITY
138 add_pointer_t<_ValueType> any_cast(any *) _NOEXCEPT;
142 using _Buffer = aligned_storage_t<3*sizeof(void*), alignment_of<void*>::value>;
145 using _IsSmallObject = integral_constant<bool
146 , sizeof(_Tp) <= sizeof(_Buffer)
147 && alignment_of<_Buffer>::value
148 % alignment_of<_Tp>::value == 0
149 && is_nothrow_move_constructible<_Tp>::value
160 template <class _Tp> struct _SmallHandler;
161 template <class _Tp> struct _LargeHandler;
164 struct _LIBCPP_TEMPLATE_VIS __unique_typeinfo { static constexpr int __id = 0; };
165 template <class _Tp> constexpr int __unique_typeinfo<_Tp>::__id;
168 inline _LIBCPP_INLINE_VISIBILITY
169 constexpr const void* __get_fallback_typeid() {
170 return &__unique_typeinfo<remove_cv_t<remove_reference_t<_Tp>>>::__id;
174 inline _LIBCPP_INLINE_VISIBILITY
175 bool __compare_typeid(type_info const* __id, const void* __fallback_id)
177 #if !defined(_LIBCPP_NO_RTTI)
178 if (__id && *__id == typeid(_Tp))
181 if (!__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>())
187 using _Handler = conditional_t<
188 _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>;
190 } // namespace __any_imp
192 class _LIBCPP_TEMPLATE_VIS any
195 // construct/destruct
196 _LIBCPP_INLINE_VISIBILITY
197 constexpr any() _NOEXCEPT : __h(nullptr) {}
199 _LIBCPP_INLINE_VISIBILITY
200 any(any const & __other) : __h(nullptr)
202 if (__other.__h) __other.__call(_Action::_Copy, this);
205 _LIBCPP_INLINE_VISIBILITY
206 any(any && __other) _NOEXCEPT : __h(nullptr)
208 if (__other.__h) __other.__call(_Action::_Move, this);
213 , class _Tp = decay_t<_ValueType>
214 , class = enable_if_t<
215 !is_same<_Tp, any>::value &&
216 !__is_inplace_type<_ValueType>::value &&
217 is_copy_constructible<_Tp>::value>
219 _LIBCPP_INLINE_VISIBILITY
220 any(_ValueType && __value);
222 template <class _ValueType, class ..._Args,
223 class _Tp = decay_t<_ValueType>,
225 is_constructible<_Tp, _Args...>::value &&
226 is_copy_constructible<_Tp>::value
229 _LIBCPP_INLINE_VISIBILITY
230 explicit any(in_place_type_t<_ValueType>, _Args&&... __args);
232 template <class _ValueType, class _Up, class ..._Args,
233 class _Tp = decay_t<_ValueType>,
235 is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
236 is_copy_constructible<_Tp>::value>
238 _LIBCPP_INLINE_VISIBILITY
239 explicit any(in_place_type_t<_ValueType>, initializer_list<_Up>, _Args&&... __args);
241 _LIBCPP_INLINE_VISIBILITY
242 ~any() { this->reset(); }
245 _LIBCPP_INLINE_VISIBILITY
246 any & operator=(any const & __rhs) {
247 any(__rhs).swap(*this);
251 _LIBCPP_INLINE_VISIBILITY
252 any & operator=(any && __rhs) _NOEXCEPT {
253 any(_VSTD::move(__rhs)).swap(*this);
259 , class _Tp = decay_t<_ValueType>
260 , class = enable_if_t<
261 !is_same<_Tp, any>::value
262 && is_copy_constructible<_Tp>::value>
264 _LIBCPP_INLINE_VISIBILITY
265 any & operator=(_ValueType && __rhs);
267 template <class _ValueType, class ..._Args,
268 class _Tp = decay_t<_ValueType>,
270 is_constructible<_Tp, _Args...>::value &&
271 is_copy_constructible<_Tp>::value>
273 _LIBCPP_INLINE_VISIBILITY
274 _Tp& emplace(_Args&&...);
276 template <class _ValueType, class _Up, class ..._Args,
277 class _Tp = decay_t<_ValueType>,
279 is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
280 is_copy_constructible<_Tp>::value>
282 _LIBCPP_INLINE_VISIBILITY
283 _Tp& emplace(initializer_list<_Up>, _Args&&...);
285 // 6.3.3 any modifiers
286 _LIBCPP_INLINE_VISIBILITY
287 void reset() _NOEXCEPT { if (__h) this->__call(_Action::_Destroy); }
289 _LIBCPP_INLINE_VISIBILITY
290 void swap(any & __rhs) _NOEXCEPT;
292 // 6.3.4 any observers
293 _LIBCPP_INLINE_VISIBILITY
294 bool has_value() const _NOEXCEPT { return __h != nullptr; }
296 #if !defined(_LIBCPP_NO_RTTI)
297 _LIBCPP_INLINE_VISIBILITY
298 const type_info & type() const _NOEXCEPT {
300 return *static_cast<type_info const *>(this->__call(_Action::_TypeInfo));
308 typedef __any_imp::_Action _Action;
309 using _HandleFuncPtr = void* (*)(_Action, any const *, any *, const type_info *,
310 const void* __fallback_info);
313 constexpr _Storage() : __ptr(nullptr) {}
315 __any_imp::_Buffer __buf;
318 _LIBCPP_INLINE_VISIBILITY
319 void * __call(_Action __a, any * __other = nullptr,
320 type_info const * __info = nullptr,
321 const void* __fallback_info = nullptr) const
323 return __h(__a, this, __other, __info, __fallback_info);
326 _LIBCPP_INLINE_VISIBILITY
327 void * __call(_Action __a, any * __other = nullptr,
328 type_info const * __info = nullptr,
329 const void* __fallback_info = nullptr)
331 return __h(__a, this, __other, __info, __fallback_info);
335 friend struct __any_imp::_SmallHandler;
337 friend struct __any_imp::_LargeHandler;
339 template <class _ValueType>
340 friend add_pointer_t<add_const_t<_ValueType>>
341 any_cast(any const *) _NOEXCEPT;
343 template <class _ValueType>
344 friend add_pointer_t<_ValueType>
345 any_cast(any *) _NOEXCEPT;
347 _HandleFuncPtr __h = nullptr;
354 struct _LIBCPP_TEMPLATE_VIS _SmallHandler
356 _LIBCPP_INLINE_VISIBILITY
357 static void* __handle(_Action __act, any const * __this, any * __other,
358 type_info const * __info, const void* __fallback_info)
362 case _Action::_Destroy:
363 __destroy(const_cast<any &>(*__this));
366 __copy(*__this, *__other);
369 __move(const_cast<any &>(*__this), *__other);
372 return __get(const_cast<any &>(*__this), __info, __fallback_info);
373 case _Action::_TypeInfo:
374 return __type_info();
376 __libcpp_unreachable();
379 template <class ..._Args>
380 _LIBCPP_INLINE_VISIBILITY
381 static _Tp& __create(any & __dest, _Args&&... __args) {
382 typedef allocator<_Tp> _Alloc;
383 typedef allocator_traits<_Alloc> _ATraits;
385 _Tp * __ret = static_cast<_Tp*>(static_cast<void*>(&__dest.__s.__buf));
386 _ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...);
387 __dest.__h = &_SmallHandler::__handle;
392 _LIBCPP_INLINE_VISIBILITY
393 static void __destroy(any & __this) {
394 typedef allocator<_Tp> _Alloc;
395 typedef allocator_traits<_Alloc> _ATraits;
397 _Tp * __p = static_cast<_Tp *>(static_cast<void*>(&__this.__s.__buf));
398 _ATraits::destroy(__a, __p);
399 __this.__h = nullptr;
402 _LIBCPP_INLINE_VISIBILITY
403 static void __copy(any const & __this, any & __dest) {
404 _SmallHandler::__create(__dest, *static_cast<_Tp const *>(
405 static_cast<void const *>(&__this.__s.__buf)));
408 _LIBCPP_INLINE_VISIBILITY
409 static void __move(any & __this, any & __dest) {
410 _SmallHandler::__create(__dest, _VSTD::move(
411 *static_cast<_Tp*>(static_cast<void*>(&__this.__s.__buf))));
415 _LIBCPP_INLINE_VISIBILITY
416 static void* __get(any & __this,
417 type_info const * __info,
418 const void* __fallback_id)
420 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id))
421 return static_cast<void*>(&__this.__s.__buf);
425 _LIBCPP_INLINE_VISIBILITY
426 static void* __type_info()
428 #if !defined(_LIBCPP_NO_RTTI)
429 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
437 struct _LIBCPP_TEMPLATE_VIS _LargeHandler
439 _LIBCPP_INLINE_VISIBILITY
440 static void* __handle(_Action __act, any const * __this,
441 any * __other, type_info const * __info,
442 void const* __fallback_info)
446 case _Action::_Destroy:
447 __destroy(const_cast<any &>(*__this));
450 __copy(*__this, *__other);
453 __move(const_cast<any &>(*__this), *__other);
456 return __get(const_cast<any &>(*__this), __info, __fallback_info);
457 case _Action::_TypeInfo:
458 return __type_info();
460 __libcpp_unreachable();
463 template <class ..._Args>
464 _LIBCPP_INLINE_VISIBILITY
465 static _Tp& __create(any & __dest, _Args&&... __args) {
466 typedef allocator<_Tp> _Alloc;
467 typedef allocator_traits<_Alloc> _ATraits;
468 typedef __allocator_destructor<_Alloc> _Dp;
470 unique_ptr<_Tp, _Dp> __hold(_ATraits::allocate(__a, 1), _Dp(__a, 1));
471 _Tp * __ret = __hold.get();
472 _ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...);
473 __dest.__s.__ptr = __hold.release();
474 __dest.__h = &_LargeHandler::__handle;
480 _LIBCPP_INLINE_VISIBILITY
481 static void __destroy(any & __this){
482 typedef allocator<_Tp> _Alloc;
483 typedef allocator_traits<_Alloc> _ATraits;
485 _Tp * __p = static_cast<_Tp *>(__this.__s.__ptr);
486 _ATraits::destroy(__a, __p);
487 _ATraits::deallocate(__a, __p, 1);
488 __this.__h = nullptr;
491 _LIBCPP_INLINE_VISIBILITY
492 static void __copy(any const & __this, any & __dest) {
493 _LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s.__ptr));
496 _LIBCPP_INLINE_VISIBILITY
497 static void __move(any & __this, any & __dest) {
498 __dest.__s.__ptr = __this.__s.__ptr;
499 __dest.__h = &_LargeHandler::__handle;
500 __this.__h = nullptr;
503 _LIBCPP_INLINE_VISIBILITY
504 static void* __get(any & __this, type_info const * __info,
505 void const* __fallback_info)
507 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info))
508 return static_cast<void*>(__this.__s.__ptr);
513 _LIBCPP_INLINE_VISIBILITY
514 static void* __type_info()
516 #if !defined(_LIBCPP_NO_RTTI)
517 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
524 } // namespace __any_imp
527 template <class _ValueType, class _Tp, class>
528 any::any(_ValueType && __v) : __h(nullptr)
530 __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_ValueType>(__v));
533 template <class _ValueType, class ..._Args, class _Tp, class>
534 any::any(in_place_type_t<_ValueType>, _Args&&... __args) {
535 __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
538 template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
539 any::any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) {
540 __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
543 template <class _ValueType, class, class>
544 inline _LIBCPP_INLINE_VISIBILITY
545 any & any::operator=(_ValueType && __v)
547 any(_VSTD::forward<_ValueType>(__v)).swap(*this);
551 template <class _ValueType, class ..._Args, class _Tp, class>
552 inline _LIBCPP_INLINE_VISIBILITY
553 _Tp& any::emplace(_Args&&... __args) {
555 return __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
558 template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
559 inline _LIBCPP_INLINE_VISIBILITY
560 _Tp& any::emplace(initializer_list<_Up> __il, _Args&&... __args) {
562 return __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
565 inline _LIBCPP_INLINE_VISIBILITY
566 void any::swap(any & __rhs) _NOEXCEPT
570 if (__h && __rhs.__h) {
572 __rhs.__call(_Action::_Move, &__tmp);
573 this->__call(_Action::_Move, &__rhs);
574 __tmp.__call(_Action::_Move, this);
577 this->__call(_Action::_Move, &__rhs);
579 else if (__rhs.__h) {
580 __rhs.__call(_Action::_Move, this);
584 // 6.4 Non-member functions
586 inline _LIBCPP_INLINE_VISIBILITY
587 void swap(any & __lhs, any & __rhs) _NOEXCEPT
592 template <class _Tp, class ..._Args>
593 inline _LIBCPP_INLINE_VISIBILITY
594 any make_any(_Args&&... __args) {
595 return any(in_place_type<_Tp>, _VSTD::forward<_Args>(__args)...);
598 template <class _Tp, class _Up, class ..._Args>
599 inline _LIBCPP_INLINE_VISIBILITY
600 any make_any(initializer_list<_Up> __il, _Args&&... __args) {
601 return any(in_place_type<_Tp>, __il, _VSTD::forward<_Args>(__args)...);
604 template <class _ValueType>
605 inline _LIBCPP_INLINE_VISIBILITY
606 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
607 _ValueType any_cast(any const & __v)
609 using _RawValueType = __uncvref_t<_ValueType>;
610 static_assert(is_constructible<_ValueType, _RawValueType const &>::value,
611 "ValueType is required to be a const lvalue reference "
612 "or a CopyConstructible type");
613 auto __tmp = _VSTD::any_cast<add_const_t<_RawValueType>>(&__v);
614 if (__tmp == nullptr)
615 __throw_bad_any_cast();
616 return static_cast<_ValueType>(*__tmp);
619 template <class _ValueType>
620 inline _LIBCPP_INLINE_VISIBILITY
621 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
622 _ValueType any_cast(any & __v)
624 using _RawValueType = __uncvref_t<_ValueType>;
625 static_assert(is_constructible<_ValueType, _RawValueType &>::value,
626 "ValueType is required to be an lvalue reference "
627 "or a CopyConstructible type");
628 auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
629 if (__tmp == nullptr)
630 __throw_bad_any_cast();
631 return static_cast<_ValueType>(*__tmp);
634 template <class _ValueType>
635 inline _LIBCPP_INLINE_VISIBILITY
636 _LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
637 _ValueType any_cast(any && __v)
639 using _RawValueType = __uncvref_t<_ValueType>;
640 static_assert(is_constructible<_ValueType, _RawValueType>::value,
641 "ValueType is required to be an rvalue reference "
642 "or a CopyConstructible type");
643 auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
644 if (__tmp == nullptr)
645 __throw_bad_any_cast();
646 return static_cast<_ValueType>(_VSTD::move(*__tmp));
649 template <class _ValueType>
650 inline _LIBCPP_INLINE_VISIBILITY
651 add_pointer_t<add_const_t<_ValueType>>
652 any_cast(any const * __any) _NOEXCEPT
654 static_assert(!is_reference<_ValueType>::value,
655 "_ValueType may not be a reference.");
656 return _VSTD::any_cast<_ValueType>(const_cast<any *>(__any));
659 template <class _RetType>
660 inline _LIBCPP_INLINE_VISIBILITY
661 _RetType __pointer_or_func_cast(void* __p, /*IsFunction*/false_type) noexcept {
662 return static_cast<_RetType>(__p);
665 template <class _RetType>
666 inline _LIBCPP_INLINE_VISIBILITY
667 _RetType __pointer_or_func_cast(void*, /*IsFunction*/true_type) noexcept {
671 template <class _ValueType>
672 _LIBCPP_HIDE_FROM_ABI
673 add_pointer_t<_ValueType>
674 any_cast(any * __any) _NOEXCEPT
676 using __any_imp::_Action;
677 static_assert(!is_reference<_ValueType>::value,
678 "_ValueType may not be a reference.");
679 typedef typename add_pointer<_ValueType>::type _ReturnType;
680 if (__any && __any->__h) {
681 void *__p = __any->__call(_Action::_Get, nullptr,
682 #if !defined(_LIBCPP_NO_RTTI)
687 __any_imp::__get_fallback_typeid<_ValueType>());
688 return _VSTD::__pointer_or_func_cast<_ReturnType>(
689 __p, is_function<_ValueType>{});
694 #endif // _LIBCPP_STD_VER > 14
696 _LIBCPP_END_NAMESPACE_STD
698 #endif // _LIBCPP_ANY