1 // <experimental/any> -*- C++ -*-
3 // Copyright (C) 2014-2015 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>
43 namespace std _GLIBCXX_VISIBILITY(default)
45 namespace experimental
47 inline namespace fundamentals_v1
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52 * @defgroup any Type-safe container of any type
53 * @ingroup experimental
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 [[gnu::noreturn]] inline void __throw_bad_any_cast()
83 * @brief A type-safe container of any type.
85 * An @c any object's state is either empty or it stores a contained object
86 * of CopyConstructible type.
90 // Holds either pointer to a heap object or the contained object itself.
93 // This constructor intentionally doesn't initialize anything.
96 // Prevent trivial copies of this type, buffer might hold a non-POD.
97 _Storage(const _Storage&) = delete;
98 _Storage& operator=(const _Storage&) = delete;
101 aligned_storage<sizeof(_M_ptr), alignof(void*)>::type _M_buffer;
104 template<typename _Tp, typename _Safe = is_nothrow_move_constructible<_Tp>,
105 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))
106 && (alignof(_Tp) <= alignof(_Storage))>
107 using _Internal = std::integral_constant<bool, _Safe::value && _Fits>;
109 template<typename _Tp>
110 struct _Manager_internal; // uses small-object optimization
112 template<typename _Tp>
113 struct _Manager_external; // creates contained object on the heap
115 template<typename _Tp>
116 using _Manager = conditional_t<_Internal<_Tp>::value,
117 _Manager_internal<_Tp>,
118 _Manager_external<_Tp>>;
120 template<typename _Tp, typename _Decayed = decay_t<_Tp>>
121 using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>;
124 // construct/destruct
126 /// Default constructor, creates an empty object.
127 any() noexcept : _M_manager(nullptr) { }
129 /// Copy constructor, copies the state of @p __other
130 any(const any& __other)
133 _M_manager = nullptr;
138 __other._M_manager(_Op_clone, &__other, &__arg);
143 * @brief Move constructor, transfer the state from @p __other
145 * @post @c __other.empty() (this postcondition is a GNU extension)
147 any(any&& __other) noexcept
150 _M_manager = nullptr;
155 __other._M_manager(_Op_xfer, &__other, &__arg);
159 /// Construct with a copy of @p __value as the contained object.
160 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
161 typename _Mgr = _Manager<_Tp>>
162 any(_ValueType&& __value)
163 : _M_manager(&_Mgr::_S_manage)
165 _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value));
166 static_assert(is_copy_constructible<_Tp>::value,
167 "The contained object must be CopyConstructible");
170 /// Destructor, calls @c clear()
175 /// Copy the state of another object.
176 any& operator=(const any& __rhs)
180 else if (this != &__rhs)
183 _M_manager(_Op_destroy, this, nullptr);
186 __rhs._M_manager(_Op_clone, &__rhs, &__arg);
192 * @brief Move assignment operator
194 * @post @c __rhs.empty() (not guaranteed for other implementations)
196 any& operator=(any&& __rhs) noexcept
200 else if (this != &__rhs)
203 _M_manager(_Op_destroy, this, nullptr);
206 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
211 /// Store a copy of @p __rhs as the contained object.
212 template<typename _ValueType>
213 enable_if_t<!is_same<any, decay_t<_ValueType>>::value, any&>
214 operator=(_ValueType&& __rhs)
216 *this = any(std::forward<_ValueType>(__rhs));
222 /// If not empty, destroy the contained object.
223 void clear() noexcept
227 _M_manager(_Op_destroy, this, nullptr);
228 _M_manager = nullptr;
232 /// Exchange state with another object.
233 void swap(any& __rhs) noexcept
235 if (empty() && __rhs.empty())
238 if (!empty() && !__rhs.empty())
245 __arg._M_any = &__tmp;
246 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
247 __arg._M_any = &__rhs;
248 _M_manager(_Op_xfer, this, &__arg);
250 __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
254 any* __empty = empty() ? this : &__rhs;
255 any* __full = empty() ? &__rhs : this;
257 __arg._M_any = __empty;
258 __full->_M_manager(_Op_xfer, __full, &__arg);
264 /// Reports whether there is a contained object or not.
265 bool empty() const noexcept { return _M_manager == nullptr; }
268 /// The @c typeid of the contained object, or @c typeid(void) if empty.
269 const type_info& type() const noexcept
274 _M_manager(_Op_get_type_info, this, &__arg);
275 return *__arg._M_typeinfo;
279 template<typename _Tp>
280 static constexpr bool __is_valid_cast()
281 { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
285 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
291 const std::type_info* _M_typeinfo;
295 void (*_M_manager)(_Op, const any*, _Arg*);
298 template<typename _Tp>
299 friend void* __any_caster(const any* __any)
301 if (__any->_M_manager != &_Manager<decay_t<_Tp>>::_S_manage)
304 __any->_M_manager(_Op_access, __any, &__arg);
308 // Manage in-place contained object.
309 template<typename _Tp>
310 struct _Manager_internal
313 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
315 template<typename _Up>
317 _S_create(_Storage& __storage, _Up&& __value)
319 void* __addr = &__storage._M_buffer;
320 ::new (__addr) _Tp(std::forward<_Up>(__value));
324 // Manage external contained object.
325 template<typename _Tp>
326 struct _Manager_external
329 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
331 template<typename _Up>
333 _S_create(_Storage& __storage, _Up&& __value)
335 __storage._M_ptr = new _Tp(std::forward<_Up>(__value));
340 /// Exchange the states of two @c any objects.
341 inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
344 * @brief Access the contained object.
346 * @tparam _ValueType A const-reference or CopyConstructible type.
347 * @param __any The object to access.
348 * @return The contained object.
349 * @throw bad_any_cast If <code>
350 * __any.type() != typeid(remove_reference_t<_ValueType>)
353 template<typename _ValueType>
354 inline _ValueType any_cast(const any& __any)
356 static_assert(any::__is_valid_cast<_ValueType>(),
357 "Template argument must be a reference or CopyConstructible type");
358 auto __p = any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
361 __throw_bad_any_cast();
365 * @brief Access the contained object.
367 * @tparam _ValueType A reference or CopyConstructible type.
368 * @param __any The object to access.
369 * @return The contained object.
370 * @throw bad_any_cast If <code>
371 * __any.type() != typeid(remove_reference_t<_ValueType>)
376 template<typename _ValueType>
377 inline _ValueType any_cast(any& __any)
379 static_assert(any::__is_valid_cast<_ValueType>(),
380 "Template argument must be a reference or CopyConstructible type");
381 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
384 __throw_bad_any_cast();
387 template<typename _ValueType>
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();
400 * @brief Access the contained object.
402 * @tparam _ValueType The type of the contained object.
403 * @param __any A pointer to the object to access.
404 * @return The address of the contained object if <code>
405 * __any != nullptr && __any.type() == typeid(_ValueType)
406 * </code>, otherwise a null pointer.
410 template<typename _ValueType>
411 inline const _ValueType* any_cast(const any* __any) noexcept
414 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
418 template<typename _ValueType>
419 inline _ValueType* any_cast(any* __any) noexcept
422 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
427 template<typename _Tp>
429 any::_Manager_internal<_Tp>::
430 _S_manage(_Op __which, const any* __any, _Arg* __arg)
432 // The contained object is in _M_storage._M_buffer
433 auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer);
437 __arg->_M_obj = const_cast<_Tp*>(__ptr);
439 case _Op_get_type_info:
441 __arg->_M_typeinfo = &typeid(_Tp);
445 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
446 __arg->_M_any->_M_manager = __any->_M_manager;
452 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
454 __arg->_M_any->_M_manager = __any->_M_manager;
455 const_cast<any*>(__any)->_M_manager = nullptr;
460 template<typename _Tp>
462 any::_Manager_external<_Tp>::
463 _S_manage(_Op __which, const any* __any, _Arg* __arg)
465 // The contained object is *_M_storage._M_ptr
466 auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr);
470 __arg->_M_obj = const_cast<_Tp*>(__ptr);
472 case _Op_get_type_info:
474 __arg->_M_typeinfo = &typeid(_Tp);
478 __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr);
479 __arg->_M_any->_M_manager = __any->_M_manager;
485 __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
486 __arg->_M_any->_M_manager = __any->_M_manager;
487 const_cast<any*>(__any)->_M_manager = nullptr;
493 _GLIBCXX_END_NAMESPACE_VERSION
494 } // namespace fundamentals_v1
495 } // namespace experimental
500 #endif // _GLIBCXX_EXPERIMENTAL_ANY