1 // <experimental/any> -*- C++ -*-
3 // Copyright (C) 2014-2022 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 #if __cplusplus >= 201402L
39 #include <type_traits>
40 #include <bits/move.h>
41 #include <experimental/bits/lfts_config.h>
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 namespace experimental
49 inline namespace fundamentals_v1
52 * @defgroup any Type-safe container of any type
55 * A type-safe container for single values of value types, as
56 * described in n3804 "Any Library Proposal (Revision 3)".
61 #define __cpp_lib_experimental_any 201411
64 * @brief Exception class thrown by a failed @c any_cast
67 class bad_any_cast : public bad_cast
70 virtual const char* what() const noexcept { return "bad any_cast"; }
73 /// @cond undocumented
74 [[gnu::noreturn]] inline void __throw_bad_any_cast()
85 * @brief A type-safe container of any type.
87 * An @c any object's state is either empty or it stores a contained object
88 * of CopyConstructible type.
92 // Holds either pointer to a heap object or the contained object itself.
95 // This constructor intentionally doesn't initialize anything.
98 // Prevent trivial copies of this type, buffer might hold a non-POD.
99 _Storage(const _Storage&) = delete;
100 _Storage& operator=(const _Storage&) = delete;
103 aligned_storage<sizeof(_M_ptr), alignof(void*)>::type _M_buffer;
106 template<typename _Tp, typename _Safe = is_nothrow_move_constructible<_Tp>,
107 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))
108 && (alignof(_Tp) <= alignof(_Storage))>
109 using _Internal = std::integral_constant<bool, _Safe::value && _Fits>;
111 template<typename _Tp>
112 struct _Manager_internal; // uses small-object optimization
114 template<typename _Tp>
115 struct _Manager_external; // creates contained object on the heap
117 template<typename _Tp>
118 using _Manager = __conditional_t<_Internal<_Tp>::value,
119 _Manager_internal<_Tp>,
120 _Manager_external<_Tp>>;
122 template<typename _Tp, typename _Decayed = decay_t<_Tp>>
123 using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>;
126 // construct/destruct
128 /// Default constructor, creates an empty object.
129 any() noexcept : _M_manager(nullptr) { }
131 /// Copy constructor, copies the state of @p __other
132 any(const any& __other)
135 _M_manager = nullptr;
140 __other._M_manager(_Op_clone, &__other, &__arg);
145 * @brief Move constructor, transfer the state from @p __other
147 * @post @c __other.empty() (this postcondition is a GNU extension)
149 any(any&& __other) noexcept
152 _M_manager = nullptr;
157 __other._M_manager(_Op_xfer, &__other, &__arg);
161 /// Construct with a copy of @p __value as the contained object.
162 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
163 typename _Mgr = _Manager<_Tp>,
164 typename enable_if<is_constructible<_Tp, _ValueType&&>::value,
166 any(_ValueType&& __value)
167 : _M_manager(&_Mgr::_S_manage)
169 _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value));
170 static_assert(is_copy_constructible<_Tp>::value,
171 "The contained object must be CopyConstructible");
174 /// Construct with a copy of @p __value as the contained object.
175 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
176 typename _Mgr = _Manager<_Tp>,
177 typename enable_if<!is_constructible<_Tp, _ValueType&&>::value,
179 any(_ValueType&& __value)
180 : _M_manager(&_Mgr::_S_manage)
182 _Mgr::_S_create(_M_storage, __value);
183 static_assert(is_copy_constructible<_Tp>::value,
184 "The contained object must be CopyConstructible");
187 /// Destructor, calls @c clear()
192 /// Copy the state of another object.
193 any& operator=(const any& __rhs)
200 * @brief Move assignment operator
202 * @post @c __rhs.empty() (not guaranteed for other implementations)
204 any& operator=(any&& __rhs) noexcept
208 else if (this != &__rhs)
213 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
218 /// Store a copy of @p __rhs as the contained object.
219 template<typename _ValueType>
220 enable_if_t<!is_same<any, decay_t<_ValueType>>::value, any&>
221 operator=(_ValueType&& __rhs)
223 *this = any(std::forward<_ValueType>(__rhs));
229 /// If not empty, destroy the contained object.
230 void clear() noexcept
234 _M_manager(_Op_destroy, this, nullptr);
235 _M_manager = nullptr;
239 /// Exchange state with another object.
240 void swap(any& __rhs) noexcept
242 if (empty() && __rhs.empty())
245 if (!empty() && !__rhs.empty())
252 __arg._M_any = &__tmp;
253 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
254 __arg._M_any = &__rhs;
255 _M_manager(_Op_xfer, this, &__arg);
257 __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
261 any* __empty = empty() ? this : &__rhs;
262 any* __full = empty() ? &__rhs : this;
264 __arg._M_any = __empty;
265 __full->_M_manager(_Op_xfer, __full, &__arg);
271 /// Reports whether there is a contained object or not.
272 _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_manager == nullptr; }
275 /// The @c typeid of the contained object, or @c typeid(void) if empty.
276 const type_info& type() const noexcept
281 _M_manager(_Op_get_type_info, this, &__arg);
282 return *__arg._M_typeinfo;
286 template<typename _Tp>
287 static constexpr bool __is_valid_cast()
288 { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
292 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
298 const std::type_info* _M_typeinfo;
302 void (*_M_manager)(_Op, const any*, _Arg*);
305 template<typename _Tp>
306 friend enable_if_t<is_object<_Tp>::value, void*>
307 __any_caster(const any* __any);
309 // Manage in-place contained object.
310 template<typename _Tp>
311 struct _Manager_internal
314 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
316 template<typename _Up>
318 _S_create(_Storage& __storage, _Up&& __value)
320 void* __addr = &__storage._M_buffer;
321 ::new (__addr) _Tp(std::forward<_Up>(__value));
325 // Manage external contained object.
326 template<typename _Tp>
327 struct _Manager_external
330 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
332 template<typename _Up>
334 _S_create(_Storage& __storage, _Up&& __value)
336 __storage._M_ptr = new _Tp(std::forward<_Up>(__value));
341 /// Exchange the states of two @c any objects.
342 inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
345 * @brief Access the contained object.
347 * @tparam _ValueType A const-reference or CopyConstructible type.
348 * @param __any The object to access.
349 * @return The contained object.
350 * @throw bad_any_cast If <code>
351 * __any.type() != typeid(remove_reference_t<_ValueType>)
354 template<typename _ValueType>
355 inline _ValueType any_cast(const any& __any)
357 static_assert(any::__is_valid_cast<_ValueType>(),
358 "Template argument must be a reference or CopyConstructible type");
359 auto __p = any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
362 __throw_bad_any_cast();
366 * @brief Access the contained object.
368 * @tparam _ValueType A reference or CopyConstructible type.
369 * @param __any The object to access.
370 * @return The contained object.
371 * @throw bad_any_cast If <code>
372 * __any.type() != typeid(remove_reference_t<_ValueType>)
377 template<typename _ValueType>
378 inline _ValueType any_cast(any& __any)
380 static_assert(any::__is_valid_cast<_ValueType>(),
381 "Template argument must be a reference or CopyConstructible type");
382 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
385 __throw_bad_any_cast();
388 template<typename _ValueType,
389 typename enable_if<!is_move_constructible<_ValueType>::value
390 || is_lvalue_reference<_ValueType>::value,
392 inline _ValueType any_cast(any&& __any)
394 static_assert(any::__is_valid_cast<_ValueType>(),
395 "Template argument must be a reference or CopyConstructible type");
396 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
399 __throw_bad_any_cast();
402 template<typename _ValueType,
403 typename enable_if<is_move_constructible<_ValueType>::value
404 && !is_lvalue_reference<_ValueType>::value,
406 inline _ValueType any_cast(any&& __any)
408 static_assert(any::__is_valid_cast<_ValueType>(),
409 "Template argument must be a reference or CopyConstructible type");
410 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
412 return std::move(*__p);
413 __throw_bad_any_cast();
417 /// @cond undocumented
418 template<typename _Tp>
419 enable_if_t<is_object<_Tp>::value, void*>
420 __any_caster(const any* __any)
422 // any_cast<T> returns non-null if __any->type() == typeid(T) and
423 // typeid(T) ignores cv-qualifiers so remove them:
424 using _Up = remove_cv_t<_Tp>;
425 // The contained value has a decayed type, so if decay_t<U> is not U,
426 // then it's not possible to have a contained value of type U.
427 using __does_not_decay = is_same<decay_t<_Up>, _Up>;
428 // Only copy constructible types can be used for contained values.
429 using __is_copyable = is_copy_constructible<_Up>;
430 // If the type _Tp could never be stored in an any we don't want to
431 // instantiate _Manager<_Tp>, so use _Manager<any::_Op> instead, which
432 // is explicitly specialized and has a no-op _S_manage function.
433 using _Vp = __conditional_t<__and_<__does_not_decay, __is_copyable>{},
435 // First try comparing function addresses, which works without RTTI
436 if (__any->_M_manager == &any::_Manager<_Vp>::_S_manage
438 || __any->type() == typeid(_Tp)
443 __any->_M_manager(any::_Op_access, __any, &__arg);
449 // This overload exists so that std::any_cast<void(*)()>(a) is well-formed.
450 template<typename _Tp>
451 enable_if_t<!is_object<_Tp>::value, _Tp*>
452 __any_caster(const any*) noexcept
457 * @brief Access the contained object.
459 * @tparam _ValueType The type of the contained object.
460 * @param __any A pointer to the object to access.
461 * @return The address of the contained object if <code>
462 * __any != nullptr && __any.type() == typeid(_ValueType)
463 * </code>, otherwise a null pointer.
467 template<typename _ValueType>
468 inline const _ValueType* any_cast(const any* __any) noexcept
471 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
475 template<typename _ValueType>
476 inline _ValueType* any_cast(any* __any) noexcept
479 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
484 template<typename _Tp>
486 any::_Manager_internal<_Tp>::
487 _S_manage(_Op __which, const any* __any, _Arg* __arg)
489 // The contained object is in _M_storage._M_buffer
490 auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer);
494 __arg->_M_obj = const_cast<_Tp*>(__ptr);
496 case _Op_get_type_info:
498 __arg->_M_typeinfo = &typeid(_Tp);
502 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
503 __arg->_M_any->_M_manager = __any->_M_manager;
509 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp
510 (std::move(*const_cast<_Tp*>(__ptr)));
512 __arg->_M_any->_M_manager = __any->_M_manager;
513 const_cast<any*>(__any)->_M_manager = nullptr;
518 template<typename _Tp>
520 any::_Manager_external<_Tp>::
521 _S_manage(_Op __which, const any* __any, _Arg* __arg)
523 // The contained object is *_M_storage._M_ptr
524 auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr);
528 __arg->_M_obj = const_cast<_Tp*>(__ptr);
530 case _Op_get_type_info:
532 __arg->_M_typeinfo = &typeid(_Tp);
536 __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr);
537 __arg->_M_any->_M_manager = __any->_M_manager;
543 __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
544 __arg->_M_any->_M_manager = __any->_M_manager;
545 const_cast<any*>(__any)->_M_manager = nullptr;
550 // Dummy specialization used by __any_caster.
552 struct any::_Manager_internal<any::_Op>
555 _S_manage(_Op, const any*, _Arg*) { }
559 } // namespace fundamentals_v1
560 } // namespace experimental
562 _GLIBCXX_END_NAMESPACE_VERSION
567 #endif // _GLIBCXX_EXPERIMENTAL_ANY