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>
42 #include <experimental/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.
95 std::aligned_storage<sizeof(_M_ptr), sizeof(_M_ptr)>::type _M_buffer;
98 template<typename _Tp, typename _Safe = is_trivially_copyable<_Tp>,
99 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))>
100 using _Internal = std::integral_constant<bool, _Safe::value && _Fits>;
102 template<typename _Tp>
103 struct _Manager_internal; // uses small-object optimization
105 template<typename _Tp>
106 struct _Manager_external; // creates contained object on the heap
108 template<typename _Tp>
109 using _Manager = conditional_t<_Internal<_Tp>::value,
110 _Manager_internal<_Tp>,
111 _Manager_external<_Tp>>;
113 template<typename _Tp, typename _Decayed = decay_t<_Tp>>
114 using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>;
117 // construct/destruct
119 /// Default constructor, creates an empty object.
120 any() noexcept : _M_manager(nullptr) { }
122 /// Copy constructor, copies the state of @p __other
123 any(const any& __other) : _M_manager(__other._M_manager)
125 if (!__other.empty())
129 _M_manager(_Op_clone, &__other, &__arg);
134 * @brief Move constructor, transfer the state from @p __other
136 * @post @c __other.empty() (not guaranteed for other implementations)
138 any(any&& __other) noexcept
139 : _M_manager(__other._M_manager),
140 _M_storage(__other._M_storage)
141 { __other._M_manager = nullptr; }
143 /// Construct with a copy of @p __value as the contained object.
144 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
145 typename _Mgr = _Manager<_Tp>>
146 any(_ValueType&& __value)
147 : _M_manager(&_Mgr::_S_manage),
148 _M_storage(_Mgr::_S_create(std::forward<_ValueType>(__value)))
150 static_assert(is_copy_constructible<_Tp>::value,
151 "The contained object must be CopyConstructible");
154 /// Destructor, calls @c clear()
159 /// Copy the state of
160 any& operator=(const any& __rhs)
162 any(__rhs).swap(*this);
167 * @brief Move assignment operator
169 * @post @c __rhs.empty() (not guaranteed for other implementations)
171 any& operator=(any&& __rhs) noexcept
173 any(std::move(__rhs)).swap(*this);
177 /// Store a copy of @p __rhs as the contained object.
178 template<typename _ValueType>
179 any& operator=(_ValueType&& __rhs)
181 any(std::forward<_ValueType>(__rhs)).swap(*this);
187 /// If not empty, destroy the contained object.
188 void clear() noexcept
192 _M_manager(_Op_destroy, this, nullptr);
193 _M_manager = nullptr;
197 /// Exchange state with another object.
198 void swap(any& __rhs) noexcept
200 std::swap(_M_manager, __rhs._M_manager);
201 std::swap(_M_storage, __rhs._M_storage);
206 /// Reports whether there is a contained object or not.
207 bool empty() const noexcept { return _M_manager == nullptr; }
210 /// The @c typeid of the contained object, or @c typeid(void) if empty.
211 const type_info& type() const noexcept
216 _M_manager(_Op_get_type_info, this, &__arg);
217 return *__arg._M_typeinfo;
221 template<typename _Tp>
222 static constexpr bool __is_valid_cast()
223 { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
226 enum _Op { _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy };
231 const std::type_info* _M_typeinfo;
235 void (*_M_manager)(_Op, const any*, _Arg*);
238 template<typename _Tp>
239 friend void* __any_caster(const any* __any)
241 if (__any->_M_manager != &_Manager<decay_t<_Tp>>::_S_manage)
244 __any->_M_manager(_Op_access, __any, &__arg);
248 // Manage in-place contained object.
249 template<typename _Tp>
250 struct _Manager_internal
253 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
255 template<typename _Up>
257 _S_create(_Up&& __value)
260 void* __addr = &__storage._M_buffer;
261 ::new (__addr) _Tp(std::forward<_Up>(__value));
265 template<typename _Alloc, typename _Up>
267 _S_alloc(const _Alloc&, _Up&& __value)
269 return _S_create(std::forward<_Up>(__value));
273 // Manage external contained object.
274 template<typename _Tp>
275 struct _Manager_external
278 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
280 template<typename _Up>
282 _S_create(_Up&& __value)
285 __storage._M_ptr = new _Tp(std::forward<_Up>(__value));
291 /// Exchange the states of two @c any objects.
292 inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
295 * @brief Access the contained object.
297 * @tparam _ValueType A const-reference or CopyConstructible type.
298 * @param __any The object to access.
299 * @return The contained object.
300 * @throw bad_any_cast If <code>
301 * __any.type() != typeid(remove_reference_t<_ValueType>)
304 template<typename _ValueType>
305 inline _ValueType any_cast(const any& __any)
307 static_assert(any::__is_valid_cast<_ValueType>(),
308 "Template argument must be a reference or CopyConstructible type");
309 auto __p = any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
312 __throw_bad_any_cast();
316 * @brief Access the contained object.
318 * @tparam _ValueType A reference or CopyConstructible type.
319 * @param __any The object to access.
320 * @return The contained object.
321 * @throw bad_any_cast If <code>
322 * __any.type() != typeid(remove_reference_t<_ValueType>)
327 template<typename _ValueType>
328 inline _ValueType any_cast(any& __any)
330 static_assert(any::__is_valid_cast<_ValueType>(),
331 "Template argument must be a reference or CopyConstructible type");
332 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
335 __throw_bad_any_cast();
338 template<typename _ValueType>
339 inline _ValueType any_cast(any&& __any)
341 static_assert(any::__is_valid_cast<_ValueType>(),
342 "Template argument must be a reference or CopyConstructible type");
343 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
346 __throw_bad_any_cast();
351 * @brief Access the contained object.
353 * @tparam _ValueType The type of the contained object.
354 * @param __any A pointer to the object to access.
355 * @return The address of the contained object if <code>
356 * __any != nullptr && __any.type() == typeid(_ValueType)
357 * </code>, otherwise a null pointer.
361 template<typename _ValueType>
362 inline const _ValueType* any_cast(const any* __any) noexcept
365 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
369 template<typename _ValueType>
370 inline _ValueType* any_cast(any* __any) noexcept
373 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
378 template<typename _Tp>
380 any::_Manager_internal<_Tp>::
381 _S_manage(_Op __which, const any* __any, _Arg* __arg)
383 // The contained object is in _M_storage._M_buffer
384 auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer);
388 __arg->_M_obj = const_cast<_Tp*>(__ptr);
390 case _Op_get_type_info:
392 __arg->_M_typeinfo = &typeid(_Tp);
396 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
404 template<typename _Tp>
406 any::_Manager_external<_Tp>::
407 _S_manage(_Op __which, const any* __any, _Arg* __arg)
409 // The contained object is *_M_storage._M_ptr
410 auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr);
414 __arg->_M_obj = const_cast<_Tp*>(__ptr);
416 case _Op_get_type_info:
418 __arg->_M_typeinfo = &typeid(_Tp);
422 __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr);
431 _GLIBCXX_END_NAMESPACE_VERSION
432 } // namespace fundamentals_v1
433 } // namespace experimental
438 #endif // _GLIBCXX_EXPERIMENTAL_ANY