29 #ifndef _GLIBCXX_EXPERIMENTAL_ANY
30 #define _GLIBCXX_EXPERIMENTAL_ANY 1
32 #pragma GCC system_header
34 #if __cplusplus <= 201103L
44 namespace std _GLIBCXX_VISIBILITY(default)
46 namespace experimental
48 inline namespace fundamentals_v1
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
62 #define __cpp_lib_experimental_any 201411
71 virtual const char*
what() const noexcept {
return "bad any_cast"; }
74 [[gnu::noreturn]]
inline void __throw_bad_any_cast()
98 _Storage(
const _Storage&) =
delete;
99 _Storage&
operator=(
const _Storage&) =
delete;
102 aligned_storage<sizeof(_M_ptr), alignof(void*)>::type _M_buffer;
105 template<
typename _Tp,
typename _Safe = is_nothrow_move_constructible<_Tp>,
106 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))
107 && (alignof(_Tp) <= alignof(_Storage))>
108 using _Internal = std::
integral_constant<
bool, _Safe::value && _Fits>;
110 template<
typename _Tp>
111 struct _Manager_internal;
113 template<
typename _Tp>
114 struct _Manager_external;
116 template<
typename _Tp>
117 using _Manager = conditional_t<_Internal<_Tp>::value,
118 _Manager_internal<_Tp>,
119 _Manager_external<_Tp>>;
121 template<
typename _Tp,
typename _Decayed = decay_t<_Tp>>
122 using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>;
128 any() noexcept : _M_manager(
nullptr) { }
134 _M_manager =
nullptr;
139 __other._M_manager(_Op_clone, &__other, &__arg);
151 _M_manager =
nullptr;
156 __other._M_manager(_Op_xfer, &__other, &__arg);
161 template <
typename _ValueType,
typename _Tp = _Decay<_ValueType>,
162 typename _Mgr = _Manager<_Tp>,
163 typename enable_if<is_constructible<_Tp, _ValueType&&>::value,
166 : _M_manager(&_Mgr::_S_manage)
168 _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value));
169 static_assert(is_copy_constructible<_Tp>::value,
170 "The contained object must be CopyConstructible");
174 template <
typename _ValueType,
typename _Tp = _Decay<_ValueType>,
175 typename _Mgr = _Manager<_Tp>,
176 typename enable_if<!is_constructible<_Tp, _ValueType&&>::value,
179 : _M_manager(&_Mgr::_S_manage)
181 _Mgr::_S_create(_M_storage, __value);
182 static_assert(is_copy_constructible<_Tp>::value,
183 "The contained object must be CopyConstructible");
196 else if (
this != &__rhs)
199 _M_manager(_Op_destroy,
this,
nullptr);
202 __rhs._M_manager(_Op_clone, &__rhs, &__arg);
216 else if (
this != &__rhs)
219 _M_manager(_Op_destroy,
this,
nullptr);
222 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
228 template<
typename _ValueType>
229 enable_if_t<!is_same<any, decay_t<_ValueType>>::value,
any&>
232 *
this =
any(std::forward<_ValueType>(__rhs));
243 _M_manager(_Op_destroy,
this,
nullptr);
244 _M_manager =
nullptr;
251 if (
empty() && __rhs.empty())
254 if (!
empty() && !__rhs.empty())
261 __arg._M_any = &__tmp;
262 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
263 __arg._M_any = &__rhs;
264 _M_manager(_Op_xfer,
this, &__arg);
266 __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
270 any* __empty =
empty() ?
this : &__rhs;
271 any* __full =
empty() ? &__rhs :
this;
273 __arg._M_any = __empty;
274 __full->_M_manager(_Op_xfer, __full, &__arg);
281 bool empty() const noexcept {
return _M_manager ==
nullptr; }
290 _M_manager(_Op_get_type_info,
this, &__arg);
291 return *__arg._M_typeinfo;
295 template<
typename _Tp>
296 static constexpr
bool __is_valid_cast()
297 {
return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
301 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
311 void (*_M_manager)(_Op,
const any*, _Arg*);
314 template<
typename _Tp>
315 friend void* __any_caster(
const any* __any);
318 template<
typename _Tp>
319 struct _Manager_internal
322 _S_manage(_Op __which,
const any* __anyp, _Arg* __arg);
324 template<
typename _Up>
326 _S_create(_Storage& __storage, _Up&& __value)
328 void* __addr = &__storage._M_buffer;
329 ::new (__addr) _Tp(std::
forward<_Up>(__value));
334 template<typename _Tp>
335 struct _Manager_external
338 _S_manage(_Op __which,
const any* __anyp, _Arg* __arg);
340 template<
typename _Up>
342 _S_create(_Storage& __storage, _Up&& __value)
344 __storage._M_ptr =
new _Tp(std::forward<_Up>(__value));
350 inline void swap(
any& __x,
any& __y) noexcept { __x.swap(__y); }
362 template<
typename _ValueType>
365 static_assert(any::__is_valid_cast<_ValueType>(),
366 "Template argument must be a reference or CopyConstructible type");
367 auto __p =
any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
370 __throw_bad_any_cast();
385 template<
typename _ValueType>
388 static_assert(any::__is_valid_cast<_ValueType>(),
389 "Template argument must be a reference or CopyConstructible type");
390 auto __p =
any_cast<remove_reference_t<_ValueType>>(&__any);
393 __throw_bad_any_cast();
396 template<
typename _ValueType,
397 typename enable_if<!is_move_constructible<_ValueType>::value
402 static_assert(any::__is_valid_cast<_ValueType>(),
403 "Template argument must be a reference or CopyConstructible type");
404 auto __p =
any_cast<remove_reference_t<_ValueType>>(&__any);
407 __throw_bad_any_cast();
410 template<
typename _ValueType,
411 typename enable_if<is_move_constructible<_ValueType>::value
416 static_assert(any::__is_valid_cast<_ValueType>(),
417 "Template argument must be a reference or CopyConstructible type");
418 auto __p =
any_cast<remove_reference_t<_ValueType>>(&__any);
420 return std::move(*__p);
421 __throw_bad_any_cast();
425 template<
typename _Tp>
426 void* __any_caster(
const any* __any)
429 using _Up = decay_t<_Tp>;
430 using _Vp = conditional_t<is_copy_constructible<_Up>::value, _Up, _None>;
431 if (__any->_M_manager != &any::_Manager<_Vp>::_S_manage)
434 __any->_M_manager(any::_Op_access, __any, &__arg);
449 template<
typename _ValueType>
453 return static_cast<_ValueType*
>(__any_caster<_ValueType>(__any));
457 template<
typename _ValueType>
461 return static_cast<_ValueType*
>(__any_caster<_ValueType>(__any));
466 template<
typename _Tp>
468 any::_Manager_internal<_Tp>::
469 _S_manage(_Op __which,
const any* __any, _Arg* __arg)
472 auto __ptr =
reinterpret_cast<const _Tp*
>(&__any->_M_storage._M_buffer);
476 __arg->_M_obj =
const_cast<_Tp*
>(__ptr);
478 case _Op_get_type_info:
480 __arg->_M_typeinfo = &
typeid(_Tp);
484 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
485 __arg->_M_any->_M_manager = __any->_M_manager;
491 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
493 __arg->_M_any->_M_manager = __any->_M_manager;
494 const_cast<
any*>(__any)->_M_manager =
nullptr;
499 template<typename _Tp>
501 any::_Manager_external<_Tp>::
502 _S_manage(_Op __which, const
any* __any, _Arg* __arg)
505 auto __ptr =
static_cast<const _Tp*
>(__any->_M_storage._M_ptr);
509 __arg->_M_obj =
const_cast<_Tp*
>(__ptr);
511 case _Op_get_type_info:
513 __arg->_M_typeinfo = &
typeid(_Tp);
517 __arg->_M_any->_M_storage._M_ptr =
new _Tp(*__ptr);
518 __arg->_M_any->_M_manager = __any->_M_manager;
524 __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
525 __arg->_M_any->_M_manager = __any->_M_manager;
526 const_cast<any*
>(__any)->_M_manager =
nullptr;
532 _GLIBCXX_END_NAMESPACE_VERSION
539 #endif // _GLIBCXX_EXPERIMENTAL_ANY
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
any(any &&__other) noexcept
Move constructor, transfer the state from __other.
any & operator=(const any &__rhs)
Copy the state of another object.
any & operator=(any &&__rhs) noexcept
Move assignment operator.
bool empty() const noexcept
Reports whether there is a contained object or not.
_ValueType * any_cast(any *__any) noexcept
Access the contained object.
const type_info & type() const noexcept
The typeid of the contained object, or typeid(void) if empty.
Exception class thrown by a failed any_cast.
enable_if_t<!is_same< any, decay_t< _ValueType > >::value, any & > operator=(_ValueType &&__rhs)
Store a copy of __rhs as the contained object.
A type-safe container of any type.
void clear() noexcept
If not empty, destroy the contained object.
any() noexcept
Default constructor, creates an empty object.
~any()
Destructor, calls clear()
any(const any &__other)
Copy constructor, copies the state of __other.
void swap(any &__rhs) noexcept
Exchange state with another object.
Thrown during incorrect typecasting.If you attempt an invalid dynamic_cast expression, an instance of this class (or something derived from this class) is thrown.
any(_ValueType &&__value)
Construct with a copy of __value as the contained object.
virtual const char * what() const noexcept