30#ifndef _GLIBCXX_EXPERIMENTAL_ANY
31#define _GLIBCXX_EXPERIMENTAL_ANY 1
34#pragma GCC system_header
39#if __cplusplus >= 201402L
47namespace std _GLIBCXX_VISIBILITY(default)
49_GLIBCXX_BEGIN_NAMESPACE_VERSION
53inline namespace fundamentals_v1
65#define __cpp_lib_experimental_any 201411
74 virtual const char*
what() const noexcept {
return "bad any_cast"; }
78 [[gnu::noreturn]]
inline void __throw_bad_any_cast()
100 _Storage() =
default;
103 _Storage(
const _Storage&) =
delete;
104 _Storage& operator=(
const _Storage&) =
delete;
107 unsigned char _M_buffer[
sizeof(_M_ptr)];
110 template<
typename _Tp,
typename _Safe = is_nothrow_move_constructible<_Tp>,
111 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))
112 && (alignof(_Tp) <= alignof(_Storage))>
113 using _Internal = std::
integral_constant<
bool, _Safe::value && _Fits>;
115 template<
typename _Tp>
116 struct _Manager_internal;
118 template<
typename _Tp>
119 struct _Manager_external;
121 template<
typename _Tp>
122 using _Manager = __conditional_t<_Internal<_Tp>::value,
123 _Manager_internal<_Tp>,
124 _Manager_external<_Tp>>;
126 template<
typename _Tp,
typename _Decayed = decay_t<_Tp>>
133 any() noexcept : _M_manager(
nullptr) { }
139 _M_manager =
nullptr;
144 __other._M_manager(_Op_clone, &__other, &__arg);
156 _M_manager =
nullptr;
161 __other._M_manager(_Op_xfer, &__other, &__arg);
166 template <
typename _ValueType,
typename _Tp = _Decay<_ValueType>,
167 typename _Mgr = _Manager<_Tp>,
168 typename enable_if<is_constructible<_Tp, _ValueType&&>::value,
171 : _M_manager(&_Mgr::_S_manage)
175 "The contained object must be CopyConstructible");
179 template <
typename _ValueType,
typename _Tp = _Decay<_ValueType>,
180 typename _Mgr = _Manager<_Tp>,
181 typename enable_if<!is_constructible<_Tp, _ValueType&&>::value,
184 : _M_manager(&_Mgr::_S_manage)
186 _Mgr::_S_create(_M_storage, __value);
188 "The contained object must be CopyConstructible");
212 else if (
this != &__rhs)
217 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
223 template<
typename _ValueType>
238 _M_manager(_Op_destroy,
this,
nullptr);
239 _M_manager =
nullptr;
246 if (
empty() && __rhs.empty())
249 if (!
empty() && !__rhs.empty())
256 __arg._M_any = &__tmp;
257 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
258 __arg._M_any = &__rhs;
259 _M_manager(_Op_xfer,
this, &__arg);
261 __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
265 any* __empty =
empty() ? this : &__rhs;
266 any* __full =
empty() ? &__rhs :
this;
268 __arg._M_any = __empty;
269 __full->_M_manager(_Op_xfer, __full, &__arg);
276 _GLIBCXX_NODISCARD
bool empty() const noexcept {
return _M_manager ==
nullptr; }
285 _M_manager(_Op_get_type_info,
this, &__arg);
286 return *__arg._M_typeinfo;
290 template<
typename _Tp>
291 static constexpr bool __is_valid_cast()
296 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
306 void (*_M_manager)(_Op,
const any*, _Arg*);
309 template<
typename _Tp>
310 friend enable_if_t<is_object<_Tp>::value,
void*>
311 __any_caster(
const any* __any);
314 template<
typename _Tp>
315 struct _Manager_internal
318 _S_manage(_Op __which,
const any* __anyp, _Arg* __arg);
320 template<
typename _Up>
322 _S_create(_Storage& __storage, _Up&& __value)
324 void* __addr = &__storage._M_buffer;
330 template<
typename _Tp>
331 struct _Manager_external
334 _S_manage(_Op __which,
const any* __anyp, _Arg* __arg);
336 template<
typename _Up>
338 _S_create(_Storage& __storage, _Up&& __value)
346 inline void swap(
any& __x,
any& __y)
noexcept { __x.swap(__y); }
358 template<
typename _ValueType>
361 static_assert(any::__is_valid_cast<_ValueType>(),
362 "Template argument must be a reference or CopyConstructible type");
366 __throw_bad_any_cast();
381 template<
typename _ValueType>
384 static_assert(any::__is_valid_cast<_ValueType>(),
385 "Template argument must be a reference or CopyConstructible type");
389 __throw_bad_any_cast();
392 template<
typename _ValueType,
398 static_assert(any::__is_valid_cast<_ValueType>(),
399 "Template argument must be a reference or CopyConstructible type");
403 __throw_bad_any_cast();
406 template<
typename _ValueType,
412 static_assert(any::__is_valid_cast<_ValueType>(),
413 "Template argument must be a reference or CopyConstructible type");
417 __throw_bad_any_cast();
422 template<
typename _Tp>
424 __any_caster(
const any* __any)
428 using _Up = remove_cv_t<_Tp>;
437 using _Vp = __conditional_t<__and_<__does_not_decay, __is_copyable>{},
440 if (__any->_M_manager == &any::_Manager<_Vp>::_S_manage
442 || __any->
type() ==
typeid(_Tp)
447 __any->_M_manager(any::_Op_access, __any, &__arg);
454 template<
typename _Tp>
455 enable_if_t<!is_object<_Tp>::value, _Tp*>
456 __any_caster(
const any*)
noexcept
471 template<
typename _ValueType>
475 return static_cast<_ValueType*
>(__any_caster<_ValueType>(__any));
479 template<
typename _ValueType>
483 return static_cast<_ValueType*
>(__any_caster<_ValueType>(__any));
488 template<
typename _Tp>
490 any::_Manager_internal<_Tp>::
491 _S_manage(_Op __which,
const any* __any, _Arg* __arg)
494 auto __ptr =
reinterpret_cast<const _Tp*
>(&__any->_M_storage._M_buffer);
498 __arg->_M_obj =
const_cast<_Tp*
>(__ptr);
500 case _Op_get_type_info:
502 __arg->_M_typeinfo = &
typeid(_Tp);
506 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
507 __arg->_M_any->_M_manager = __any->_M_manager;
513 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp
514 (
std::
move(*const_cast<_Tp*>(__ptr)));
516 __arg->_M_any->_M_manager = __any->_M_manager;
517 const_cast<
any*>(__any)->_M_manager =
nullptr;
522 template<typename _Tp>
524 any::_Manager_external<_Tp>::
525 _S_manage(_Op __which, const
any* __any, _Arg* __arg)
528 auto __ptr =
static_cast<const _Tp*
>(__any->_M_storage._M_ptr);
532 __arg->_M_obj =
const_cast<_Tp*
>(__ptr);
534 case _Op_get_type_info:
536 __arg->_M_typeinfo = &
typeid(_Tp);
540 __arg->_M_any->_M_storage._M_ptr =
new _Tp(*__ptr);
541 __arg->_M_any->_M_manager = __any->_M_manager;
547 __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
548 __arg->_M_any->_M_manager = __any->_M_manager;
549 const_cast<any*
>(__any)->_M_manager =
nullptr;
556 struct any::_Manager_internal<any::_Op>
559 _S_manage(_Op,
const any*, _Arg*) { }
566_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.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
_ValueType any_cast(const any &__any)
Access the contained object.
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
ISO C++ entities toplevel namespace is std.
Namespace for features defined in ISO Technical Specifications.
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.