1 | // -*- C++ -*- |
2 | //===----------------------------------------------------------------------===// |
3 | // |
4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
5 | // See https://llvm.org/LICENSE.txt for license information. |
6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | |
10 | #ifndef _LIBCPP_ANY |
11 | #define _LIBCPP_ANY |
12 | |
13 | /* |
14 | any synopsis |
15 | |
16 | namespace std { |
17 | |
18 | class bad_any_cast : public bad_cast |
19 | { |
20 | public: |
21 | virtual const char* what() const noexcept; |
22 | }; |
23 | |
24 | class any |
25 | { |
26 | public: |
27 | |
28 | // 6.3.1 any construct/destruct |
29 | any() noexcept; |
30 | |
31 | any(const any& other); |
32 | any(any&& other) noexcept; |
33 | |
34 | template <class ValueType> |
35 | any(ValueType&& value); |
36 | |
37 | ~any(); |
38 | |
39 | // 6.3.2 any assignments |
40 | any& operator=(const any& rhs); |
41 | any& operator=(any&& rhs) noexcept; |
42 | |
43 | template <class ValueType> |
44 | any& operator=(ValueType&& rhs); |
45 | |
46 | // 6.3.3 any modifiers |
47 | template <class ValueType, class... Args> |
48 | decay_t<ValueType>& emplace(Args&&... args); |
49 | template <class ValueType, class U, class... Args> |
50 | decay_t<ValueType>& emplace(initializer_list<U>, Args&&...); |
51 | void reset() noexcept; |
52 | void swap(any& rhs) noexcept; |
53 | |
54 | // 6.3.4 any observers |
55 | bool has_value() const noexcept; |
56 | const type_info& type() const noexcept; |
57 | }; |
58 | |
59 | // 6.4 Non-member functions |
60 | void swap(any& x, any& y) noexcept; |
61 | |
62 | template <class T, class ...Args> |
63 | any make_any(Args&& ...args); |
64 | template <class T, class U, class ...Args> |
65 | any make_any(initializer_list<U>, Args&& ...args); |
66 | |
67 | template<class ValueType> |
68 | ValueType any_cast(const any& operand); |
69 | template<class ValueType> |
70 | ValueType any_cast(any& operand); |
71 | template<class ValueType> |
72 | ValueType any_cast(any&& operand); |
73 | |
74 | template<class ValueType> |
75 | const ValueType* any_cast(const any* operand) noexcept; |
76 | template<class ValueType> |
77 | ValueType* any_cast(any* operand) noexcept; |
78 | |
79 | } // namespace std |
80 | |
81 | */ |
82 | |
83 | #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS) |
84 | # include <__cxx03/__config> |
85 | #else |
86 | # include <__config> |
87 | # include <__memory/allocator.h> |
88 | # include <__memory/allocator_destructor.h> |
89 | # include <__memory/allocator_traits.h> |
90 | # include <__memory/unique_ptr.h> |
91 | # include <__type_traits/add_cv_quals.h> |
92 | # include <__type_traits/add_pointer.h> |
93 | # include <__type_traits/aligned_storage.h> |
94 | # include <__type_traits/conditional.h> |
95 | # include <__type_traits/decay.h> |
96 | # include <__type_traits/enable_if.h> |
97 | # include <__type_traits/is_constructible.h> |
98 | # include <__type_traits/is_function.h> |
99 | # include <__type_traits/is_nothrow_constructible.h> |
100 | # include <__type_traits/is_reference.h> |
101 | # include <__type_traits/is_same.h> |
102 | # include <__type_traits/is_void.h> |
103 | # include <__type_traits/remove_cv.h> |
104 | # include <__type_traits/remove_cvref.h> |
105 | # include <__type_traits/remove_reference.h> |
106 | # include <__utility/forward.h> |
107 | # include <__utility/in_place.h> |
108 | # include <__utility/move.h> |
109 | # include <__utility/unreachable.h> |
110 | # include <__verbose_abort> |
111 | # include <initializer_list> |
112 | # include <typeinfo> |
113 | # include <version> |
114 | |
115 | # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
116 | # pragma GCC system_header |
117 | # endif |
118 | |
119 | _LIBCPP_PUSH_MACROS |
120 | # include <__undef_macros> |
121 | |
122 | _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD |
123 | class _LIBCPP_EXPORTED_FROM_ABI bad_any_cast : public bad_cast { |
124 | public: |
125 | const char* what() const _NOEXCEPT override; |
126 | }; |
127 | _LIBCPP_END_UNVERSIONED_NAMESPACE_STD |
128 | |
129 | _LIBCPP_BEGIN_NAMESPACE_STD |
130 | |
131 | # if _LIBCPP_STD_VER >= 17 |
132 | |
133 | [[noreturn]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_any_cast() { |
134 | # if _LIBCPP_HAS_EXCEPTIONS |
135 | throw bad_any_cast(); |
136 | # else |
137 | _LIBCPP_VERBOSE_ABORT("bad_any_cast was thrown in -fno-exceptions mode" ); |
138 | # endif |
139 | } |
140 | |
141 | // Forward declarations |
142 | class any; |
143 | |
144 | template <class _ValueType> |
145 | _LIBCPP_HIDE_FROM_ABI add_pointer_t<add_const_t<_ValueType>> any_cast(any const*) _NOEXCEPT; |
146 | |
147 | template <class _ValueType> |
148 | _LIBCPP_HIDE_FROM_ABI add_pointer_t<_ValueType> any_cast(any*) _NOEXCEPT; |
149 | |
150 | namespace __any_imp { |
151 | _LIBCPP_SUPPRESS_DEPRECATED_PUSH |
152 | using _Buffer _LIBCPP_NODEBUG = aligned_storage_t<3 * sizeof(void*), alignof(void*)>; |
153 | _LIBCPP_SUPPRESS_DEPRECATED_POP |
154 | |
155 | template <class _Tp> |
156 | using _IsSmallObject _LIBCPP_NODEBUG = |
157 | integral_constant<bool, |
158 | sizeof(_Tp) <= sizeof(_Buffer) && alignof(_Buffer) % alignof(_Tp) == 0 && |
159 | is_nothrow_move_constructible<_Tp>::value >; |
160 | |
161 | enum class _Action { _Destroy, _Copy, _Move, _Get, _TypeInfo }; |
162 | |
163 | template <class _Tp> |
164 | struct _SmallHandler; |
165 | template <class _Tp> |
166 | struct _LargeHandler; |
167 | |
168 | template <class _Tp> |
169 | struct __unique_typeinfo { |
170 | static constexpr int __id = 0; |
171 | }; |
172 | |
173 | template <class _Tp> |
174 | inline _LIBCPP_HIDE_FROM_ABI constexpr const void* __get_fallback_typeid() { |
175 | return &__unique_typeinfo<remove_cv_t<remove_reference_t<_Tp>>>::__id; |
176 | } |
177 | |
178 | template <class _Tp> |
179 | inline _LIBCPP_HIDE_FROM_ABI bool __compare_typeid(type_info const* __id, const void* __fallback_id) { |
180 | # if _LIBCPP_HAS_RTTI |
181 | if (__id && *__id == typeid(_Tp)) |
182 | return true; |
183 | # endif |
184 | return !__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>(); |
185 | } |
186 | |
187 | template <class _Tp> |
188 | using _Handler _LIBCPP_NODEBUG = conditional_t< _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>; |
189 | |
190 | } // namespace __any_imp |
191 | |
192 | class any { |
193 | public: |
194 | // construct/destruct |
195 | _LIBCPP_HIDE_FROM_ABI constexpr any() _NOEXCEPT : __h_(nullptr) {} |
196 | |
197 | _LIBCPP_HIDE_FROM_ABI any(any const& __other) : __h_(nullptr) { |
198 | if (__other.__h_) |
199 | __other.__call(a: _Action::_Copy, other: this); |
200 | } |
201 | |
202 | _LIBCPP_HIDE_FROM_ABI any(any&& __other) _NOEXCEPT : __h_(nullptr) { |
203 | if (__other.__h_) |
204 | __other.__call(a: _Action::_Move, other: this); |
205 | } |
206 | |
207 | template < class _ValueType, |
208 | class _Tp = decay_t<_ValueType>, |
209 | class = enable_if_t< !is_same<_Tp, any>::value && !__is_inplace_type<_ValueType>::value && |
210 | is_copy_constructible<_Tp>::value> > |
211 | _LIBCPP_HIDE_FROM_ABI any(_ValueType&& __value); |
212 | |
213 | template <class _ValueType, |
214 | class... _Args, |
215 | class _Tp = decay_t<_ValueType>, |
216 | class = enable_if_t< is_constructible<_Tp, _Args...>::value && is_copy_constructible<_Tp>::value > > |
217 | _LIBCPP_HIDE_FROM_ABI explicit any(in_place_type_t<_ValueType>, _Args&&... __args); |
218 | |
219 | template <class _ValueType, |
220 | class _Up, |
221 | class... _Args, |
222 | class _Tp = decay_t<_ValueType>, |
223 | class = enable_if_t< is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value && |
224 | is_copy_constructible<_Tp>::value> > |
225 | _LIBCPP_HIDE_FROM_ABI explicit any(in_place_type_t<_ValueType>, initializer_list<_Up>, _Args&&... __args); |
226 | |
227 | _LIBCPP_HIDE_FROM_ABI ~any() { this->reset(); } |
228 | |
229 | // assignments |
230 | _LIBCPP_HIDE_FROM_ABI any& operator=(any const& __rhs) { |
231 | any(__rhs).swap(rhs&: *this); |
232 | return *this; |
233 | } |
234 | |
235 | _LIBCPP_HIDE_FROM_ABI any& operator=(any&& __rhs) _NOEXCEPT { |
236 | any(std::move(__rhs)).swap(rhs&: *this); |
237 | return *this; |
238 | } |
239 | |
240 | template < class _ValueType, |
241 | class _Tp = decay_t<_ValueType>, |
242 | class = enable_if_t< !is_same<_Tp, any>::value && is_copy_constructible<_Tp>::value> > |
243 | _LIBCPP_HIDE_FROM_ABI any& operator=(_ValueType&& __rhs); |
244 | |
245 | template <class _ValueType, |
246 | class... _Args, |
247 | class _Tp = decay_t<_ValueType>, |
248 | class = enable_if_t< is_constructible<_Tp, _Args...>::value && is_copy_constructible<_Tp>::value> > |
249 | _LIBCPP_HIDE_FROM_ABI _Tp& emplace(_Args&&...); |
250 | |
251 | template <class _ValueType, |
252 | class _Up, |
253 | class... _Args, |
254 | class _Tp = decay_t<_ValueType>, |
255 | class = enable_if_t< is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value && |
256 | is_copy_constructible<_Tp>::value> > |
257 | _LIBCPP_HIDE_FROM_ABI _Tp& emplace(initializer_list<_Up>, _Args&&...); |
258 | |
259 | // 6.3.3 any modifiers |
260 | _LIBCPP_HIDE_FROM_ABI void reset() _NOEXCEPT { |
261 | if (__h_) |
262 | this->__call(a: _Action::_Destroy); |
263 | } |
264 | |
265 | _LIBCPP_HIDE_FROM_ABI void swap(any& __rhs) _NOEXCEPT; |
266 | |
267 | // 6.3.4 any observers |
268 | _LIBCPP_HIDE_FROM_ABI bool has_value() const _NOEXCEPT { return __h_ != nullptr; } |
269 | |
270 | # if _LIBCPP_HAS_RTTI |
271 | _LIBCPP_HIDE_FROM_ABI const type_info& type() const _NOEXCEPT { |
272 | if (__h_) { |
273 | return *static_cast<type_info const*>(this->__call(a: _Action::_TypeInfo)); |
274 | } else { |
275 | return typeid(void); |
276 | } |
277 | } |
278 | # endif |
279 | |
280 | private: |
281 | using _Action _LIBCPP_NODEBUG = __any_imp::_Action; |
282 | using _HandleFuncPtr |
283 | _LIBCPP_NODEBUG = void* (*)(_Action, any const*, any*, const type_info*, const void* __fallback_info); |
284 | |
285 | union _Storage { |
286 | _LIBCPP_HIDE_FROM_ABI constexpr _Storage() : __ptr(nullptr) {} |
287 | void* __ptr; |
288 | __any_imp::_Buffer __buf; |
289 | }; |
290 | |
291 | _LIBCPP_HIDE_FROM_ABI void* |
292 | __call(_Action __a, any* __other = nullptr, type_info const* __info = nullptr, const void* __fallback_info = nullptr) |
293 | const { |
294 | return __h_(__a, this, __other, __info, __fallback_info); |
295 | } |
296 | |
297 | _LIBCPP_HIDE_FROM_ABI void* __call( |
298 | _Action __a, any* __other = nullptr, type_info const* __info = nullptr, const void* __fallback_info = nullptr) { |
299 | return __h_(__a, this, __other, __info, __fallback_info); |
300 | } |
301 | |
302 | template <class> |
303 | friend struct __any_imp::_SmallHandler; |
304 | template <class> |
305 | friend struct __any_imp::_LargeHandler; |
306 | |
307 | template <class _ValueType> |
308 | friend add_pointer_t<add_const_t<_ValueType>> any_cast(any const*) _NOEXCEPT; |
309 | |
310 | template <class _ValueType> |
311 | friend add_pointer_t<_ValueType> any_cast(any*) _NOEXCEPT; |
312 | |
313 | _HandleFuncPtr __h_ = nullptr; |
314 | _Storage __s_; |
315 | }; |
316 | |
317 | namespace __any_imp { |
318 | template <class _Tp> |
319 | struct _SmallHandler { |
320 | _LIBCPP_HIDE_FROM_ABI static void* |
321 | __handle(_Action __act, any const* __this, any* __other, type_info const* __info, const void* __fallback_info) { |
322 | switch (__act) { |
323 | case _Action::_Destroy: |
324 | __destroy(this&: const_cast<any&>(*__this)); |
325 | return nullptr; |
326 | case _Action::_Copy: |
327 | __copy(this: *__this, dest&: *__other); |
328 | return nullptr; |
329 | case _Action::_Move: |
330 | __move(this&: const_cast<any&>(*__this), dest&: *__other); |
331 | return nullptr; |
332 | case _Action::_Get: |
333 | return __get(this&: const_cast<any&>(*__this), __info, fallback_id: __fallback_info); |
334 | case _Action::_TypeInfo: |
335 | return __type_info(); |
336 | } |
337 | __libcpp_unreachable(); |
338 | } |
339 | |
340 | template <class... _Args> |
341 | _LIBCPP_HIDE_FROM_ABI static _Tp& __create(any& __dest, _Args&&... __args) { |
342 | typedef allocator<_Tp> _Alloc; |
343 | typedef allocator_traits<_Alloc> _ATraits; |
344 | _Alloc __a; |
345 | _Tp* __ret = static_cast<_Tp*>(static_cast<void*>(&__dest.__s_.__buf)); |
346 | _ATraits::construct(__a, __ret, std::forward<_Args>(__args)...); |
347 | __dest.__h_ = &_SmallHandler::__handle; |
348 | return *__ret; |
349 | } |
350 | |
351 | private: |
352 | _LIBCPP_HIDE_FROM_ABI static void __destroy(any& __this) { |
353 | typedef allocator<_Tp> _Alloc; |
354 | typedef allocator_traits<_Alloc> _ATraits; |
355 | _Alloc __a; |
356 | _Tp* __p = static_cast<_Tp*>(static_cast<void*>(&__this.__s_.__buf)); |
357 | _ATraits::destroy(__a, __p); |
358 | __this.__h_ = nullptr; |
359 | } |
360 | |
361 | _LIBCPP_HIDE_FROM_ABI static void __copy(any const& __this, any& __dest) { |
362 | _SmallHandler::__create(__dest, *static_cast<_Tp const*>(static_cast<void const*>(&__this.__s_.__buf))); |
363 | } |
364 | |
365 | _LIBCPP_HIDE_FROM_ABI static void __move(any& __this, any& __dest) { |
366 | _SmallHandler::__create(__dest, std::move(*static_cast<_Tp*>(static_cast<void*>(&__this.__s_.__buf)))); |
367 | __destroy(__this); |
368 | } |
369 | |
370 | _LIBCPP_HIDE_FROM_ABI static void* __get(any& __this, type_info const* __info, const void* __fallback_id) { |
371 | if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id)) |
372 | return static_cast<void*>(&__this.__s_.__buf); |
373 | return nullptr; |
374 | } |
375 | |
376 | _LIBCPP_HIDE_FROM_ABI static void* __type_info() { |
377 | # if _LIBCPP_HAS_RTTI |
378 | return const_cast<void*>(static_cast<void const*>(&typeid(_Tp))); |
379 | # else |
380 | return nullptr; |
381 | # endif |
382 | } |
383 | }; |
384 | |
385 | template <class _Tp> |
386 | struct _LargeHandler { |
387 | _LIBCPP_HIDE_FROM_ABI static void* |
388 | __handle(_Action __act, any const* __this, any* __other, type_info const* __info, void const* __fallback_info) { |
389 | switch (__act) { |
390 | case _Action::_Destroy: |
391 | __destroy(this&: const_cast<any&>(*__this)); |
392 | return nullptr; |
393 | case _Action::_Copy: |
394 | __copy(this: *__this, dest&: *__other); |
395 | return nullptr; |
396 | case _Action::_Move: |
397 | __move(this&: const_cast<any&>(*__this), dest&: *__other); |
398 | return nullptr; |
399 | case _Action::_Get: |
400 | return __get(this&: const_cast<any&>(*__this), __info, __fallback_info); |
401 | case _Action::_TypeInfo: |
402 | return __type_info(); |
403 | } |
404 | __libcpp_unreachable(); |
405 | } |
406 | |
407 | template <class... _Args> |
408 | _LIBCPP_HIDE_FROM_ABI static _Tp& __create(any& __dest, _Args&&... __args) { |
409 | typedef allocator<_Tp> _Alloc; |
410 | typedef allocator_traits<_Alloc> _ATraits; |
411 | typedef __allocator_destructor<_Alloc> _Dp; |
412 | _Alloc __a; |
413 | unique_ptr<_Tp, _Dp> __hold(_ATraits::allocate(__a, 1), _Dp(__a, 1)); |
414 | _Tp* __ret = __hold.get(); |
415 | _ATraits::construct(__a, __ret, std::forward<_Args>(__args)...); |
416 | __dest.__s_.__ptr = __hold.release(); |
417 | __dest.__h_ = &_LargeHandler::__handle; |
418 | return *__ret; |
419 | } |
420 | |
421 | private: |
422 | _LIBCPP_HIDE_FROM_ABI static void __destroy(any& __this) { |
423 | typedef allocator<_Tp> _Alloc; |
424 | typedef allocator_traits<_Alloc> _ATraits; |
425 | _Alloc __a; |
426 | _Tp* __p = static_cast<_Tp*>(__this.__s_.__ptr); |
427 | _ATraits::destroy(__a, __p); |
428 | _ATraits::deallocate(__a, __p, 1); |
429 | __this.__h_ = nullptr; |
430 | } |
431 | |
432 | _LIBCPP_HIDE_FROM_ABI static void __copy(any const& __this, any& __dest) { |
433 | _LargeHandler::__create(__dest, *static_cast<_Tp const*>(__this.__s_.__ptr)); |
434 | } |
435 | |
436 | _LIBCPP_HIDE_FROM_ABI static void __move(any& __this, any& __dest) { |
437 | __dest.__s_.__ptr = __this.__s_.__ptr; |
438 | __dest.__h_ = &_LargeHandler::__handle; |
439 | __this.__h_ = nullptr; |
440 | } |
441 | |
442 | _LIBCPP_HIDE_FROM_ABI static void* __get(any& __this, type_info const* __info, void const* __fallback_info) { |
443 | if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info)) |
444 | return static_cast<void*>(__this.__s_.__ptr); |
445 | return nullptr; |
446 | } |
447 | |
448 | _LIBCPP_HIDE_FROM_ABI static void* __type_info() { |
449 | # if _LIBCPP_HAS_RTTI |
450 | return const_cast<void*>(static_cast<void const*>(&typeid(_Tp))); |
451 | # else |
452 | return nullptr; |
453 | # endif |
454 | } |
455 | }; |
456 | |
457 | } // namespace __any_imp |
458 | |
459 | template <class _ValueType, class _Tp, class> |
460 | any::any(_ValueType&& __v) : __h_(nullptr) { |
461 | __any_imp::_Handler<_Tp>::__create(*this, std::forward<_ValueType>(__v)); |
462 | } |
463 | |
464 | template <class _ValueType, class... _Args, class _Tp, class> |
465 | any::any(in_place_type_t<_ValueType>, _Args&&... __args) { |
466 | __any_imp::_Handler<_Tp>::__create(*this, std::forward<_Args>(__args)...); |
467 | } |
468 | |
469 | template <class _ValueType, class _Up, class... _Args, class _Tp, class> |
470 | any::any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) { |
471 | __any_imp::_Handler<_Tp>::__create(*this, __il, std::forward<_Args>(__args)...); |
472 | } |
473 | |
474 | template <class _ValueType, class, class> |
475 | inline _LIBCPP_HIDE_FROM_ABI any& any::operator=(_ValueType&& __v) { |
476 | any(std::forward<_ValueType>(__v)).swap(rhs&: *this); |
477 | return *this; |
478 | } |
479 | |
480 | template <class _ValueType, class... _Args, class _Tp, class> |
481 | inline _LIBCPP_HIDE_FROM_ABI _Tp& any::emplace(_Args&&... __args) { |
482 | reset(); |
483 | return __any_imp::_Handler<_Tp>::__create(*this, std::forward<_Args>(__args)...); |
484 | } |
485 | |
486 | template <class _ValueType, class _Up, class... _Args, class _Tp, class> |
487 | inline _LIBCPP_HIDE_FROM_ABI _Tp& any::emplace(initializer_list<_Up> __il, _Args&&... __args) { |
488 | reset(); |
489 | return __any_imp::_Handler<_Tp>::__create(*this, __il, std::forward<_Args>(__args)...); |
490 | } |
491 | |
492 | inline _LIBCPP_HIDE_FROM_ABI void any::swap(any& __rhs) _NOEXCEPT { |
493 | if (this == &__rhs) |
494 | return; |
495 | if (__h_ && __rhs.__h_) { |
496 | any __tmp; |
497 | __rhs.__call(a: _Action::_Move, other: &__tmp); |
498 | this->__call(a: _Action::_Move, other: &__rhs); |
499 | __tmp.__call(a: _Action::_Move, other: this); |
500 | } else if (__h_) { |
501 | this->__call(a: _Action::_Move, other: &__rhs); |
502 | } else if (__rhs.__h_) { |
503 | __rhs.__call(a: _Action::_Move, other: this); |
504 | } |
505 | } |
506 | |
507 | // 6.4 Non-member functions |
508 | |
509 | inline _LIBCPP_HIDE_FROM_ABI void swap(any& __lhs, any& __rhs) _NOEXCEPT { __lhs.swap(__rhs); } |
510 | |
511 | template <class _Tp, class... _Args> |
512 | inline _LIBCPP_HIDE_FROM_ABI any make_any(_Args&&... __args) { |
513 | return any(in_place_type<_Tp>, std::forward<_Args>(__args)...); |
514 | } |
515 | |
516 | template <class _Tp, class _Up, class... _Args> |
517 | inline _LIBCPP_HIDE_FROM_ABI any make_any(initializer_list<_Up> __il, _Args&&... __args) { |
518 | return any(in_place_type<_Tp>, __il, std::forward<_Args>(__args)...); |
519 | } |
520 | |
521 | template <class _ValueType> |
522 | inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any const& __v) { |
523 | using _RawValueType = __remove_cvref_t<_ValueType>; |
524 | static_assert(is_constructible<_ValueType, _RawValueType const&>::value, |
525 | "ValueType is required to be a const lvalue reference " |
526 | "or a CopyConstructible type" ); |
527 | auto __tmp = std::any_cast<add_const_t<_RawValueType>>(&__v); |
528 | if (__tmp == nullptr) |
529 | std::__throw_bad_any_cast(); |
530 | return static_cast<_ValueType>(*__tmp); |
531 | } |
532 | |
533 | template <class _ValueType> |
534 | inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any& __v) { |
535 | using _RawValueType = __remove_cvref_t<_ValueType>; |
536 | static_assert(is_constructible<_ValueType, _RawValueType&>::value, |
537 | "ValueType is required to be an lvalue reference " |
538 | "or a CopyConstructible type" ); |
539 | auto __tmp = std::any_cast<_RawValueType>(&__v); |
540 | if (__tmp == nullptr) |
541 | std::__throw_bad_any_cast(); |
542 | return static_cast<_ValueType>(*__tmp); |
543 | } |
544 | |
545 | template <class _ValueType> |
546 | inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any&& __v) { |
547 | using _RawValueType = __remove_cvref_t<_ValueType>; |
548 | static_assert(is_constructible<_ValueType, _RawValueType>::value, |
549 | "ValueType is required to be an rvalue reference " |
550 | "or a CopyConstructible type" ); |
551 | auto __tmp = std::any_cast<_RawValueType>(&__v); |
552 | if (__tmp == nullptr) |
553 | std::__throw_bad_any_cast(); |
554 | return static_cast<_ValueType>(std::move(*__tmp)); |
555 | } |
556 | |
557 | template <class _ValueType> |
558 | inline _LIBCPP_HIDE_FROM_ABI add_pointer_t<add_const_t<_ValueType>> any_cast(any const* __any) _NOEXCEPT { |
559 | static_assert(!is_void_v<_ValueType>, "_ValueType may not be void." ); |
560 | static_assert(!is_reference<_ValueType>::value, "_ValueType may not be a reference." ); |
561 | return std::any_cast<_ValueType>(const_cast<any*>(__any)); |
562 | } |
563 | |
564 | template <class _RetType> |
565 | inline _LIBCPP_HIDE_FROM_ABI _RetType __pointer_or_func_cast(void* __p, /*IsFunction*/ false_type) noexcept { |
566 | return static_cast<_RetType>(__p); |
567 | } |
568 | |
569 | template <class _RetType> |
570 | inline _LIBCPP_HIDE_FROM_ABI _RetType __pointer_or_func_cast(void*, /*IsFunction*/ true_type) noexcept { |
571 | return nullptr; |
572 | } |
573 | |
574 | template <class _ValueType> |
575 | _LIBCPP_HIDE_FROM_ABI add_pointer_t<_ValueType> any_cast(any* __any) _NOEXCEPT { |
576 | using __any_imp::_Action; |
577 | static_assert(!is_void_v<_ValueType>, "_ValueType may not be void." ); |
578 | static_assert(!is_reference<_ValueType>::value, "_ValueType may not be a reference." ); |
579 | typedef add_pointer_t<_ValueType> _ReturnType; |
580 | if (__any && __any->__h_) { |
581 | void* __p = __any->__call( |
582 | _Action::_Get, |
583 | nullptr, |
584 | # if _LIBCPP_HAS_RTTI |
585 | &typeid(_ValueType), |
586 | # else |
587 | nullptr, |
588 | # endif |
589 | __any_imp::__get_fallback_typeid<_ValueType>()); |
590 | return std::__pointer_or_func_cast<_ReturnType>(__p, is_function<_ValueType>{}); |
591 | } |
592 | return nullptr; |
593 | } |
594 | |
595 | # endif // _LIBCPP_STD_VER >= 17 |
596 | |
597 | _LIBCPP_END_NAMESPACE_STD |
598 | |
599 | _LIBCPP_POP_MACROS |
600 | |
601 | # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17 |
602 | # include <chrono> |
603 | # endif |
604 | |
605 | # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 |
606 | # include <atomic> |
607 | # include <concepts> |
608 | # include <cstdlib> |
609 | # include <iosfwd> |
610 | # include <iterator> |
611 | # include <memory> |
612 | # include <stdexcept> |
613 | # include <type_traits> |
614 | # include <variant> |
615 | # endif |
616 | #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS) |
617 | |
618 | #endif // _LIBCPP_ANY |
619 | |