29 #ifndef _GLIBCXX_EXPERIMENTAL_ANY
30 #define _GLIBCXX_EXPERIMENTAL_ANY 1
32 #pragma GCC system_header
34 #if __cplusplus >= 201402L
42 namespace std _GLIBCXX_VISIBILITY(default)
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 namespace experimental
48 inline namespace fundamentals_v1
60 #define __cpp_lib_experimental_any 201411
69 virtual const char*
what() const
noexcept {
return "bad any_cast"; }
72 [[gnu::noreturn]]
inline void __throw_bad_any_cast()
96 _Storage(
const _Storage&) =
delete;
97 _Storage&
operator=(
const _Storage&) =
delete;
103 template<
typename _Tp,
typename _Safe = is_nothrow_move_constructible<_Tp>,
104 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))
105 && (alignof(_Tp) <= alignof(_Storage))>
106 using _Internal = std::
integral_constant<
bool, _Safe::value && _Fits>;
108 template<
typename _Tp>
109 struct _Manager_internal;
111 template<
typename _Tp>
112 struct _Manager_external;
114 template<
typename _Tp>
116 _Manager_internal<_Tp>,
117 _Manager_external<_Tp>>;
119 template<
typename _Tp,
typename _Decayed = decay_t<_Tp>>
132 _M_manager =
nullptr;
137 __other._M_manager(_Op_clone, &__other, &__arg);
149 _M_manager =
nullptr;
154 __other._M_manager(_Op_xfer, &__other, &__arg);
159 template <
typename _ValueType,
typename _Tp = _Decay<_ValueType>,
160 typename _Mgr = _Manager<_Tp>,
161 typename enable_if<is_constructible<_Tp, _ValueType&&>::value,
164 : _M_manager(&_Mgr::_S_manage)
166 _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value));
168 "The contained object must be CopyConstructible");
172 template <
typename _ValueType,
typename _Tp = _Decay<_ValueType>,
173 typename _Mgr = _Manager<_Tp>,
174 typename enable_if<!is_constructible<_Tp, _ValueType&&>::value,
177 : _M_manager(&_Mgr::_S_manage)
179 _Mgr::_S_create(_M_storage, __value);
181 "The contained object must be CopyConstructible");
205 else if (
this != &__rhs)
210 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
216 template<
typename _ValueType>
220 *
this =
any(std::forward<_ValueType>(__rhs));
231 _M_manager(_Op_destroy,
this,
nullptr);
232 _M_manager =
nullptr;
239 if (
empty() && __rhs.empty())
242 if (!
empty() && !__rhs.empty())
249 __arg._M_any = &__tmp;
250 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
251 __arg._M_any = &__rhs;
252 _M_manager(_Op_xfer,
this, &__arg);
254 __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
258 any* __empty =
empty() ?
this : &__rhs;
259 any* __full =
empty() ? &__rhs :
this;
261 __arg._M_any = __empty;
262 __full->_M_manager(_Op_xfer, __full, &__arg);
269 _GLIBCXX_NODISCARD
bool empty() const
noexcept {
return _M_manager ==
nullptr; }
278 _M_manager(_Op_get_type_info,
this, &__arg);
279 return *__arg._M_typeinfo;
283 template<
typename _Tp>
284 static constexpr
bool __is_valid_cast()
289 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
299 void (*_M_manager)(_Op,
const any*, _Arg*);
302 template<
typename _Tp>
303 friend enable_if_t<is_object<_Tp>::value,
void*>
304 __any_caster(
const any* __any);
307 template<
typename _Tp>
308 struct _Manager_internal
311 _S_manage(_Op __which,
const any* __anyp, _Arg* __arg);
313 template<
typename _Up>
315 _S_create(_Storage& __storage, _Up&& __value)
317 void* __addr = &__storage._M_buffer;
318 ::new (__addr) _Tp(std::
forward<_Up>(__value));
323 template<typename _Tp>
324 struct _Manager_external
327 _S_manage(_Op __which,
const any* __anyp, _Arg* __arg);
329 template<
typename _Up>
331 _S_create(_Storage& __storage, _Up&& __value)
333 __storage._M_ptr =
new _Tp(std::forward<_Up>(__value));
351 template<
typename _ValueType>
354 static_assert(any::__is_valid_cast<_ValueType>(),
355 "Template argument must be a reference or CopyConstructible type");
359 __throw_bad_any_cast();
374 template<
typename _ValueType>
377 static_assert(any::__is_valid_cast<_ValueType>(),
378 "Template argument must be a reference or CopyConstructible type");
382 __throw_bad_any_cast();
385 template<
typename _ValueType,
391 static_assert(any::__is_valid_cast<_ValueType>(),
392 "Template argument must be a reference or CopyConstructible type");
396 __throw_bad_any_cast();
399 template<
typename _ValueType,
405 static_assert(any::__is_valid_cast<_ValueType>(),
406 "Template argument must be a reference or CopyConstructible type");
409 return std::move(*__p);
410 __throw_bad_any_cast();
415 template<
typename _Tp>
417 __any_caster(
const any* __any)
433 if (__any->_M_manager == &any::_Manager<_Vp>::_S_manage
435 || __any->type() ==
typeid(_Tp)
440 __any->_M_manager(any::_Op_access, __any, &__arg);
447 template<
typename _Tp>
448 enable_if_t<!is_object<_Tp>::value, _Tp*>
464 template<
typename _ValueType>
468 return static_cast<_ValueType*
>(__any_caster<_ValueType>(__any));
472 template<
typename _ValueType>
476 return static_cast<_ValueType*
>(__any_caster<_ValueType>(__any));
481 template<
typename _Tp>
483 any::_Manager_internal<_Tp>::
484 _S_manage(_Op __which,
const any* __any, _Arg* __arg)
487 auto __ptr =
reinterpret_cast<const _Tp*
>(&__any->_M_storage._M_buffer);
491 __arg->_M_obj =
const_cast<_Tp*
>(__ptr);
493 case _Op_get_type_info:
495 __arg->_M_typeinfo = &
typeid(_Tp);
499 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
500 __arg->_M_any->_M_manager = __any->_M_manager;
506 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp
507 (std::move(*const_cast<_Tp*>(__ptr)));
509 __arg->_M_any->_M_manager = __any->_M_manager;
510 const_cast<
any*>(__any)->_M_manager =
nullptr;
515 template<typename _Tp>
517 any::_Manager_external<_Tp>::
518 _S_manage(_Op __which, const
any* __any, _Arg* __arg)
521 auto __ptr =
static_cast<const _Tp*
>(__any->_M_storage._M_ptr);
525 __arg->_M_obj =
const_cast<_Tp*
>(__ptr);
527 case _Op_get_type_info:
529 __arg->_M_typeinfo = &
typeid(_Tp);
533 __arg->_M_any->_M_storage._M_ptr =
new _Tp(*__ptr);
534 __arg->_M_any->_M_manager = __any->_M_manager;
540 __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
541 __arg->_M_any->_M_manager = __any->_M_manager;
542 const_cast<any*
>(__any)->_M_manager =
nullptr;
549 struct any::_Manager_internal<
any::_Op>
552 _S_manage(_Op,
const any*, _Arg*) { }
559 _GLIBCXX_END_NAMESPACE_VERSION
564 #endif // _GLIBCXX_EXPERIMENTAL_ANY
typename conditional< _Cond, _Iftrue, _Iffalse >::type conditional_t
Alias template for conditional.
~any()
Destructor, calls clear()
any(const any &__other)
Copy constructor, copies the state of __other.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
any & operator=(any &&__rhs) noexcept
Move assignment operator.
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.
const type_info & type() const noexcept
The typeid of the contained object, or typeid(void) if empty.
any & operator=(const any &__rhs)
Copy the state of another object.
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
any(any &&__other) noexcept
Move constructor, transfer the state from __other.
Exception class thrown by a failed any_cast.
enable_if< ::__array_traits< _Tp, _Nm >::_Is_swappable::value >::type noexcept(noexcept(__one.swap(__two)))
swap
Define a member typedef type only if a boolean constant is true.
virtual const char * what() const noexcept
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
A type-safe container of any type.
_GLIBCXX_NODISCARD bool empty() const noexcept
Reports whether there is a contained object or not.
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.
typename add_const< _Tp >::type add_const_t
Alias template for add_const.
any(_ValueType &&__value)
Construct with a copy of __value as the contained object.
void clear() noexcept
If not empty, destroy the contained object.
_ValueType any_cast(const any &__any)
Access the contained object.
typename remove_cv< _Tp >::type remove_cv_t
Alias template for remove_cv.