]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/std/any
libstdc++: Add std::__conditional_t alias template
[thirdparty/gcc.git] / libstdc++-v3 / include / std / any
CommitLineData
52e86221
VV
1// <any> -*- C++ -*-
2
99dee823 3// Copyright (C) 2014-2021 Free Software Foundation, Inc.
52e86221
VV
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
c6888c62 34#if __cplusplus >= 201703L
52e86221 35
261d5a4a 36#include <initializer_list>
52e86221
VV
37#include <typeinfo>
38#include <new>
52e86221 39#include <type_traits>
261d5a4a 40#include <bits/utility.h> // in_place_type_t
52e86221
VV
41
42namespace std _GLIBCXX_VISIBILITY(default)
43{
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
45
46 /**
47 * @addtogroup utilities
48 * @{
49 */
50
51 /**
52 * @brief Exception class thrown by a failed @c any_cast
53 * @ingroup exceptions
54 */
55 class bad_any_cast : public bad_cast
56 {
57 public:
58 virtual const char* what() const noexcept { return "bad any_cast"; }
59 };
60
61 [[gnu::noreturn]] inline void __throw_bad_any_cast()
62 {
63#if __cpp_exceptions
64 throw bad_any_cast{};
65#else
66 __builtin_abort();
67#endif
68 }
69
56a9eaf9 70#define __cpp_lib_any 201606L
c6888c62 71
52e86221
VV
72 /**
73 * @brief A type-safe container of any type.
73ebece3 74 *
52e86221
VV
75 * An @c any object's state is either empty or it stores a contained object
76 * of CopyConstructible type.
77 */
78 class any
79 {
80 // Holds either pointer to a heap object or the contained object itself.
81 union _Storage
82 {
25a69162 83 constexpr _Storage() : _M_ptr{nullptr} {}
52e86221
VV
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>
a09bb4a8
JW
105 using _Manager = __conditional_t<_Internal<_Tp>::value,
106 _Manager_internal<_Tp>,
107 _Manager_external<_Tp>>;
52e86221 108
d1462b07
JW
109 template<typename _Tp, typename _VTp = decay_t<_Tp>>
110 using _Decay_if_not_any = enable_if_t<!is_same_v<_VTp, any>, _VTp>;
52e86221 111
ca9e949f
VV
112 /// Emplace with an object created from @p __args as the contained object.
113 template <typename _Tp, typename... _Args,
114 typename _Mgr = _Manager<_Tp>>
115 void __do_emplace(_Args&&... __args)
116 {
117 reset();
ca9e949f 118 _Mgr::_S_create(_M_storage, std::forward<_Args>(__args)...);
73ebece3 119 _M_manager = &_Mgr::_S_manage;
ca9e949f
VV
120 }
121
122 /// Emplace with an object created from @p __il and @p __args as
123 /// the contained object.
124 template <typename _Tp, typename _Up, typename... _Args,
125 typename _Mgr = _Manager<_Tp>>
126 void __do_emplace(initializer_list<_Up> __il, _Args&&... __args)
127 {
128 reset();
d1462b07 129 _Mgr::_S_create(_M_storage, __il, std::forward<_Args>(__args)...);
73ebece3 130 _M_manager = &_Mgr::_S_manage;
ca9e949f
VV
131 }
132
d1462b07
JW
133 template <typename _Res, typename _Tp, typename... _Args>
134 using __any_constructible
135 = enable_if<__and_<is_copy_constructible<_Tp>,
136 is_constructible<_Tp, _Args...>>::value,
137 _Res>;
138
139 template <typename _Tp, typename... _Args>
140 using __any_constructible_t
141 = typename __any_constructible<bool, _Tp, _Args...>::type;
142
143 template<typename _VTp, typename... _Args>
144 using __emplace_t
145 = typename __any_constructible<_VTp&, _VTp, _Args...>::type;
146
52e86221
VV
147 public:
148 // construct/destruct
149
150 /// Default constructor, creates an empty object.
25a69162 151 constexpr any() noexcept : _M_manager(nullptr) { }
52e86221
VV
152
153 /// Copy constructor, copies the state of @p __other
154 any(const any& __other)
155 {
25a69162 156 if (!__other.has_value())
52e86221
VV
157 _M_manager = nullptr;
158 else
159 {
160 _Arg __arg;
161 __arg._M_any = this;
162 __other._M_manager(_Op_clone, &__other, &__arg);
163 }
164 }
165
166 /**
167 * @brief Move constructor, transfer the state from @p __other
168 *
25a69162 169 * @post @c !__other.has_value() (this postcondition is a GNU extension)
52e86221
VV
170 */
171 any(any&& __other) noexcept
172 {
25a69162 173 if (!__other.has_value())
52e86221
VV
174 _M_manager = nullptr;
175 else
176 {
177 _Arg __arg;
178 __arg._M_any = this;
179 __other._M_manager(_Op_xfer, &__other, &__arg);
180 }
181 }
182
183 /// Construct with a copy of @p __value as the contained object.
d1462b07
JW
184 template <typename _Tp, typename _VTp = _Decay_if_not_any<_Tp>,
185 typename _Mgr = _Manager<_VTp>,
186 enable_if_t<is_copy_constructible<_VTp>::value
187 && !__is_in_place_type<_VTp>::value, bool> = true>
188 any(_Tp&& __value)
52e86221
VV
189 : _M_manager(&_Mgr::_S_manage)
190 {
d1462b07 191 _Mgr::_S_create(_M_storage, std::forward<_Tp>(__value));
52e86221
VV
192 }
193
25a69162 194 /// Construct with an object created from @p __args as the contained object.
d1462b07
JW
195 template <typename _Tp, typename... _Args, typename _VTp = decay_t<_Tp>,
196 typename _Mgr = _Manager<_VTp>,
197 __any_constructible_t<_VTp, _Args&&...> = false>
1725d05d 198 explicit
d1462b07 199 any(in_place_type_t<_Tp>, _Args&&... __args)
25a69162
VV
200 : _M_manager(&_Mgr::_S_manage)
201 {
d1462b07 202 _Mgr::_S_create(_M_storage, std::forward<_Args>(__args)...);
25a69162
VV
203 }
204
205 /// Construct with an object created from @p __il and @p __args as
206 /// the contained object.
d1462b07
JW
207 template <typename _Tp, typename _Up, typename... _Args,
208 typename _VTp = decay_t<_Tp>, typename _Mgr = _Manager<_VTp>,
14f26c75 209 __any_constructible_t<_VTp, initializer_list<_Up>&,
25a69162 210 _Args&&...> = false>
1725d05d 211 explicit
d1462b07 212 any(in_place_type_t<_Tp>, initializer_list<_Up> __il, _Args&&... __args)
25a69162
VV
213 : _M_manager(&_Mgr::_S_manage)
214 {
d1462b07 215 _Mgr::_S_create(_M_storage, __il, std::forward<_Args>(__args)...);
25a69162
VV
216 }
217
218 /// Destructor, calls @c reset()
219 ~any() { reset(); }
52e86221
VV
220
221 // assignments
222
223 /// Copy the state of another object.
d1462b07
JW
224 any&
225 operator=(const any& __rhs)
52e86221 226 {
1725d05d 227 *this = any(__rhs);
52e86221
VV
228 return *this;
229 }
230
231 /**
232 * @brief Move assignment operator
233 *
25a69162 234 * @post @c !__rhs.has_value() (not guaranteed for other implementations)
52e86221 235 */
d1462b07
JW
236 any&
237 operator=(any&& __rhs) noexcept
52e86221 238 {
25a69162
VV
239 if (!__rhs.has_value())
240 reset();
52e86221
VV
241 else if (this != &__rhs)
242 {
1725d05d 243 reset();
52e86221
VV
244 _Arg __arg;
245 __arg._M_any = this;
246 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
247 }
248 return *this;
249 }
250
251 /// Store a copy of @p __rhs as the contained object.
d1462b07
JW
252 template<typename _Tp>
253 enable_if_t<is_copy_constructible<_Decay_if_not_any<_Tp>>::value, any&>
254 operator=(_Tp&& __rhs)
52e86221 255 {
d1462b07 256 *this = any(std::forward<_Tp>(__rhs));
52e86221
VV
257 return *this;
258 }
259
25a69162 260 /// Emplace with an object created from @p __args as the contained object.
d1462b07
JW
261 template <typename _Tp, typename... _Args>
262 __emplace_t<decay_t<_Tp>, _Args...>
ca9e949f 263 emplace(_Args&&... __args)
25a69162 264 {
d1462b07
JW
265 using _VTp = decay_t<_Tp>;
266 __do_emplace<_VTp>(std::forward<_Args>(__args)...);
f6bb145c 267 return *any::_Manager<_VTp>::_S_access(_M_storage);
25a69162
VV
268 }
269
270 /// Emplace with an object created from @p __il and @p __args as
271 /// the contained object.
d1462b07 272 template <typename _Tp, typename _Up, typename... _Args>
14f26c75 273 __emplace_t<decay_t<_Tp>, initializer_list<_Up>&, _Args&&...>
ca9e949f 274 emplace(initializer_list<_Up> __il, _Args&&... __args)
25a69162 275 {
d1462b07
JW
276 using _VTp = decay_t<_Tp>;
277 __do_emplace<_VTp, _Up>(__il, std::forward<_Args>(__args)...);
f6bb145c 278 return *any::_Manager<_VTp>::_S_access(_M_storage);
25a69162
VV
279 }
280
52e86221
VV
281 // modifiers
282
283 /// If not empty, destroy the contained object.
25a69162 284 void reset() noexcept
52e86221 285 {
25a69162 286 if (has_value())
52e86221
VV
287 {
288 _M_manager(_Op_destroy, this, nullptr);
289 _M_manager = nullptr;
290 }
291 }
292
293 /// Exchange state with another object.
294 void swap(any& __rhs) noexcept
295 {
25a69162 296 if (!has_value() && !__rhs.has_value())
52e86221
VV
297 return;
298
25a69162 299 if (has_value() && __rhs.has_value())
52e86221
VV
300 {
301 if (this == &__rhs)
302 return;
303
304 any __tmp;
305 _Arg __arg;
306 __arg._M_any = &__tmp;
307 __rhs._M_manager(_Op_xfer, &__rhs, &__arg);
308 __arg._M_any = &__rhs;
309 _M_manager(_Op_xfer, this, &__arg);
310 __arg._M_any = this;
311 __tmp._M_manager(_Op_xfer, &__tmp, &__arg);
312 }
313 else
314 {
25a69162
VV
315 any* __empty = !has_value() ? this : &__rhs;
316 any* __full = !has_value() ? &__rhs : this;
52e86221
VV
317 _Arg __arg;
318 __arg._M_any = __empty;
319 __full->_M_manager(_Op_xfer, __full, &__arg);
320 }
321 }
322
323 // observers
324
325 /// Reports whether there is a contained object or not.
25a69162 326 bool has_value() const noexcept { return _M_manager != nullptr; }
52e86221
VV
327
328#if __cpp_rtti
329 /// The @c typeid of the contained object, or @c typeid(void) if empty.
330 const type_info& type() const noexcept
331 {
25a69162 332 if (!has_value())
52e86221
VV
333 return typeid(void);
334 _Arg __arg;
335 _M_manager(_Op_get_type_info, this, &__arg);
336 return *__arg._M_typeinfo;
337 }
338#endif
339
340 template<typename _Tp>
341 static constexpr bool __is_valid_cast()
342 { return __or_<is_reference<_Tp>, is_copy_constructible<_Tp>>::value; }
343
344 private:
345 enum _Op {
346 _Op_access, _Op_get_type_info, _Op_clone, _Op_destroy, _Op_xfer
347 };
348
349 union _Arg
350 {
351 void* _M_obj;
352 const std::type_info* _M_typeinfo;
353 any* _M_any;
354 };
355
356 void (*_M_manager)(_Op, const any*, _Arg*);
357 _Storage _M_storage;
358
359 template<typename _Tp>
360 friend void* __any_caster(const any* __any);
361
362 // Manage in-place contained object.
363 template<typename _Tp>
364 struct _Manager_internal
365 {
366 static void
367 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
368
369 template<typename _Up>
370 static void
371 _S_create(_Storage& __storage, _Up&& __value)
372 {
373 void* __addr = &__storage._M_buffer;
374 ::new (__addr) _Tp(std::forward<_Up>(__value));
375 }
25a69162
VV
376
377 template<typename... _Args>
378 static void
379 _S_create(_Storage& __storage, _Args&&... __args)
380 {
381 void* __addr = &__storage._M_buffer;
382 ::new (__addr) _Tp(std::forward<_Args>(__args)...);
383 }
f6bb145c
TA
384
385 static _Tp*
386 _S_access(const _Storage& __storage)
387 {
388 // The contained object is in __storage._M_buffer
389 const void* __addr = &__storage._M_buffer;
390 return static_cast<_Tp*>(const_cast<void*>(__addr));
391 }
52e86221
VV
392 };
393
394 // Manage external contained object.
395 template<typename _Tp>
396 struct _Manager_external
397 {
398 static void
399 _S_manage(_Op __which, const any* __anyp, _Arg* __arg);
400
401 template<typename _Up>
402 static void
403 _S_create(_Storage& __storage, _Up&& __value)
404 {
405 __storage._M_ptr = new _Tp(std::forward<_Up>(__value));
406 }
25a69162
VV
407 template<typename... _Args>
408 static void
409 _S_create(_Storage& __storage, _Args&&... __args)
410 {
411 __storage._M_ptr = new _Tp(std::forward<_Args>(__args)...);
412 }
f6bb145c
TA
413 static _Tp*
414 _S_access(const _Storage& __storage)
415 {
416 // The contained object is in *__storage._M_ptr
417 return static_cast<_Tp*>(__storage._M_ptr);
418 }
52e86221
VV
419 };
420 };
421
422 /// Exchange the states of two @c any objects.
423 inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
424
25a69162
VV
425 /// Create an any holding a @c _Tp constructed from @c __args.
426 template <typename _Tp, typename... _Args>
427 any make_any(_Args&&... __args)
428 {
627a2f59 429 return any(in_place_type<_Tp>, std::forward<_Args>(__args)...);
25a69162
VV
430 }
431
432 /// Create an any holding a @c _Tp constructed from @c __il and @c __args.
433 template <typename _Tp, typename _Up, typename... _Args>
434 any make_any(initializer_list<_Up> __il, _Args&&... __args)
435 {
627a2f59 436 return any(in_place_type<_Tp>, __il, std::forward<_Args>(__args)...);
25a69162
VV
437 }
438
52e86221
VV
439 /**
440 * @brief Access the contained object.
441 *
442 * @tparam _ValueType A const-reference or CopyConstructible type.
443 * @param __any The object to access.
444 * @return The contained object.
445 * @throw bad_any_cast If <code>
446 * __any.type() != typeid(remove_reference_t<_ValueType>)
447 * </code>
448 */
449 template<typename _ValueType>
450 inline _ValueType any_cast(const any& __any)
451 {
6791489e 452 using _Up = __remove_cvref_t<_ValueType>;
52e86221
VV
453 static_assert(any::__is_valid_cast<_ValueType>(),
454 "Template argument must be a reference or CopyConstructible type");
23c3a059
VV
455 static_assert(is_constructible_v<_ValueType, const _Up&>,
456 "Template argument must be constructible from a const value.");
457 auto __p = any_cast<_Up>(&__any);
52e86221 458 if (__p)
62549523 459 return static_cast<_ValueType>(*__p);
52e86221
VV
460 __throw_bad_any_cast();
461 }
462
463 /**
464 * @brief Access the contained object.
465 *
466 * @tparam _ValueType A reference or CopyConstructible type.
467 * @param __any The object to access.
468 * @return The contained object.
469 * @throw bad_any_cast If <code>
470 * __any.type() != typeid(remove_reference_t<_ValueType>)
471 * </code>
472 *
473 * @{
474 */
475 template<typename _ValueType>
476 inline _ValueType any_cast(any& __any)
477 {
6791489e 478 using _Up = __remove_cvref_t<_ValueType>;
52e86221
VV
479 static_assert(any::__is_valid_cast<_ValueType>(),
480 "Template argument must be a reference or CopyConstructible type");
23c3a059
VV
481 static_assert(is_constructible_v<_ValueType, _Up&>,
482 "Template argument must be constructible from an lvalue.");
483 auto __p = any_cast<_Up>(&__any);
52e86221 484 if (__p)
62549523 485 return static_cast<_ValueType>(*__p);
52e86221
VV
486 __throw_bad_any_cast();
487 }
488
23c3a059 489 template<typename _ValueType>
52e86221
VV
490 inline _ValueType any_cast(any&& __any)
491 {
6791489e 492 using _Up = __remove_cvref_t<_ValueType>;
52e86221
VV
493 static_assert(any::__is_valid_cast<_ValueType>(),
494 "Template argument must be a reference or CopyConstructible type");
23c3a059
VV
495 static_assert(is_constructible_v<_ValueType, _Up>,
496 "Template argument must be constructible from an rvalue.");
497 auto __p = any_cast<_Up>(&__any);
52e86221 498 if (__p)
62549523 499 return static_cast<_ValueType>(std::move(*__p));
52e86221
VV
500 __throw_bad_any_cast();
501 }
f0b88346 502 /// @}
52e86221 503
aa573a6a 504 /// @cond undocumented
52e86221
VV
505 template<typename _Tp>
506 void* __any_caster(const any* __any)
507 {
92750002
JW
508 // any_cast<T> returns non-null if __any->type() == typeid(T) and
509 // typeid(T) ignores cv-qualifiers so remove them:
510 using _Up = remove_cv_t<_Tp>;
511 // The contained value has a decayed type, so if decay_t<U> is not U,
512 // then it's not possible to have a contained value of type U:
513 if constexpr (!is_same_v<decay_t<_Up>, _Up>)
514 return nullptr;
515 // Only copy constructible types can be used for contained values:
516 else if constexpr (!is_copy_constructible_v<_Up>)
517 return nullptr;
aa573a6a
JW
518 // First try comparing function addresses, which works without RTTI
519 else if (__any->_M_manager == &any::_Manager<_Up>::_S_manage
520#if __cpp_rtti
521 || __any->type() == typeid(_Tp)
522#endif
523 )
38a9e5a6 524 {
f6bb145c 525 return any::_Manager<_Up>::_S_access(__any->_M_storage);
38a9e5a6
JW
526 }
527 return nullptr;
52e86221 528 }
aa573a6a 529 /// @endcond
52e86221
VV
530
531 /**
532 * @brief Access the contained object.
533 *
534 * @tparam _ValueType The type of the contained object.
535 * @param __any A pointer to the object to access.
536 * @return The address of the contained object if <code>
537 * __any != nullptr && __any.type() == typeid(_ValueType)
538 * </code>, otherwise a null pointer.
539 *
540 * @{
541 */
542 template<typename _ValueType>
543 inline const _ValueType* any_cast(const any* __any) noexcept
544 {
f9bfdfa2
JW
545 if constexpr (is_object_v<_ValueType>)
546 if (__any)
547 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
52e86221
VV
548 return nullptr;
549 }
550
551 template<typename _ValueType>
552 inline _ValueType* any_cast(any* __any) noexcept
553 {
f9bfdfa2
JW
554 if constexpr (is_object_v<_ValueType>)
555 if (__any)
556 return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
52e86221
VV
557 return nullptr;
558 }
f0b88346 559 /// @}
52e86221
VV
560
561 template<typename _Tp>
562 void
563 any::_Manager_internal<_Tp>::
564 _S_manage(_Op __which, const any* __any, _Arg* __arg)
565 {
566 // The contained object is in _M_storage._M_buffer
567 auto __ptr = reinterpret_cast<const _Tp*>(&__any->_M_storage._M_buffer);
568 switch (__which)
569 {
570 case _Op_access:
571 __arg->_M_obj = const_cast<_Tp*>(__ptr);
572 break;
573 case _Op_get_type_info:
574#if __cpp_rtti
575 __arg->_M_typeinfo = &typeid(_Tp);
576#endif
577 break;
578 case _Op_clone:
579 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp(*__ptr);
580 __arg->_M_any->_M_manager = __any->_M_manager;
581 break;
582 case _Op_destroy:
583 __ptr->~_Tp();
584 break;
585 case _Op_xfer:
1725d05d
VV
586 ::new(&__arg->_M_any->_M_storage._M_buffer) _Tp
587 (std::move(*const_cast<_Tp*>(__ptr)));
52e86221
VV
588 __ptr->~_Tp();
589 __arg->_M_any->_M_manager = __any->_M_manager;
590 const_cast<any*>(__any)->_M_manager = nullptr;
591 break;
592 }
593 }
594
595 template<typename _Tp>
596 void
597 any::_Manager_external<_Tp>::
598 _S_manage(_Op __which, const any* __any, _Arg* __arg)
599 {
600 // The contained object is *_M_storage._M_ptr
601 auto __ptr = static_cast<const _Tp*>(__any->_M_storage._M_ptr);
602 switch (__which)
603 {
604 case _Op_access:
605 __arg->_M_obj = const_cast<_Tp*>(__ptr);
606 break;
607 case _Op_get_type_info:
608#if __cpp_rtti
609 __arg->_M_typeinfo = &typeid(_Tp);
610#endif
611 break;
612 case _Op_clone:
613 __arg->_M_any->_M_storage._M_ptr = new _Tp(*__ptr);
614 __arg->_M_any->_M_manager = __any->_M_manager;
615 break;
616 case _Op_destroy:
617 delete __ptr;
618 break;
619 case _Op_xfer:
620 __arg->_M_any->_M_storage._M_ptr = __any->_M_storage._M_ptr;
621 __arg->_M_any->_M_manager = __any->_M_manager;
622 const_cast<any*>(__any)->_M_manager = nullptr;
623 break;
624 }
625 }
626
627 /// @}
73ebece3 628
10f26de9
JW
629 namespace __detail::__variant
630 {
631 template<typename> struct _Never_valueless_alt; // see <variant>
632
633 // Provide the strong exception-safety guarantee when emplacing an
634 // any into a variant.
635 template<>
636 struct _Never_valueless_alt<std::any>
637 : std::true_type
638 { };
639 } // namespace __detail::__variant
640
52e86221
VV
641_GLIBCXX_END_NAMESPACE_VERSION
642} // namespace std
643
10f26de9 644#endif // C++17
52e86221 645#endif // _GLIBCXX_ANY