blob: e2945bdfa9877c260d963e37c9dadf07c174ddbc [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 {
97class _LIBCPP_EXCEPTION_ABI bad_any_cast : public bad_cast
98{
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
Marshall Clowed3e2292016-08-25 17:47:09 +0000109void __throw_bad_any_cast()
110{
111#ifndef _LIBCPP_NO_EXCEPTIONS
112 throw bad_any_cast();
113#else
Louis Dionne44bcff92018-08-03 22:36:53 +0000114 _VSTD::abort();
Marshall Clowed3e2292016-08-25 17:47:09 +0000115#endif
116}
117
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000118// Forward declarations
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000119class _LIBCPP_TEMPLATE_VIS any;
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000120
121template <class _ValueType>
122_LIBCPP_INLINE_VISIBILITY
123add_pointer_t<add_const_t<_ValueType>>
124any_cast(any const *) _NOEXCEPT;
125
126template <class _ValueType>
127_LIBCPP_INLINE_VISIBILITY
128add_pointer_t<_ValueType> any_cast(any *) _NOEXCEPT;
129
130namespace __any_imp
131{
132 using _Buffer = aligned_storage_t<3*sizeof(void*), alignment_of<void*>::value>;
133
134 template <class _Tp>
135 using _IsSmallObject = integral_constant<bool
136 , sizeof(_Tp) <= sizeof(_Buffer)
137 && alignment_of<_Buffer>::value
138 % alignment_of<_Tp>::value == 0
139 && is_nothrow_move_constructible<_Tp>::value
140 >;
141
142 enum class _Action {
143 _Destroy,
144 _Copy,
145 _Move,
146 _Get,
147 _TypeInfo
148 };
149
150 template <class _Tp> struct _SmallHandler;
151 template <class _Tp> struct _LargeHandler;
152
153 template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000154 struct _LIBCPP_TEMPLATE_VIS __unique_typeinfo { static constexpr int __id = 0; };
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000155 template <class _Tp> constexpr int __unique_typeinfo<_Tp>::__id;
156
157 template <class _Tp>
158 inline _LIBCPP_INLINE_VISIBILITY
159 constexpr const void* __get_fallback_typeid() {
160 return &__unique_typeinfo<decay_t<_Tp>>::__id;
161 }
162
163 template <class _Tp>
164 inline _LIBCPP_INLINE_VISIBILITY
165 bool __compare_typeid(type_info const* __id, const void* __fallback_id)
166 {
167#if !defined(_LIBCPP_NO_RTTI)
168 if (__id && *__id == typeid(_Tp))
169 return true;
170#endif
171 if (!__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>())
172 return true;
173 return false;
174 }
175
176 template <class _Tp>
177 using _Handler = conditional_t<
178 _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>;
179
180} // namespace __any_imp
181
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000182class _LIBCPP_TEMPLATE_VIS any
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000183{
184public:
185 // construct/destruct
186 _LIBCPP_INLINE_VISIBILITY
187 constexpr any() _NOEXCEPT : __h(nullptr) {}
188
189 _LIBCPP_INLINE_VISIBILITY
190 any(any const & __other) : __h(nullptr)
191 {
192 if (__other.__h) __other.__call(_Action::_Copy, this);
193 }
194
195 _LIBCPP_INLINE_VISIBILITY
196 any(any && __other) _NOEXCEPT : __h(nullptr)
197 {
198 if (__other.__h) __other.__call(_Action::_Move, this);
199 }
200
201 template <
202 class _ValueType
Eric Fiselier58614252016-10-07 21:27:45 +0000203 , class _Tp = decay_t<_ValueType>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000204 , class = enable_if_t<
Eric Fiselier58614252016-10-07 21:27:45 +0000205 !is_same<_Tp, any>::value &&
Eric Fiselier99b81712016-11-17 19:24:04 +0000206 !__is_inplace_type<_ValueType>::value &&
Eric Fiselier58614252016-10-07 21:27:45 +0000207 is_copy_constructible<_Tp>::value>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000208 >
209 _LIBCPP_INLINE_VISIBILITY
210 any(_ValueType && __value);
211
Eric Fiselier58614252016-10-07 21:27:45 +0000212 template <class _ValueType, class ..._Args,
213 class _Tp = decay_t<_ValueType>,
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000214 class = enable_if_t<
215 is_constructible<_Tp, _Args...>::value &&
216 is_copy_constructible<_Tp>::value
217 >
218 >
219 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier58614252016-10-07 21:27:45 +0000220 explicit any(in_place_type_t<_ValueType>, _Args&&... __args);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000221
Eric Fiselier58614252016-10-07 21:27:45 +0000222 template <class _ValueType, class _Up, class ..._Args,
223 class _Tp = decay_t<_ValueType>,
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000224 class = enable_if_t<
225 is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
226 is_copy_constructible<_Tp>::value>
227 >
228 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier58614252016-10-07 21:27:45 +0000229 explicit any(in_place_type_t<_ValueType>, initializer_list<_Up>, _Args&&... __args);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000230
231 _LIBCPP_INLINE_VISIBILITY
232 ~any() { this->reset(); }
233
234 // assignments
235 _LIBCPP_INLINE_VISIBILITY
236 any & operator=(any const & __rhs) {
237 any(__rhs).swap(*this);
238 return *this;
239 }
240
241 _LIBCPP_INLINE_VISIBILITY
242 any & operator=(any && __rhs) _NOEXCEPT {
243 any(_VSTD::move(__rhs)).swap(*this);
244 return *this;
245 }
246
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000247 template <
248 class _ValueType
Eric Fiselier58614252016-10-07 21:27:45 +0000249 , class _Tp = decay_t<_ValueType>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000250 , class = enable_if_t<
Eric Fiselier58614252016-10-07 21:27:45 +0000251 !is_same<_Tp, any>::value
Eric Fiselieraea4e192016-10-16 02:51:50 +0000252 && is_copy_constructible<_Tp>::value>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000253 >
254 _LIBCPP_INLINE_VISIBILITY
255 any & operator=(_ValueType && __rhs);
256
Eric Fiselier58614252016-10-07 21:27:45 +0000257 template <class _ValueType, class ..._Args,
258 class _Tp = decay_t<_ValueType>,
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000259 class = enable_if_t<
260 is_constructible<_Tp, _Args...>::value &&
261 is_copy_constructible<_Tp>::value>
262 >
263 _LIBCPP_INLINE_VISIBILITY
Marshall Clow45ff9822017-04-12 22:51:27 +0000264 _Tp& emplace(_Args&&... args);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000265
Eric Fiselier58614252016-10-07 21:27:45 +0000266 template <class _ValueType, class _Up, class ..._Args,
267 class _Tp = decay_t<_ValueType>,
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000268 class = enable_if_t<
269 is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
270 is_copy_constructible<_Tp>::value>
271 >
272 _LIBCPP_INLINE_VISIBILITY
Marshall Clow45ff9822017-04-12 22:51:27 +0000273 _Tp& emplace(initializer_list<_Up>, _Args&&...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000274
275 // 6.3.3 any modifiers
276 _LIBCPP_INLINE_VISIBILITY
277 void reset() _NOEXCEPT { if (__h) this->__call(_Action::_Destroy); }
278
279 _LIBCPP_INLINE_VISIBILITY
280 void swap(any & __rhs) _NOEXCEPT;
281
282 // 6.3.4 any observers
283 _LIBCPP_INLINE_VISIBILITY
284 bool has_value() const _NOEXCEPT { return __h != nullptr; }
285
286#if !defined(_LIBCPP_NO_RTTI)
287 _LIBCPP_INLINE_VISIBILITY
288 const type_info & type() const _NOEXCEPT {
289 if (__h) {
290 return *static_cast<type_info const *>(this->__call(_Action::_TypeInfo));
291 } else {
292 return typeid(void);
293 }
294 }
295#endif
296
297private:
298 typedef __any_imp::_Action _Action;
299 using _HandleFuncPtr = void* (*)(_Action, any const *, any *, const type_info *,
300 const void* __fallback_info);
301
302 union _Storage {
303 constexpr _Storage() : __ptr(nullptr) {}
304 void * __ptr;
305 __any_imp::_Buffer __buf;
306 };
307
Louis Dionne16fe2952018-07-11 23:14:33 +0000308 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000309 void * __call(_Action __a, any * __other = nullptr,
310 type_info const * __info = nullptr,
311 const void* __fallback_info = nullptr) const
312 {
313 return __h(__a, this, __other, __info, __fallback_info);
314 }
315
Louis Dionne16fe2952018-07-11 23:14:33 +0000316 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000317 void * __call(_Action __a, any * __other = nullptr,
318 type_info const * __info = nullptr,
319 const void* __fallback_info = nullptr)
320 {
321 return __h(__a, this, __other, __info, __fallback_info);
322 }
323
324 template <class>
325 friend struct __any_imp::_SmallHandler;
326 template <class>
327 friend struct __any_imp::_LargeHandler;
328
329 template <class _ValueType>
330 friend add_pointer_t<add_const_t<_ValueType>>
331 any_cast(any const *) _NOEXCEPT;
332
333 template <class _ValueType>
334 friend add_pointer_t<_ValueType>
335 any_cast(any *) _NOEXCEPT;
336
337 _HandleFuncPtr __h = nullptr;
338 _Storage __s;
339};
340
341namespace __any_imp
342{
343 template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000344 struct _LIBCPP_TEMPLATE_VIS _SmallHandler
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000345 {
346 _LIBCPP_INLINE_VISIBILITY
347 static void* __handle(_Action __act, any const * __this, any * __other,
348 type_info const * __info, const void* __fallback_info)
349 {
350 switch (__act)
351 {
352 case _Action::_Destroy:
353 __destroy(const_cast<any &>(*__this));
354 return nullptr;
355 case _Action::_Copy:
356 __copy(*__this, *__other);
357 return nullptr;
358 case _Action::_Move:
359 __move(const_cast<any &>(*__this), *__other);
360 return nullptr;
361 case _Action::_Get:
362 return __get(const_cast<any &>(*__this), __info, __fallback_info);
363 case _Action::_TypeInfo:
364 return __type_info();
365 }
366 }
367
368 template <class ..._Args>
369 _LIBCPP_INLINE_VISIBILITY
Marshall Clow45ff9822017-04-12 22:51:27 +0000370 static _Tp& __create(any & __dest, _Args&&... __args) {
371 _Tp* __ret = ::new (static_cast<void*>(&__dest.__s.__buf)) _Tp(_VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000372 __dest.__h = &_SmallHandler::__handle;
Marshall Clow45ff9822017-04-12 22:51:27 +0000373 return *__ret;
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000374 }
375
376 private:
377 _LIBCPP_INLINE_VISIBILITY
378 static void __destroy(any & __this) {
379 _Tp & __value = *static_cast<_Tp *>(static_cast<void*>(&__this.__s.__buf));
380 __value.~_Tp();
381 __this.__h = nullptr;
382 }
383
384 _LIBCPP_INLINE_VISIBILITY
385 static void __copy(any const & __this, any & __dest) {
386 _SmallHandler::__create(__dest, *static_cast<_Tp const *>(
387 static_cast<void const *>(&__this.__s.__buf)));
388 }
389
390 _LIBCPP_INLINE_VISIBILITY
391 static void __move(any & __this, any & __dest) {
392 _SmallHandler::__create(__dest, _VSTD::move(
393 *static_cast<_Tp*>(static_cast<void*>(&__this.__s.__buf))));
394 __destroy(__this);
395 }
396
397 _LIBCPP_INLINE_VISIBILITY
398 static void* __get(any & __this,
399 type_info const * __info,
400 const void* __fallback_id)
401 {
402 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id))
403 return static_cast<void*>(&__this.__s.__buf);
404 return nullptr;
405 }
406
407 _LIBCPP_INLINE_VISIBILITY
408 static void* __type_info()
409 {
410#if !defined(_LIBCPP_NO_RTTI)
411 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
412#else
413 return nullptr;
414#endif
415 }
416 };
417
418 template <class _Tp>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000419 struct _LIBCPP_TEMPLATE_VIS _LargeHandler
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000420 {
421 _LIBCPP_INLINE_VISIBILITY
422 static void* __handle(_Action __act, any const * __this,
423 any * __other, type_info const * __info,
424 void const* __fallback_info)
425 {
426 switch (__act)
427 {
428 case _Action::_Destroy:
429 __destroy(const_cast<any &>(*__this));
430 return nullptr;
431 case _Action::_Copy:
432 __copy(*__this, *__other);
433 return nullptr;
434 case _Action::_Move:
435 __move(const_cast<any &>(*__this), *__other);
436 return nullptr;
437 case _Action::_Get:
438 return __get(const_cast<any &>(*__this), __info, __fallback_info);
439 case _Action::_TypeInfo:
440 return __type_info();
441 }
442 }
443
444 template <class ..._Args>
445 _LIBCPP_INLINE_VISIBILITY
Marshall Clow45ff9822017-04-12 22:51:27 +0000446 static _Tp& __create(any & __dest, _Args&&... __args) {
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000447 typedef allocator<_Tp> _Alloc;
448 typedef __allocator_destructor<_Alloc> _Dp;
449 _Alloc __a;
450 unique_ptr<_Tp, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Marshall Clow45ff9822017-04-12 22:51:27 +0000451 _Tp* __ret = ::new ((void*)__hold.get()) _Tp(_VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000452 __dest.__s.__ptr = __hold.release();
453 __dest.__h = &_LargeHandler::__handle;
Marshall Clow45ff9822017-04-12 22:51:27 +0000454 return *__ret;
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000455 }
456
457 private:
458
459 _LIBCPP_INLINE_VISIBILITY
460 static void __destroy(any & __this){
461 delete static_cast<_Tp*>(__this.__s.__ptr);
462 __this.__h = nullptr;
463 }
464
465 _LIBCPP_INLINE_VISIBILITY
466 static void __copy(any const & __this, any & __dest) {
467 _LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s.__ptr));
468 }
469
470 _LIBCPP_INLINE_VISIBILITY
471 static void __move(any & __this, any & __dest) {
472 __dest.__s.__ptr = __this.__s.__ptr;
473 __dest.__h = &_LargeHandler::__handle;
474 __this.__h = nullptr;
475 }
476
477 _LIBCPP_INLINE_VISIBILITY
478 static void* __get(any & __this, type_info const * __info,
479 void const* __fallback_info)
480 {
481 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info))
482 return static_cast<void*>(__this.__s.__ptr);
483 return nullptr;
484
485 }
486
487 _LIBCPP_INLINE_VISIBILITY
488 static void* __type_info()
489 {
490#if !defined(_LIBCPP_NO_RTTI)
491 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
492#else
493 return nullptr;
494#endif
495 }
496 };
497
498} // namespace __any_imp
499
500
Eric Fiselier58614252016-10-07 21:27:45 +0000501template <class _ValueType, class _Tp, class>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000502any::any(_ValueType && __v) : __h(nullptr)
503{
Eric Fiselier58614252016-10-07 21:27:45 +0000504 __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_ValueType>(__v));
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000505}
506
Eric Fiselier58614252016-10-07 21:27:45 +0000507template <class _ValueType, class ..._Args, class _Tp, class>
508any::any(in_place_type_t<_ValueType>, _Args&&... __args) {
509 __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000510};
511
Eric Fiselier58614252016-10-07 21:27:45 +0000512template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
513any::any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) {
514 __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000515}
516
Eric Fiselier58614252016-10-07 21:27:45 +0000517template <class _ValueType, class, class>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000518inline _LIBCPP_INLINE_VISIBILITY
519any & any::operator=(_ValueType && __v)
520{
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000521 any(_VSTD::forward<_ValueType>(__v)).swap(*this);
522 return *this;
523}
524
Eric Fiselier58614252016-10-07 21:27:45 +0000525template <class _ValueType, class ..._Args, class _Tp, class>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000526inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow45ff9822017-04-12 22:51:27 +0000527_Tp& any::emplace(_Args&&... __args) {
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000528 reset();
Marshall Clow45ff9822017-04-12 22:51:27 +0000529 return __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000530}
531
Eric Fiselier58614252016-10-07 21:27:45 +0000532template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000533inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow45ff9822017-04-12 22:51:27 +0000534_Tp& any::emplace(initializer_list<_Up> __il, _Args&&... __args) {
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000535 reset();
Marshall Clow45ff9822017-04-12 22:51:27 +0000536 return __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000537}
538
539inline _LIBCPP_INLINE_VISIBILITY
540void any::swap(any & __rhs) _NOEXCEPT
541{
Eric Fiselier58614252016-10-07 21:27:45 +0000542 if (this == &__rhs)
543 return;
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000544 if (__h && __rhs.__h) {
545 any __tmp;
546 __rhs.__call(_Action::_Move, &__tmp);
547 this->__call(_Action::_Move, &__rhs);
548 __tmp.__call(_Action::_Move, this);
549 }
550 else if (__h) {
551 this->__call(_Action::_Move, &__rhs);
552 }
553 else if (__rhs.__h) {
554 __rhs.__call(_Action::_Move, this);
555 }
556}
557
558// 6.4 Non-member functions
559
560inline _LIBCPP_INLINE_VISIBILITY
561void swap(any & __lhs, any & __rhs) _NOEXCEPT
562{
563 __lhs.swap(__rhs);
564}
565
566template <class _Tp, class ..._Args>
567inline _LIBCPP_INLINE_VISIBILITY
568any make_any(_Args&&... __args) {
Eric Fiselier99b81712016-11-17 19:24:04 +0000569 return any(in_place_type<_Tp>, _VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000570}
571
572template <class _Tp, class _Up, class ..._Args>
573inline _LIBCPP_INLINE_VISIBILITY
574any make_any(initializer_list<_Up> __il, _Args&&... __args) {
Eric Fiselier99b81712016-11-17 19:24:04 +0000575 return any(in_place_type<_Tp>, __il, _VSTD::forward<_Args>(__args)...);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000576}
577
578template <class _ValueType>
579inline _LIBCPP_INLINE_VISIBILITY
580_ValueType any_cast(any const & __v)
581{
Eric Fiselier58614252016-10-07 21:27:45 +0000582 using _RawValueType = __uncvref_t<_ValueType>;
583 static_assert(is_constructible<_ValueType, _RawValueType const &>::value,
Eric Fiselier0e1f0902016-10-16 01:43:43 +0000584 "ValueType is required to be a const lvalue reference "
585 "or a CopyConstructible type");
Eric Fiselier58614252016-10-07 21:27:45 +0000586 auto __tmp = _VSTD::any_cast<add_const_t<_RawValueType>>(&__v);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000587 if (__tmp == nullptr)
Marshall Clowed3e2292016-08-25 17:47:09 +0000588 __throw_bad_any_cast();
Eric Fiselier58614252016-10-07 21:27:45 +0000589 return static_cast<_ValueType>(*__tmp);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000590}
591
592template <class _ValueType>
593inline _LIBCPP_INLINE_VISIBILITY
594_ValueType any_cast(any & __v)
595{
Eric Fiselier58614252016-10-07 21:27:45 +0000596 using _RawValueType = __uncvref_t<_ValueType>;
597 static_assert(is_constructible<_ValueType, _RawValueType &>::value,
Eric Fiselier0e1f0902016-10-16 01:43:43 +0000598 "ValueType is required to be an lvalue reference "
599 "or a CopyConstructible type");
Eric Fiselier58614252016-10-07 21:27:45 +0000600 auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000601 if (__tmp == nullptr)
Marshall Clowed3e2292016-08-25 17:47:09 +0000602 __throw_bad_any_cast();
Eric Fiselier58614252016-10-07 21:27:45 +0000603 return static_cast<_ValueType>(*__tmp);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000604}
605
606template <class _ValueType>
607inline _LIBCPP_INLINE_VISIBILITY
608_ValueType any_cast(any && __v)
609{
Eric Fiselier58614252016-10-07 21:27:45 +0000610 using _RawValueType = __uncvref_t<_ValueType>;
611 static_assert(is_constructible<_ValueType, _RawValueType>::value,
Eric Fiselier0e1f0902016-10-16 01:43:43 +0000612 "ValueType is required to be an rvalue reference "
613 "or a CopyConstructible type");
Eric Fiselier58614252016-10-07 21:27:45 +0000614 auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000615 if (__tmp == nullptr)
Marshall Clowed3e2292016-08-25 17:47:09 +0000616 __throw_bad_any_cast();
Eric Fiselier58614252016-10-07 21:27:45 +0000617 return static_cast<_ValueType>(_VSTD::move(*__tmp));
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000618}
619
620template <class _ValueType>
621inline _LIBCPP_INLINE_VISIBILITY
622add_pointer_t<add_const_t<_ValueType>>
623any_cast(any const * __any) _NOEXCEPT
624{
625 static_assert(!is_reference<_ValueType>::value,
626 "_ValueType may not be a reference.");
627 return _VSTD::any_cast<_ValueType>(const_cast<any *>(__any));
628}
629
Eric Fiselier9af18272016-10-16 11:56:38 +0000630template <class _RetType>
631inline _LIBCPP_INLINE_VISIBILITY
632_RetType __pointer_or_func_cast(void* __p, /*IsFunction*/false_type) noexcept {
633 return static_cast<_RetType>(__p);
634}
635
636template <class _RetType>
637inline _LIBCPP_INLINE_VISIBILITY
638_RetType __pointer_or_func_cast(void*, /*IsFunction*/true_type) noexcept {
639 return nullptr;
640}
641
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000642template <class _ValueType>
643add_pointer_t<_ValueType>
644any_cast(any * __any) _NOEXCEPT
645{
646 using __any_imp::_Action;
647 static_assert(!is_reference<_ValueType>::value,
648 "_ValueType may not be a reference.");
649 typedef typename add_pointer<_ValueType>::type _ReturnType;
650 if (__any && __any->__h) {
Eric Fiselier9af18272016-10-16 11:56:38 +0000651 void *__p = __any->__call(_Action::_Get, nullptr,
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000652#if !defined(_LIBCPP_NO_RTTI)
653 &typeid(_ValueType),
654#else
655 nullptr,
656#endif
Eric Fiselier9af18272016-10-16 11:56:38 +0000657 __any_imp::__get_fallback_typeid<_ValueType>());
658 return _VSTD::__pointer_or_func_cast<_ReturnType>(
659 __p, is_function<_ValueType>{});
Eric Fiselierf6b3bfe2016-08-11 03:13:11 +0000660 }
661 return nullptr;
662}
663
664#endif // _LIBCPP_STD_VER > 14
665
666_LIBCPP_END_NAMESPACE_STD
667
668#endif // _LIBCPP_ANY