1 // <experimental/any> -*- C++ -*-
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/>.
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 <= 201103L
35 # include <bits/c++14_warning.h>
41 #include <type_traits>
42 #include <experimental/bits/lfts_config.h>
44 namespace std _GLIBCXX_VISIBILITY(default)
46 namespace experimental
48 inline namespace fundamentals_v1
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 * @defgroup any Type-safe container of any type
54 * @ingroup experimental
56 * A type-safe container for single values of value types, as
57 * described in n3804 "Any Library Proposal (Revision 3)".
62 #define __cpp_lib_experimental_any 201411
65 * @brief Exception class thrown by a failed @c any_cast
68 class bad_any_cast : public bad_cast
71 virtual const char* what() const noexcept { return "bad any_cast"; }
74 [[gnu::noreturn]] inline void __throw_bad_any_cast()
84 * @brief A type-safe container of any type.
86 * An @c any object's state is either empty or it stores a contained object
87 * of CopyConstructible type.
91 // Holds either pointer to a heap object or the contained object itself.
94 // This constructor intentionally doesn't initialize anything.
97 // Prevent trivial copies of this type, buffer might hold a non-POD.
98 _Storage(const _Storage&) = delete;
99 _Storage& operator=(const _Storage&) = delete;
102 aligned_storage<sizeof(_M_ptr), alignof(void*)>::type _M_buffer;
105 template<typename _Tp, typename _Safe = is_nothrow_move_constructible<_Tp>,
106 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))
107 && (alignof(_Tp) <= alignof(_Storage))>
108 using _Internal = std::integral_constant<bool, _Safe::value && _Fits>;
110 template<typename _Tp>
111 struct _Manager_internal; // uses small-object optimization
113 template<typename _Tp>
114 struct _Manager_external; // creates contained object on the heap
116 template<typename _Tp>
117 using _Manager = conditional_t<_Internal<_Tp>::value,
118 _Manager_internal<_Tp>,
119 _Manager_external<_Tp>>;
121 template<typename _Tp, typename _Decayed = decay_t<_Tp>>
122 using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>;
125 // construct/destruct
127 /// Default constructor, creates an empty object.
128 any() noexcept : _M_manager(nullptr) { }
130 /// Copy constructor, copies the state of @p __other
131 any(const any& __other)
134 _M_manager = nullptr;
139 __other._M_manager(_Op_clone, &__other, &__arg);
144 * @brief Move constructor, transfer the state from @p __other
146 * @post @c __other.empty() (this postcondition is a GNU extension)
148 any(any&& __other) noexcept
151 _M_manager = nullptr;
156 __other._M_manager(_Op_xfer, &__other, &__arg);
160 /// Construct with a copy of @p __value as the contained object.
161 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
162 typename _Mgr = _Manager<_Tp>,
163 typename enable_if<is_constructible<_Tp, _ValueType&&>::value,
165 any(_ValueType&& __value)
166 : _M_manager(&_Mgr::_S_manage)
168 _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value));
169 static_assert(is_copy_constructible<_Tp>::value,
170 "The contained object must be CopyConstructible");
173 /// Construct with a copy of @p __value as the contained object.
174 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
175 typename _Mgr = _Manager<_Tp>,
176 typename enable_if<!is_constructible<_Tp, _ValueType&&>::value,
178 any(_ValueType&& __value)
179 : _M_manager(&_Mgr::_S_manage)
181 _Mgr::_S_create(_M_storage, __value);
182 static_assert(is_copy_constructible<_Tp>::value,
183 "The contained object must be CopyConstructible");
186 /// Destructor, calls @c clear()
191 /// Copy the state of another object.
192 any& operator=(const any& __rhs)
199 * @brief Move assignment operator
201 * @post @c __rhs.empty() (not guaranteed for other implementations)
203 any& operator=(any&& __rhs) noexcept
207 else if (this != &__rhs)
212 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
217 /// Store a copy of @p __rhs as the contained object.
218 template<typename _ValueType>
219 enable_if_t<!is_same<any, decay_t<_ValueType>>::value, any&>
220 operator=(_ValueType&& __rhs)
222 *this = any(std::forward<_ValueType>(__rhs));
228 /// If not empty, destroy the contained object.
229 void clear() noexcept
233 _M_manager(_Op_destroy, this, nullptr);
234 _M_manager = nullptr;
238 /// Exchange state with another object.
239 void swap(any& __rhs) noexcept
241 if (empty() && __rhs.empty())
244 if (!empty() && !__rhs.empty())
251 __arg._M_any = &__tmp;
252 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
253 __arg._M_any = &__rhs;
254 _M_manager(_Op_xfer, this, &__arg);
256 __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
260 any* __empty = empty() ? this : &__rhs;
261 any* __full = empty() ? &__rhs : this;
263 __arg._M_any = __empty;
264 __full->_M_manager(_Op_xfer, __full, &__arg);
270 /// Reports whether there is a contained object or not.
271 bool empty() const noexcept { return _M_manager == nullptr; }
274 /// The @c typeid of the contained object, or @c typeid(void) if empty.
275 const type_info& type() const noexcept
280 _M_manager(_Op_get_type_info, this, &__arg);
281 return *__arg._M_typeinfo;
285 template<typename _Tp>
286 static constexpr bool __is_valid_cast()
287 { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
291 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
297 const std::type_info* _M_typeinfo;
301 void (*_M_manager)(_Op, const any*, _Arg*);
304 template<typename _Tp>
305 friend void* __any_caster(const any* __any);
307 // Manage in-place contained object.
308 template<typename _Tp>
309 struct _Manager_internal
312 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
314 template<typename _Up>
316 _S_create(_Storage& __storage, _Up&& __value)
318 void* __addr = &__storage._M_buffer;
319 ::new (__addr) _Tp(std::forward<_Up>(__value));
323 // Manage external contained object.
324 template<typename _Tp>
325 struct _Manager_external
328 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
330 template<typename _Up>
332 _S_create(_Storage& __storage, _Up&& __value)
334 __storage._M_ptr = new _Tp(std::forward<_Up>(__value));
339 /// Exchange the states of two @c any objects.
340 inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
343 * @brief Access the contained object.
345 * @tparam _ValueType A const-reference or CopyConstructible type.
346 * @param __any The object to access.
347 * @return The contained object.
348 * @throw bad_any_cast If <code>
349 * __any.type() != typeid(remove_reference_t<_ValueType>)
352 template<typename _ValueType>
353 inline _ValueType any_cast(const any& __any)
355 static_assert(any::__is_valid_cast<_ValueType>(),
356 "Template argument must be a reference or CopyConstructible type");
357 auto __p = any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
360 __throw_bad_any_cast();
364 * @brief Access the contained object.
366 * @tparam _ValueType A reference or CopyConstructible type.
367 * @param __any The object to access.
368 * @return The contained object.
369 * @throw bad_any_cast If <code>
370 * __any.type() != typeid(remove_reference_t<_ValueType>)
375 template<typename _ValueType>
376 inline _ValueType any_cast(any& __any)
378 static_assert(any::__is_valid_cast<_ValueType>(),
379 "Template argument must be a reference or CopyConstructible type");
380 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
383 __throw_bad_any_cast();
386 template<typename _ValueType,
387 typename enable_if<!is_move_constructible<_ValueType>::value
388 || is_lvalue_reference<_ValueType>::value,
390 inline _ValueType any_cast(any&& __any)
392 static_assert(any::__is_valid_cast<_ValueType>(),
393 "Template argument must be a reference or CopyConstructible type");
394 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
397 __throw_bad_any_cast();
400 template<typename _ValueType,
401 typename enable_if<is_move_constructible<_ValueType>::value
402 && !is_lvalue_reference<_ValueType>::value,
404 inline _ValueType any_cast(any&& __any)
406 static_assert(any::__is_valid_cast<_ValueType>(),
407 "Template argument must be a reference or CopyConstructible type");
408 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
410 return std::move(*__p);
411 __throw_bad_any_cast();
415 template<typename _Tp>
416 void* __any_caster(const any* __any)
419 using _Up = decay_t<_Tp>;
420 using _Vp = conditional_t<is_copy_constructible<_Up>::value, _Up, _None>;
421 if (__any->_M_manager != &any::_Manager<_Vp>::_S_manage)
424 __any->_M_manager(any::_Op_access, __any, &__arg);
429 * @brief Access the contained object.
431 * @tparam _ValueType The type of the contained object.
432 * @param __any A pointer to the object to access.
433 * @return The address of the contained object if <code>
434 * __any != nullptr && __any.type() == typeid(_ValueType)
435 * </code>, otherwise a null pointer.
439 template<typename _ValueType>
440 inline const _ValueType* any_cast(const any* __any) noexcept
443 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
447 template<typename _ValueType>
448 inline _ValueType* any_cast(any* __any) noexcept
451 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
456 template<typename _Tp>
458 any::_Manager_internal<_Tp>::
459 _S_manage(_Op __which, const any* __any, _Arg* __arg)
461 // The contained object is in _M_storage._M_buffer
462 auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer);
466 __arg->_M_obj = const_cast<_Tp*>(__ptr);
468 case _Op_get_type_info:
470 __arg->_M_typeinfo = &typeid(_Tp);
474 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
475 __arg->_M_any->_M_manager = __any->_M_manager;
481 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp
482 (std::move(*const_cast<_Tp*>(__ptr)));
484 __arg->_M_any->_M_manager = __any->_M_manager;
485 const_cast<any*>(__any)->_M_manager = nullptr;
490 template<typename _Tp>
492 any::_Manager_external<_Tp>::
493 _S_manage(_Op __which, const any* __any, _Arg* __arg)
495 // The contained object is *_M_storage._M_ptr
496 auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr);
500 __arg->_M_obj = const_cast<_Tp*>(__ptr);
502 case _Op_get_type_info:
504 __arg->_M_typeinfo = &typeid(_Tp);
508 __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr);
509 __arg->_M_any->_M_manager = __any->_M_manager;
515 __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
516 __arg->_M_any->_M_manager = __any->_M_manager;
517 const_cast<any*>(__any)->_M_manager = nullptr;
523 _GLIBCXX_END_NAMESPACE_VERSION
524 } // namespace fundamentals_v1
525 } // namespace experimental
530 #endif // _GLIBCXX_EXPERIMENTAL_ANY