| 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 |
#include <__assert> // all public C++ headers provide the assertion handler |
| 84 |
#include <__availability> |
| 85 |
#include <__config> |
| 86 |
#include <__memory/allocator.h> |
| 87 |
#include <__memory/allocator_destructor.h> |
| 88 |
#include <__memory/allocator_traits.h> |
| 89 |
#include <__memory/unique_ptr.h> |
| 90 |
#include <__utility/forward.h> |
| 91 |
#include <__utility/in_place.h> |
| 92 |
#include <__utility/move.h> |
| 93 |
#include <__utility/unreachable.h> |
| 94 |
#include <cstdlib> |
| 95 |
#include <initializer_list> |
| 96 |
#include <type_traits> |
| 97 |
#include <typeinfo> |
| 98 |
#include <version> |
| 99 |
|
| 100 |
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 101 |
# pragma GCC system_header |
| 102 |
#endif |
| 103 |
|
| 104 |
namespace std { |
| 105 |
class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_ANY_CAST bad_any_cast : public bad_cast |
| 106 |
{ |
| 107 |
public: |
| 108 |
const char* what() const _NOEXCEPT override; |
| 109 |
}; |
| 110 |
} // namespace std |
| 111 |
|
| 112 |
_LIBCPP_BEGIN_NAMESPACE_STD |
| 113 |
|
| 114 |
#if _LIBCPP_STD_VER > 14 |
| 115 |
|
| 116 |
_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY |
| 117 |
_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST |
| 118 |
void __throw_bad_any_cast() |
| 119 |
{ |
| 120 |
#ifndef _LIBCPP_NO_EXCEPTIONS |
| 121 |
throw bad_any_cast(); |
| 122 |
#else |
| 123 |
_VSTD::abort(); |
| 124 |
#endif |
| 125 |
} |
| 126 |
|
| 127 |
// Forward declarations |
| 128 |
class _LIBCPP_TEMPLATE_VIS any; |
| 129 |
|
| 130 |
template <class _ValueType> |
| 131 |
_LIBCPP_INLINE_VISIBILITY |
| 132 |
add_pointer_t<add_const_t<_ValueType>> |
| 133 |
any_cast(any const *) _NOEXCEPT; |
| 134 |
|
| 135 |
template <class _ValueType> |
| 136 |
_LIBCPP_INLINE_VISIBILITY |
| 137 |
add_pointer_t<_ValueType> any_cast(any *) _NOEXCEPT; |
| 138 |
|
| 139 |
namespace __any_imp |
| 140 |
{ |
| 141 |
_LIBCPP_SUPPRESS_DEPRECATED_PUSH |
| 142 |
using _Buffer = aligned_storage_t<3*sizeof(void*), alignment_of<void*>::value>; |
| 143 |
_LIBCPP_SUPPRESS_DEPRECATED_POP |
| 144 |
|
| 145 |
template <class _Tp> |
| 146 |
using _IsSmallObject = integral_constant<bool |
| 147 |
, sizeof(_Tp) <= sizeof(_Buffer) |
| 148 |
&& alignment_of<_Buffer>::value |
| 149 |
% alignment_of<_Tp>::value == 0 |
| 150 |
&& is_nothrow_move_constructible<_Tp>::value |
| 151 |
>; |
| 152 |
|
| 153 |
enum class _Action { |
| 154 |
_Destroy, |
| 155 |
_Copy, |
| 156 |
_Move, |
| 157 |
_Get, |
| 158 |
_TypeInfo |
| 159 |
}; |
| 160 |
|
| 161 |
template <class _Tp> struct _SmallHandler; |
| 162 |
template <class _Tp> struct _LargeHandler; |
| 163 |
|
| 164 |
template <class _Tp> |
| 165 |
struct _LIBCPP_TEMPLATE_VIS __unique_typeinfo { static constexpr int __id = 0; }; |
| 166 |
template <class _Tp> constexpr int __unique_typeinfo<_Tp>::__id; |
| 167 |
|
| 168 |
template <class _Tp> |
| 169 |
inline _LIBCPP_INLINE_VISIBILITY |
| 170 |
constexpr const void* __get_fallback_typeid() { |
| 171 |
return &__unique_typeinfo<remove_cv_t<remove_reference_t<_Tp>>>::__id; |
| 172 |
} |
| 173 |
|
| 174 |
template <class _Tp> |
| 175 |
inline _LIBCPP_INLINE_VISIBILITY |
| 176 |
bool __compare_typeid(type_info const* __id, const void* __fallback_id) |
| 177 |
{ |
| 178 |
#if !defined(_LIBCPP_HAS_NO_RTTI) |
| 179 |
if (__id && *__id == typeid(_Tp)) |
| 180 |
return true; |
| 181 |
#endif |
| 182 |
if (!__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>()) |
| 183 |
return true; |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
template <class _Tp> |
| 188 |
using _Handler = conditional_t< |
| 189 |
_IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>; |
| 190 |
|
| 191 |
} // namespace __any_imp |
| 192 |
|
| 193 |
class _LIBCPP_TEMPLATE_VIS any |
| 194 |
{ |
| 195 |
public: |
| 196 |
// construct/destruct |
| 197 |
_LIBCPP_INLINE_VISIBILITY |
| 198 |
constexpr any() _NOEXCEPT : __h_(nullptr) {} |
| 199 |
|
| 200 |
_LIBCPP_INLINE_VISIBILITY |
| 201 |
any(any const & __other) : __h_(nullptr) |
| 202 |
{ |
| 203 |
if (__other.__h_) __other.__call(_Action::_Copy, this); |
| 204 |
} |
| 205 |
|
| 206 |
_LIBCPP_INLINE_VISIBILITY |
| 207 |
any(any && __other) _NOEXCEPT : __h_(nullptr) |
| 208 |
{ |
| 209 |
if (__other.__h_) __other.__call(_Action::_Move, this); |
| 210 |
} |
| 211 |
|
| 212 |
template < |
| 213 |
class _ValueType |
| 214 |
, class _Tp = decay_t<_ValueType> |
| 215 |
, class = enable_if_t< |
| 216 |
!is_same<_Tp, any>::value && |
| 217 |
!__is_inplace_type<_ValueType>::value && |
| 218 |
is_copy_constructible<_Tp>::value> |
| 219 |
> |
| 220 |
_LIBCPP_INLINE_VISIBILITY |
| 221 |
any(_ValueType && __value); |
| 222 |
|
| 223 |
template <class _ValueType, class ..._Args, |
| 224 |
class _Tp = decay_t<_ValueType>, |
| 225 |
class = enable_if_t< |
| 226 |
is_constructible<_Tp, _Args...>::value && |
| 227 |
is_copy_constructible<_Tp>::value |
| 228 |
> |
| 229 |
> |
| 230 |
_LIBCPP_INLINE_VISIBILITY |
| 231 |
explicit any(in_place_type_t<_ValueType>, _Args&&... __args); |
| 232 |
|
| 233 |
template <class _ValueType, class _Up, class ..._Args, |
| 234 |
class _Tp = decay_t<_ValueType>, |
| 235 |
class = enable_if_t< |
| 236 |
is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value && |
| 237 |
is_copy_constructible<_Tp>::value> |
| 238 |
> |
| 239 |
_LIBCPP_INLINE_VISIBILITY |
| 240 |
explicit any(in_place_type_t<_ValueType>, initializer_list<_Up>, _Args&&... __args); |
| 241 |
|
| 242 |
_LIBCPP_INLINE_VISIBILITY |
| 243 |
~any() { this->reset(); } |
| 244 |
|
| 245 |
// assignments |
| 246 |
_LIBCPP_INLINE_VISIBILITY |
| 247 |
any & operator=(any const & __rhs) { |
| 248 |
any(__rhs).swap(*this); |
| 249 |
return *this; |
| 250 |
} |
| 251 |
|
| 252 |
_LIBCPP_INLINE_VISIBILITY |
| 253 |
any & operator=(any && __rhs) _NOEXCEPT { |
| 254 |
any(_VSTD::move(__rhs)).swap(*this); |
| 255 |
return *this; |
| 256 |
} |
| 257 |
|
| 258 |
template < |
| 259 |
class _ValueType |
| 260 |
, class _Tp = decay_t<_ValueType> |
| 261 |
, class = enable_if_t< |
| 262 |
!is_same<_Tp, any>::value |
| 263 |
&& is_copy_constructible<_Tp>::value> |
| 264 |
> |
| 265 |
_LIBCPP_INLINE_VISIBILITY |
| 266 |
any & operator=(_ValueType && __rhs); |
| 267 |
|
| 268 |
template <class _ValueType, class ..._Args, |
| 269 |
class _Tp = decay_t<_ValueType>, |
| 270 |
class = enable_if_t< |
| 271 |
is_constructible<_Tp, _Args...>::value && |
| 272 |
is_copy_constructible<_Tp>::value> |
| 273 |
> |
| 274 |
_LIBCPP_INLINE_VISIBILITY |
| 275 |
_Tp& emplace(_Args&&...); |
| 276 |
|
| 277 |
template <class _ValueType, class _Up, class ..._Args, |
| 278 |
class _Tp = decay_t<_ValueType>, |
| 279 |
class = enable_if_t< |
| 280 |
is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value && |
| 281 |
is_copy_constructible<_Tp>::value> |
| 282 |
> |
| 283 |
_LIBCPP_INLINE_VISIBILITY |
| 284 |
_Tp& emplace(initializer_list<_Up>, _Args&&...); |
| 285 |
|
| 286 |
// 6.3.3 any modifiers |
| 287 |
_LIBCPP_INLINE_VISIBILITY |
| 288 |
void reset() _NOEXCEPT { if (__h_) this->__call(_Action::_Destroy); } |
| 289 |
|
| 290 |
_LIBCPP_INLINE_VISIBILITY |
| 291 |
void swap(any & __rhs) _NOEXCEPT; |
| 292 |
|
| 293 |
// 6.3.4 any observers |
| 294 |
_LIBCPP_INLINE_VISIBILITY |
| 295 |
bool has_value() const _NOEXCEPT { return __h_ != nullptr; } |
| 296 |
|
| 297 |
#if !defined(_LIBCPP_HAS_NO_RTTI) |
| 298 |
_LIBCPP_INLINE_VISIBILITY |
| 299 |
const type_info & type() const _NOEXCEPT { |
| 300 |
if (__h_) { |
| 301 |
return *static_cast<type_info const *>(this->__call(_Action::_TypeInfo)); |
| 302 |
} else { |
| 303 |
return typeid(void); |
| 304 |
} |
| 305 |
} |
| 306 |
#endif |
| 307 |
|
| 308 |
private: |
| 309 |
typedef __any_imp::_Action _Action; |
| 310 |
using _HandleFuncPtr = void* (*)(_Action, any const *, any *, const type_info *, |
| 311 |
const void* __fallback_info); |
| 312 |
|
| 313 |
union _Storage { |
| 314 |
constexpr _Storage() : __ptr(nullptr) {} |
| 315 |
void * __ptr; |
| 316 |
__any_imp::_Buffer __buf; |
| 317 |
}; |
| 318 |
|
| 319 |
_LIBCPP_INLINE_VISIBILITY |
| 320 |
void * __call(_Action __a, any * __other = nullptr, |
| 321 |
type_info const * __info = nullptr, |
| 322 |
const void* __fallback_info = nullptr) const |
| 323 |
{ |
| 324 |
return __h_(__a, this, __other, __info, __fallback_info); |
| 325 |
} |
| 326 |
|
| 327 |
_LIBCPP_INLINE_VISIBILITY |
| 328 |
void * __call(_Action __a, any * __other = nullptr, |
| 329 |
type_info const * __info = nullptr, |
| 330 |
const void* __fallback_info = nullptr) |
| 331 |
{ |
| 332 |
return __h_(__a, this, __other, __info, __fallback_info); |
| 333 |
} |
| 334 |
|
| 335 |
template <class> |
| 336 |
friend struct __any_imp::_SmallHandler; |
| 337 |
template <class> |
| 338 |
friend struct __any_imp::_LargeHandler; |
| 339 |
|
| 340 |
template <class _ValueType> |
| 341 |
friend add_pointer_t<add_const_t<_ValueType>> |
| 342 |
any_cast(any const *) _NOEXCEPT; |
| 343 |
|
| 344 |
template <class _ValueType> |
| 345 |
friend add_pointer_t<_ValueType> |
| 346 |
any_cast(any *) _NOEXCEPT; |
| 347 |
|
| 348 |
_HandleFuncPtr __h_ = nullptr; |
| 349 |
_Storage __s_; |
| 350 |
}; |
| 351 |
|
| 352 |
namespace __any_imp |
| 353 |
{ |
| 354 |
template <class _Tp> |
| 355 |
struct _LIBCPP_TEMPLATE_VIS _SmallHandler |
| 356 |
{ |
| 357 |
_LIBCPP_INLINE_VISIBILITY |
| 358 |
static void* __handle(_Action __act, any const * __this, any * __other, |
| 359 |
type_info const * __info, const void* __fallback_info) |
| 360 |
{ |
| 361 |
switch (__act) |
| 362 |
{ |
| 363 |
case _Action::_Destroy: |
| 364 |
__destroy(const_cast<any &>(*__this)); |
| 365 |
return nullptr; |
| 366 |
case _Action::_Copy: |
| 367 |
__copy(*__this, *__other); |
| 368 |
return nullptr; |
| 369 |
case _Action::_Move: |
| 370 |
__move(const_cast<any &>(*__this), *__other); |
| 371 |
return nullptr; |
| 372 |
case _Action::_Get: |
| 373 |
return __get(const_cast<any &>(*__this), __info, __fallback_info); |
| 374 |
case _Action::_TypeInfo: |
| 375 |
return __type_info(); |
| 376 |
} |
| 377 |
__libcpp_unreachable(); |
| 378 |
} |
| 379 |
|
| 380 |
template <class ..._Args> |
| 381 |
_LIBCPP_INLINE_VISIBILITY |
| 382 |
static _Tp& __create(any & __dest, _Args&&... __args) { |
| 383 |
typedef allocator<_Tp> _Alloc; |
| 384 |
typedef allocator_traits<_Alloc> _ATraits; |
| 385 |
_Alloc __a; |
| 386 |
_Tp * __ret = static_cast<_Tp*>(static_cast<void*>(&__dest.__s_.__buf)); |
| 387 |
_ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...); |
| 388 |
__dest.__h_ = &_SmallHandler::__handle; |
| 389 |
return *__ret; |
| 390 |
} |
| 391 |
|
| 392 |
private: |
| 393 |
_LIBCPP_INLINE_VISIBILITY |
| 394 |
static void __destroy(any & __this) { |
| 395 |
typedef allocator<_Tp> _Alloc; |
| 396 |
typedef allocator_traits<_Alloc> _ATraits; |
| 397 |
_Alloc __a; |
| 398 |
_Tp * __p = static_cast<_Tp *>(static_cast<void*>(&__this.__s_.__buf)); |
| 399 |
_ATraits::destroy(__a, __p); |
| 400 |
__this.__h_ = nullptr; |
| 401 |
} |
| 402 |
|
| 403 |
_LIBCPP_INLINE_VISIBILITY |
| 404 |
static void __copy(any const & __this, any & __dest) { |
| 405 |
_SmallHandler::__create(__dest, *static_cast<_Tp const *>( |
| 406 |
static_cast<void const *>(&__this.__s_.__buf))); |
| 407 |
} |
| 408 |
|
| 409 |
_LIBCPP_INLINE_VISIBILITY |
| 410 |
static void __move(any & __this, any & __dest) { |
| 411 |
_SmallHandler::__create(__dest, _VSTD::move( |
| 412 |
*static_cast<_Tp*>(static_cast<void*>(&__this.__s_.__buf)))); |
| 413 |
__destroy(__this); |
| 414 |
} |
| 415 |
|
| 416 |
_LIBCPP_INLINE_VISIBILITY |
| 417 |
static void* __get(any & __this, |
| 418 |
type_info const * __info, |
| 419 |
const void* __fallback_id) |
| 420 |
{ |
| 421 |
if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id)) |
| 422 |
return static_cast<void*>(&__this.__s_.__buf); |
| 423 |
return nullptr; |
| 424 |
} |
| 425 |
|
| 426 |
_LIBCPP_INLINE_VISIBILITY |
| 427 |
static void* __type_info() |
| 428 |
{ |
| 429 |
#if !defined(_LIBCPP_HAS_NO_RTTI) |
| 430 |
return const_cast<void*>(static_cast<void const *>(&typeid(_Tp))); |
| 431 |
#else |
| 432 |
return nullptr; |
| 433 |
#endif |
| 434 |
} |
| 435 |
}; |
| 436 |
|
| 437 |
template <class _Tp> |
| 438 |
struct _LIBCPP_TEMPLATE_VIS _LargeHandler |
| 439 |
{ |
| 440 |
_LIBCPP_INLINE_VISIBILITY |
| 441 |
static void* __handle(_Action __act, any const * __this, |
| 442 |
any * __other, type_info const * __info, |
| 443 |
void const* __fallback_info) |
| 444 |
{ |
| 445 |
switch (__act) |
| 446 |
{ |
| 447 |
case _Action::_Destroy: |
| 448 |
__destroy(const_cast<any &>(*__this)); |
| 449 |
return nullptr; |
| 450 |
case _Action::_Copy: |
| 451 |
__copy(*__this, *__other); |
| 452 |
return nullptr; |
| 453 |
case _Action::_Move: |
| 454 |
__move(const_cast<any &>(*__this), *__other); |
| 455 |
return nullptr; |
| 456 |
case _Action::_Get: |
| 457 |
return __get(const_cast<any &>(*__this), __info, __fallback_info); |
| 458 |
case _Action::_TypeInfo: |
| 459 |
return __type_info(); |
| 460 |
} |
| 461 |
__libcpp_unreachable(); |
| 462 |
} |
| 463 |
|
| 464 |
template <class ..._Args> |
| 465 |
_LIBCPP_INLINE_VISIBILITY |
| 466 |
static _Tp& __create(any & __dest, _Args&&... __args) { |
| 467 |
typedef allocator<_Tp> _Alloc; |
| 468 |
typedef allocator_traits<_Alloc> _ATraits; |
| 469 |
typedef __allocator_destructor<_Alloc> _Dp; |
| 470 |
_Alloc __a; |
| 471 |
unique_ptr<_Tp, _Dp> __hold(_ATraits::allocate(__a, 1), _Dp(__a, 1)); |
| 472 |
_Tp * __ret = __hold.get(); |
| 473 |
_ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...); |
| 474 |
__dest.__s_.__ptr = __hold.release(); |
| 475 |
__dest.__h_ = &_LargeHandler::__handle; |
| 476 |
return *__ret; |
| 477 |
} |
| 478 |
|
| 479 |
private: |
| 480 |
|
| 481 |
_LIBCPP_INLINE_VISIBILITY |
| 482 |
static void __destroy(any & __this){ |
| 483 |
typedef allocator<_Tp> _Alloc; |
| 484 |
typedef allocator_traits<_Alloc> _ATraits; |
| 485 |
_Alloc __a; |
| 486 |
_Tp * __p = static_cast<_Tp *>(__this.__s_.__ptr); |
| 487 |
_ATraits::destroy(__a, __p); |
| 488 |
_ATraits::deallocate(__a, __p, 1); |
| 489 |
__this.__h_ = nullptr; |
| 490 |
} |
| 491 |
|
| 492 |
_LIBCPP_INLINE_VISIBILITY |
| 493 |
static void __copy(any const & __this, any & __dest) { |
| 494 |
_LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s_.__ptr)); |
| 495 |
} |
| 496 |
|
| 497 |
_LIBCPP_INLINE_VISIBILITY |
| 498 |
static void __move(any & __this, any & __dest) { |
| 499 |
__dest.__s_.__ptr = __this.__s_.__ptr; |
| 500 |
__dest.__h_ = &_LargeHandler::__handle; |
| 501 |
__this.__h_ = nullptr; |
| 502 |
} |
| 503 |
|
| 504 |
_LIBCPP_INLINE_VISIBILITY |
| 505 |
static void* __get(any & __this, type_info const * __info, |
| 506 |
void const* __fallback_info) |
| 507 |
{ |
| 508 |
if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info)) |
| 509 |
return static_cast<void*>(__this.__s_.__ptr); |
| 510 |
return nullptr; |
| 511 |
|
| 512 |
} |
| 513 |
|
| 514 |
_LIBCPP_INLINE_VISIBILITY |
| 515 |
static void* __type_info() |
| 516 |
{ |
| 517 |
#if !defined(_LIBCPP_HAS_NO_RTTI) |
| 518 |
return const_cast<void*>(static_cast<void const *>(&typeid(_Tp))); |
| 519 |
#else |
| 520 |
return nullptr; |
| 521 |
#endif |
| 522 |
} |
| 523 |
}; |
| 524 |
|
| 525 |
} // namespace __any_imp |
| 526 |
|
| 527 |
|
| 528 |
template <class _ValueType, class _Tp, class> |
| 529 |
any::any(_ValueType && __v) : __h_(nullptr) |
| 530 |
{ |
| 531 |
__any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_ValueType>(__v)); |
| 532 |
} |
| 533 |
|
| 534 |
template <class _ValueType, class ..._Args, class _Tp, class> |
| 535 |
any::any(in_place_type_t<_ValueType>, _Args&&... __args) { |
| 536 |
__any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...); |
| 537 |
} |
| 538 |
|
| 539 |
template <class _ValueType, class _Up, class ..._Args, class _Tp, class> |
| 540 |
any::any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) { |
| 541 |
__any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...); |
| 542 |
} |
| 543 |
|
| 544 |
template <class _ValueType, class, class> |
| 545 |
inline _LIBCPP_INLINE_VISIBILITY |
| 546 |
any & any::operator=(_ValueType && __v) |
| 547 |
{ |
| 548 |
any(_VSTD::forward<_ValueType>(__v)).swap(*this); |
| 549 |
return *this; |
| 550 |
} |
| 551 |
|
| 552 |
template <class _ValueType, class ..._Args, class _Tp, class> |
| 553 |
inline _LIBCPP_INLINE_VISIBILITY |
| 554 |
_Tp& any::emplace(_Args&&... __args) { |
| 555 |
reset(); |
| 556 |
return __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...); |
| 557 |
} |
| 558 |
|
| 559 |
template <class _ValueType, class _Up, class ..._Args, class _Tp, class> |
| 560 |
inline _LIBCPP_INLINE_VISIBILITY |
| 561 |
_Tp& any::emplace(initializer_list<_Up> __il, _Args&&... __args) { |
| 562 |
reset(); |
| 563 |
return __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...); |
| 564 |
} |
| 565 |
|
| 566 |
inline _LIBCPP_INLINE_VISIBILITY |
| 567 |
void any::swap(any & __rhs) _NOEXCEPT |
| 568 |
{ |
| 569 |
if (this == &__rhs) |
| 570 |
return; |
| 571 |
if (__h_ && __rhs.__h_) { |
| 572 |
any __tmp; |
| 573 |
__rhs.__call(_Action::_Move, &__tmp); |
| 574 |
this->__call(_Action::_Move, &__rhs); |
| 575 |
__tmp.__call(_Action::_Move, this); |
| 576 |
} |
| 577 |
else if (__h_) { |
| 578 |
this->__call(_Action::_Move, &__rhs); |
| 579 |
} |
| 580 |
else if (__rhs.__h_) { |
| 581 |
__rhs.__call(_Action::_Move, this); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
// 6.4 Non-member functions |
| 586 |
|
| 587 |
inline _LIBCPP_INLINE_VISIBILITY |
| 588 |
void swap(any & __lhs, any & __rhs) _NOEXCEPT |
| 589 |
{ |
| 590 |
__lhs.swap(__rhs); |
| 591 |
} |
| 592 |
|
| 593 |
template <class _Tp, class ..._Args> |
| 594 |
inline _LIBCPP_INLINE_VISIBILITY |
| 595 |
any make_any(_Args&&... __args) { |
| 596 |
return any(in_place_type<_Tp>, _VSTD::forward<_Args>(__args)...); |
| 597 |
} |
| 598 |
|
| 599 |
template <class _Tp, class _Up, class ..._Args> |
| 600 |
inline _LIBCPP_INLINE_VISIBILITY |
| 601 |
any make_any(initializer_list<_Up> __il, _Args&&... __args) { |
| 602 |
return any(in_place_type<_Tp>, __il, _VSTD::forward<_Args>(__args)...); |
| 603 |
} |
| 604 |
|
| 605 |
template <class _ValueType> |
| 606 |
inline _LIBCPP_INLINE_VISIBILITY |
| 607 |
_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST |
| 608 |
_ValueType any_cast(any const & __v) |
| 609 |
{ |
| 610 |
using _RawValueType = __remove_cvref_t<_ValueType>; |
| 611 |
static_assert(is_constructible<_ValueType, _RawValueType const &>::value, |
| 612 |
"ValueType is required to be a const lvalue reference " |
| 613 |
"or a CopyConstructible type"); |
| 614 |
auto __tmp = _VSTD::any_cast<add_const_t<_RawValueType>>(&__v); |
| 615 |
if (__tmp == nullptr) |
| 616 |
__throw_bad_any_cast(); |
| 617 |
return static_cast<_ValueType>(*__tmp); |
| 618 |
} |
| 619 |
|
| 620 |
template <class _ValueType> |
| 621 |
inline _LIBCPP_INLINE_VISIBILITY |
| 622 |
_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST |
| 623 |
_ValueType any_cast(any & __v) |
| 624 |
{ |
| 625 |
using _RawValueType = __remove_cvref_t<_ValueType>; |
| 626 |
static_assert(is_constructible<_ValueType, _RawValueType &>::value, |
| 627 |
"ValueType is required to be an lvalue reference " |
| 628 |
"or a CopyConstructible type"); |
| 629 |
auto __tmp = _VSTD::any_cast<_RawValueType>(&__v); |
| 630 |
if (__tmp == nullptr) |
| 631 |
__throw_bad_any_cast(); |
| 632 |
return static_cast<_ValueType>(*__tmp); |
| 633 |
} |
| 634 |
|
| 635 |
template <class _ValueType> |
| 636 |
inline _LIBCPP_INLINE_VISIBILITY |
| 637 |
_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST |
| 638 |
_ValueType any_cast(any && __v) |
| 639 |
{ |
| 640 |
using _RawValueType = __remove_cvref_t<_ValueType>; |
| 641 |
static_assert(is_constructible<_ValueType, _RawValueType>::value, |
| 642 |
"ValueType is required to be an rvalue reference " |
| 643 |
"or a CopyConstructible type"); |
| 644 |
auto __tmp = _VSTD::any_cast<_RawValueType>(&__v); |
| 645 |
if (__tmp == nullptr) |
| 646 |
__throw_bad_any_cast(); |
| 647 |
return static_cast<_ValueType>(_VSTD::move(*__tmp)); |
| 648 |
} |
| 649 |
|
| 650 |
template <class _ValueType> |
| 651 |
inline _LIBCPP_INLINE_VISIBILITY |
| 652 |
add_pointer_t<add_const_t<_ValueType>> |
| 653 |
any_cast(any const * __any) _NOEXCEPT |
| 654 |
{ |
| 655 |
static_assert(!is_reference<_ValueType>::value, |
| 656 |
"_ValueType may not be a reference."); |
| 657 |
return _VSTD::any_cast<_ValueType>(const_cast<any *>(__any)); |
| 658 |
} |
| 659 |
|
| 660 |
template <class _RetType> |
| 661 |
inline _LIBCPP_INLINE_VISIBILITY |
| 662 |
_RetType __pointer_or_func_cast(void* __p, /*IsFunction*/false_type) noexcept { |
| 663 |
return static_cast<_RetType>(__p); |
| 664 |
} |
| 665 |
|
| 666 |
template <class _RetType> |
| 667 |
inline _LIBCPP_INLINE_VISIBILITY |
| 668 |
_RetType __pointer_or_func_cast(void*, /*IsFunction*/true_type) noexcept { |
| 669 |
return nullptr; |
| 670 |
} |
| 671 |
|
| 672 |
template <class _ValueType> |
| 673 |
_LIBCPP_HIDE_FROM_ABI |
| 674 |
add_pointer_t<_ValueType> |
| 675 |
any_cast(any * __any) _NOEXCEPT |
| 676 |
{ |
| 677 |
using __any_imp::_Action; |
| 678 |
static_assert(!is_reference<_ValueType>::value, |
| 679 |
"_ValueType may not be a reference."); |
| 680 |
typedef add_pointer_t<_ValueType> _ReturnType; |
| 681 |
if (__any && __any->__h_) { |
| 682 |
void *__p = __any->__call(_Action::_Get, nullptr, |
| 683 |
#if !defined(_LIBCPP_HAS_NO_RTTI) |
| 684 |
&typeid(_ValueType), |
| 685 |
#else |
| 686 |
nullptr, |
| 687 |
#endif |
| 688 |
__any_imp::__get_fallback_typeid<_ValueType>()); |
| 689 |
return _VSTD::__pointer_or_func_cast<_ReturnType>( |
| 690 |
__p, is_function<_ValueType>{}); |
| 691 |
} |
| 692 |
return nullptr; |
| 693 |
} |
| 694 |
|
| 695 |
#endif // _LIBCPP_STD_VER > 14 |
| 696 |
|
| 697 |
_LIBCPP_END_NAMESPACE_STD |
| 698 |
|
| 699 |
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17 |
| 700 |
# include <chrono> |
| 701 |
#endif |
| 702 |
|
| 703 |
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 |
| 704 |
# include <atomic> |
| 705 |
# include <concepts> |
| 706 |
# include <iosfwd> |
| 707 |
# include <iterator> |
| 708 |
# include <memory> |
| 709 |
# include <variant> |
| 710 |
#endif |
| 711 |
|
| 712 |
#endif // _LIBCPP_ANY |