3 // Copyright (C) 2014-2017 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/>.
26 * This is a Standard C++ Library header.
30 #define _GLIBCXX_ANY 1
32 #pragma GCC system_header
34 #if __cplusplus >= 201703L
39 #include <type_traits>
41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 * @addtogroup utilities
51 * @brief Exception class thrown by a failed @c any_cast
54 class bad_any_cast : public bad_cast
57 virtual const char* what() const noexcept { return "bad any_cast"; }
60 [[gnu::noreturn]] inline void __throw_bad_any_cast()
69 #define __cpp_lib_any 201603
72 * @brief A type-safe container of any type.
74 * An @c any object's state is either empty or it stores a contained object
75 * of CopyConstructible type.
79 // Holds either pointer to a heap object or the contained object itself.
82 constexpr _Storage() : _M_ptr{nullptr} {}
84 // Prevent trivial copies of this type, buffer might hold a non-POD.
85 _Storage(const _Storage&) = delete;
86 _Storage& operator=(const _Storage&) = delete;
89 aligned_storage<sizeof(_M_ptr), alignof(void*)>::type _M_buffer;
92 template<typename _Tp, typename _Safe = is_nothrow_move_constructible<_Tp>,
93 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))
94 && (alignof(_Tp) <= alignof(_Storage))>
95 using _Internal = std::integral_constant<bool, _Safe::value && _Fits>;
97 template<typename _Tp>
98 struct _Manager_internal; // uses small-object optimization
100 template<typename _Tp>
101 struct _Manager_external; // creates contained object on the heap
103 template<typename _Tp>
104 using _Manager = conditional_t<_Internal<_Tp>::value,
105 _Manager_internal<_Tp>,
106 _Manager_external<_Tp>>;
108 template<typename _Tp, typename _Decayed = decay_t<_Tp>>
109 using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>;
111 /// Emplace with an object created from @p __args as the contained object.
112 template <typename _Tp, typename... _Args,
113 typename _Mgr = _Manager<_Tp>>
114 void __do_emplace(_Args&&... __args)
117 _M_manager = &_Mgr::_S_manage;
118 _Mgr::_S_create(_M_storage, std::forward<_Args>(__args)...);
121 /// Emplace with an object created from @p __il and @p __args as
122 /// the contained object.
123 template <typename _Tp, typename _Up, typename... _Args,
124 typename _Mgr = _Manager<_Tp>>
125 void __do_emplace(initializer_list<_Up> __il, _Args&&... __args)
128 _M_manager = &_Mgr::_S_manage;
129 _Mgr::_S_create(_M_storage, __il, std::forward<_Args>(__args)...);
133 // construct/destruct
135 /// Default constructor, creates an empty object.
136 constexpr any() noexcept : _M_manager(nullptr) { }
138 /// Copy constructor, copies the state of @p __other
139 any(const any& __other)
141 if (!__other.has_value())
142 _M_manager = nullptr;
147 __other._M_manager(_Op_clone, &__other, &__arg);
152 * @brief Move constructor, transfer the state from @p __other
154 * @post @c !__other.has_value() (this postcondition is a GNU extension)
156 any(any&& __other) noexcept
158 if (!__other.has_value())
159 _M_manager = nullptr;
164 __other._M_manager(_Op_xfer, &__other, &__arg);
168 template <typename _Res, typename _Tp, typename... _Args>
169 using __any_constructible =
170 enable_if<__and_<is_copy_constructible<_Tp>,
171 is_constructible<_Tp, _Args...>>::value,
174 template <typename _Tp, typename... _Args>
175 using __any_constructible_t =
176 typename __any_constructible<bool, _Tp, _Args...>::type;
178 /// Construct with a copy of @p __value as the contained object.
179 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
180 typename _Mgr = _Manager<_Tp>,
181 __any_constructible_t<_Tp, _ValueType&&> = true,
182 enable_if_t<!__is_in_place_type<_Tp>::value, bool> = true>
183 any(_ValueType&& __value)
184 : _M_manager(&_Mgr::_S_manage)
186 _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value));
189 /// Construct with a copy of @p __value as the contained object.
190 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
191 typename _Mgr = _Manager<_Tp>,
192 enable_if_t<__and_<is_copy_constructible<_Tp>,
193 __not_<is_constructible<_Tp, _ValueType&&>>,
194 __not_<__is_in_place_type<_Tp>>>::value,
196 any(_ValueType&& __value)
197 : _M_manager(&_Mgr::_S_manage)
199 _Mgr::_S_create(_M_storage, __value);
202 /// Construct with an object created from @p __args as the contained object.
203 template <typename _ValueType, typename... _Args,
204 typename _Tp = _Decay<_ValueType>,
205 typename _Mgr = _Manager<_Tp>,
206 __any_constructible_t<_Tp, _Args&&...> = false>
208 any(in_place_type_t<_ValueType>, _Args&&... __args)
209 : _M_manager(&_Mgr::_S_manage)
211 _Mgr::_S_create(_M_storage, std::forward<_Args>(__args)...);
214 /// Construct with an object created from @p __il and @p __args as
215 /// the contained object.
216 template <typename _ValueType, typename _Up, typename... _Args,
217 typename _Tp = _Decay<_ValueType>,
218 typename _Mgr = _Manager<_Tp>,
219 __any_constructible_t<_Tp, initializer_list<_Up>,
222 any(in_place_type_t<_ValueType>,
223 initializer_list<_Up> __il, _Args&&... __args)
224 : _M_manager(&_Mgr::_S_manage)
226 _Mgr::_S_create(_M_storage, __il, std::forward<_Args>(__args)...);
229 /// Destructor, calls @c reset()
234 /// Copy the state of another object.
235 any& operator=(const any& __rhs)
242 * @brief Move assignment operator
244 * @post @c !__rhs.has_value() (not guaranteed for other implementations)
246 any& operator=(any&& __rhs) noexcept
248 if (!__rhs.has_value())
250 else if (this != &__rhs)
255 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
260 /// Store a copy of @p __rhs as the contained object.
261 template<typename _ValueType>
262 enable_if_t<is_copy_constructible<_Decay<_ValueType>>::value, any&>
263 operator=(_ValueType&& __rhs)
265 *this = any(std::forward<_ValueType>(__rhs));
269 /// Emplace with an object created from @p __args as the contained object.
270 template <typename _ValueType, typename... _Args>
271 typename __any_constructible<_Decay<_ValueType>&,
272 _Decay<_ValueType>, _Args&&...>::type
273 emplace(_Args&&... __args)
275 __do_emplace<_Decay<_ValueType>>
276 (std::forward<_Args>(__args)...);
278 this->_M_manager(any::_Op_access, this, &__arg);
279 return *static_cast<_Decay<_ValueType>*>(__arg._M_obj);
282 /// Emplace with an object created from @p __il and @p __args as
283 /// the contained object.
284 template <typename _ValueType, typename _Up, typename... _Args>
285 typename __any_constructible<_Decay<_ValueType>&,
287 initializer_list<_Up>,
289 emplace(initializer_list<_Up> __il, _Args&&... __args)
291 __do_emplace<_Decay<_ValueType>, _Up>
292 (__il, std::forward<_Args>(__args)...);
294 this->_M_manager(any::_Op_access, this, &__arg);
295 return *static_cast<_Decay<_ValueType>*>(__arg._M_obj);
300 /// If not empty, destroy the contained object.
301 void reset() noexcept
305 _M_manager(_Op_destroy, this, nullptr);
306 _M_manager = nullptr;
310 /// Exchange state with another object.
311 void swap(any& __rhs) noexcept
313 if (!has_value() && !__rhs.has_value())
316 if (has_value() && __rhs.has_value())
323 __arg._M_any = &__tmp;
324 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
325 __arg._M_any = &__rhs;
326 _M_manager(_Op_xfer, this, &__arg);
328 __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
332 any* __empty = !has_value() ? this : &__rhs;
333 any* __full = !has_value() ? &__rhs : this;
335 __arg._M_any = __empty;
336 __full->_M_manager(_Op_xfer, __full, &__arg);
342 /// Reports whether there is a contained object or not.
343 bool has_value() const noexcept { return _M_manager != nullptr; }
346 /// The @c typeid of the contained object, or @c typeid(void) if empty.
347 const type_info& type() const noexcept
352 _M_manager(_Op_get_type_info, this, &__arg);
353 return *__arg._M_typeinfo;
357 template<typename _Tp>
358 static constexpr bool __is_valid_cast()
359 { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
363 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
369 const std::type_info* _M_typeinfo;
373 void (*_M_manager)(_Op, const any*, _Arg*);
376 template<typename _Tp>
377 friend void* __any_caster(const any* __any);
379 // Manage in-place contained object.
380 template<typename _Tp>
381 struct _Manager_internal
384 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
386 template<typename _Up>
388 _S_create(_Storage& __storage, _Up&& __value)
390 void* __addr = &__storage._M_buffer;
391 ::new (__addr) _Tp(std::forward<_Up>(__value));
394 template<typename... _Args>
396 _S_create(_Storage& __storage, _Args&&... __args)
398 void* __addr = &__storage._M_buffer;
399 ::new (__addr) _Tp(std::forward<_Args>(__args)...);
403 // Manage external contained object.
404 template<typename _Tp>
405 struct _Manager_external
408 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
410 template<typename _Up>
412 _S_create(_Storage& __storage, _Up&& __value)
414 __storage._M_ptr = new _Tp(std::forward<_Up>(__value));
416 template<typename... _Args>
418 _S_create(_Storage& __storage, _Args&&... __args)
420 __storage._M_ptr = new _Tp(std::forward<_Args>(__args)...);
425 /// Exchange the states of two @c any objects.
426 inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
428 /// Create an any holding a @c _Tp constructed from @c __args.
429 template <typename _Tp, typename... _Args>
430 any make_any(_Args&&... __args)
432 return any(in_place_type<_Tp>, std::forward<_Args>(__args)...);
435 /// Create an any holding a @c _Tp constructed from @c __il and @c __args.
436 template <typename _Tp, typename _Up, typename... _Args>
437 any make_any(initializer_list<_Up> __il, _Args&&... __args)
439 return any(in_place_type<_Tp>, __il, std::forward<_Args>(__args)...);
442 template <typename _Tp>
443 using _AnyCast = remove_cv_t<remove_reference_t<_Tp>>;
445 * @brief Access the contained object.
447 * @tparam _ValueType A const-reference or CopyConstructible type.
448 * @param __any The object to access.
449 * @return The contained object.
450 * @throw bad_any_cast If <code>
451 * __any.type() != typeid(remove_reference_t<_ValueType>)
454 template<typename _ValueType>
455 inline _ValueType any_cast(const any& __any)
457 static_assert(any::__is_valid_cast<_ValueType>(),
458 "Template argument must be a reference or CopyConstructible type");
459 auto __p = any_cast<_AnyCast<_ValueType>>(&__any);
461 return static_cast<_ValueType>(*__p);
462 __throw_bad_any_cast();
466 * @brief Access the contained object.
468 * @tparam _ValueType A reference or CopyConstructible type.
469 * @param __any The object to access.
470 * @return The contained object.
471 * @throw bad_any_cast If <code>
472 * __any.type() != typeid(remove_reference_t<_ValueType>)
477 template<typename _ValueType>
478 inline _ValueType any_cast(any& __any)
480 static_assert(any::__is_valid_cast<_ValueType>(),
481 "Template argument must be a reference or CopyConstructible type");
482 auto __p = any_cast<_AnyCast<_ValueType>>(&__any);
484 return static_cast<_ValueType>(*__p);
485 __throw_bad_any_cast();
488 template<typename _ValueType,
489 typename enable_if<!is_move_constructible<_ValueType>::value
490 || is_lvalue_reference<_ValueType>::value,
492 inline _ValueType any_cast(any&& __any)
494 static_assert(any::__is_valid_cast<_ValueType>(),
495 "Template argument must be a reference or CopyConstructible type");
496 auto __p = any_cast<_AnyCast<_ValueType>>(&__any);
498 return static_cast<_ValueType>(*__p);
499 __throw_bad_any_cast();
502 template<typename _ValueType,
503 typename enable_if<is_move_constructible<_ValueType>::value
504 && !is_lvalue_reference<_ValueType>::value,
506 inline _ValueType any_cast(any&& __any)
508 static_assert(any::__is_valid_cast<_ValueType>(),
509 "Template argument must be a reference or CopyConstructible type");
510 auto __p = any_cast<_AnyCast<_ValueType>>(&__any);
512 return static_cast<_ValueType>(std::move(*__p));
513 __throw_bad_any_cast();
517 template<typename _Tp>
518 void* __any_caster(const any* __any)
520 if constexpr (is_copy_constructible_v<decay_t<_Tp>>)
522 if (__any->_M_manager == &any::_Manager<decay_t<_Tp>>::_S_manage)
525 __any->_M_manager(any::_Op_access, __any, &__arg);
533 * @brief Access the contained object.
535 * @tparam _ValueType The type of the contained object.
536 * @param __any A pointer to the object to access.
537 * @return The address of the contained object if <code>
538 * __any != nullptr && __any.type() == typeid(_ValueType)
539 * </code>, otherwise a null pointer.
543 template<typename _ValueType>
544 inline const _ValueType* any_cast(const any* __any) noexcept
547 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
551 template<typename _ValueType>
552 inline _ValueType* any_cast(any* __any) noexcept
555 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
560 template<typename _Tp>
562 any::_Manager_internal<_Tp>::
563 _S_manage(_Op __which, const any* __any, _Arg* __arg)
565 // The contained object is in _M_storage._M_buffer
566 auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer);
570 __arg->_M_obj = const_cast<_Tp*>(__ptr);
572 case _Op_get_type_info:
574 __arg->_M_typeinfo = &typeid(_Tp);
578 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
579 __arg->_M_any->_M_manager = __any->_M_manager;
585 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp
586 (std::move(*const_cast<_Tp*>(__ptr)));
588 __arg->_M_any->_M_manager = __any->_M_manager;
589 const_cast<any*>(__any)->_M_manager = nullptr;
594 template<typename _Tp>
596 any::_Manager_external<_Tp>::
597 _S_manage(_Op __which, const any* __any, _Arg* __arg)
599 // The contained object is *_M_storage._M_ptr
600 auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr);
604 __arg->_M_obj = const_cast<_Tp*>(__ptr);
606 case _Op_get_type_info:
608 __arg->_M_typeinfo = &typeid(_Tp);
612 __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr);
613 __arg->_M_any->_M_manager = __any->_M_manager;
619 __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
620 __arg->_M_any->_M_manager = __any->_M_manager;
621 const_cast<any*>(__any)->_M_manager = nullptr;
628 _GLIBCXX_END_NAMESPACE_VERSION
633 #endif // _GLIBCXX_ANY