blob: 781eee7869c0c0eb5ac321c7280fb8f4ef39de78 [file] [log] [blame]
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +00001// -*- C++ -*-
2//===------------------------------ any -----------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_ANY
12#define _LIBCPP_ANY
13
14/*
15 any synopsis
16
17namespace std {
18
19 class bad_any_cast : public bad_cast
20 {
21 public:
22 virtual const char* what() const noexcept;
23 };
24
25 class any
26 {
27 public:
28
29 // 6.3.1 any construct/destruct
30 any() noexcept;
31
32 any(const any& other);
33 any(any&& other) noexcept;
34
35 template <class ValueType>
36 any(ValueType&& value);
37
38 ~any();
39
40 // 6.3.2 any assignments
41 any& operator=(const any& rhs);
42 any& operator=(any&& rhs) noexcept;
43
44 template <class ValueType>
45 any& operator=(ValueType&& rhs);
46
47 // 6.3.3 any modifiers
Marshall Clow45ff9822017-04-12 22:51:27 +000048 template <class ValueType, class... Args>
49 decay_t<ValueType>& emplace(Args&&... args);
50 template <class ValueType, class U, class... Args>
51 decay_t<ValueType>& emplace(initializer_list<U>, Args&&...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +000052 void reset() noexcept;
53 void swap(any& rhs) noexcept;
54
55 // 6.3.4 any observers
56 bool has_value() const noexcept;
57 const type_info& type() const noexcept;
58 };
59
60 // 6.4 Non-member functions
61 void swap(any& x, any& y) noexcept;
62
63 template <class T, class ...Args>
64 any make_any(Args&& ...args);
65 template <class T, class U, class ...Args>
66 any make_any(initializer_list<U>, Args&& ...args);
67
68 template<class ValueType>
69 ValueType any_cast(const any& operand);
70 template<class ValueType>
71 ValueType any_cast(any& operand);
72 template<class ValueType>
73 ValueType any_cast(any&& operand);
74
75 template<class ValueType>
76 const ValueType* any_cast(const any* operand) noexcept;
77 template<class ValueType>
78 ValueType* any_cast(any* operand) noexcept;
79
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +000080} // namespace std
81
82*/
83
84#include <experimental/__config>
85#include <memory>
86#include <new>
87#include <typeinfo>
88#include <type_traits>
89#include <cstdlib>
Marshall Clow0a1e7502018-09-12 19:41:40 +000090#include <version>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +000091
92#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
93#pragma GCC system_header
94#endif
95
96namespace std {
Louis Dionnecef92e62018-11-19 15:37:04 +000097class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_ANY_CAST bad_any_cast : public bad_cast
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +000098{
99public:
100 virtual const char* what() const _NOEXCEPT;
101};
102} // namespace std
103
104_LIBCPP_BEGIN_NAMESPACE_STD
105
106#if _LIBCPP_STD_VER > 14
107
Louis Dionne16fe2952018-07-11 23:14:33 +0000108_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +0000109_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
Marshall Clowed3e2292016-08-25 17:47:09 +0000110void __throw_bad_any_cast()
111{
112#ifndef _LIBCPP_NO_EXCEPTIONS
113 throw bad_any_cast();
114#else
Louis Dionne44bcff92018-08-03 22:36:53 +0000115 _VSTD::abort();
Marshall Clowed3e2292016-08-25 17:47:09 +0000116#endif
117}
118
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000119// Forward declarations
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000120class _LIBCPP_TEMPLATE_VIS any;
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000121
122template <class _ValueType>
123_LIBCPP_INLINE_VISIBILITY
124add_pointer_t<add_const_t<_ValueType>>
125any_cast(any const *) _NOEXCEPT;
126
127template <class _ValueType>
128_LIBCPP_INLINE_VISIBILITY
129add_pointer_t<_ValueType> any_cast(any *) _NOEXCEPT;
130
131namespace __any_imp
132{
133 using _Buffer = aligned_storage_t<3*sizeof(void*), alignment_of<void*>::value>;
134
135 template <class _Tp>
136 using _IsSmallObject = integral_constant<bool
137 , sizeof(_Tp) <= sizeof(_Buffer)
138 && alignment_of<_Buffer>::value
139 % alignment_of<_Tp>::value == 0
140 && is_nothrow_move_constructible<_Tp>::value
141 >;
142
143 enum class _Action {
144 _Destroy,
145 _Copy,
146 _Move,
147 _Get,
148 _TypeInfo
149 };
150
151 template <class _Tp> struct _SmallHandler;
152 template <class _Tp> struct _LargeHandler;
153
154 template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000155 struct _LIBCPP_TEMPLATE_VIS __unique_typeinfo { static constexpr int __id = 0; };
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000156 template <class _Tp> constexpr int __unique_typeinfo<_Tp>::__id;
157
158 template <class _Tp>
159 inline _LIBCPP_INLINE_VISIBILITY
160 constexpr const void* __get_fallback_typeid() {
161 return &__unique_typeinfo<decay_t<_Tp>>::__id;
162 }
163
164 template <class _Tp>
165 inline _LIBCPP_INLINE_VISIBILITY
166 bool __compare_typeid(type_info const* __id, const void* __fallback_id)
167 {
168#if !defined(_LIBCPP_NO_RTTI)
169 if (__id && *__id == typeid(_Tp))
170 return true;
171#endif
172 if (!__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>())
173 return true;
174 return false;
175 }
176
177 template <class _Tp>
178 using _Handler = conditional_t<
179 _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>;
180
181} // namespace __any_imp
182
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000183class _LIBCPP_TEMPLATE_VIS any
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000184{
185public:
186 // construct/destruct
187 _LIBCPP_INLINE_VISIBILITY
188 constexpr any() _NOEXCEPT : __h(nullptr) {}
189
190 _LIBCPP_INLINE_VISIBILITY
191 any(any const & __other) : __h(nullptr)
192 {
193 if (__other.__h) __other.__call(_Action::_Copy, this);
194 }
195
196 _LIBCPP_INLINE_VISIBILITY
197 any(any && __other) _NOEXCEPT : __h(nullptr)
198 {
199 if (__other.__h) __other.__call(_Action::_Move, this);
200 }
201
202 template <
203 class _ValueType
Eric Fiselier58614252016-10-07 21:27:45 +0000204 , class _Tp = decay_t<_ValueType>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000205 , class = enable_if_t<
Eric Fiselier58614252016-10-07 21:27:45 +0000206 !is_same<_Tp, any>::value &&
Eric Fiselier99b81712016-11-17 19:24:04 +0000207 !__is_inplace_type<_ValueType>::value &&
Eric Fiselier58614252016-10-07 21:27:45 +0000208 is_copy_constructible<_Tp>::value>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000209 >
210 _LIBCPP_INLINE_VISIBILITY
211 any(_ValueType && __value);
212
Eric Fiselier58614252016-10-07 21:27:45 +0000213 template <class _ValueType, class ..._Args,
214 class _Tp = decay_t<_ValueType>,
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000215 class = enable_if_t<
216 is_constructible<_Tp, _Args...>::value &&
217 is_copy_constructible<_Tp>::value
218 >
219 >
220 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier58614252016-10-07 21:27:45 +0000221 explicit any(in_place_type_t<_ValueType>, _Args&&... __args);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000222
Eric Fiselier58614252016-10-07 21:27:45 +0000223 template <class _ValueType, class _Up, class ..._Args,
224 class _Tp = decay_t<_ValueType>,
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000225 class = enable_if_t<
226 is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
227 is_copy_constructible<_Tp>::value>
228 >
229 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier58614252016-10-07 21:27:45 +0000230 explicit any(in_place_type_t<_ValueType>, initializer_list<_Up>, _Args&&... __args);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000231
232 _LIBCPP_INLINE_VISIBILITY
233 ~any() { this->reset(); }
234
235 // assignments
236 _LIBCPP_INLINE_VISIBILITY
237 any & operator=(any const & __rhs) {
238 any(__rhs).swap(*this);
239 return *this;
240 }
241
242 _LIBCPP_INLINE_VISIBILITY
243 any & operator=(any && __rhs) _NOEXCEPT {
244 any(_VSTD::move(__rhs)).swap(*this);
245 return *this;
246 }
247
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000248 template <
249 class _ValueType
Eric Fiselier58614252016-10-07 21:27:45 +0000250 , class _Tp = decay_t<_ValueType>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000251 , class = enable_if_t<
Eric Fiselier58614252016-10-07 21:27:45 +0000252 !is_same<_Tp, any>::value
Eric Fiselieraea4e192016-10-16 02:51:50 +0000253 && is_copy_constructible<_Tp>::value>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000254 >
255 _LIBCPP_INLINE_VISIBILITY
256 any & operator=(_ValueType && __rhs);
257
Eric Fiselier58614252016-10-07 21:27:45 +0000258 template <class _ValueType, class ..._Args,
259 class _Tp = decay_t<_ValueType>,
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000260 class = enable_if_t<
261 is_constructible<_Tp, _Args...>::value &&
262 is_copy_constructible<_Tp>::value>
263 >
264 _LIBCPP_INLINE_VISIBILITY
Marshall Clow45ff9822017-04-12 22:51:27 +0000265 _Tp& emplace(_Args&&... args);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000266
Eric Fiselier58614252016-10-07 21:27:45 +0000267 template <class _ValueType, class _Up, class ..._Args,
268 class _Tp = decay_t<_ValueType>,
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000269 class = enable_if_t<
270 is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
271 is_copy_constructible<_Tp>::value>
272 >
273 _LIBCPP_INLINE_VISIBILITY
Marshall Clow45ff9822017-04-12 22:51:27 +0000274 _Tp& emplace(initializer_list<_Up>, _Args&&...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000275
276 // 6.3.3 any modifiers
277 _LIBCPP_INLINE_VISIBILITY
278 void reset() _NOEXCEPT { if (__h) this->__call(_Action::_Destroy); }
279
280 _LIBCPP_INLINE_VISIBILITY
281 void swap(any & __rhs) _NOEXCEPT;
282
283 // 6.3.4 any observers
284 _LIBCPP_INLINE_VISIBILITY
285 bool has_value() const _NOEXCEPT { return __h != nullptr; }
286
287#if !defined(_LIBCPP_NO_RTTI)
288 _LIBCPP_INLINE_VISIBILITY
289 const type_info & type() const _NOEXCEPT {
290 if (__h) {
291 return *static_cast<type_info const *>(this->__call(_Action::_TypeInfo));
292 } else {
293 return typeid(void);
294 }
295 }
296#endif
297
298private:
299 typedef __any_imp::_Action _Action;
300 using _HandleFuncPtr = void* (*)(_Action, any const *, any *, const type_info *,
301 const void* __fallback_info);
302
303 union _Storage {
304 constexpr _Storage() : __ptr(nullptr) {}
305 void * __ptr;
306 __any_imp::_Buffer __buf;
307 };
308
Louis Dionne16fe2952018-07-11 23:14:33 +0000309 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000310 void * __call(_Action __a, any * __other = nullptr,
311 type_info const * __info = nullptr,
312 const void* __fallback_info = nullptr) const
313 {
314 return __h(__a, this, __other, __info, __fallback_info);
315 }
316
Louis Dionne16fe2952018-07-11 23:14:33 +0000317 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000318 void * __call(_Action __a, any * __other = nullptr,
319 type_info const * __info = nullptr,
320 const void* __fallback_info = nullptr)
321 {
322 return __h(__a, this, __other, __info, __fallback_info);
323 }
324
325 template <class>
326 friend struct __any_imp::_SmallHandler;
327 template <class>
328 friend struct __any_imp::_LargeHandler;
329
330 template <class _ValueType>
331 friend add_pointer_t<add_const_t<_ValueType>>
332 any_cast(any const *) _NOEXCEPT;
333
334 template <class _ValueType>
335 friend add_pointer_t<_ValueType>
336 any_cast(any *) _NOEXCEPT;
337
338 _HandleFuncPtr __h = nullptr;
339 _Storage __s;
340};
341
342namespace __any_imp
343{
344 template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000345 struct _LIBCPP_TEMPLATE_VIS _SmallHandler
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000346 {
347 _LIBCPP_INLINE_VISIBILITY
348 static void* __handle(_Action __act, any const * __this, any * __other,
349 type_info const * __info, const void* __fallback_info)
350 {
351 switch (__act)
352 {
353 case _Action::_Destroy:
354 __destroy(const_cast<any &>(*__this));
355 return nullptr;
356 case _Action::_Copy:
357 __copy(*__this, *__other);
358 return nullptr;
359 case _Action::_Move:
360 __move(const_cast<any &>(*__this), *__other);
361 return nullptr;
362 case _Action::_Get:
363 return __get(const_cast<any &>(*__this), __info, __fallback_info);
364 case _Action::_TypeInfo:
365 return __type_info();
366 }
367 }
368
369 template <class ..._Args>
370 _LIBCPP_INLINE_VISIBILITY
Marshall Clow45ff9822017-04-12 22:51:27 +0000371 static _Tp& __create(any & __dest, _Args&&... __args) {
372 _Tp* __ret = ::new (static_cast<void*>(&__dest.__s.__buf)) _Tp(_VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000373 __dest.__h = &_SmallHandler::__handle;
Marshall Clow45ff9822017-04-12 22:51:27 +0000374 return *__ret;
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000375 }
376
377 private:
378 _LIBCPP_INLINE_VISIBILITY
379 static void __destroy(any & __this) {
380 _Tp & __value = *static_cast<_Tp *>(static_cast<void*>(&__this.__s.__buf));
381 __value.~_Tp();
382 __this.__h = nullptr;
383 }
384
385 _LIBCPP_INLINE_VISIBILITY
386 static void __copy(any const & __this, any & __dest) {
387 _SmallHandler::__create(__dest, *static_cast<_Tp const *>(
388 static_cast<void const *>(&__this.__s.__buf)));
389 }
390
391 _LIBCPP_INLINE_VISIBILITY
392 static void __move(any & __this, any & __dest) {
393 _SmallHandler::__create(__dest, _VSTD::move(
394 *static_cast<_Tp*>(static_cast<void*>(&__this.__s.__buf))));
395 __destroy(__this);
396 }
397
398 _LIBCPP_INLINE_VISIBILITY
399 static void* __get(any & __this,
400 type_info const * __info,
401 const void* __fallback_id)
402 {
403 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id))
404 return static_cast<void*>(&__this.__s.__buf);
405 return nullptr;
406 }
407
408 _LIBCPP_INLINE_VISIBILITY
409 static void* __type_info()
410 {
411#if !defined(_LIBCPP_NO_RTTI)
412 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
413#else
414 return nullptr;
415#endif
416 }
417 };
418
419 template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000420 struct _LIBCPP_TEMPLATE_VIS _LargeHandler
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000421 {
422 _LIBCPP_INLINE_VISIBILITY
423 static void* __handle(_Action __act, any const * __this,
424 any * __other, type_info const * __info,
425 void const* __fallback_info)
426 {
427 switch (__act)
428 {
429 case _Action::_Destroy:
430 __destroy(const_cast<any &>(*__this));
431 return nullptr;
432 case _Action::_Copy:
433 __copy(*__this, *__other);
434 return nullptr;
435 case _Action::_Move:
436 __move(const_cast<any &>(*__this), *__other);
437 return nullptr;
438 case _Action::_Get:
439 return __get(const_cast<any &>(*__this), __info, __fallback_info);
440 case _Action::_TypeInfo:
441 return __type_info();
442 }
443 }
444
445 template <class ..._Args>
446 _LIBCPP_INLINE_VISIBILITY
Marshall Clow45ff9822017-04-12 22:51:27 +0000447 static _Tp& __create(any & __dest, _Args&&... __args) {
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000448 typedef allocator<_Tp> _Alloc;
449 typedef __allocator_destructor<_Alloc> _Dp;
450 _Alloc __a;
451 unique_ptr<_Tp, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Marshall Clow45ff9822017-04-12 22:51:27 +0000452 _Tp* __ret = ::new ((void*)__hold.get()) _Tp(_VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000453 __dest.__s.__ptr = __hold.release();
454 __dest.__h = &_LargeHandler::__handle;
Marshall Clow45ff9822017-04-12 22:51:27 +0000455 return *__ret;
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000456 }
457
458 private:
459
460 _LIBCPP_INLINE_VISIBILITY
461 static void __destroy(any & __this){
462 delete static_cast<_Tp*>(__this.__s.__ptr);
463 __this.__h = nullptr;
464 }
465
466 _LIBCPP_INLINE_VISIBILITY
467 static void __copy(any const & __this, any & __dest) {
468 _LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s.__ptr));
469 }
470
471 _LIBCPP_INLINE_VISIBILITY
472 static void __move(any & __this, any & __dest) {
473 __dest.__s.__ptr = __this.__s.__ptr;
474 __dest.__h = &_LargeHandler::__handle;
475 __this.__h = nullptr;
476 }
477
478 _LIBCPP_INLINE_VISIBILITY
479 static void* __get(any & __this, type_info const * __info,
480 void const* __fallback_info)
481 {
482 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info))
483 return static_cast<void*>(__this.__s.__ptr);
484 return nullptr;
485
486 }
487
488 _LIBCPP_INLINE_VISIBILITY
489 static void* __type_info()
490 {
491#if !defined(_LIBCPP_NO_RTTI)
492 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
493#else
494 return nullptr;
495#endif
496 }
497 };
498
499} // namespace __any_imp
500
501
Eric Fiselier58614252016-10-07 21:27:45 +0000502template <class _ValueType, class _Tp, class>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000503any::any(_ValueType && __v) : __h(nullptr)
504{
Eric Fiselier58614252016-10-07 21:27:45 +0000505 __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_ValueType>(__v));
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000506}
507
Eric Fiselier58614252016-10-07 21:27:45 +0000508template <class _ValueType, class ..._Args, class _Tp, class>
509any::any(in_place_type_t<_ValueType>, _Args&&... __args) {
510 __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000511};
512
Eric Fiselier58614252016-10-07 21:27:45 +0000513template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
514any::any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) {
515 __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000516}
517
Eric Fiselier58614252016-10-07 21:27:45 +0000518template <class _ValueType, class, class>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000519inline _LIBCPP_INLINE_VISIBILITY
520any & any::operator=(_ValueType && __v)
521{
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000522 any(_VSTD::forward<_ValueType>(__v)).swap(*this);
523 return *this;
524}
525
Eric Fiselier58614252016-10-07 21:27:45 +0000526template <class _ValueType, class ..._Args, class _Tp, class>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000527inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow45ff9822017-04-12 22:51:27 +0000528_Tp& any::emplace(_Args&&... __args) {
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000529 reset();
Marshall Clow45ff9822017-04-12 22:51:27 +0000530 return __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000531}
532
Eric Fiselier58614252016-10-07 21:27:45 +0000533template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000534inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow45ff9822017-04-12 22:51:27 +0000535_Tp& any::emplace(initializer_list<_Up> __il, _Args&&... __args) {
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000536 reset();
Marshall Clow45ff9822017-04-12 22:51:27 +0000537 return __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000538}
539
540inline _LIBCPP_INLINE_VISIBILITY
541void any::swap(any & __rhs) _NOEXCEPT
542{
Eric Fiselier58614252016-10-07 21:27:45 +0000543 if (this == &__rhs)
544 return;
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000545 if (__h && __rhs.__h) {
546 any __tmp;
547 __rhs.__call(_Action::_Move, &__tmp);
548 this->__call(_Action::_Move, &__rhs);
549 __tmp.__call(_Action::_Move, this);
550 }
551 else if (__h) {
552 this->__call(_Action::_Move, &__rhs);
553 }
554 else if (__rhs.__h) {
555 __rhs.__call(_Action::_Move, this);
556 }
557}
558
559// 6.4 Non-member functions
560
561inline _LIBCPP_INLINE_VISIBILITY
562void swap(any & __lhs, any & __rhs) _NOEXCEPT
563{
564 __lhs.swap(__rhs);
565}
566
567template <class _Tp, class ..._Args>
568inline _LIBCPP_INLINE_VISIBILITY
569any make_any(_Args&&... __args) {
Eric Fiselier99b81712016-11-17 19:24:04 +0000570 return any(in_place_type<_Tp>, _VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000571}
572
573template <class _Tp, class _Up, class ..._Args>
574inline _LIBCPP_INLINE_VISIBILITY
575any make_any(initializer_list<_Up> __il, _Args&&... __args) {
Eric Fiselier99b81712016-11-17 19:24:04 +0000576 return any(in_place_type<_Tp>, __il, _VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000577}
578
579template <class _ValueType>
580inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +0000581_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000582_ValueType any_cast(any const & __v)
583{
Eric Fiselier58614252016-10-07 21:27:45 +0000584 using _RawValueType = __uncvref_t<_ValueType>;
585 static_assert(is_constructible<_ValueType, _RawValueType const &>::value,
Eric Fiselier0e1f0902016-10-16 01:43:43 +0000586 "ValueType is required to be a const lvalue reference "
587 "or a CopyConstructible type");
Eric Fiselier58614252016-10-07 21:27:45 +0000588 auto __tmp = _VSTD::any_cast<add_const_t<_RawValueType>>(&__v);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000589 if (__tmp == nullptr)
Marshall Clowed3e2292016-08-25 17:47:09 +0000590 __throw_bad_any_cast();
Eric Fiselier58614252016-10-07 21:27:45 +0000591 return static_cast<_ValueType>(*__tmp);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000592}
593
594template <class _ValueType>
595inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +0000596_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000597_ValueType any_cast(any & __v)
598{
Eric Fiselier58614252016-10-07 21:27:45 +0000599 using _RawValueType = __uncvref_t<_ValueType>;
600 static_assert(is_constructible<_ValueType, _RawValueType &>::value,
Eric Fiselier0e1f0902016-10-16 01:43:43 +0000601 "ValueType is required to be an lvalue reference "
602 "or a CopyConstructible type");
Eric Fiselier58614252016-10-07 21:27:45 +0000603 auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000604 if (__tmp == nullptr)
Marshall Clowed3e2292016-08-25 17:47:09 +0000605 __throw_bad_any_cast();
Eric Fiselier58614252016-10-07 21:27:45 +0000606 return static_cast<_ValueType>(*__tmp);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000607}
608
609template <class _ValueType>
610inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +0000611_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000612_ValueType any_cast(any && __v)
613{
Eric Fiselier58614252016-10-07 21:27:45 +0000614 using _RawValueType = __uncvref_t<_ValueType>;
615 static_assert(is_constructible<_ValueType, _RawValueType>::value,
Eric Fiselier0e1f0902016-10-16 01:43:43 +0000616 "ValueType is required to be an rvalue reference "
617 "or a CopyConstructible type");
Eric Fiselier58614252016-10-07 21:27:45 +0000618 auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000619 if (__tmp == nullptr)
Marshall Clowed3e2292016-08-25 17:47:09 +0000620 __throw_bad_any_cast();
Eric Fiselier58614252016-10-07 21:27:45 +0000621 return static_cast<_ValueType>(_VSTD::move(*__tmp));
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000622}
623
624template <class _ValueType>
625inline _LIBCPP_INLINE_VISIBILITY
626add_pointer_t<add_const_t<_ValueType>>
627any_cast(any const * __any) _NOEXCEPT
628{
629 static_assert(!is_reference<_ValueType>::value,
630 "_ValueType may not be a reference.");
631 return _VSTD::any_cast<_ValueType>(const_cast<any *>(__any));
632}
633
Eric Fiselier9af18272016-10-16 11:56:38 +0000634template <class _RetType>
635inline _LIBCPP_INLINE_VISIBILITY
636_RetType __pointer_or_func_cast(void* __p, /*IsFunction*/false_type) noexcept {
637 return static_cast<_RetType>(__p);
638}
639
640template <class _RetType>
641inline _LIBCPP_INLINE_VISIBILITY
642_RetType __pointer_or_func_cast(void*, /*IsFunction*/true_type) noexcept {
643 return nullptr;
644}
645
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000646template <class _ValueType>
647add_pointer_t<_ValueType>
648any_cast(any * __any) _NOEXCEPT
649{
650 using __any_imp::_Action;
651 static_assert(!is_reference<_ValueType>::value,
652 "_ValueType may not be a reference.");
653 typedef typename add_pointer<_ValueType>::type _ReturnType;
654 if (__any && __any->__h) {
Eric Fiselier9af18272016-10-16 11:56:38 +0000655 void *__p = __any->__call(_Action::_Get, nullptr,
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000656#if !defined(_LIBCPP_NO_RTTI)
657 &typeid(_ValueType),
658#else
659 nullptr,
660#endif
Eric Fiselier9af18272016-10-16 11:56:38 +0000661 __any_imp::__get_fallback_typeid<_ValueType>());
662 return _VSTD::__pointer_or_func_cast<_ReturnType>(
663 __p, is_function<_ValueType>{});
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000664 }
665 return nullptr;
666}
667
668#endif // _LIBCPP_STD_VER > 14
669
670_LIBCPP_END_NAMESPACE_STD
671
672#endif // _LIBCPP_ANY