1 // <experimental/any> -*- C++ -*-
3 // Copyright (C) 2014-2019 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.
29 #ifndef _GLIBCXX_EXPERIMENTAL_ANY
30 #define _GLIBCXX_EXPERIMENTAL_ANY 1
32 #pragma GCC system_header
34 #if __cplusplus >= 201402L
39 #include <type_traits>
40 #include <experimental/bits/lfts_config.h>
42 namespace std _GLIBCXX_VISIBILITY(default)
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 namespace experimental
48 inline namespace fundamentals_v1
51 * @defgroup any Type-safe container of any type
52 * @ingroup experimental
54 * A type-safe container for single values of value types, as
55 * described in n3804 "Any Library Proposal (Revision 3)".
60 #define __cpp_lib_experimental_any 201411
63 * @brief Exception class thrown by a failed @c any_cast
66 class bad_any_cast : public bad_cast
69 virtual const char* what() const noexcept { return "bad any_cast"; }
72 [[gnu::noreturn]] inline void __throw_bad_any_cast()
82 * @brief A type-safe container of any type.
84 * An @c any object's state is either empty or it stores a contained object
85 * of CopyConstructible type.
89 // Holds either pointer to a heap object or the contained object itself.
92 // This constructor intentionally doesn't initialize anything.
95 // Prevent trivial copies of this type, buffer might hold a non-POD.
96 _Storage(const _Storage&) = delete;
97 _Storage& operator=(const _Storage&) = delete;
100 aligned_storage<sizeof(_M_ptr), alignof(void*)>::type _M_buffer;
103 template<typename _Tp, typename _Safe = is_nothrow_move_constructible<_Tp>,
104 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))
105 && (alignof(_Tp) <= alignof(_Storage))>
106 using _Internal = std::integral_constant<bool, _Safe::value && _Fits>;
108 template<typename _Tp>
109 struct _Manager_internal; // uses small-object optimization
111 template<typename _Tp>
112 struct _Manager_external; // creates contained object on the heap
114 template<typename _Tp>
115 using _Manager = conditional_t<_Internal<_Tp>::value,
116 _Manager_internal<_Tp>,
117 _Manager_external<_Tp>>;
119 template<typename _Tp, typename _Decayed = decay_t<_Tp>>
120 using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>;
123 // construct/destruct
125 /// Default constructor, creates an empty object.
126 any() noexcept : _M_manager(nullptr) { }
128 /// Copy constructor, copies the state of @p __other
129 any(const any& __other)
132 _M_manager = nullptr;
137 __other._M_manager(_Op_clone, &__other, &__arg);
142 * @brief Move constructor, transfer the state from @p __other
144 * @post @c __other.empty() (this postcondition is a GNU extension)
146 any(any&& __other) noexcept
149 _M_manager = nullptr;
154 __other._M_manager(_Op_xfer, &__other, &__arg);
158 /// Construct with a copy of @p __value as the contained object.
159 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
160 typename _Mgr = _Manager<_Tp>,
161 typename enable_if<is_constructible<_Tp, _ValueType&&>::value,
163 any(_ValueType&& __value)
164 : _M_manager(&_Mgr::_S_manage)
166 _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value));
167 static_assert(is_copy_constructible<_Tp>::value,
168 "The contained object must be CopyConstructible");
171 /// Construct with a copy of @p __value as the contained object.
172 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
173 typename _Mgr = _Manager<_Tp>,
174 typename enable_if<!is_constructible<_Tp, _ValueType&&>::value,
176 any(_ValueType&& __value)
177 : _M_manager(&_Mgr::_S_manage)
179 _Mgr::_S_create(_M_storage, __value);
180 static_assert(is_copy_constructible<_Tp>::value,
181 "The contained object must be CopyConstructible");
184 /// Destructor, calls @c clear()
189 /// Copy the state of another object.
190 any& operator=(const any& __rhs)
197 * @brief Move assignment operator
199 * @post @c __rhs.empty() (not guaranteed for other implementations)
201 any& operator=(any&& __rhs) noexcept
205 else if (this != &__rhs)
210 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
215 /// Store a copy of @p __rhs as the contained object.
216 template<typename _ValueType>
217 enable_if_t<!is_same<any, decay_t<_ValueType>>::value, any&>
218 operator=(_ValueType&& __rhs)
220 *this = any(std::forward<_ValueType>(__rhs));
226 /// If not empty, destroy the contained object.
227 void clear() noexcept
231 _M_manager(_Op_destroy, this, nullptr);
232 _M_manager = nullptr;
236 /// Exchange state with another object.
237 void swap(any& __rhs) noexcept
239 if (empty() && __rhs.empty())
242 if (!empty() && !__rhs.empty())
249 __arg._M_any = &__tmp;
250 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
251 __arg._M_any = &__rhs;
252 _M_manager(_Op_xfer, this, &__arg);
254 __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
258 any* __empty = empty() ? this : &__rhs;
259 any* __full = empty() ? &__rhs : this;
261 __arg._M_any = __empty;
262 __full->_M_manager(_Op_xfer, __full, &__arg);
268 /// Reports whether there is a contained object or not.
269 _GLIBCXX_NODISCARD bool empty() const noexcept { return _M_manager == nullptr; }
272 /// The @c typeid of the contained object, or @c typeid(void) if empty.
273 const type_info& type() const noexcept
278 _M_manager(_Op_get_type_info, this, &__arg);
279 return *__arg._M_typeinfo;
283 template<typename _Tp>
284 static constexpr bool __is_valid_cast()
285 { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
289 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
295 const std::type_info* _M_typeinfo;
299 void (*_M_manager)(_Op, const any*, _Arg*);
302 template<typename _Tp>
303 friend void* __any_caster(const any* __any);
305 // Manage in-place contained object.
306 template<typename _Tp>
307 struct _Manager_internal
310 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
312 template<typename _Up>
314 _S_create(_Storage& __storage, _Up&& __value)
316 void* __addr = &__storage._M_buffer;
317 ::new (__addr) _Tp(std::forward<_Up>(__value));
321 // Manage external contained object.
322 template<typename _Tp>
323 struct _Manager_external
326 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
328 template<typename _Up>
330 _S_create(_Storage& __storage, _Up&& __value)
332 __storage._M_ptr = new _Tp(std::forward<_Up>(__value));
337 /// Exchange the states of two @c any objects.
338 inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
341 * @brief Access the contained object.
343 * @tparam _ValueType A const-reference or CopyConstructible type.
344 * @param __any The object to access.
345 * @return The contained object.
346 * @throw bad_any_cast If <code>
347 * __any.type() != typeid(remove_reference_t<_ValueType>)
350 template<typename _ValueType>
351 inline _ValueType any_cast(const any& __any)
353 static_assert(any::__is_valid_cast<_ValueType>(),
354 "Template argument must be a reference or CopyConstructible type");
355 auto __p = any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
358 __throw_bad_any_cast();
362 * @brief Access the contained object.
364 * @tparam _ValueType A reference or CopyConstructible type.
365 * @param __any The object to access.
366 * @return The contained object.
367 * @throw bad_any_cast If <code>
368 * __any.type() != typeid(remove_reference_t<_ValueType>)
373 template<typename _ValueType>
374 inline _ValueType any_cast(any& __any)
376 static_assert(any::__is_valid_cast<_ValueType>(),
377 "Template argument must be a reference or CopyConstructible type");
378 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
381 __throw_bad_any_cast();
384 template<typename _ValueType,
385 typename enable_if<!is_move_constructible<_ValueType>::value
386 || is_lvalue_reference<_ValueType>::value,
388 inline _ValueType any_cast(any&& __any)
390 static_assert(any::__is_valid_cast<_ValueType>(),
391 "Template argument must be a reference or CopyConstructible type");
392 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
395 __throw_bad_any_cast();
398 template<typename _ValueType,
399 typename enable_if<is_move_constructible<_ValueType>::value
400 && !is_lvalue_reference<_ValueType>::value,
402 inline _ValueType any_cast(any&& __any)
404 static_assert(any::__is_valid_cast<_ValueType>(),
405 "Template argument must be a reference or CopyConstructible type");
406 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
408 return std::move(*__p);
409 __throw_bad_any_cast();
413 template<typename _Tp>
414 void* __any_caster(const any* __any)
417 using _Up = decay_t<_Tp>;
418 using _Vp = conditional_t<is_copy_constructible<_Up>::value, _Up, _None>;
419 if (__any->_M_manager != &any::_Manager<_Vp>::_S_manage)
422 __any->_M_manager(any::_Op_access, __any, &__arg);
427 * @brief Access the contained object.
429 * @tparam _ValueType The type of the contained object.
430 * @param __any A pointer to the object to access.
431 * @return The address of the contained object if <code>
432 * __any != nullptr && __any.type() == typeid(_ValueType)
433 * </code>, otherwise a null pointer.
437 template<typename _ValueType>
438 inline const _ValueType* any_cast(const any* __any) noexcept
441 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
445 template<typename _ValueType>
446 inline _ValueType* any_cast(any* __any) noexcept
449 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
454 template<typename _Tp>
456 any::_Manager_internal<_Tp>::
457 _S_manage(_Op __which, const any* __any, _Arg* __arg)
459 // The contained object is in _M_storage._M_buffer
460 auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer);
464 __arg->_M_obj = const_cast<_Tp*>(__ptr);
466 case _Op_get_type_info:
468 __arg->_M_typeinfo = &typeid(_Tp);
472 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
473 __arg->_M_any->_M_manager = __any->_M_manager;
479 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp
480 (std::move(*const_cast<_Tp*>(__ptr)));
482 __arg->_M_any->_M_manager = __any->_M_manager;
483 const_cast<any*>(__any)->_M_manager = nullptr;
488 template<typename _Tp>
490 any::_Manager_external<_Tp>::
491 _S_manage(_Op __which, const any* __any, _Arg* __arg)
493 // The contained object is *_M_storage._M_ptr
494 auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr);
498 __arg->_M_obj = const_cast<_Tp*>(__ptr);
500 case _Op_get_type_info:
502 __arg->_M_typeinfo = &typeid(_Tp);
506 __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr);
507 __arg->_M_any->_M_manager = __any->_M_manager;
513 __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
514 __arg->_M_any->_M_manager = __any->_M_manager;
515 const_cast<any*>(__any)->_M_manager = nullptr;
521 } // namespace fundamentals_v1
522 } // namespace experimental
524 _GLIBCXX_END_NAMESPACE_VERSION
529 #endif // _GLIBCXX_EXPERIMENTAL_ANY