2 //===------------------------------ any -----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_EXPERIMENTAL_ANY
12 #define _LIBCPP_EXPERIMENTAL_ANY
15 experimental/any synopsis
18 namespace experimental {
19 inline namespace fundamentals_v1 {
21 class bad_any_cast : public bad_cast
24 virtual const char* what() const noexcept;
31 // 6.3.1 any construct/destruct
34 any(const any& other);
35 any(any&& other) noexcept;
37 template <class ValueType>
38 any(ValueType&& value);
42 // 6.3.2 any assignments
43 any& operator=(const any& rhs);
44 any& operator=(any&& rhs) noexcept;
46 template <class ValueType>
47 any& operator=(ValueType&& rhs);
49 // 6.3.3 any modifiers
50 void clear() noexcept;
51 void swap(any& rhs) noexcept;
53 // 6.3.4 any observers
54 bool empty() const noexcept;
55 const type_info& type() const noexcept;
58 // 6.4 Non-member functions
59 void swap(any& x, any& y) noexcept;
61 template<class ValueType>
62 ValueType any_cast(const any& operand);
63 template<class ValueType>
64 ValueType any_cast(any& operand);
65 template<class ValueType>
66 ValueType any_cast(any&& operand);
68 template<class ValueType>
69 const ValueType* any_cast(const any* operand) noexcept;
70 template<class ValueType>
71 ValueType* any_cast(any* operand) noexcept;
73 } // namespace fundamentals_v1
74 } // namespace experimental
79 #include <experimental/__config>
83 #include <type_traits>
87 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
88 #pragma GCC system_header
91 _LIBCPP_BEGIN_NAMESPACE_LFTS
93 class _LIBCPP_EXCEPTION_ABI bad_any_cast : public bad_cast
96 virtual const char* what() const _NOEXCEPT;
99 #if _LIBCPP_STD_VER > 11 // C++ > 11
101 _LIBCPP_NORETURN _LIBCPP_INLINE_VISIBILITY
102 inline void __throw_bad_any_cast()
104 #ifndef _LIBCPP_NO_EXCEPTIONS
105 throw bad_any_cast();
107 assert(!"bad_any_cast");
111 // Forward declarations
114 template <class _ValueType>
115 typename add_pointer<typename add_const<_ValueType>::type>::type
116 any_cast(any const *) _NOEXCEPT;
118 template <class _ValueType>
119 typename add_pointer<_ValueType>::type
120 any_cast(any *) _NOEXCEPT;
124 typedef typename aligned_storage<3*sizeof(void*), alignment_of<void*>::value>::type
128 struct _IsSmallObject
129 : public integral_constant<bool
130 , sizeof(_Tp) <= sizeof(_Buffer)
131 && alignment_of<_Buffer>::value
132 % alignment_of<_Tp>::value == 0
133 && is_nothrow_move_constructible<_Tp>::value
147 struct _SmallHandler;
150 struct _LargeHandler;
153 using _Handler = typename conditional<_IsSmallObject<_Tp>::value
157 template <class _ValueType>
158 using _EnableIfNotAny = typename
160 !is_same<typename decay<_ValueType>::type, any>::value
163 } // namespace __any_imp
168 // 6.3.1 any construct/destruct
169 _LIBCPP_INLINE_VISIBILITY
170 any() _NOEXCEPT : __h(nullptr) {}
172 _LIBCPP_INLINE_VISIBILITY
173 any(any const & __other) : __h(nullptr)
175 if (__other.__h) __other.__call(_Action::_Copy, this);
178 _LIBCPP_INLINE_VISIBILITY
179 any(any && __other) _NOEXCEPT : __h(nullptr)
181 if (__other.__h) __other.__call(_Action::_Move, this);
186 , class = __any_imp::_EnableIfNotAny<_ValueType>
188 any(_ValueType && __value);
190 _LIBCPP_INLINE_VISIBILITY
196 // 6.3.2 any assignments
197 _LIBCPP_INLINE_VISIBILITY
198 any & operator=(any const & __rhs)
200 any(__rhs).swap(*this);
204 _LIBCPP_INLINE_VISIBILITY
205 any & operator=(any && __rhs) _NOEXCEPT
207 any(_VSTD::move(__rhs)).swap(*this);
213 , class = __any_imp::_EnableIfNotAny<_ValueType>
215 any & operator=(_ValueType && __rhs);
217 // 6.3.3 any modifiers
218 _LIBCPP_INLINE_VISIBILITY
219 void clear() _NOEXCEPT
221 if (__h) this->__call(_Action::_Destroy);
224 void swap(any & __rhs) _NOEXCEPT;
226 // 6.3.4 any observers
227 _LIBCPP_INLINE_VISIBILITY
228 bool empty() const _NOEXCEPT
230 return __h == nullptr;
233 #if !defined(_LIBCPP_NO_RTTI)
234 _LIBCPP_INLINE_VISIBILITY
235 const type_info & type() const _NOEXCEPT
238 return *static_cast<type_info const *>(this->__call(_Action::_TypeInfo));
246 typedef __any_imp::_Action _Action;
248 typedef void* (*_HandleFuncPtr)(_Action, any const *, any *, const type_info *);
253 __any_imp::_Buffer __buf;
256 _LIBCPP_ALWAYS_INLINE
257 void * __call(_Action __a, any * __other = nullptr,
258 type_info const * __info = nullptr) const
260 return __h(__a, this, __other, __info);
263 _LIBCPP_ALWAYS_INLINE
264 void * __call(_Action __a, any * __other = nullptr,
265 type_info const * __info = nullptr)
267 return __h(__a, this, __other, __info);
271 friend struct __any_imp::_SmallHandler;
273 friend struct __any_imp::_LargeHandler;
275 template <class _ValueType>
276 friend typename add_pointer<typename add_const<_ValueType>::type>::type
277 any_cast(any const *) _NOEXCEPT;
279 template <class _ValueType>
280 friend typename add_pointer<_ValueType>::type
281 any_cast(any *) _NOEXCEPT;
291 struct _LIBCPP_TYPE_VIS_ONLY _SmallHandler
293 _LIBCPP_INLINE_VISIBILITY
294 static void* __handle(_Action __act, any const * __this, any * __other,
295 type_info const * __info)
299 case _Action::_Destroy:
300 __destroy(const_cast<any &>(*__this));
303 __copy(*__this, *__other);
306 __move(const_cast<any &>(*__this), *__other);
309 return __get(const_cast<any &>(*__this), __info);
310 case _Action::_TypeInfo:
311 return __type_info();
316 _LIBCPP_INLINE_VISIBILITY
317 static void __create(any & __dest, _Up && __v)
319 ::new (static_cast<void*>(&__dest.__s.__buf)) _Tp(_VSTD::forward<_Up>(__v));
320 __dest.__h = &_SmallHandler::__handle;
324 _LIBCPP_ALWAYS_INLINE _LIBCPP_INLINE_VISIBILITY
325 static void __destroy(any & __this)
327 _Tp & __value = *static_cast<_Tp *>(static_cast<void*>(&__this.__s.__buf));
329 __this.__h = nullptr;
332 _LIBCPP_ALWAYS_INLINE _LIBCPP_INLINE_VISIBILITY
333 static void __copy(any const & __this, any & __dest)
335 _SmallHandler::__create(__dest, *static_cast<_Tp const *>(
336 static_cast<void const *>(&__this.__s.__buf)));
339 _LIBCPP_ALWAYS_INLINE _LIBCPP_INLINE_VISIBILITY
340 static void __move(any & __this, any & __dest)
342 _SmallHandler::__create(__dest, _VSTD::move(
343 *static_cast<_Tp*>(static_cast<void*>(&__this.__s.__buf))));
347 _LIBCPP_ALWAYS_INLINE _LIBCPP_INLINE_VISIBILITY
348 static void* __get(any & __this, type_info const * __info)
350 #if !defined(_LIBCPP_NO_RTTI)
351 if (typeid(_Tp) == *__info) {
352 return static_cast<void*>(&__this.__s.__buf);
356 return static_cast<void*>(&__this.__s.__buf);
360 _LIBCPP_ALWAYS_INLINE _LIBCPP_INLINE_VISIBILITY
361 static void* __type_info()
363 #if !defined(_LIBCPP_NO_RTTI)
364 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
372 struct _LIBCPP_TYPE_VIS_ONLY _LargeHandler
374 _LIBCPP_INLINE_VISIBILITY
375 static void* __handle(_Action __act, any const * __this, any * __other,
376 type_info const * __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);
391 case _Action::_TypeInfo:
392 return __type_info();
397 _LIBCPP_INLINE_VISIBILITY
398 static void __create(any & __dest, _Up && __v)
400 typedef allocator<_Tp> _Alloc;
401 typedef __allocator_destructor<_Alloc> _Dp;
403 unique_ptr<_Tp, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
404 ::new ((void*)__hold.get()) _Tp(_VSTD::forward<_Up>(__v));
405 __dest.__s.__ptr = __hold.release();
406 __dest.__h = &_LargeHandler::__handle;
411 _LIBCPP_ALWAYS_INLINE _LIBCPP_INLINE_VISIBILITY
412 static void __destroy(any & __this)
414 delete static_cast<_Tp*>(__this.__s.__ptr);
415 __this.__h = nullptr;
418 _LIBCPP_ALWAYS_INLINE _LIBCPP_INLINE_VISIBILITY
419 static void __copy(any const & __this, any & __dest)
421 _LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s.__ptr));
424 _LIBCPP_ALWAYS_INLINE _LIBCPP_INLINE_VISIBILITY
425 static void __move(any & __this, any & __dest)
427 __dest.__s.__ptr = __this.__s.__ptr;
428 __dest.__h = &_LargeHandler::__handle;
429 __this.__h = nullptr;
432 _LIBCPP_ALWAYS_INLINE _LIBCPP_INLINE_VISIBILITY
433 static void* __get(any & __this, type_info const * __info)
435 #if !defined(_LIBCPP_NO_RTTI)
436 if (typeid(_Tp) == *__info) {
437 return static_cast<void*>(__this.__s.__ptr);
441 return static_cast<void*>(__this.__s.__ptr);
445 _LIBCPP_ALWAYS_INLINE _LIBCPP_INLINE_VISIBILITY
446 static void* __type_info()
448 #if !defined(_LIBCPP_NO_RTTI)
449 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
456 } // namespace __any_imp
459 template <class _ValueType, class>
460 _LIBCPP_INLINE_VISIBILITY
461 any::any(_ValueType && __v) : __h(nullptr)
463 typedef typename decay<_ValueType>::type _Tp;
464 static_assert(is_copy_constructible<_Tp>::value,
465 "_ValueType must be CopyConstructible.");
466 typedef __any_imp::_Handler<_Tp> _HandlerType;
467 _HandlerType::__create(*this, _VSTD::forward<_ValueType>(__v));
470 template <class _ValueType, class>
471 _LIBCPP_INLINE_VISIBILITY
472 any & any::operator=(_ValueType && __v)
474 typedef typename decay<_ValueType>::type _Tp;
475 static_assert(is_copy_constructible<_Tp>::value,
476 "_ValueType must be CopyConstructible.");
477 any(_VSTD::forward<_ValueType>(__v)).swap(*this);
481 inline _LIBCPP_INLINE_VISIBILITY
482 void any::swap(any & __rhs) _NOEXCEPT
484 if (__h && __rhs.__h) {
486 __rhs.__call(_Action::_Move, &__tmp);
487 this->__call(_Action::_Move, &__rhs);
488 __tmp.__call(_Action::_Move, this);
491 this->__call(_Action::_Move, &__rhs);
493 else if (__rhs.__h) {
494 __rhs.__call(_Action::_Move, this);
498 // 6.4 Non-member functions
500 inline _LIBCPP_INLINE_VISIBILITY
501 void swap(any & __lhs, any & __rhs) _NOEXCEPT
506 template <class _ValueType>
507 _LIBCPP_INLINE_VISIBILITY
508 _ValueType any_cast(any const & __v)
511 is_reference<_ValueType>::value
512 || is_copy_constructible<_ValueType>::value,
513 "_ValueType is required to be a reference or a CopyConstructible type.");
514 typedef typename add_const<typename remove_reference<_ValueType>::type>::type
516 _Tp * __tmp = any_cast<_Tp>(&__v);
517 if (__tmp == nullptr)
518 __throw_bad_any_cast();
522 template <class _ValueType>
523 _LIBCPP_INLINE_VISIBILITY
524 _ValueType any_cast(any & __v)
527 is_reference<_ValueType>::value
528 || is_copy_constructible<_ValueType>::value,
529 "_ValueType is required to be a reference or a CopyConstructible type.");
530 typedef typename remove_reference<_ValueType>::type _Tp;
531 _Tp * __tmp = any_cast<_Tp>(&__v);
532 if (__tmp == nullptr)
533 __throw_bad_any_cast();
537 template <class _ValueType>
538 _LIBCPP_INLINE_VISIBILITY
539 _ValueType any_cast(any && __v)
542 is_reference<_ValueType>::value
543 || is_copy_constructible<_ValueType>::value,
544 "_ValueType is required to be a reference or a CopyConstructible type.");
545 typedef typename remove_reference<_ValueType>::type _Tp;
546 _Tp * __tmp = any_cast<_Tp>(&__v);
547 if (__tmp == nullptr)
548 __throw_bad_any_cast();
552 template <class _ValueType>
553 inline _LIBCPP_INLINE_VISIBILITY
554 typename add_pointer<typename add_const<_ValueType>::type>::type
555 any_cast(any const * __any) _NOEXCEPT
557 static_assert(!is_reference<_ValueType>::value,
558 "_ValueType may not be a reference.");
559 return any_cast<_ValueType>(const_cast<any *>(__any));
562 template <class _ValueType>
563 _LIBCPP_INLINE_VISIBILITY
564 typename add_pointer<_ValueType>::type
565 any_cast(any * __any) _NOEXCEPT
567 using __any_imp::_Action;
568 static_assert(!is_reference<_ValueType>::value,
569 "_ValueType may not be a reference.");
570 typedef typename add_pointer<_ValueType>::type _ReturnType;
571 if (__any && __any->__h) {
573 return static_cast<_ReturnType>(
574 __any->__call(_Action::_Get, nullptr,
575 #if !defined(_LIBCPP_NO_RTTI)
586 #endif // _LIBCPP_STD_VER > 11
588 _LIBCPP_END_NAMESPACE_LFTS
590 #endif // _LIBCPP_EXPERIMENTAL_ANY