30 #ifndef _GLIBCXX_EXPERIMENTAL_ANY 31 #define _GLIBCXX_EXPERIMENTAL_ANY 1 33 #ifdef _GLIBCXX_SYSHDR 34 #pragma GCC system_header 39 #if __cplusplus >= 201402L 47 namespace std _GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
51 namespace experimental
53 inline 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)
173 _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value));
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>
227 *
this =
any(std::forward<_ValueType>(__rhs));
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>
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;
325 ::new (__addr) _Tp(std::forward<_Up>(__value));
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)
340 __storage._M_ptr =
new _Tp(std::forward<_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");
363 auto __p =
any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
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>
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
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
571 #endif // _GLIBCXX_EXPERIMENTAL_ANY typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
virtual const char * what() const noexcept
_ValueType * any_cast(any *__any) noexcept
Access the contained object.
~any()
Destructor, calls clear()
enable_if_t<!is_same< any, decay_t< _ValueType > >::value, any & > operator=(_ValueType &&__rhs)
Store a copy of __rhs as the contained object.
any(const any &__other)
Copy constructor, copies the state of __other.
any() noexcept
Default constructor, creates an empty object.
bool empty() const noexcept
Reports whether there is a contained object or not.
void clear() noexcept
If not empty, destroy the contained object.
any & operator=(const any &__rhs)
Copy the state of another object.
constexpr auto empty(const _Container &__cont) noexcept(noexcept(__cont.empty())) -> decltype(__cont.empty())
Return whether a container is empty.
A type-safe container of any type.
ISO C++ entities toplevel namespace is std.
any(any &&__other) noexcept
Move constructor, transfer the state from __other.
Exception class thrown by a failed any_cast.
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 std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Define a member typedef type only if a boolean constant is true.
any & operator=(any &&__rhs) noexcept
Move assignment operator.
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
const type_info & type() const noexcept
The typeid of the contained object, or typeid(void) if empty.
any(_ValueType &&__value)
Construct with a copy of __value as the contained object.
void swap(any &__rhs) noexcept
Exchange state with another object.