codekingpro/portable-devtools
114k
1// -*- C++ -*-2//===------------------------------ any -----------------------------------===//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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCUDACXX_ANY11#define _LIBCUDACXX_ANY12 13/*14 any synopsis15 16namespace std {17 18 class bad_any_cast : public bad_cast19 {20 public:21 virtual const char* what() const noexcept;22 };23 24 class any25 {26 public:27 28 // 6.3.1 any construct/destruct29 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 assignments40 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 modifiers47 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 observers55 bool has_value() const noexcept;56 const type_info& type() const noexcept;57 };58 59 // 6.4 Non-member functions60 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 std80 81*/82 83#include <experimental/__config>84#include <memory>85#include <new>86#include <typeinfo>87#include <type_traits>88#include <cstdlib>89#include <version>90 91#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)92# pragma GCC system_header93#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)94# pragma clang system_header95#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)96# pragma system_header97#endif // no system header98 99namespace std {100class _LIBCUDACXX_EXCEPTION_ABI _LIBCUDACXX_AVAILABILITY_BAD_ANY_CAST bad_any_cast : public bad_cast101{102public:103 virtual const char* what() const noexcept;104};105} // namespace std106 107_LIBCUDACXX_BEGIN_NAMESPACE_STD108 109#if _LIBCUDACXX_STD_VER > 14110 111_LIBCUDACXX_NORETURN inline _LIBCUDACXX_INLINE_VISIBILITY112_LIBCUDACXX_AVAILABILITY_THROW_BAD_ANY_CAST113void __throw_bad_any_cast()114{115#ifndef _LIBCUDACXX_NO_EXCEPTIONS116 throw bad_any_cast();117#else118 _CUDA_VSTD::abort();119#endif120}121 122// Forward declarations123class _LIBCUDACXX_TEMPLATE_VIS any;124 125template <class _ValueType>126_LIBCUDACXX_INLINE_VISIBILITY127add_pointer_t<add_const_t<_ValueType>>128any_cast(any const *) noexcept;129 130template <class _ValueType>131_LIBCUDACXX_INLINE_VISIBILITY132add_pointer_t<_ValueType> any_cast(any *) noexcept;133 134namespace __any_imp135{136 using _Buffer = aligned_storage_t<3*sizeof(void*), alignment_of<void*>::value>;137 138 template <class _Tp>139 using _IsSmallObject = integral_constant<bool140 , sizeof(_Tp) <= sizeof(_Buffer)141 && alignment_of<_Buffer>::value142 % alignment_of<_Tp>::value == 0143 && is_nothrow_move_constructible<_Tp>::value144 >;145 146 enum class _Action {147 _Destroy,148 _Copy,149 _Move,150 _Get,151 _TypeInfo152 };153 154 template <class _Tp> struct _SmallHandler;155 template <class _Tp> struct _LargeHandler;156 157 template <class _Tp>158 struct _LIBCUDACXX_TEMPLATE_VIS __unique_typeinfo { static constexpr int __id = 0; };159 template <class _Tp> constexpr int __unique_typeinfo<_Tp>::__id;160 161 template <class _Tp>162 inline _LIBCUDACXX_INLINE_VISIBILITY163 constexpr const void* __get_fallback_typeid() {164 return &__unique_typeinfo<decay_t<_Tp>>::__id;165 }166 167 template <class _Tp>168 inline _LIBCUDACXX_INLINE_VISIBILITY169 bool __compare_typeid(type_info const* __id, const void* __fallback_id)170 {171#if !defined(_LIBCUDACXX_NO_RTTI)172 if (__id && *__id == typeid(_Tp))173 return true;174#endif175 if (!__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>())176 return true;177 return false;178 }179 180 template <class _Tp>181 using _Handler = conditional_t<182 _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>;183 184} // namespace __any_imp185 186class _LIBCUDACXX_TEMPLATE_VIS any187{188public:189 // construct/destruct190 _LIBCUDACXX_INLINE_VISIBILITY191 constexpr any() noexcept : __h(nullptr) {}192 193 _LIBCUDACXX_INLINE_VISIBILITY194 any(any const & __other) : __h(nullptr)195 {196 if (__other.__h) __other.__call(_Action::_Copy, this);197 }198 199 _LIBCUDACXX_INLINE_VISIBILITY200 any(any && __other) noexcept : __h(nullptr)201 {202 if (__other.__h) __other.__call(_Action::_Move, this);203 }204 205 template <206 class _ValueType207 , class _Tp = decay_t<_ValueType>208 , class = enable_if_t<209 !is_same<_Tp, any>::value &&210 !__is_inplace_type<_ValueType>::value &&211 is_copy_constructible<_Tp>::value>212 >213 _LIBCUDACXX_INLINE_VISIBILITY214 any(_ValueType && __value);215 216 template <class _ValueType, class ..._Args,217 class _Tp = decay_t<_ValueType>,218 class = enable_if_t<219 is_constructible<_Tp, _Args...>::value &&220 is_copy_constructible<_Tp>::value221 >222 >223 _LIBCUDACXX_INLINE_VISIBILITY224 explicit any(in_place_type_t<_ValueType>, _Args&&... __args);225 226 template <class _ValueType, class _Up, class ..._Args,227 class _Tp = decay_t<_ValueType>,228 class = enable_if_t<229 is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&230 is_copy_constructible<_Tp>::value>231 >232 _LIBCUDACXX_INLINE_VISIBILITY233 explicit any(in_place_type_t<_ValueType>, initializer_list<_Up>, _Args&&... __args);234 235 _LIBCUDACXX_INLINE_VISIBILITY236 ~any() { this->reset(); }237 238 // assignments239 _LIBCUDACXX_INLINE_VISIBILITY240 any & operator=(any const & __rhs) {241 any(__rhs).swap(*this);242 return *this;243 }244 245 _LIBCUDACXX_INLINE_VISIBILITY246 any & operator=(any && __rhs) noexcept {247 any(_CUDA_VSTD::move(__rhs)).swap(*this);248 return *this;249 }250 251 template <252 class _ValueType253 , class _Tp = decay_t<_ValueType>254 , class = enable_if_t<255 !is_same<_Tp, any>::value256 && is_copy_constructible<_Tp>::value>257 >258 _LIBCUDACXX_INLINE_VISIBILITY259 any & operator=(_ValueType && __rhs);260 261 template <class _ValueType, class ..._Args,262 class _Tp = decay_t<_ValueType>,263 class = enable_if_t<264 is_constructible<_Tp, _Args...>::value &&265 is_copy_constructible<_Tp>::value>266 >267 _LIBCUDACXX_INLINE_VISIBILITY268 _Tp& emplace(_Args&&... args);269 270 template <class _ValueType, class _Up, class ..._Args,271 class _Tp = decay_t<_ValueType>,272 class = enable_if_t<273 is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&274 is_copy_constructible<_Tp>::value>275 >276 _LIBCUDACXX_INLINE_VISIBILITY277 _Tp& emplace(initializer_list<_Up>, _Args&&...);278 279 // 6.3.3 any modifiers280 _LIBCUDACXX_INLINE_VISIBILITY281 void reset() noexcept { if (__h) this->__call(_Action::_Destroy); }282 283 _LIBCUDACXX_INLINE_VISIBILITY284 void swap(any & __rhs) noexcept;285 286 // 6.3.4 any observers287 _LIBCUDACXX_INLINE_VISIBILITY288 bool has_value() const noexcept { return __h != nullptr; }289 290#if !defined(_LIBCUDACXX_NO_RTTI)291 _LIBCUDACXX_INLINE_VISIBILITY292 const type_info & type() const noexcept {293 if (__h) {294 return *static_cast<type_info const *>(this->__call(_Action::_TypeInfo));295 } else {296 return typeid(void);297 }298 }299#endif300 301private:302 typedef __any_imp::_Action _Action;303 using _HandleFuncPtr = void* (*)(_Action, any const *, any *, const type_info *,304 const void* __fallback_info);305 306 union _Storage {307 constexpr _Storage() : __ptr(nullptr) {}308 void * __ptr;309 __any_imp::_Buffer __buf;310 };311 312 _LIBCUDACXX_INLINE_VISIBILITY313 void * __call(_Action __a, any * __other = nullptr,314 type_info const * __info = nullptr,315 const void* __fallback_info = nullptr) const316 {317 return __h(__a, this, __other, __info, __fallback_info);318 }319 320 _LIBCUDACXX_INLINE_VISIBILITY321 void * __call(_Action __a, any * __other = nullptr,322 type_info const * __info = nullptr,323 const void* __fallback_info = nullptr)324 {325 return __h(__a, this, __other, __info, __fallback_info);326 }327 328 template <class>329 friend struct __any_imp::_SmallHandler;330 template <class>331 friend struct __any_imp::_LargeHandler;332 333 template <class _ValueType>334 friend add_pointer_t<add_const_t<_ValueType>>335 any_cast(any const *) noexcept;336 337 template <class _ValueType>338 friend add_pointer_t<_ValueType>339 any_cast(any *) noexcept;340 341 _HandleFuncPtr __h = nullptr;342 _Storage __s;343};344 345namespace __any_imp346{347 template <class _Tp>348 struct _LIBCUDACXX_TEMPLATE_VIS _SmallHandler349 {350 _LIBCUDACXX_INLINE_VISIBILITY351 static void* __handle(_Action __act, any const * __this, any * __other,352 type_info const * __info, const void* __fallback_info)353 {354 switch (__act)355 {356 case _Action::_Destroy:357 __destroy(const_cast<any &>(*__this));358 return nullptr;359 case _Action::_Copy:360 __copy(*__this, *__other);361 return nullptr;362 case _Action::_Move:363 __move(const_cast<any &>(*__this), *__other);364 return nullptr;365 case _Action::_Get:366 return __get(const_cast<any &>(*__this), __info, __fallback_info);367 case _Action::_TypeInfo:368 return __type_info();369 }370 }371 372 template <class ..._Args>373 _LIBCUDACXX_INLINE_VISIBILITY374 static _Tp& __create(any & __dest, _Args&&... __args) {375 _Tp* __ret = ::new (static_cast<void*>(&__dest.__s.__buf)) _Tp(_CUDA_VSTD::forward<_Args>(__args)...);376 __dest.__h = &_SmallHandler::__handle;377 return *__ret;378 }379 380 private:381 _LIBCUDACXX_INLINE_VISIBILITY382 static void __destroy(any & __this) {383 _Tp & __value = *static_cast<_Tp *>(static_cast<void*>(&__this.__s.__buf));384 __value.~_Tp();385 __this.__h = nullptr;386 }387 388 _LIBCUDACXX_INLINE_VISIBILITY389 static void __copy(any const & __this, any & __dest) {390 _SmallHandler::__create(__dest, *static_cast<_Tp const *>(391 static_cast<void const *>(&__this.__s.__buf)));392 }393 394 _LIBCUDACXX_INLINE_VISIBILITY395 static void __move(any & __this, any & __dest) {396 _SmallHandler::__create(__dest, _CUDA_VSTD::move(397 *static_cast<_Tp*>(static_cast<void*>(&__this.__s.__buf))));398 __destroy(__this);399 }400 401 _LIBCUDACXX_INLINE_VISIBILITY402 static void* __get(any & __this,403 type_info const * __info,404 const void* __fallback_id)405 {406 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id))407 return static_cast<void*>(&__this.__s.__buf);408 return nullptr;409 }410 411 _LIBCUDACXX_INLINE_VISIBILITY412 static void* __type_info()413 {414#if !defined(_LIBCUDACXX_NO_RTTI)415 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));416#else417 return nullptr;418#endif419 }420 };421 422 template <class _Tp>423 struct _LIBCUDACXX_TEMPLATE_VIS _LargeHandler424 {425 _LIBCUDACXX_INLINE_VISIBILITY426 static void* __handle(_Action __act, any const * __this,427 any * __other, type_info const * __info,428 void const* __fallback_info)429 {430 switch (__act)431 {432 case _Action::_Destroy:433 __destroy(const_cast<any &>(*__this));434 return nullptr;435 case _Action::_Copy:436 __copy(*__this, *__other);437 return nullptr;438 case _Action::_Move:439 __move(const_cast<any &>(*__this), *__other);440 return nullptr;441 case _Action::_Get:442 return __get(const_cast<any &>(*__this), __info, __fallback_info);443 case _Action::_TypeInfo:444 return __type_info();445 }446 }447 448 template <class ..._Args>449 _LIBCUDACXX_INLINE_VISIBILITY450 static _Tp& __create(any & __dest, _Args&&... __args) {451 typedef allocator<_Tp> _Alloc;452 typedef __allocator_destructor<_Alloc> _Dp;453 _Alloc __a;454 unique_ptr<_Tp, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));455 _Tp* __ret = ::new ((void*)__hold.get()) _Tp(_CUDA_VSTD::forward<_Args>(__args)...);456 __dest.__s.__ptr = __hold.release();457 __dest.__h = &_LargeHandler::__handle;458 return *__ret;459 }460 461 private:462 463 _LIBCUDACXX_INLINE_VISIBILITY464 static void __destroy(any & __this){465 delete static_cast<_Tp*>(__this.__s.__ptr);466 __this.__h = nullptr;467 }468 469 _LIBCUDACXX_INLINE_VISIBILITY470 static void __copy(any const & __this, any & __dest) {471 _LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s.__ptr));472 }473 474 _LIBCUDACXX_INLINE_VISIBILITY475 static void __move(any & __this, any & __dest) {476 __dest.__s.__ptr = __this.__s.__ptr;477 __dest.__h = &_LargeHandler::__handle;478 __this.__h = nullptr;479 }480 481 _LIBCUDACXX_INLINE_VISIBILITY482 static void* __get(any & __this, type_info const * __info,483 void const* __fallback_info)484 {485 if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info))486 return static_cast<void*>(__this.__s.__ptr);487 return nullptr;488 489 }490 491 _LIBCUDACXX_INLINE_VISIBILITY492 static void* __type_info()493 {494#if !defined(_LIBCUDACXX_NO_RTTI)495 return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));496#else497 return nullptr;498#endif499 }500 };501 502} // namespace __any_imp503 504 505template <class _ValueType, class _Tp, class>506any::any(_ValueType && __v) : __h(nullptr)507{508 __any_imp::_Handler<_Tp>::__create(*this, _CUDA_VSTD::forward<_ValueType>(__v));509}510 511template <class _ValueType, class ..._Args, class _Tp, class>512any::any(in_place_type_t<_ValueType>, _Args&&... __args) {513 __any_imp::_Handler<_Tp>::__create(*this, _CUDA_VSTD::forward<_Args>(__args)...);514}515 516template <class _ValueType, class _Up, class ..._Args, class _Tp, class>517any::any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) {518 __any_imp::_Handler<_Tp>::__create(*this, __il, _CUDA_VSTD::forward<_Args>(__args)...);519}520 521template <class _ValueType, class, class>522inline _LIBCUDACXX_INLINE_VISIBILITY523any & any::operator=(_ValueType && __v)524{525 any(_CUDA_VSTD::forward<_ValueType>(__v)).swap(*this);526 return *this;527}528 529template <class _ValueType, class ..._Args, class _Tp, class>530inline _LIBCUDACXX_INLINE_VISIBILITY531_Tp& any::emplace(_Args&&... __args) {532 reset();533 return __any_imp::_Handler<_Tp>::__create(*this, _CUDA_VSTD::forward<_Args>(__args)...);534}535 536template <class _ValueType, class _Up, class ..._Args, class _Tp, class>537inline _LIBCUDACXX_INLINE_VISIBILITY538_Tp& any::emplace(initializer_list<_Up> __il, _Args&&... __args) {539 reset();540 return __any_imp::_Handler<_Tp>::__create(*this, __il, _CUDA_VSTD::forward<_Args>(__args)...);541}542 543inline _LIBCUDACXX_INLINE_VISIBILITY544void any::swap(any & __rhs) noexcept545{546 if (this == &__rhs)547 return;548 if (__h && __rhs.__h) {549 any __tmp;550 __rhs.__call(_Action::_Move, &__tmp);551 this->__call(_Action::_Move, &__rhs);552 __tmp.__call(_Action::_Move, this);553 }554 else if (__h) {555 this->__call(_Action::_Move, &__rhs);556 }557 else if (__rhs.__h) {558 __rhs.__call(_Action::_Move, this);559 }560}561 562// 6.4 Non-member functions563 564inline _LIBCUDACXX_INLINE_VISIBILITY565void swap(any & __lhs, any & __rhs) noexcept566{567 __lhs.swap(__rhs);568}569 570template <class _Tp, class ..._Args>571inline _LIBCUDACXX_INLINE_VISIBILITY572any make_any(_Args&&... __args) {573 return any(in_place_type<_Tp>, _CUDA_VSTD::forward<_Args>(__args)...);574}575 576template <class _Tp, class _Up, class ..._Args>577inline _LIBCUDACXX_INLINE_VISIBILITY578any make_any(initializer_list<_Up> __il, _Args&&... __args) {579 return any(in_place_type<_Tp>, __il, _CUDA_VSTD::forward<_Args>(__args)...);580}581 582template <class _ValueType>583inline _LIBCUDACXX_INLINE_VISIBILITY584_LIBCUDACXX_AVAILABILITY_THROW_BAD_ANY_CAST585_ValueType any_cast(any const & __v)586{587 using _RawValueType = __remove_cvref_t<_ValueType>;588 static_assert(is_constructible<_ValueType, _RawValueType const &>::value,589 "ValueType is required to be a const lvalue reference "590 "or a CopyConstructible type");591 auto __tmp = _CUDA_VSTD::any_cast<add_const_t<_RawValueType>>(&__v);592 if (__tmp == nullptr)593 __throw_bad_any_cast();594 return static_cast<_ValueType>(*__tmp);595}596 597template <class _ValueType>598inline _LIBCUDACXX_INLINE_VISIBILITY599_LIBCUDACXX_AVAILABILITY_THROW_BAD_ANY_CAST600_ValueType any_cast(any & __v)601{602 using _RawValueType = __remove_cvref_t<_ValueType>;603 static_assert(is_constructible<_ValueType, _RawValueType &>::value,604 "ValueType is required to be an lvalue reference "605 "or a CopyConstructible type");606 auto __tmp = _CUDA_VSTD::any_cast<_RawValueType>(&__v);607 if (__tmp == nullptr)608 __throw_bad_any_cast();609 return static_cast<_ValueType>(*__tmp);610}611 612template <class _ValueType>613inline _LIBCUDACXX_INLINE_VISIBILITY614_LIBCUDACXX_AVAILABILITY_THROW_BAD_ANY_CAST615_ValueType any_cast(any && __v)616{617 using _RawValueType = __remove_cvref_t<_ValueType>;618 static_assert(is_constructible<_ValueType, _RawValueType>::value,619 "ValueType is required to be an rvalue reference "620 "or a CopyConstructible type");621 auto __tmp = _CUDA_VSTD::any_cast<_RawValueType>(&__v);622 if (__tmp == nullptr)623 __throw_bad_any_cast();624 return static_cast<_ValueType>(_CUDA_VSTD::move(*__tmp));625}626 627template <class _ValueType>628inline _LIBCUDACXX_INLINE_VISIBILITY629add_pointer_t<add_const_t<_ValueType>>630any_cast(any const * __any) noexcept631{632 static_assert(!is_reference<_ValueType>::value,633 "_ValueType may not be a reference.");634 return _CUDA_VSTD::any_cast<_ValueType>(const_cast<any *>(__any));635}636 637template <class _RetType>638inline _LIBCUDACXX_INLINE_VISIBILITY639_RetType __pointer_or_func_cast(void* __p, /*IsFunction*/false_type) noexcept {640 return static_cast<_RetType>(__p);641}642 643template <class _RetType>644inline _LIBCUDACXX_INLINE_VISIBILITY645_RetType __pointer_or_func_cast(void*, /*IsFunction*/true_type) noexcept {646 return nullptr;647}648 649template <class _ValueType>650add_pointer_t<_ValueType>651any_cast(any * __any) noexcept652{653 using __any_imp::_Action;654 static_assert(!is_reference<_ValueType>::value,655 "_ValueType may not be a reference.");656 typedef typename add_pointer<_ValueType>::type _ReturnType;657 if (__any && __any->__h) {658 void *__p = __any->__call(_Action::_Get, nullptr,659#if !defined(_LIBCUDACXX_NO_RTTI)660 &typeid(_ValueType),661#else662 nullptr,663#endif664 __any_imp::__get_fallback_typeid<_ValueType>());665 return _CUDA_VSTD::__pointer_or_func_cast<_ReturnType>(666 __p, is_function<_ValueType>{});667 }668 return nullptr;669}670 671#endif // _LIBCUDACXX_STD_VER > 14672 673_LIBCUDACXX_END_NAMESPACE_STD674 675#endif // _LIBCUDACXX_ANY676 