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
16namespace 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/construct_at.h>
88# include <__new/allocate.h>
89# include <__type_traits/add_cv_quals.h>
90# include <__type_traits/add_pointer.h>
91# include <__type_traits/conditional.h>
92# include <__type_traits/conjunction.h>
93# include <__type_traits/decay.h>
94# include <__type_traits/enable_if.h>
95# include <__type_traits/is_constructible.h>
96# include <__type_traits/is_function.h>
97# include <__type_traits/is_nothrow_constructible.h>
98# include <__type_traits/is_reference.h>
99# include <__type_traits/is_same.h>
100# include <__type_traits/is_void.h>
101# include <__type_traits/negation.h>
102# include <__type_traits/remove_cv.h>
103# include <__type_traits/remove_cvref.h>
104# include <__type_traits/remove_reference.h>
105# include <__utility/exception_guard.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
123class _LIBCPP_EXPORTED_FROM_ABI bad_any_cast : public bad_cast {
124public:
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
142class any;
143
144template <class _ValueType>
145_LIBCPP_HIDE_FROM_ABI add_pointer_t<add_const_t<_ValueType>> any_cast(any const*) noexcept;
146
147template <class _ValueType>
148_LIBCPP_HIDE_FROM_ABI add_pointer_t<_ValueType> any_cast(any*) noexcept;
149
150namespace __any_imp {
151inline constexpr size_t __small_buffer_size = 3 * sizeof(void*);
152inline constexpr size_t __small_buffer_alignment = alignof(void*);
153
154template <class _Tp>
155using _IsSmallObject _LIBCPP_NODEBUG =
156 integral_constant<bool,
157 sizeof(_Tp) <= __small_buffer_size && alignof(_Tp) <= __small_buffer_alignment &&
158 is_nothrow_move_constructible_v<_Tp>>;
159
160enum class _Action { _Destroy, _Copy, _Move, _Get, _TypeInfo };
161
162template <class _Tp>
163struct _SmallHandler;
164template <class _Tp>
165struct _LargeHandler;
166
167template <class _Tp>
168struct __unique_typeinfo {
169 static constexpr int __id = 0;
170};
171
172template <class _Tp>
173inline _LIBCPP_HIDE_FROM_ABI constexpr const void* __get_fallback_typeid() {
174 return &__unique_typeinfo<remove_cv_t<remove_reference_t<_Tp>>>::__id;
175}
176
177template <class _Tp>
178inline _LIBCPP_HIDE_FROM_ABI bool __compare_typeid(type_info const* __id, const void* __fallback_id) {
179# if _LIBCPP_HAS_RTTI
180 if (__id && *__id == typeid(_Tp))
181 return true;
182# endif
183 return !__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>();
184}
185
186template <class _Tp>
187using _Handler _LIBCPP_NODEBUG = conditional_t< _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>;
188
189} // namespace __any_imp
190
191class any {
192public:
193 // construct/destruct
194 _LIBCPP_HIDE_FROM_ABI constexpr any() noexcept : __h_(nullptr) {}
195
196 _LIBCPP_HIDE_FROM_ABI any(any const& __other) : __h_(nullptr) {
197 if (__other.__h_)
198 __other.__call(a: _Action::_Copy, other: this);
199 }
200
201 _LIBCPP_HIDE_FROM_ABI any(any&& __other) noexcept : __h_(nullptr) {
202 if (__other.__h_)
203 __other.__call(a: _Action::_Move, other: this);
204 }
205
206 template <
207 class _ValueType,
208 class _Tp = decay_t<_ValueType>,
209 enable_if_t<_And<_Not<is_same<_Tp, any>>, _Not<__is_inplace_type<_ValueType>>, is_copy_constructible<_Tp>>::value,
210 int> = 0>
211 _LIBCPP_HIDE_FROM_ABI any(_ValueType&& __value) : __h_(nullptr) {
212 __any_imp::_Handler<_Tp>::__create(*this, std::forward<_ValueType>(__value));
213 }
214
215 template <class _ValueType,
216 class... _Args,
217 class _Tp = decay_t<_ValueType>,
218 enable_if_t<is_constructible_v<_Tp, _Args...> && is_copy_constructible_v<_Tp>, int> = 0>
219 _LIBCPP_HIDE_FROM_ABI explicit any(in_place_type_t<_ValueType>, _Args&&... __args) {
220 __any_imp::_Handler<_Tp>::__create(*this, std::forward<_Args>(__args)...);
221 }
222
223 template <
224 class _ValueType,
225 class _Up,
226 class... _Args,
227 class _Tp = decay_t<_ValueType>,
228 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...> && is_copy_constructible_v<_Tp>, int> = 0>
229 _LIBCPP_HIDE_FROM_ABI explicit any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) {
230 __any_imp::_Handler<_Tp>::__create(*this, __il, std::forward<_Args>(__args)...);
231 }
232
233 _LIBCPP_HIDE_FROM_ABI ~any() { this->reset(); }
234
235 // assignments
236 _LIBCPP_HIDE_FROM_ABI any& operator=(any const& __rhs) {
237 any(__rhs).swap(rhs&: *this);
238 return *this;
239 }
240
241 _LIBCPP_HIDE_FROM_ABI any& operator=(any&& __rhs) noexcept {
242 any(std::move(__rhs)).swap(rhs&: *this);
243 return *this;
244 }
245
246 template <class _ValueType,
247 class _Tp = decay_t<_ValueType>,
248 enable_if_t<!is_same_v<_Tp, any> && is_copy_constructible_v<_Tp>, int> = 0>
249 _LIBCPP_HIDE_FROM_ABI any& operator=(_ValueType&& __rhs) {
250 any(std::forward<_ValueType>(__rhs)).swap(rhs&: *this);
251 return *this;
252 }
253
254 template <class _ValueType,
255 class... _Args,
256 class _Tp = decay_t<_ValueType>,
257 enable_if_t<is_constructible_v<_Tp, _Args...> && is_copy_constructible_v<_Tp>, int> = 0>
258 _LIBCPP_HIDE_FROM_ABI _Tp& emplace(_Args&&... __args) {
259 reset();
260 return __any_imp::_Handler<_Tp>::__create(*this, std::forward<_Args>(__args)...);
261 }
262
263 template <
264 class _ValueType,
265 class _Up,
266 class... _Args,
267 class _Tp = decay_t<_ValueType>,
268 enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...> && is_copy_constructible_v<_Tp>, int> = 0>
269 _LIBCPP_HIDE_FROM_ABI _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
270 reset();
271 return __any_imp::_Handler<_Tp>::__create(*this, __il, std::forward<_Args>(__args)...);
272 }
273
274 // 6.3.3 any modifiers
275 _LIBCPP_HIDE_FROM_ABI void reset() noexcept {
276 if (__h_)
277 this->__call(a: _Action::_Destroy);
278 }
279
280 _LIBCPP_HIDE_FROM_ABI void swap(any& __rhs) noexcept {
281 if (this == &__rhs)
282 return;
283 if (__h_ && __rhs.__h_) {
284 any __tmp;
285 __rhs.__call(a: _Action::_Move, other: &__tmp);
286 this->__call(a: _Action::_Move, other: &__rhs);
287 __tmp.__call(a: _Action::_Move, other: this);
288 } else if (__h_) {
289 this->__call(a: _Action::_Move, other: &__rhs);
290 } else if (__rhs.__h_) {
291 __rhs.__call(a: _Action::_Move, other: this);
292 }
293 }
294
295 // 6.3.4 any observers
296 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool has_value() const noexcept { return __h_ != nullptr; }
297
298# if _LIBCPP_HAS_RTTI
299 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI const type_info& type() const noexcept {
300 if (__h_) {
301 return *static_cast<type_info const*>(this->__call(a: _Action::_TypeInfo));
302 } else {
303 return typeid(void);
304 }
305 }
306# endif
307
308private:
309 using _Action _LIBCPP_NODEBUG = __any_imp::_Action;
310 using _HandleFuncPtr
311 _LIBCPP_NODEBUG = void* (*)(_Action, any const*, any*, const type_info*, const void* __fallback_info);
312
313 union _Storage {
314 _LIBCPP_HIDE_FROM_ABI constexpr _Storage() : __ptr(nullptr) {}
315 void* __ptr;
316 alignas(__any_imp::__small_buffer_alignment) char __buf[__any_imp::__small_buffer_size];
317 };
318
319 _LIBCPP_HIDE_FROM_ABI void*
320 __call(_Action __a, any* __other = nullptr, type_info const* __info = nullptr, const void* __fallback_info = nullptr)
321 const {
322 return __h_(__a, this, __other, __info, __fallback_info);
323 }
324
325 _LIBCPP_HIDE_FROM_ABI void* __call(
326 _Action __a, any* __other = nullptr, type_info const* __info = nullptr, const void* __fallback_info = nullptr) {
327 return __h_(__a, this, __other, __info, __fallback_info);
328 }
329
330 template <class>
331 friend struct __any_imp::_SmallHandler;
332 template <class>
333 friend struct __any_imp::_LargeHandler;
334
335 template <class _ValueType>
336 friend add_pointer_t<add_const_t<_ValueType>> any_cast(any const*) noexcept;
337
338 template <class _ValueType>
339 friend add_pointer_t<_ValueType> any_cast(any*) noexcept;
340
341 _HandleFuncPtr __h_ = nullptr;
342 _Storage __s_;
343};
344
345namespace __any_imp {
346template <class _Tp>
347struct _SmallHandler {
348 _LIBCPP_HIDE_FROM_ABI static void*
349 __handle(_Action __act, any const* __this, any* __other, type_info const* __info, const void* __fallback_info) {
350 switch (__act) {
351 case _Action::_Destroy:
352 __destroy(this&: const_cast<any&>(*__this));
353 return nullptr;
354 case _Action::_Copy:
355 __copy(this: *__this, dest&: *__other);
356 return nullptr;
357 case _Action::_Move:
358 __move(this&: const_cast<any&>(*__this), dest&: *__other);
359 return nullptr;
360 case _Action::_Get:
361 return __get(this&: const_cast<any&>(*__this), __info, fallback_id: __fallback_info);
362 case _Action::_TypeInfo:
363 return __type_info();
364 }
365 __libcpp_unreachable();
366 }
367
368 template <class... _Args>
369 _LIBCPP_HIDE_FROM_ABI static _Tp& __create(any& __dest, _Args&&... __args) {
370 auto __ret = std::__construct_at(reinterpret_cast<_Tp*>(&__dest.__s_.__buf), std::forward<_Args>(__args)...);
371 __dest.__h_ = &_SmallHandler::__handle;
372 return *__ret;
373 }
374
375private:
376 _LIBCPP_HIDE_FROM_ABI static void __destroy(any& __this) {
377 std::__destroy_at(reinterpret_cast<_Tp*>(&__this.__s_.__buf));
378 __this.__h_ = nullptr;
379 }
380
381 _LIBCPP_HIDE_FROM_ABI static void __copy(any const& __this, any& __dest) {
382 _SmallHandler::__create(__dest, *static_cast<_Tp const*>(static_cast<void const*>(&__this.__s_.__buf)));
383 }
384
385 _LIBCPP_HIDE_FROM_ABI static void __move(any& __this, any& __dest) {
386 _SmallHandler::__create(__dest, std::move(*static_cast<_Tp*>(static_cast<void*>(&__this.__s_.__buf))));
387 __destroy(__this);
388 }
389
390 _LIBCPP_HIDE_FROM_ABI static void* __get(any& __this, type_info const* __info, const void* __fallback_id) {
391 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id))
392 return static_cast<void*>(&__this.__s_.__buf);
393 return nullptr;
394 }
395
396 _LIBCPP_HIDE_FROM_ABI static void* __type_info() {
397# if _LIBCPP_HAS_RTTI
398 return const_cast<void*>(static_cast<void const*>(&typeid(_Tp)));
399# else
400 return nullptr;
401# endif
402 }
403};
404
405template <class _Tp>
406struct _LargeHandler {
407 _LIBCPP_HIDE_FROM_ABI static void*
408 __handle(_Action __act, any const* __this, any* __other, type_info const* __info, void const* __fallback_info) {
409 switch (__act) {
410 case _Action::_Destroy:
411 __destroy(this&: const_cast<any&>(*__this));
412 return nullptr;
413 case _Action::_Copy:
414 __copy(this: *__this, dest&: *__other);
415 return nullptr;
416 case _Action::_Move:
417 __move(this&: const_cast<any&>(*__this), dest&: *__other);
418 return nullptr;
419 case _Action::_Get:
420 return __get(this&: const_cast<any&>(*__this), __info, __fallback_info);
421 case _Action::_TypeInfo:
422 return __type_info();
423 }
424 __libcpp_unreachable();
425 }
426
427 template <class... _Args>
428 _LIBCPP_HIDE_FROM_ABI static _Tp& __create(any& __dest, _Args&&... __args) {
429 _Tp* __ptr = static_cast<_Tp*>(std::__libcpp_allocate<_Tp>(__element_count(1)));
430 std::__exception_guard __guard([&] { std::__libcpp_deallocate<_Tp>(__ptr, __element_count(1)); });
431 std::__construct_at(__ptr, std::forward<_Args>(__args)...);
432 __guard.__complete();
433 __dest.__s_.__ptr = __ptr;
434 __dest.__h_ = &_LargeHandler::__handle;
435 return *__ptr;
436 }
437
438private:
439 _LIBCPP_HIDE_FROM_ABI static void __destroy(any& __this) {
440 _Tp* __p = static_cast<_Tp*>(__this.__s_.__ptr);
441 std::__destroy_at(__p);
442 std::__libcpp_deallocate<_Tp>(__p, __element_count(1));
443 __this.__h_ = nullptr;
444 }
445
446 _LIBCPP_HIDE_FROM_ABI static void __copy(any const& __this, any& __dest) {
447 _LargeHandler::__create(__dest, *static_cast<_Tp const*>(__this.__s_.__ptr));
448 }
449
450 _LIBCPP_HIDE_FROM_ABI static void __move(any& __this, any& __dest) {
451 __dest.__s_.__ptr = __this.__s_.__ptr;
452 __dest.__h_ = &_LargeHandler::__handle;
453 __this.__h_ = nullptr;
454 }
455
456 _LIBCPP_HIDE_FROM_ABI static void* __get(any& __this, type_info const* __info, void const* __fallback_info) {
457 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info))
458 return static_cast<void*>(__this.__s_.__ptr);
459 return nullptr;
460 }
461
462 _LIBCPP_HIDE_FROM_ABI static void* __type_info() {
463# if _LIBCPP_HAS_RTTI
464 return const_cast<void*>(static_cast<void const*>(&typeid(_Tp)));
465# else
466 return nullptr;
467# endif
468 }
469};
470
471} // namespace __any_imp
472
473// 6.4 Non-member functions
474
475inline _LIBCPP_HIDE_FROM_ABI void swap(any& __lhs, any& __rhs) noexcept { __lhs.swap(__rhs); }
476
477template <class _Tp, class... _Args>
478[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI any make_any(_Args&&... __args) {
479 return any(in_place_type<_Tp>, std::forward<_Args>(__args)...);
480}
481
482template <class _Tp, class _Up, class... _Args>
483[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI any make_any(initializer_list<_Up> __il, _Args&&... __args) {
484 return any(in_place_type<_Tp>, __il, std::forward<_Args>(__args)...);
485}
486
487template <class _ValueType>
488[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any const& __v) {
489 using _RawValueType = __remove_cvref_t<_ValueType>;
490 static_assert(is_constructible_v<_ValueType, _RawValueType const&>,
491 "ValueType is required to be a const lvalue reference "
492 "or a CopyConstructible type");
493 auto __tmp = std::any_cast<add_const_t<_RawValueType>>(&__v);
494 if (__tmp == nullptr)
495 std::__throw_bad_any_cast();
496 return static_cast<_ValueType>(*__tmp);
497}
498
499template <class _ValueType>
500[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any& __v) {
501 using _RawValueType = __remove_cvref_t<_ValueType>;
502 static_assert(is_constructible_v<_ValueType, _RawValueType&>,
503 "ValueType is required to be an lvalue reference "
504 "or a CopyConstructible type");
505 auto __tmp = std::any_cast<_RawValueType>(&__v);
506 if (__tmp == nullptr)
507 std::__throw_bad_any_cast();
508 return static_cast<_ValueType>(*__tmp);
509}
510
511template <class _ValueType>
512[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI _ValueType any_cast(any&& __v) {
513 using _RawValueType = __remove_cvref_t<_ValueType>;
514 static_assert(is_constructible_v<_ValueType, _RawValueType>,
515 "ValueType is required to be an rvalue reference "
516 "or a CopyConstructible type");
517 auto __tmp = std::any_cast<_RawValueType>(&__v);
518 if (__tmp == nullptr)
519 std::__throw_bad_any_cast();
520 return static_cast<_ValueType>(std::move(*__tmp));
521}
522
523template <class _ValueType>
524[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI add_pointer_t<add_const_t<_ValueType>> any_cast(any const* __any) noexcept {
525 static_assert(!is_void_v<_ValueType>, "_ValueType may not be void.");
526 static_assert(!is_reference_v<_ValueType>, "_ValueType may not be a reference.");
527 return std::any_cast<_ValueType>(const_cast<any*>(__any));
528}
529
530template <class _ValueType>
531[[nodiscard]] _LIBCPP_HIDE_FROM_ABI add_pointer_t<_ValueType> any_cast(any* __any) noexcept {
532 using __any_imp::_Action;
533 static_assert(!is_void_v<_ValueType>, "_ValueType may not be void.");
534 static_assert(!is_reference_v<_ValueType>, "_ValueType may not be a reference.");
535 if constexpr (!is_function_v<_ValueType>) {
536 using _ReturnType = add_pointer_t<_ValueType>;
537 if (__any && __any->__h_) {
538 void* __p = __any->__call(
539 _Action::_Get,
540 nullptr,
541# if _LIBCPP_HAS_RTTI
542 &typeid(_ValueType),
543# else
544 nullptr,
545# endif
546 __any_imp::__get_fallback_typeid<_ValueType>());
547 return static_cast<_ReturnType>(__p);
548 }
549 }
550 return nullptr;
551}
552
553# endif // _LIBCPP_STD_VER >= 17
554
555_LIBCPP_END_NAMESPACE_STD
556
557_LIBCPP_POP_MACROS
558
559# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
560# include <chrono>
561# endif
562
563# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
564# include <atomic>
565# include <concepts>
566# include <cstdlib>
567# include <iosfwd>
568# include <iterator>
569# include <memory>
570# include <stdexcept>
571# include <type_traits>
572# include <variant>
573# endif
574
575# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 23
576# include <cstring>
577# include <limits>
578# endif
579#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
580
581#endif // _LIBCPP_ANY
582