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 // Manage in-place contained object.
242 template<typename _Tp>
243 struct _Manager_internal
246 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
248 template<typename _Up>
250 _S_create(_Up&& __value)
253 void* __addr = &__storage._M_buffer;
254 ::new (__addr) _Tp(std::forward<_Up>(__value));
258 template<typename _Alloc, typename _Up>
260 _S_alloc(const _Alloc&, _Up&& __value)
262 return _S_create(std::forward<_Up>(__value));
266 // Manage external contained object.
267 template<typename _Tp>
268 struct _Manager_external
271 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
273 template<typename _Up>
275 _S_create(_Up&& __value)
278 __storage._M_ptr = new _Tp(std::forward<_Up>(__value));
284 /// Exchange the states of two @c any objects.
285 inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
288 * @brief Access the contained object.
290 * @tparam _ValueType A const-reference or CopyConstructible type.
291 * @param __any The object to access.
292 * @return The contained object.
293 * @throw bad_any_cast If <code>
294 * __any.type() != typeid(remove_reference_t<_ValueType>)
297 template<typename _ValueType>
298 inline _ValueType any_cast(const any& __any)
300 static_assert(any::__is_valid_cast<_ValueType>(),
301 "Template argument must be a reference or CopyConstructible type");
302 auto __p = any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
305 __throw_bad_any_cast();
309 * @brief Access the contained object.
311 * @tparam _ValueType A reference or CopyConstructible type.
312 * @param __any The object to access.
313 * @return The contained object.
314 * @throw bad_any_cast If <code>
315 * __any.type() != typeid(remove_reference_t<_ValueType>)
320 template<typename _ValueType>
321 inline _ValueType any_cast(any& __any)
323 static_assert(any::__is_valid_cast<_ValueType>(),
324 "Template argument must be a reference or CopyConstructible type");
325 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
328 __throw_bad_any_cast();
331 template<typename _ValueType>
332 inline _ValueType any_cast(any&& __any)
334 static_assert(any::__is_valid_cast<_ValueType>(),
335 "Template argument must be a reference or CopyConstructible type");
336 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
339 __throw_bad_any_cast();
343 template<typename _Tp>
344 void* __any_caster(const any* __any)
347 using _Up = decay_t<_Tp>;
348 using _Vp = conditional_t<is_copy_constructible<_Up>::value, _Up, _None>;
349 if (__any->_M_manager != &any::_Manager<_Vp>::_S_manage)
352 __any->_M_manager(any::_Op_access, __any, &__arg);
357 * @brief Access the contained object.
359 * @tparam _ValueType The type of the contained object.
360 * @param __any A pointer to the object to access.
361 * @return The address of the contained object if <code>
362 * __any != nullptr && __any.type() == typeid(_ValueType)
363 * </code>, otherwise a null pointer.
367 template<typename _ValueType>
368 inline const _ValueType* any_cast(const any* __any) noexcept
371 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
375 template<typename _ValueType>
376 inline _ValueType* any_cast(any* __any) noexcept
379 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
384 template<typename _Tp>
386 any::_Manager_internal<_Tp>::
387 _S_manage(_Op __which, const any* __any, _Arg* __arg)
389 // The contained object is in _M_storage._M_buffer
390 auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer);
394 __arg->_M_obj = const_cast<_Tp*>(__ptr);
396 case _Op_get_type_info:
398 __arg->_M_typeinfo = &typeid(_Tp);
402 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
410 template<typename _Tp>
412 any::_Manager_external<_Tp>::
413 _S_manage(_Op __which, const any* __any, _Arg* __arg)
415 // The contained object is *_M_storage._M_ptr
416 auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr);
420 __arg->_M_obj = const_cast<_Tp*>(__ptr);
422 case _Op_get_type_info:
424 __arg->_M_typeinfo = &typeid(_Tp);
428 __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr);
437 _GLIBCXX_END_NAMESPACE_VERSION
438 } // namespace fundamentals_v1
439 } // namespace experimental
444 #endif // _GLIBCXX_EXPERIMENTAL_ANY