29 #ifndef _GLIBCXX_EXPERIMENTAL_ANY
30 #define _GLIBCXX_EXPERIMENTAL_ANY 1
32 #pragma GCC system_header
34 #if __cplusplus <= 201103L
44 namespace std _GLIBCXX_VISIBILITY(default)
46 namespace experimental
48 inline namespace fundamentals_v1
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
62 #define __cpp_lib_experimental_any 201411
71 virtual const char*
what() const noexcept {
return "bad any_cast"; }
74 [[gnu::noreturn]]
inline void __throw_bad_any_cast()
95 std::aligned_storage<sizeof(_M_ptr), sizeof(_M_ptr)>::type _M_buffer;
98 template<
typename _Tp,
typename _Safe = is_trivially_copyable<_Tp>,
99 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))>
100 using _Internal = std::
integral_constant<
bool, _Safe::value && _Fits>;
102 template<
typename _Tp>
103 struct _Manager_internal;
105 template<
typename _Tp>
106 struct _Manager_external;
108 template<
typename _Tp>
109 using _Manager = conditional_t<_Internal<_Tp>::value,
110 _Manager_internal<_Tp>,
111 _Manager_external<_Tp>>;
113 template<
typename _Tp,
typename _Decayed = decay_t<_Tp>>
114 using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>;
120 any() noexcept : _M_manager(
nullptr) { }
123 any(
const any& __other) : _M_manager(__other._M_manager)
125 if (!__other.
empty())
129 _M_manager(_Op_clone, &__other, &__arg);
139 : _M_manager(__other._M_manager),
140 _M_storage(__other._M_storage)
141 { __other._M_manager =
nullptr; }
144 template <
typename _ValueType,
typename _Tp = _Decay<_ValueType>,
145 typename _Mgr = _Manager<_Tp>>
147 : _M_manager(&_Mgr::_S_manage),
148 _M_storage(_Mgr::_S_create(
std::
forward<_ValueType>(__value)))
150 static_assert(is_copy_constructible<_Tp>::value,
151 "The contained object must be CopyConstructible");
162 any(__rhs).swap(*
this);
173 any(std::move(__rhs)).swap(*
this);
178 template<
typename _ValueType>
181 any(std::forward<_ValueType>(__rhs)).swap(*
this);
192 _M_manager(_Op_destroy,
this,
nullptr);
193 _M_manager =
nullptr;
200 std::swap(_M_manager, __rhs._M_manager);
201 std::swap(_M_storage, __rhs._M_storage);
207 bool empty() const noexcept {
return _M_manager ==
nullptr; }
216 _M_manager(_Op_get_type_info,
this, &__arg);
217 return *__arg._M_typeinfo;
221 template<
typename _Tp>
222 static constexpr
bool __is_valid_cast()
223 {
return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
226 enum _Op { _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy };
235 void (*_M_manager)(_Op,
const any*, _Arg*);
238 template<
typename _Tp>
239 friend void* __any_caster(
const any* __any);
242 template<
typename _Tp>
243 struct _Manager_internal
246 _S_manage(_Op __which,
const any* __anyp, _Arg* __arg);
248 template<
typename _Up>
250 _S_create(_Up&& __value)
253 void* __addr = &__storage._M_buffer;
254 ::new (__addr) _Tp(
std::
forward<_Up>(__value));
258 template<typename _Alloc, typename _Up>
260 _S_alloc(const _Alloc&, _Up&& __value)
262 return _S_create(std::forward<_Up>(__value));
267 template<
typename _Tp>
268 struct _Manager_external
271 _S_manage(_Op __which,
const any* __anyp, _Arg* __arg);
273 template<
typename _Up>
275 _S_create(_Up&& __value)
278 __storage._M_ptr =
new _Tp(std::forward<_Up>(__value));
285 inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
297 template<
typename _ValueType>
300 static_assert(any::__is_valid_cast<_ValueType>(),
301 "Template argument must be a reference or CopyConstructible type");
302 auto __p =
any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
305 __throw_bad_any_cast();
320 template<
typename _ValueType>
323 static_assert(any::__is_valid_cast<_ValueType>(),
324 "Template argument must be a reference or CopyConstructible type");
325 auto __p =
any_cast<remove_reference_t<_ValueType>>(&__any);
328 __throw_bad_any_cast();
331 template<
typename _ValueType>
334 static_assert(any::__is_valid_cast<_ValueType>(),
335 "Template argument must be a reference or CopyConstructible type");
336 auto __p =
any_cast<remove_reference_t<_ValueType>>(&__any);
339 __throw_bad_any_cast();
343 template<
typename _Tp>
344 void* __any_caster(
const any* __any)
347 using _Up = decay_t<_Tp>;
348 using _Vp = conditional_t<is_copy_constructible<_Up>::value, _Up, _None>;
349 if (__any->_M_manager != &any::_Manager<_Vp>::_S_manage)
352 __any->_M_manager(any::_Op_access, __any, &__arg);
367 template<
typename _ValueType>
368 inline const _ValueType*
any_cast(
const any* __any) noexcept
371 return static_cast<_ValueType*
>(__any_caster<_ValueType>(__any));
375 template<
typename _ValueType>
379 return static_cast<_ValueType*
>(__any_caster<_ValueType>(__any));
384 template<
typename _Tp>
386 any::_Manager_internal<_Tp>::
387 _S_manage(_Op __which,
const any* __any, _Arg* __arg)
390 auto __ptr =
reinterpret_cast<const _Tp*
>(&__any->_M_storage._M_buffer);
394 __arg->_M_obj =
const_cast<_Tp*
>(__ptr);
396 case _Op_get_type_info:
398 __arg->_M_typeinfo = &
typeid(_Tp);
402 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
410 template<typename _Tp>
412 any::_Manager_external<_Tp>::
413 _S_manage(_Op __which, const any* __any, _Arg* __arg)
416 auto __ptr =
static_cast<const _Tp*
>(__any->_M_storage._M_ptr);
420 __arg->_M_obj =
const_cast<_Tp*
>(__ptr);
422 case _Op_get_type_info:
424 __arg->_M_typeinfo = &
typeid(_Tp);
428 __arg->_M_any->_M_storage._M_ptr =
new _Tp(*__ptr);
437 _GLIBCXX_END_NAMESPACE_VERSION
444 #endif // _GLIBCXX_EXPERIMENTAL_ANY
A type-safe container of any type.
void clear() noexcept
If not empty, destroy the contained object.
any(_ValueType &&__value)
Construct with a copy of __value as the contained object.
any & operator=(const any &__rhs)
Copy the state of.
any(const any &__other)
Copy constructor, copies the state of __other.
~any()
Destructor, calls clear()
any & operator=(any &&__rhs) noexcept
Move assignment operator.
_ValueType * any_cast(any *__any) noexcept
Access the contained object.
const type_info & type() const noexcept
The typeid of the contained object, or typeid(void) if empty.
ISO C++ entities toplevel namespace is std.
any(any &&__other) noexcept
Move constructor, transfer the state from __other.
virtual const char * what() const noexcept
any() noexcept
Default constructor, creates an empty object.
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.
any & operator=(_ValueType &&__rhs)
Store a copy of __rhs as the contained object.
Exception class thrown by a failed any_cast.
bool empty() const noexcept
Reports whether there is a contained object or not.
void swap(any &__rhs) noexcept
Exchange state with another object.