1// <experimental/any> -*- C++ -*-
3// Copyright (C) 2014-2023 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
25/** @file experimental/any
26 * This is a TS C++ Library header.
30#ifndef _GLIBCXX_EXPERIMENTAL_ANY
31#define _GLIBCXX_EXPERIMENTAL_ANY 1
33#pragma GCC system_header
35#include <bits/requires_hosted.h> // experimental is currently omitted
37#if __cplusplus >= 201402L
43#include <experimental/bits/lfts_config.h>
45namespace std _GLIBCXX_VISIBILITY(default)
47_GLIBCXX_BEGIN_NAMESPACE_VERSION
51inline namespace fundamentals_v1
54 * @defgroup any Type-safe container of any type
57 * A type-safe container for single values of value types, as
58 * described in n3804 "Any Library Proposal (Revision 3)".
63#define __cpp_lib_experimental_any 201411
66 * @brief Exception class thrown by a failed @c any_cast
69 class bad_any_cast : public bad_cast
72 virtual const char* what() const noexcept { return "bad any_cast"; }
75 /// @cond undocumented
76 [[gnu::noreturn]] inline void __throw_bad_any_cast()
87 * @brief A type-safe container of any type.
89 * An @c any object's state is either empty or it stores a contained object
90 * of CopyConstructible type.
94 // Holds either pointer to a heap object or the contained object itself.
97 // This constructor intentionally doesn't initialize anything.
100 // Prevent trivial copies of this type, buffer might hold a non-POD.
101 _Storage(const _Storage&) = delete;
102 _Storage& operator=(const _Storage&) = delete;
105 aligned_storage<sizeof(_M_ptr), alignof(void*)>::type _M_buffer;
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; // uses small-object optimization
116 template<typename _Tp>
117 struct _Manager_external; // creates contained object on the heap
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>>
125 using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>;
128 // construct/destruct
130 /// Default constructor, creates an empty object.
131 any() noexcept : _M_manager(nullptr) { }
133 /// Copy constructor, copies the state of @p __other
134 any(const any& __other)
137 _M_manager = nullptr;
142 __other._M_manager(_Op_clone, &__other, &__arg);
147 * @brief Move constructor, transfer the state from @p __other
149 * @post @c __other.empty() (this postcondition is a GNU extension)
151 any(any&& __other) noexcept
154 _M_manager = nullptr;
159 __other._M_manager(_Op_xfer, &__other, &__arg);
163 /// Construct with a copy of @p __value as the contained object.
164 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
165 typename _Mgr = _Manager<_Tp>,
166 typename enable_if<is_constructible<_Tp, _ValueType&&>::value,
168 any(_ValueType&& __value)
169 : _M_manager(&_Mgr::_S_manage)
171 _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value));
172 static_assert(is_copy_constructible<_Tp>::value,
173 "The contained object must be CopyConstructible");
176 /// Construct with a copy of @p __value as the contained object.
177 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
178 typename _Mgr = _Manager<_Tp>,
179 typename enable_if<!is_constructible<_Tp, _ValueType&&>::value,
181 any(_ValueType&& __value)
182 : _M_manager(&_Mgr::_S_manage)
184 _Mgr::_S_create(_M_storage, __value);
185 static_assert(is_copy_constructible<_Tp>::value,
186 "The contained object must be CopyConstructible");
189 /// Destructor, calls @c clear()
194 /// Copy the state of another object.
195 any& operator=(const any& __rhs)
202 * @brief Move assignment operator
204 * @post @c __rhs.empty() (not guaranteed for other implementations)
206 any& operator=(any&& __rhs) noexcept
210 else if (this != &__rhs)
215 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
220 /// Store a copy of @p __rhs as the contained object.
221 template<typename _ValueType>
222 enable_if_t<!is_same<any, decay_t<_ValueType>>::value, any&>
223 operator=(_ValueType&& __rhs)
225 *this = any(std::forward<_ValueType>(__rhs));
231 /// If not empty, destroy the contained object.
232 void clear() noexcept
236 _M_manager(_Op_destroy, this, nullptr);
237 _M_manager = nullptr;
241 /// Exchange state with another object.
242 void swap(any& __rhs) noexcept
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);
273 /// Reports whether there is a contained object or not.
274 _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_manager == nullptr; }
277 /// The @c typeid of the contained object, or @c typeid(void) if empty.
278 const type_info& type() const noexcept
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()
290 { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
294 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
300 const std::type_info* _M_typeinfo;
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);
311 // Manage in-place contained object.
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));
327 // Manage external contained object.
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));
343 /// Exchange the states of two @c any objects.
344 inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
347 * @brief Access the contained object.
349 * @tparam _ValueType A const-reference or CopyConstructible type.
350 * @param __any The object to access.
351 * @return The contained object.
352 * @throw bad_any_cast If <code>
353 * __any.type() != typeid(remove_reference_t<_ValueType>)
356 template<typename _ValueType>
357 inline _ValueType any_cast(const any& __any)
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();
368 * @brief Access the contained object.
370 * @tparam _ValueType A reference or CopyConstructible type.
371 * @param __any The object to access.
372 * @return The contained object.
373 * @throw bad_any_cast If <code>
374 * __any.type() != typeid(remove_reference_t<_ValueType>)
379 template<typename _ValueType>
380 inline _ValueType any_cast(any& __any)
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,
391 typename enable_if<!is_move_constructible<_ValueType>::value
392 || is_lvalue_reference<_ValueType>::value,
394 inline _ValueType any_cast(any&& __any)
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,
405 typename enable_if<is_move_constructible<_ValueType>::value
406 && !is_lvalue_reference<_ValueType>::value,
408 inline _ValueType any_cast(any&& __any)
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);
414 return std::move(*__p);
415 __throw_bad_any_cast();
419 /// @cond undocumented
420 template<typename _Tp>
421 enable_if_t<is_object<_Tp>::value, void*>
422 __any_caster(const any* __any)
424 // any_cast<T> returns non-null if __any->type() == typeid(T) and
425 // typeid(T) ignores cv-qualifiers so remove them:
426 using _Up = remove_cv_t<_Tp>;
427 // The contained value has a decayed type, so if decay_t<U> is not U,
428 // then it's not possible to have a contained value of type U.
429 using __does_not_decay = is_same<decay_t<_Up>, _Up>;
430 // Only copy constructible types can be used for contained values.
431 using __is_copyable = is_copy_constructible<_Up>;
432 // If the type _Tp could never be stored in an any we don't want to
433 // instantiate _Manager<_Tp>, so use _Manager<any::_Op> instead, which
434 // is explicitly specialized and has a no-op _S_manage function.
435 using _Vp = __conditional_t<__and_<__does_not_decay, __is_copyable>{},
437 // First try comparing function addresses, which works without RTTI
438 if (__any->_M_manager == &any::_Manager<_Vp>::_S_manage
440 || __any->type() == typeid(_Tp)
445 __any->_M_manager(any::_Op_access, __any, &__arg);
451 // This overload exists so that std::any_cast<void(*)()>(a) is well-formed.
452 template<typename _Tp>
453 enable_if_t<!is_object<_Tp>::value, _Tp*>
454 __any_caster(const any*) noexcept
459 * @brief Access the contained object.
461 * @tparam _ValueType The type of the contained object.
462 * @param __any A pointer to the object to access.
463 * @return The address of the contained object if <code>
464 * __any != nullptr && __any.type() == typeid(_ValueType)
465 * </code>, otherwise a null pointer.
469 template<typename _ValueType>
470 inline const _ValueType* any_cast(const any* __any) noexcept
473 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
477 template<typename _ValueType>
478 inline _ValueType* any_cast(any* __any) noexcept
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)
491 // The contained object is in _M_storage._M_buffer
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)
525 // The contained object is *_M_storage._M_ptr
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;
552 // Dummy specialization used by __any_caster.
554 struct any::_Manager_internal<any::_Op>
557 _S_manage(_Op, const any*, _Arg*) { }
561} // namespace fundamentals_v1
562} // namespace experimental
564_GLIBCXX_END_NAMESPACE_VERSION
569#endif // _GLIBCXX_EXPERIMENTAL_ANY