29 #ifndef _GLIBCXX_EXPERIMENTAL_ANY
30 #define _GLIBCXX_EXPERIMENTAL_ANY 1
32 #pragma GCC system_header
34 #if __cplusplus <= 201103L
43 namespace std _GLIBCXX_VISIBILITY(default)
45 namespace experimental
47 inline namespace fundamentals_v1
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
61 #define __cpp_lib_experimental_any 201411
70 virtual const char*
what() const noexcept {
return "bad any_cast"; }
73 [[gnu::noreturn]]
inline void __throw_bad_any_cast()
97 _Storage(
const _Storage&) =
delete;
98 _Storage&
operator=(
const _Storage&) =
delete;
101 aligned_storage<sizeof(_M_ptr), alignof(void*)>::type _M_buffer;
104 template<
typename _Tp,
typename _Safe = is_nothrow_move_constructible<_Tp>,
105 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))
106 && (alignof(_Tp) <= alignof(_Storage))>
107 using _Internal = std::
integral_constant<
bool, _Safe::value && _Fits>;
109 template<
typename _Tp>
110 struct _Manager_internal;
112 template<
typename _Tp>
113 struct _Manager_external;
115 template<
typename _Tp>
116 using _Manager = conditional_t<_Internal<_Tp>::value,
117 _Manager_internal<_Tp>,
118 _Manager_external<_Tp>>;
120 template<
typename _Tp,
typename _Decayed = decay_t<_Tp>>
121 using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>;
127 any() noexcept : _M_manager(
nullptr) { }
133 _M_manager =
nullptr;
138 __other._M_manager(_Op_clone, &__other, &__arg);
150 _M_manager =
nullptr;
155 __other._M_manager(_Op_xfer, &__other, &__arg);
160 template <
typename _ValueType,
typename _Tp = _Decay<_ValueType>,
161 typename _Mgr = _Manager<_Tp>,
162 typename enable_if<is_constructible<_Tp, _ValueType&&>::value,
165 : _M_manager(&_Mgr::_S_manage)
167 _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value));
168 static_assert(is_copy_constructible<_Tp>::value,
169 "The contained object must be CopyConstructible");
173 template <
typename _ValueType,
typename _Tp = _Decay<_ValueType>,
174 typename _Mgr = _Manager<_Tp>,
175 typename enable_if<!is_constructible<_Tp, _ValueType&&>::value,
178 : _M_manager(&_Mgr::_S_manage)
180 _Mgr::_S_create(_M_storage, __value);
181 static_assert(is_copy_constructible<_Tp>::value,
182 "The contained object must be CopyConstructible");
195 else if (
this != &__rhs)
198 _M_manager(_Op_destroy,
this,
nullptr);
201 __rhs._M_manager(_Op_clone, &__rhs, &__arg);
215 else if (
this != &__rhs)
218 _M_manager(_Op_destroy,
this,
nullptr);
221 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
227 template<
typename _ValueType>
228 enable_if_t<!is_same<any, decay_t<_ValueType>>::value,
any&>
231 *
this =
any(std::forward<_ValueType>(__rhs));
242 _M_manager(_Op_destroy,
this,
nullptr);
243 _M_manager =
nullptr;
250 if (
empty() && __rhs.empty())
253 if (!
empty() && !__rhs.empty())
260 __arg._M_any = &__tmp;
261 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
262 __arg._M_any = &__rhs;
263 _M_manager(_Op_xfer,
this, &__arg);
265 __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
269 any* __empty =
empty() ?
this : &__rhs;
270 any* __full =
empty() ? &__rhs :
this;
272 __arg._M_any = __empty;
273 __full->_M_manager(_Op_xfer, __full, &__arg);
280 bool empty() const noexcept {
return _M_manager ==
nullptr; }
289 _M_manager(_Op_get_type_info,
this, &__arg);
290 return *__arg._M_typeinfo;
294 template<
typename _Tp>
295 static constexpr
bool __is_valid_cast()
296 {
return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
300 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
310 void (*_M_manager)(_Op,
const any*, _Arg*);
313 template<
typename _Tp>
314 friend void* __any_caster(
const any* __any);
317 template<
typename _Tp>
318 struct _Manager_internal
321 _S_manage(_Op __which,
const any* __anyp, _Arg* __arg);
323 template<
typename _Up>
325 _S_create(_Storage& __storage, _Up&& __value)
327 void* __addr = &__storage._M_buffer;
328 ::new (__addr) _Tp(std::
forward<_Up>(__value));
333 template<typename _Tp>
334 struct _Manager_external
337 _S_manage(_Op __which,
const any* __anyp, _Arg* __arg);
339 template<
typename _Up>
341 _S_create(_Storage& __storage, _Up&& __value)
343 __storage._M_ptr =
new _Tp(std::forward<_Up>(__value));
349 inline void swap(
any& __x,
any& __y) noexcept { __x.swap(__y); }
361 template<
typename _ValueType>
364 static_assert(any::__is_valid_cast<_ValueType>(),
365 "Template argument must be a reference or CopyConstructible type");
366 auto __p =
any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
369 __throw_bad_any_cast();
384 template<
typename _ValueType>
387 static_assert(any::__is_valid_cast<_ValueType>(),
388 "Template argument must be a reference or CopyConstructible type");
389 auto __p =
any_cast<remove_reference_t<_ValueType>>(&__any);
392 __throw_bad_any_cast();
395 template<
typename _ValueType,
396 typename enable_if<!is_move_constructible<_ValueType>::value
401 static_assert(any::__is_valid_cast<_ValueType>(),
402 "Template argument must be a reference or CopyConstructible type");
403 auto __p =
any_cast<remove_reference_t<_ValueType>>(&__any);
406 __throw_bad_any_cast();
409 template<
typename _ValueType,
410 typename enable_if<is_move_constructible<_ValueType>::value
415 static_assert(any::__is_valid_cast<_ValueType>(),
416 "Template argument must be a reference or CopyConstructible type");
417 auto __p =
any_cast<remove_reference_t<_ValueType>>(&__any);
419 return std::move(*__p);
420 __throw_bad_any_cast();
424 template<
typename _Tp>
425 void* __any_caster(
const any* __any)
427 if (__any->_M_manager != &any::_Manager<decay_t<_Tp>>::_S_manage)
430 __any->_M_manager(any::_Op_access, __any, &__arg);
445 template<
typename _ValueType>
449 return static_cast<_ValueType*
>(__any_caster<_ValueType>(__any));
453 template<
typename _ValueType>
457 return static_cast<_ValueType*
>(__any_caster<_ValueType>(__any));
462 template<
typename _Tp>
464 any::_Manager_internal<_Tp>::
465 _S_manage(_Op __which,
const any* __any, _Arg* __arg)
468 auto __ptr =
reinterpret_cast<const _Tp*
>(&__any->_M_storage._M_buffer);
472 __arg->_M_obj =
const_cast<_Tp*
>(__ptr);
474 case _Op_get_type_info:
476 __arg->_M_typeinfo = &
typeid(_Tp);
480 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
481 __arg->_M_any->_M_manager = __any->_M_manager;
487 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
489 __arg->_M_any->_M_manager = __any->_M_manager;
490 const_cast<
any*>(__any)->_M_manager =
nullptr;
495 template<typename _Tp>
497 any::_Manager_external<_Tp>::
498 _S_manage(_Op __which, const
any* __any, _Arg* __arg)
501 auto __ptr =
static_cast<const _Tp*
>(__any->_M_storage._M_ptr);
505 __arg->_M_obj =
const_cast<_Tp*
>(__ptr);
507 case _Op_get_type_info:
509 __arg->_M_typeinfo = &
typeid(_Tp);
513 __arg->_M_any->_M_storage._M_ptr =
new _Tp(*__ptr);
514 __arg->_M_any->_M_manager = __any->_M_manager;
520 __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
521 __arg->_M_any->_M_manager = __any->_M_manager;
522 const_cast<any*
>(__any)->_M_manager =
nullptr;
528 _GLIBCXX_END_NAMESPACE_VERSION
535 #endif // _GLIBCXX_EXPERIMENTAL_ANY
~any()
Destructor, calls clear()
Exception class thrown by a failed any_cast.
any & operator=(any &&__rhs) noexcept
Move assignment operator.
const type_info & type() const noexcept
The typeid of the contained object, or typeid(void) if empty.
bool empty() const noexcept
Reports whether there is a contained object or not.
A type-safe container of any type.
any(const any &__other)
Copy constructor, copies the state of __other.
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.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
_ValueType any_cast(const any &__any)
Access the contained object.
any & operator=(const any &__rhs)
Copy the state of another object.
virtual const char * what() const noexcept
void clear() noexcept
If not empty, destroy the contained object.
any(_ValueType &&__value)
Construct with a copy of __value as the contained object.
any(any &&__other) noexcept
Move constructor, transfer the state from __other.
enable_if_t<!is_same< any, decay_t< _ValueType > >::value, any & > operator=(_ValueType &&__rhs)
Store a copy of __rhs as the contained object.
any() noexcept
Default constructor, creates an empty object.
void swap(any &__rhs) noexcept
Exchange state with another object.