30#ifndef _GLIBCXX_EXPERIMENTAL_ANY
31#define _GLIBCXX_EXPERIMENTAL_ANY 1
33#pragma GCC system_header
37#if __cplusplus >= 201402L
45namespace std _GLIBCXX_VISIBILITY(default)
47_GLIBCXX_BEGIN_NAMESPACE_VERSION
51inline namespace fundamentals_v1
63#define __cpp_lib_experimental_any 201411
72 virtual const char*
what() const noexcept {
return "bad any_cast"; }
76 [[gnu::noreturn]]
inline void __throw_bad_any_cast()
101 _Storage(
const _Storage&) =
delete;
102 _Storage& operator=(
const _Storage&) =
delete;
108 template<
typename _Tp,
typename _Safe = is_nothrow_move_constructible<_Tp>,
109 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))
110 && (alignof(_Tp) <= alignof(_Storage))>
111 using _Internal = std::
integral_constant<
bool, _Safe::value && _Fits>;
113 template<
typename _Tp>
114 struct _Manager_internal;
116 template<
typename _Tp>
117 struct _Manager_external;
119 template<
typename _Tp>
120 using _Manager = __conditional_t<_Internal<_Tp>::value,
121 _Manager_internal<_Tp>,
122 _Manager_external<_Tp>>;
124 template<
typename _Tp,
typename _Decayed = decay_t<_Tp>>
131 any() noexcept : _M_manager(
nullptr) { }
137 _M_manager =
nullptr;
142 __other._M_manager(_Op_clone, &__other, &__arg);
154 _M_manager =
nullptr;
159 __other._M_manager(_Op_xfer, &__other, &__arg);
164 template <
typename _ValueType,
typename _Tp = _Decay<_ValueType>,
165 typename _Mgr = _Manager<_Tp>,
166 typename enable_if<is_constructible<_Tp, _ValueType&&>::value,
169 : _M_manager(&_Mgr::_S_manage)
171 _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value));
173 "The contained object must be CopyConstructible");
177 template <
typename _ValueType,
typename _Tp = _Decay<_ValueType>,
178 typename _Mgr = _Manager<_Tp>,
179 typename enable_if<!is_constructible<_Tp, _ValueType&&>::value,
182 : _M_manager(&_Mgr::_S_manage)
184 _Mgr::_S_create(_M_storage, __value);
186 "The contained object must be CopyConstructible");
210 else if (
this != &__rhs)
215 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
221 template<
typename _ValueType>
225 *
this =
any(std::forward<_ValueType>(__rhs));
236 _M_manager(_Op_destroy,
this,
nullptr);
237 _M_manager =
nullptr;
244 if (
empty() && __rhs.empty())
247 if (!
empty() && !__rhs.empty())
254 __arg._M_any = &__tmp;
255 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
256 __arg._M_any = &__rhs;
257 _M_manager(_Op_xfer,
this, &__arg);
259 __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
263 any* __empty =
empty() ? this : &__rhs;
264 any* __full =
empty() ? &__rhs :
this;
266 __arg._M_any = __empty;
267 __full->_M_manager(_Op_xfer, __full, &__arg);
274 _GLIBCXX_NODISCARD
bool empty() const noexcept {
return _M_manager ==
nullptr; }
283 _M_manager(_Op_get_type_info,
this, &__arg);
284 return *__arg._M_typeinfo;
288 template<
typename _Tp>
289 static constexpr bool __is_valid_cast()
294 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
304 void (*_M_manager)(_Op,
const any*, _Arg*);
307 template<
typename _Tp>
308 friend enable_if_t<is_object<_Tp>::value,
void*>
309 __any_caster(
const any* __any);
312 template<
typename _Tp>
313 struct _Manager_internal
316 _S_manage(_Op __which,
const any* __anyp, _Arg* __arg);
318 template<
typename _Up>
320 _S_create(_Storage& __storage, _Up&& __value)
322 void* __addr = &__storage._M_buffer;
323 ::new (__addr) _Tp(std::forward<_Up>(__value));
328 template<
typename _Tp>
329 struct _Manager_external
332 _S_manage(_Op __which,
const any* __anyp, _Arg* __arg);
334 template<
typename _Up>
336 _S_create(_Storage& __storage, _Up&& __value)
338 __storage._M_ptr =
new _Tp(std::forward<_Up>(__value));
344 inline void swap(
any& __x,
any& __y)
noexcept { __x.swap(__y); }
356 template<
typename _ValueType>
359 static_assert(any::__is_valid_cast<_ValueType>(),
360 "Template argument must be a reference or CopyConstructible type");
361 auto __p = any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
364 __throw_bad_any_cast();
379 template<
typename _ValueType>
382 static_assert(any::__is_valid_cast<_ValueType>(),
383 "Template argument must be a reference or CopyConstructible type");
384 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
387 __throw_bad_any_cast();
390 template<
typename _ValueType,
396 static_assert(any::__is_valid_cast<_ValueType>(),
397 "Template argument must be a reference or CopyConstructible type");
398 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
401 __throw_bad_any_cast();
404 template<
typename _ValueType,
410 static_assert(any::__is_valid_cast<_ValueType>(),
411 "Template argument must be a reference or CopyConstructible type");
412 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
415 __throw_bad_any_cast();
420 template<
typename _Tp>
422 __any_caster(
const any* __any)
426 using _Up = remove_cv_t<_Tp>;
435 using _Vp = __conditional_t<__and_<__does_not_decay, __is_copyable>{},
438 if (__any->_M_manager == &any::_Manager<_Vp>::_S_manage
440 || __any->
type() ==
typeid(_Tp)
445 __any->_M_manager(any::_Op_access, __any, &__arg);
452 template<
typename _Tp>
453 enable_if_t<!is_object<_Tp>::value, _Tp*>
454 __any_caster(
const any*)
noexcept
469 template<
typename _ValueType>
473 return static_cast<_ValueType*
>(__any_caster<_ValueType>(__any));
477 template<
typename _ValueType>
481 return static_cast<_ValueType*
>(__any_caster<_ValueType>(__any));
486 template<
typename _Tp>
488 any::_Manager_internal<_Tp>::
489 _S_manage(_Op __which,
const any* __any, _Arg* __arg)
492 auto __ptr =
reinterpret_cast<const _Tp*
>(&__any->_M_storage._M_buffer);
496 __arg->_M_obj =
const_cast<_Tp*
>(__ptr);
498 case _Op_get_type_info:
500 __arg->_M_typeinfo = &
typeid(_Tp);
504 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
505 __arg->_M_any->_M_manager = __any->_M_manager;
511 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp
512 (
std::
move(*const_cast<_Tp*>(__ptr)));
514 __arg->_M_any->_M_manager = __any->_M_manager;
515 const_cast<
any*>(__any)->_M_manager =
nullptr;
520 template<typename _Tp>
522 any::_Manager_external<_Tp>::
523 _S_manage(_Op __which, const
any* __any, _Arg* __arg)
526 auto __ptr =
static_cast<const _Tp*
>(__any->_M_storage._M_ptr);
530 __arg->_M_obj =
const_cast<_Tp*
>(__ptr);
532 case _Op_get_type_info:
534 __arg->_M_typeinfo = &
typeid(_Tp);
538 __arg->_M_any->_M_storage._M_ptr =
new _Tp(*__ptr);
539 __arg->_M_any->_M_manager = __any->_M_manager;
545 __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
546 __arg->_M_any->_M_manager = __any->_M_manager;
547 const_cast<any*
>(__any)->_M_manager =
nullptr;
554 struct any::_Manager_internal<any::_Op>
557 _S_manage(_Op,
const any*, _Arg*) { }
564_GLIBCXX_END_NAMESPACE_VERSION
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
_ValueType any_cast(const any &__any)
Access the contained object.
ISO C++ entities toplevel namespace is std.
Thrown during incorrect typecasting.
Define a member typedef type only if a boolean constant is true.
Exception class thrown by a failed any_cast.
virtual const char * what() const noexcept
A type-safe container of any type.
bool empty() const noexcept
Reports whether there is a contained object or not.
any(const any &__other)
Copy constructor, copies the state of __other.
any(any &&__other) noexcept
Move constructor, transfer the state from __other.
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.
void clear() noexcept
If not empty, destroy the contained object.
any() noexcept
Default constructor, creates an empty object.
void swap(any &__rhs) noexcept
Exchange state with another object.
~any()
Destructor, calls clear()
any & operator=(const any &__rhs)
Copy the state of another object.
any(_ValueType &&__value)
Construct with a copy of __value as the contained object.
enable_if_t<!is_same< any, decay_t< _ValueType > >::value, any & > operator=(_ValueType &&__rhs)
Store a copy of __rhs as the contained object.