]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/any
Implement std::any.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / any
1 // <any> -*- C++ -*-
2
3 // Copyright (C) 2014-2016 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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.
19
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/>.
24
25 /** @file include/any
26 * This is a Standard C++ Library header.
27 */
28
29 #ifndef _GLIBCXX_ANY
30 #define _GLIBCXX_ANY 1
31
32 #pragma GCC system_header
33
34 #if __cplusplus <= 201402L
35 # include <bits/c++17_warning.h>
36 #else
37
38 #include <typeinfo>
39 #include <new>
40 #include <utility>
41 #include <type_traits>
42
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47 /**
48 * @addtogroup utilities
49 * @{
50 */
51
52 /**
53 * @brief Exception class thrown by a failed @c any_cast
54 * @ingroup exceptions
55 */
56 class bad_any_cast : public bad_cast
57 {
58 public:
59 virtual const char* what() const noexcept { return "bad any_cast"; }
60 };
61
62 [[gnu::noreturn]] inline void __throw_bad_any_cast()
63 {
64 #if __cpp_exceptions
65 throw bad_any_cast{};
66 #else
67 __builtin_abort();
68 #endif
69 }
70
71 /**
72 * @brief A type-safe container of any type.
73 *
74 * An @c any object's state is either empty or it stores a contained object
75 * of CopyConstructible type.
76 */
77 class any
78 {
79 // Holds either pointer to a heap object or the contained object itself.
80 union _Storage
81 {
82 // This constructor intentionally doesn't initialize anything.
83 _Storage() = default;
84
85 // Prevent trivial copies of this type, buffer might hold a non-POD.
86 _Storage(const _Storage&) = delete;
87 _Storage& operator=(const _Storage&) = delete;
88
89 void* _M_ptr;
90 aligned_storage<sizeof(_M_ptr), alignof(void*)>::type _M_buffer;
91 };
92
93 template<typename _Tp, typename _Safe = is_nothrow_move_constructible<_Tp>,
94 bool _Fits = (sizeof(_Tp) <= sizeof(_Storage))
95 && (alignof(_Tp) <= alignof(_Storage))>
96 using _Internal = std::integral_constant<bool, _Safe::value && _Fits>;
97
98 template<typename _Tp>
99 struct _Manager_internal; // uses small-object optimization
100
101 template<typename _Tp>
102 struct _Manager_external; // creates contained object on the heap
103
104 template<typename _Tp>
105 using _Manager = conditional_t<_Internal<_Tp>::value,
106 _Manager_internal<_Tp>,
107 _Manager_external<_Tp>>;
108
109 template<typename _Tp, typename _Decayed = decay_t<_Tp>>
110 using _Decay = enable_if_t<!is_same<_Decayed, any>::value, _Decayed>;
111
112 public:
113 // construct/destruct
114
115 /// Default constructor, creates an empty object.
116 any() noexcept : _M_manager(nullptr) { }
117
118 /// Copy constructor, copies the state of @p __other
119 any(const any& __other)
120 {
121 if (__other.empty())
122 _M_manager = nullptr;
123 else
124 {
125 _Arg __arg;
126 __arg._M_any = this;
127 __other._M_manager(_Op_clone, &__other, &__arg);
128 }
129 }
130
131 /**
132 * @brief Move constructor, transfer the state from @p __other
133 *
134 * @post @c __other.empty() (this postcondition is a GNU extension)
135 */
136 any(any&& __other) noexcept
137 {
138 if (__other.empty())
139 _M_manager = nullptr;
140 else
141 {
142 _Arg __arg;
143 __arg._M_any = this;
144 __other._M_manager(_Op_xfer, &__other, &__arg);
145 }
146 }
147
148 /// Construct with a copy of @p __value as the contained object.
149 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
150 typename _Mgr = _Manager<_Tp>,
151 typename enable_if<is_constructible<_Tp, _ValueType&&>::value,
152 bool>::type = true>
153 any(_ValueType&& __value)
154 : _M_manager(&_Mgr::_S_manage)
155 {
156 _Mgr::_S_create(_M_storage, std::forward<_ValueType>(__value));
157 static_assert(is_copy_constructible<_Tp>::value,
158 "The contained object must be CopyConstructible");
159 }
160
161 /// Construct with a copy of @p __value as the contained object.
162 template <typename _ValueType, typename _Tp = _Decay<_ValueType>,
163 typename _Mgr = _Manager<_Tp>,
164 typename enable_if<!is_constructible<_Tp, _ValueType&&>::value,
165 bool>::type = false>
166 any(_ValueType&& __value)
167 : _M_manager(&_Mgr::_S_manage)
168 {
169 _Mgr::_S_create(_M_storage, __value);
170 static_assert(is_copy_constructible<_Tp>::value,
171 "The contained object must be CopyConstructible");
172 }
173
174 /// Destructor, calls @c clear()
175 ~any() { clear(); }
176
177 // assignments
178
179 /// Copy the state of another object.
180 any& operator=(const any& __rhs)
181 {
182 if (__rhs.empty())
183 clear();
184 else if (this != &__rhs)
185 {
186 if (!empty())
187 _M_manager(_Op_destroy, this, nullptr);
188 _Arg __arg;
189 __arg._M_any = this;
190 __rhs._M_manager(_Op_clone, &__rhs, &__arg);
191 }
192 return *this;
193 }
194
195 /**
196 * @brief Move assignment operator
197 *
198 * @post @c __rhs.empty() (not guaranteed for other implementations)
199 */
200 any& operator=(any&& __rhs) noexcept
201 {
202 if (__rhs.empty())
203 clear();
204 else if (this != &__rhs)
205 {
206 if (!empty())
207 _M_manager(_Op_destroy, this, nullptr);
208 _Arg __arg;
209 __arg._M_any = this;
210 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
211 }
212 return *this;
213 }
214
215 /// Store a copy of @p __rhs as the contained object.
216 template<typename _ValueType>
217 enable_if_t<!is_same<any, decay_t<_ValueType>>::value, any&>
218 operator=(_ValueType&& __rhs)
219 {
220 *this = any(std::forward<_ValueType>(__rhs));
221 return *this;
222 }
223
224 // modifiers
225
226 /// If not empty, destroy the contained object.
227 void clear() noexcept
228 {
229 if (!empty())
230 {
231 _M_manager(_Op_destroy, this, nullptr);
232 _M_manager = nullptr;
233 }
234 }
235
236 /// Exchange state with another object.
237 void swap(any& __rhs) noexcept
238 {
239 if (empty() && __rhs.empty())
240 return;
241
242 if (!empty() && !__rhs.empty())
243 {
244 if (this == &__rhs)
245 return;
246
247 any __tmp;
248 _Arg __arg;
249 __arg._M_any = &__tmp;
250 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
251 __arg._M_any = &__rhs;
252 _M_manager(_Op_xfer, this, &__arg);
253 __arg._M_any = this;
254 __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
255 }
256 else
257 {
258 any* __empty = empty() ? this : &__rhs;
259 any* __full = empty() ? &__rhs : this;
260 _Arg __arg;
261 __arg._M_any = __empty;
262 __full->_M_manager(_Op_xfer, __full, &__arg);
263 }
264 }
265
266 // observers
267
268 /// Reports whether there is a contained object or not.
269 bool empty() const noexcept { return _M_manager == nullptr; }
270
271 #if __cpp_rtti
272 /// The @c typeid of the contained object, or @c typeid(void) if empty.
273 const type_info& type() const noexcept
274 {
275 if (empty())
276 return typeid(void);
277 _Arg __arg;
278 _M_manager(_Op_get_type_info, this, &__arg);
279 return *__arg._M_typeinfo;
280 }
281 #endif
282
283 template<typename _Tp>
284 static constexpr bool __is_valid_cast()
285 { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
286
287 private:
288 enum _Op {
289 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
290 };
291
292 union _Arg
293 {
294 void* _M_obj;
295 const std::type_info* _M_typeinfo;
296 any* _M_any;
297 };
298
299 void (*_M_manager)(_Op, const any*, _Arg*);
300 _Storage _M_storage;
301
302 template<typename _Tp>
303 friend void* __any_caster(const any* __any);
304
305 // Manage in-place contained object.
306 template<typename _Tp>
307 struct _Manager_internal
308 {
309 static void
310 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
311
312 template<typename _Up>
313 static void
314 _S_create(_Storage& __storage, _Up&& __value)
315 {
316 void* __addr = &__storage._M_buffer;
317 ::new (__addr) _Tp(std::forward<_Up>(__value));
318 }
319 };
320
321 // Manage external contained object.
322 template<typename _Tp>
323 struct _Manager_external
324 {
325 static void
326 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
327
328 template<typename _Up>
329 static void
330 _S_create(_Storage& __storage, _Up&& __value)
331 {
332 __storage._M_ptr = new _Tp(std::forward<_Up>(__value));
333 }
334 };
335 };
336
337 /// Exchange the states of two @c any objects.
338 inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
339
340 /**
341 * @brief Access the contained object.
342 *
343 * @tparam _ValueType A const-reference or CopyConstructible type.
344 * @param __any The object to access.
345 * @return The contained object.
346 * @throw bad_any_cast If <code>
347 * __any.type() != typeid(remove_reference_t<_ValueType>)
348 * </code>
349 */
350 template<typename _ValueType>
351 inline _ValueType any_cast(const any& __any)
352 {
353 static_assert(any::__is_valid_cast<_ValueType>(),
354 "Template argument must be a reference or CopyConstructible type");
355 auto __p = any_cast<add_const_t<remove_reference_t<_ValueType>>>(&__any);
356 if (__p)
357 return *__p;
358 __throw_bad_any_cast();
359 }
360
361 /**
362 * @brief Access the contained object.
363 *
364 * @tparam _ValueType A reference or CopyConstructible type.
365 * @param __any The object to access.
366 * @return The contained object.
367 * @throw bad_any_cast If <code>
368 * __any.type() != typeid(remove_reference_t<_ValueType>)
369 * </code>
370 *
371 * @{
372 */
373 template<typename _ValueType>
374 inline _ValueType any_cast(any& __any)
375 {
376 static_assert(any::__is_valid_cast<_ValueType>(),
377 "Template argument must be a reference or CopyConstructible type");
378 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
379 if (__p)
380 return *__p;
381 __throw_bad_any_cast();
382 }
383
384 template<typename _ValueType,
385 typename enable_if<!is_move_constructible<_ValueType>::value
386 || is_lvalue_reference<_ValueType>::value,
387 bool>::type = true>
388 inline _ValueType any_cast(any&& __any)
389 {
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);
393 if (__p)
394 return *__p;
395 __throw_bad_any_cast();
396 }
397
398 template<typename _ValueType,
399 typename enable_if<is_move_constructible<_ValueType>::value
400 && !is_lvalue_reference<_ValueType>::value,
401 bool>::type = false>
402 inline _ValueType any_cast(any&& __any)
403 {
404 static_assert(any::__is_valid_cast<_ValueType>(),
405 "Template argument must be a reference or CopyConstructible type");
406 auto __p = any_cast<remove_reference_t<_ValueType>>(&__any);
407 if (__p)
408 return std::move(*__p);
409 __throw_bad_any_cast();
410 }
411 // @}
412
413 template<typename _Tp>
414 void* __any_caster(const any* __any)
415 {
416 if (__any->_M_manager != &any::_Manager<decay_t<_Tp>>::_S_manage)
417 return nullptr;
418 any::_Arg __arg;
419 __any->_M_manager(any::_Op_access, __any, &__arg);
420 return __arg._M_obj;
421 }
422
423 /**
424 * @brief Access the contained object.
425 *
426 * @tparam _ValueType The type of the contained object.
427 * @param __any A pointer to the object to access.
428 * @return The address of the contained object if <code>
429 * __any != nullptr && __any.type() == typeid(_ValueType)
430 * </code>, otherwise a null pointer.
431 *
432 * @{
433 */
434 template<typename _ValueType>
435 inline const _ValueType* any_cast(const any* __any) noexcept
436 {
437 if (__any)
438 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
439 return nullptr;
440 }
441
442 template<typename _ValueType>
443 inline _ValueType* any_cast(any* __any) noexcept
444 {
445 if (__any)
446 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
447 return nullptr;
448 }
449 // @}
450
451 template<typename _Tp>
452 void
453 any::_Manager_internal<_Tp>::
454 _S_manage(_Op __which, const any* __any, _Arg* __arg)
455 {
456 // The contained object is in _M_storage._M_buffer
457 auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer);
458 switch (__which)
459 {
460 case _Op_access:
461 __arg->_M_obj = const_cast<_Tp*>(__ptr);
462 break;
463 case _Op_get_type_info:
464 #if __cpp_rtti
465 __arg->_M_typeinfo = &typeid(_Tp);
466 #endif
467 break;
468 case _Op_clone:
469 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
470 __arg->_M_any->_M_manager = __any->_M_manager;
471 break;
472 case _Op_destroy:
473 __ptr->~_Tp();
474 break;
475 case _Op_xfer:
476 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
477 __ptr->~_Tp();
478 __arg->_M_any->_M_manager = __any->_M_manager;
479 const_cast<any*>(__any)->_M_manager = nullptr;
480 break;
481 }
482 }
483
484 template<typename _Tp>
485 void
486 any::_Manager_external<_Tp>::
487 _S_manage(_Op __which, const any* __any, _Arg* __arg)
488 {
489 // The contained object is *_M_storage._M_ptr
490 auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr);
491 switch (__which)
492 {
493 case _Op_access:
494 __arg->_M_obj = const_cast<_Tp*>(__ptr);
495 break;
496 case _Op_get_type_info:
497 #if __cpp_rtti
498 __arg->_M_typeinfo = &typeid(_Tp);
499 #endif
500 break;
501 case _Op_clone:
502 __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr);
503 __arg->_M_any->_M_manager = __any->_M_manager;
504 break;
505 case _Op_destroy:
506 delete __ptr;
507 break;
508 case _Op_xfer:
509 __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
510 __arg->_M_any->_M_manager = __any->_M_manager;
511 const_cast<any*>(__any)->_M_manager = nullptr;
512 break;
513 }
514 }
515
516 /// @}
517
518 _GLIBCXX_END_NAMESPACE_VERSION
519 } // namespace std
520
521 #endif // C++14
522
523 #endif // _GLIBCXX_ANY