Eric Fiselier | f6b3bfe | 2016-08-11 03:13:11 +0000 | [diff] [blame^] | 1 | // -*- 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 | |
| 17 | namespace 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 |
| 48 | void reset() noexcept; |
| 49 | void swap(any& rhs) noexcept; |
| 50 | |
| 51 | // 6.3.4 any observers |
| 52 | bool has_value() const noexcept; |
| 53 | const type_info& type() const noexcept; |
| 54 | }; |
| 55 | |
| 56 | // 6.4 Non-member functions |
| 57 | void swap(any& x, any& y) noexcept; |
| 58 | |
| 59 | template <class T, class ...Args> |
| 60 | any make_any(Args&& ...args); |
| 61 | template <class T, class U, class ...Args> |
| 62 | any make_any(initializer_list<U>, Args&& ...args); |
| 63 | |
| 64 | template<class ValueType> |
| 65 | ValueType any_cast(const any& operand); |
| 66 | template<class ValueType> |
| 67 | ValueType any_cast(any& operand); |
| 68 | template<class ValueType> |
| 69 | ValueType any_cast(any&& operand); |
| 70 | |
| 71 | template<class ValueType> |
| 72 | const ValueType* any_cast(const any* operand) noexcept; |
| 73 | template<class ValueType> |
| 74 | ValueType* any_cast(any* operand) noexcept; |
| 75 | |
| 76 | } // namespace fundamentals_v1 |
| 77 | } // namespace experimental |
| 78 | } // namespace std |
| 79 | |
| 80 | */ |
| 81 | |
| 82 | #include <experimental/__config> |
| 83 | #include <memory> |
| 84 | #include <new> |
| 85 | #include <typeinfo> |
| 86 | #include <type_traits> |
| 87 | #include <cstdlib> |
| 88 | #include <cassert> |
| 89 | |
| 90 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 91 | #pragma GCC system_header |
| 92 | #endif |
| 93 | |
| 94 | namespace std { |
| 95 | class _LIBCPP_EXCEPTION_ABI bad_any_cast : public bad_cast |
| 96 | { |
| 97 | public: |
| 98 | virtual const char* what() const _NOEXCEPT; |
| 99 | }; |
| 100 | } // namespace std |
| 101 | |
| 102 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 103 | |
| 104 | #if _LIBCPP_STD_VER > 14 |
| 105 | |
| 106 | // Forward declarations |
| 107 | class _LIBCPP_TYPE_VIS_ONLY any; |
| 108 | |
| 109 | template <class _ValueType> |
| 110 | _LIBCPP_INLINE_VISIBILITY |
| 111 | add_pointer_t<add_const_t<_ValueType>> |
| 112 | any_cast(any const *) _NOEXCEPT; |
| 113 | |
| 114 | template <class _ValueType> |
| 115 | _LIBCPP_INLINE_VISIBILITY |
| 116 | add_pointer_t<_ValueType> any_cast(any *) _NOEXCEPT; |
| 117 | |
| 118 | namespace __any_imp |
| 119 | { |
| 120 | using _Buffer = aligned_storage_t<3*sizeof(void*), alignment_of<void*>::value>; |
| 121 | |
| 122 | template <class _Tp> |
| 123 | using _IsSmallObject = integral_constant<bool |
| 124 | , sizeof(_Tp) <= sizeof(_Buffer) |
| 125 | && alignment_of<_Buffer>::value |
| 126 | % alignment_of<_Tp>::value == 0 |
| 127 | && is_nothrow_move_constructible<_Tp>::value |
| 128 | >; |
| 129 | |
| 130 | enum class _Action { |
| 131 | _Destroy, |
| 132 | _Copy, |
| 133 | _Move, |
| 134 | _Get, |
| 135 | _TypeInfo |
| 136 | }; |
| 137 | |
| 138 | template <class _Tp> struct _SmallHandler; |
| 139 | template <class _Tp> struct _LargeHandler; |
| 140 | |
| 141 | template <class _Tp> |
| 142 | struct _LIBCPP_TYPE_VIS_ONLY __unique_typeinfo { static constexpr int __id = 0; }; |
| 143 | template <class _Tp> constexpr int __unique_typeinfo<_Tp>::__id; |
| 144 | |
| 145 | template <class _Tp> |
| 146 | inline _LIBCPP_INLINE_VISIBILITY |
| 147 | constexpr const void* __get_fallback_typeid() { |
| 148 | return &__unique_typeinfo<decay_t<_Tp>>::__id; |
| 149 | } |
| 150 | |
| 151 | template <class _Tp> |
| 152 | inline _LIBCPP_INLINE_VISIBILITY |
| 153 | bool __compare_typeid(type_info const* __id, const void* __fallback_id) |
| 154 | { |
| 155 | #if !defined(_LIBCPP_NO_RTTI) |
| 156 | if (__id && *__id == typeid(_Tp)) |
| 157 | return true; |
| 158 | #endif |
| 159 | if (!__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>()) |
| 160 | return true; |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | template <class _Tp> |
| 165 | using _Handler = conditional_t< |
| 166 | _IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>; |
| 167 | |
| 168 | } // namespace __any_imp |
| 169 | |
| 170 | class _LIBCPP_TYPE_VIS_ONLY any |
| 171 | { |
| 172 | public: |
| 173 | // construct/destruct |
| 174 | _LIBCPP_INLINE_VISIBILITY |
| 175 | constexpr any() _NOEXCEPT : __h(nullptr) {} |
| 176 | |
| 177 | _LIBCPP_INLINE_VISIBILITY |
| 178 | any(any const & __other) : __h(nullptr) |
| 179 | { |
| 180 | if (__other.__h) __other.__call(_Action::_Copy, this); |
| 181 | } |
| 182 | |
| 183 | _LIBCPP_INLINE_VISIBILITY |
| 184 | any(any && __other) _NOEXCEPT : __h(nullptr) |
| 185 | { |
| 186 | if (__other.__h) __other.__call(_Action::_Move, this); |
| 187 | } |
| 188 | |
| 189 | template < |
| 190 | class _ValueType |
| 191 | , class = enable_if_t< |
| 192 | !is_same<decay_t<_ValueType>, any>::value && |
| 193 | !__is_inplace_type<_ValueType>::value && |
| 194 | is_copy_constructible<_ValueType>::value> |
| 195 | > |
| 196 | _LIBCPP_INLINE_VISIBILITY |
| 197 | any(_ValueType && __value); |
| 198 | |
| 199 | template <class _Tp, class ..._Args, |
| 200 | class = enable_if_t< |
| 201 | is_constructible<_Tp, _Args...>::value && |
| 202 | is_copy_constructible<_Tp>::value |
| 203 | > |
| 204 | > |
| 205 | _LIBCPP_INLINE_VISIBILITY |
| 206 | explicit any(in_place_type_t<_Tp>, _Args&&... __args); |
| 207 | |
| 208 | template <class _Tp, class _Up, class ..._Args, |
| 209 | class = enable_if_t< |
| 210 | is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value && |
| 211 | is_copy_constructible<_Tp>::value> |
| 212 | > |
| 213 | _LIBCPP_INLINE_VISIBILITY |
| 214 | explicit any(in_place_type_t<_Tp>, initializer_list<_Up>, _Args&&... __args); |
| 215 | |
| 216 | _LIBCPP_INLINE_VISIBILITY |
| 217 | ~any() { this->reset(); } |
| 218 | |
| 219 | // assignments |
| 220 | _LIBCPP_INLINE_VISIBILITY |
| 221 | any & operator=(any const & __rhs) { |
| 222 | any(__rhs).swap(*this); |
| 223 | return *this; |
| 224 | } |
| 225 | |
| 226 | _LIBCPP_INLINE_VISIBILITY |
| 227 | any & operator=(any && __rhs) _NOEXCEPT { |
| 228 | any(_VSTD::move(__rhs)).swap(*this); |
| 229 | return *this; |
| 230 | } |
| 231 | |
| 232 | // TODO: Should this be constrained to disallow in_place types like the |
| 233 | // ValueType constructor? |
| 234 | template < |
| 235 | class _ValueType |
| 236 | , class = enable_if_t< |
| 237 | !is_same<decay_t<_ValueType>, any>::value |
| 238 | && is_copy_constructible<_ValueType>::value |
| 239 | && !__is_inplace_type<_ValueType>::value> |
| 240 | > |
| 241 | _LIBCPP_INLINE_VISIBILITY |
| 242 | any & operator=(_ValueType && __rhs); |
| 243 | |
| 244 | template <class _Tp, class ..._Args, |
| 245 | class = enable_if_t< |
| 246 | is_constructible<_Tp, _Args...>::value && |
| 247 | is_copy_constructible<_Tp>::value> |
| 248 | > |
| 249 | _LIBCPP_INLINE_VISIBILITY |
| 250 | void emplace(_Args&&... args); |
| 251 | |
| 252 | template <class _Tp, class _Up, class ..._Args, |
| 253 | class = enable_if_t< |
| 254 | is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value && |
| 255 | is_copy_constructible<_Tp>::value> |
| 256 | > |
| 257 | _LIBCPP_INLINE_VISIBILITY |
| 258 | void emplace(initializer_list<_Up>, _Args&&...); |
| 259 | |
| 260 | // 6.3.3 any modifiers |
| 261 | _LIBCPP_INLINE_VISIBILITY |
| 262 | void reset() _NOEXCEPT { if (__h) this->__call(_Action::_Destroy); } |
| 263 | |
| 264 | _LIBCPP_INLINE_VISIBILITY |
| 265 | void swap(any & __rhs) _NOEXCEPT; |
| 266 | |
| 267 | // 6.3.4 any observers |
| 268 | _LIBCPP_INLINE_VISIBILITY |
| 269 | bool has_value() const _NOEXCEPT { return __h != nullptr; } |
| 270 | |
| 271 | #if !defined(_LIBCPP_NO_RTTI) |
| 272 | _LIBCPP_INLINE_VISIBILITY |
| 273 | const type_info & type() const _NOEXCEPT { |
| 274 | if (__h) { |
| 275 | return *static_cast<type_info const *>(this->__call(_Action::_TypeInfo)); |
| 276 | } else { |
| 277 | return typeid(void); |
| 278 | } |
| 279 | } |
| 280 | #endif |
| 281 | |
| 282 | private: |
| 283 | typedef __any_imp::_Action _Action; |
| 284 | using _HandleFuncPtr = void* (*)(_Action, any const *, any *, const type_info *, |
| 285 | const void* __fallback_info); |
| 286 | |
| 287 | union _Storage { |
| 288 | constexpr _Storage() : __ptr(nullptr) {} |
| 289 | void * __ptr; |
| 290 | __any_imp::_Buffer __buf; |
| 291 | }; |
| 292 | |
| 293 | _LIBCPP_ALWAYS_INLINE |
| 294 | void * __call(_Action __a, any * __other = nullptr, |
| 295 | type_info const * __info = nullptr, |
| 296 | const void* __fallback_info = nullptr) const |
| 297 | { |
| 298 | return __h(__a, this, __other, __info, __fallback_info); |
| 299 | } |
| 300 | |
| 301 | _LIBCPP_ALWAYS_INLINE |
| 302 | void * __call(_Action __a, any * __other = nullptr, |
| 303 | type_info const * __info = nullptr, |
| 304 | const void* __fallback_info = nullptr) |
| 305 | { |
| 306 | return __h(__a, this, __other, __info, __fallback_info); |
| 307 | } |
| 308 | |
| 309 | template <class> |
| 310 | friend struct __any_imp::_SmallHandler; |
| 311 | template <class> |
| 312 | friend struct __any_imp::_LargeHandler; |
| 313 | |
| 314 | template <class _ValueType> |
| 315 | friend add_pointer_t<add_const_t<_ValueType>> |
| 316 | any_cast(any const *) _NOEXCEPT; |
| 317 | |
| 318 | template <class _ValueType> |
| 319 | friend add_pointer_t<_ValueType> |
| 320 | any_cast(any *) _NOEXCEPT; |
| 321 | |
| 322 | _HandleFuncPtr __h = nullptr; |
| 323 | _Storage __s; |
| 324 | }; |
| 325 | |
| 326 | namespace __any_imp |
| 327 | { |
| 328 | template <class _Tp> |
| 329 | struct _LIBCPP_TYPE_VIS_ONLY _SmallHandler |
| 330 | { |
| 331 | _LIBCPP_INLINE_VISIBILITY |
| 332 | static void* __handle(_Action __act, any const * __this, any * __other, |
| 333 | type_info const * __info, const void* __fallback_info) |
| 334 | { |
| 335 | switch (__act) |
| 336 | { |
| 337 | case _Action::_Destroy: |
| 338 | __destroy(const_cast<any &>(*__this)); |
| 339 | return nullptr; |
| 340 | case _Action::_Copy: |
| 341 | __copy(*__this, *__other); |
| 342 | return nullptr; |
| 343 | case _Action::_Move: |
| 344 | __move(const_cast<any &>(*__this), *__other); |
| 345 | return nullptr; |
| 346 | case _Action::_Get: |
| 347 | return __get(const_cast<any &>(*__this), __info, __fallback_info); |
| 348 | case _Action::_TypeInfo: |
| 349 | return __type_info(); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | template <class ..._Args> |
| 354 | _LIBCPP_INLINE_VISIBILITY |
| 355 | static void __create(any & __dest, _Args&&... __args) { |
| 356 | ::new (static_cast<void*>(&__dest.__s.__buf)) _Tp(_VSTD::forward<_Args>(__args)...); |
| 357 | __dest.__h = &_SmallHandler::__handle; |
| 358 | } |
| 359 | |
| 360 | private: |
| 361 | _LIBCPP_INLINE_VISIBILITY |
| 362 | static void __destroy(any & __this) { |
| 363 | _Tp & __value = *static_cast<_Tp *>(static_cast<void*>(&__this.__s.__buf)); |
| 364 | __value.~_Tp(); |
| 365 | __this.__h = nullptr; |
| 366 | } |
| 367 | |
| 368 | _LIBCPP_INLINE_VISIBILITY |
| 369 | static void __copy(any const & __this, any & __dest) { |
| 370 | _SmallHandler::__create(__dest, *static_cast<_Tp const *>( |
| 371 | static_cast<void const *>(&__this.__s.__buf))); |
| 372 | } |
| 373 | |
| 374 | _LIBCPP_INLINE_VISIBILITY |
| 375 | static void __move(any & __this, any & __dest) { |
| 376 | _SmallHandler::__create(__dest, _VSTD::move( |
| 377 | *static_cast<_Tp*>(static_cast<void*>(&__this.__s.__buf)))); |
| 378 | __destroy(__this); |
| 379 | } |
| 380 | |
| 381 | _LIBCPP_INLINE_VISIBILITY |
| 382 | static void* __get(any & __this, |
| 383 | type_info const * __info, |
| 384 | const void* __fallback_id) |
| 385 | { |
| 386 | if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id)) |
| 387 | return static_cast<void*>(&__this.__s.__buf); |
| 388 | return nullptr; |
| 389 | } |
| 390 | |
| 391 | _LIBCPP_INLINE_VISIBILITY |
| 392 | static void* __type_info() |
| 393 | { |
| 394 | #if !defined(_LIBCPP_NO_RTTI) |
| 395 | return const_cast<void*>(static_cast<void const *>(&typeid(_Tp))); |
| 396 | #else |
| 397 | return nullptr; |
| 398 | #endif |
| 399 | } |
| 400 | }; |
| 401 | |
| 402 | template <class _Tp> |
| 403 | struct _LIBCPP_TYPE_VIS_ONLY _LargeHandler |
| 404 | { |
| 405 | _LIBCPP_INLINE_VISIBILITY |
| 406 | static void* __handle(_Action __act, any const * __this, |
| 407 | any * __other, type_info const * __info, |
| 408 | void const* __fallback_info) |
| 409 | { |
| 410 | switch (__act) |
| 411 | { |
| 412 | case _Action::_Destroy: |
| 413 | __destroy(const_cast<any &>(*__this)); |
| 414 | return nullptr; |
| 415 | case _Action::_Copy: |
| 416 | __copy(*__this, *__other); |
| 417 | return nullptr; |
| 418 | case _Action::_Move: |
| 419 | __move(const_cast<any &>(*__this), *__other); |
| 420 | return nullptr; |
| 421 | case _Action::_Get: |
| 422 | return __get(const_cast<any &>(*__this), __info, __fallback_info); |
| 423 | case _Action::_TypeInfo: |
| 424 | return __type_info(); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | template <class ..._Args> |
| 429 | _LIBCPP_INLINE_VISIBILITY |
| 430 | static void __create(any & __dest, _Args&&... __args) { |
| 431 | typedef allocator<_Tp> _Alloc; |
| 432 | typedef __allocator_destructor<_Alloc> _Dp; |
| 433 | _Alloc __a; |
| 434 | unique_ptr<_Tp, _Dp> __hold(__a.allocate(1), _Dp(__a, 1)); |
| 435 | ::new ((void*)__hold.get()) _Tp(_VSTD::forward<_Args>(__args)...); |
| 436 | __dest.__s.__ptr = __hold.release(); |
| 437 | __dest.__h = &_LargeHandler::__handle; |
| 438 | } |
| 439 | |
| 440 | private: |
| 441 | |
| 442 | _LIBCPP_INLINE_VISIBILITY |
| 443 | static void __destroy(any & __this){ |
| 444 | delete static_cast<_Tp*>(__this.__s.__ptr); |
| 445 | __this.__h = nullptr; |
| 446 | } |
| 447 | |
| 448 | _LIBCPP_INLINE_VISIBILITY |
| 449 | static void __copy(any const & __this, any & __dest) { |
| 450 | _LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s.__ptr)); |
| 451 | } |
| 452 | |
| 453 | _LIBCPP_INLINE_VISIBILITY |
| 454 | static void __move(any & __this, any & __dest) { |
| 455 | __dest.__s.__ptr = __this.__s.__ptr; |
| 456 | __dest.__h = &_LargeHandler::__handle; |
| 457 | __this.__h = nullptr; |
| 458 | } |
| 459 | |
| 460 | _LIBCPP_INLINE_VISIBILITY |
| 461 | static void* __get(any & __this, type_info const * __info, |
| 462 | void const* __fallback_info) |
| 463 | { |
| 464 | if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info)) |
| 465 | return static_cast<void*>(__this.__s.__ptr); |
| 466 | return nullptr; |
| 467 | |
| 468 | } |
| 469 | |
| 470 | _LIBCPP_INLINE_VISIBILITY |
| 471 | static void* __type_info() |
| 472 | { |
| 473 | #if !defined(_LIBCPP_NO_RTTI) |
| 474 | return const_cast<void*>(static_cast<void const *>(&typeid(_Tp))); |
| 475 | #else |
| 476 | return nullptr; |
| 477 | #endif |
| 478 | } |
| 479 | }; |
| 480 | |
| 481 | } // namespace __any_imp |
| 482 | |
| 483 | |
| 484 | template <class _ValueType, class> |
| 485 | any::any(_ValueType && __v) : __h(nullptr) |
| 486 | { |
| 487 | typedef typename decay<_ValueType>::type _Tp; |
| 488 | static_assert(is_copy_constructible<_Tp>::value, |
| 489 | "_ValueType must be CopyConstructible."); |
| 490 | using _ForwardTp = conditional_t< |
| 491 | is_move_constructible<_Tp>::value, _ValueType, _ValueType&>; |
| 492 | typedef __any_imp::_Handler<_Tp> _HandlerType; |
| 493 | _HandlerType::__create(*this, _VSTD::forward<_ForwardTp>(__v)); |
| 494 | } |
| 495 | |
| 496 | template <class _Tp, class ..._Args, class> |
| 497 | any::any(in_place_type_t<_Tp>, _Args&&... __args) { |
| 498 | using _Hp = __any_imp::_Handler<_Tp>; |
| 499 | _Hp::__create(*this, _VSTD::forward<_Args>(__args)...); |
| 500 | }; |
| 501 | |
| 502 | template <class _Tp, class _Up, class ..._Args, class> |
| 503 | any::any(in_place_type_t<_Tp>, initializer_list<_Up> __il, _Args&&... __args) { |
| 504 | using _Hp = __any_imp::_Handler<_Tp>; |
| 505 | _Hp::__create(*this, __il, _VSTD::forward<_Args>(__args)...); |
| 506 | } |
| 507 | |
| 508 | template <class _ValueType, class> |
| 509 | inline _LIBCPP_INLINE_VISIBILITY |
| 510 | any & any::operator=(_ValueType && __v) |
| 511 | { |
| 512 | typedef typename decay<_ValueType>::type _Tp; |
| 513 | static_assert(is_copy_constructible<_Tp>::value, |
| 514 | "_ValueType must be CopyConstructible."); |
| 515 | any(_VSTD::forward<_ValueType>(__v)).swap(*this); |
| 516 | return *this; |
| 517 | } |
| 518 | |
| 519 | template <class _Tp, class ..._Args, class> |
| 520 | inline _LIBCPP_INLINE_VISIBILITY |
| 521 | void any::emplace(_Args&&... __args) { |
| 522 | using _Hp = __any_imp::_Handler<_Tp>; |
| 523 | reset(); |
| 524 | _Hp::__create(*this, _VSTD::forward<_Args>(__args)...); |
| 525 | } |
| 526 | |
| 527 | template <class _Tp, class _Up, class ..._Args, class> |
| 528 | inline _LIBCPP_INLINE_VISIBILITY |
| 529 | void any::emplace(initializer_list<_Up> __il, _Args&&... __args) { |
| 530 | using _Hp = __any_imp::_Handler<_Tp>; |
| 531 | reset(); |
| 532 | _Hp::__create(*this, __il, _VSTD::forward<_Args>(__args)...); |
| 533 | } |
| 534 | |
| 535 | inline _LIBCPP_INLINE_VISIBILITY |
| 536 | void any::swap(any & __rhs) _NOEXCEPT |
| 537 | { |
| 538 | if (__h && __rhs.__h) { |
| 539 | any __tmp; |
| 540 | __rhs.__call(_Action::_Move, &__tmp); |
| 541 | this->__call(_Action::_Move, &__rhs); |
| 542 | __tmp.__call(_Action::_Move, this); |
| 543 | } |
| 544 | else if (__h) { |
| 545 | this->__call(_Action::_Move, &__rhs); |
| 546 | } |
| 547 | else if (__rhs.__h) { |
| 548 | __rhs.__call(_Action::_Move, this); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | // 6.4 Non-member functions |
| 553 | |
| 554 | inline _LIBCPP_INLINE_VISIBILITY |
| 555 | void swap(any & __lhs, any & __rhs) _NOEXCEPT |
| 556 | { |
| 557 | __lhs.swap(__rhs); |
| 558 | } |
| 559 | |
| 560 | template <class _Tp, class ..._Args> |
| 561 | inline _LIBCPP_INLINE_VISIBILITY |
| 562 | any make_any(_Args&&... __args) { |
| 563 | return any(in_place<_Tp>, _VSTD::forward<_Args>(__args)...); |
| 564 | } |
| 565 | |
| 566 | template <class _Tp, class _Up, class ..._Args> |
| 567 | inline _LIBCPP_INLINE_VISIBILITY |
| 568 | any make_any(initializer_list<_Up> __il, _Args&&... __args) { |
| 569 | return any(in_place<_Tp>, __il, _VSTD::forward<_Args>(__args)...); |
| 570 | } |
| 571 | |
| 572 | template <class _ValueType> |
| 573 | inline _LIBCPP_INLINE_VISIBILITY |
| 574 | _ValueType any_cast(any const & __v) |
| 575 | { |
| 576 | static_assert( |
| 577 | is_reference<_ValueType>::value |
| 578 | || is_copy_constructible<_ValueType>::value, |
| 579 | "_ValueType is required to be a reference or a CopyConstructible type."); |
| 580 | using _Tp = add_const_t<remove_reference_t<_ValueType>>; |
| 581 | _Tp * __tmp = _VSTD::any_cast<_Tp>(&__v); |
| 582 | if (__tmp == nullptr) |
| 583 | __libcpp_throw(bad_any_cast()); |
| 584 | return *__tmp; |
| 585 | } |
| 586 | |
| 587 | template <class _ValueType> |
| 588 | inline _LIBCPP_INLINE_VISIBILITY |
| 589 | _ValueType any_cast(any & __v) |
| 590 | { |
| 591 | static_assert( |
| 592 | is_reference<_ValueType>::value |
| 593 | || is_copy_constructible<_ValueType>::value, |
| 594 | "_ValueType is required to be a reference or a CopyConstructible type."); |
| 595 | typedef typename remove_reference<_ValueType>::type _Tp; |
| 596 | _Tp * __tmp = _VSTD::any_cast<_Tp>(&__v); |
| 597 | if (__tmp == nullptr) |
| 598 | __libcpp_throw(bad_any_cast()); |
| 599 | return *__tmp; |
| 600 | } |
| 601 | |
| 602 | template <class _ValueType> |
| 603 | inline _LIBCPP_INLINE_VISIBILITY |
| 604 | _ValueType any_cast(any && __v) |
| 605 | { |
| 606 | static_assert( |
| 607 | is_reference<_ValueType>::value |
| 608 | || is_copy_constructible<_ValueType>::value, |
| 609 | "_ValueType is required to be a reference or a CopyConstructible type."); |
| 610 | typedef typename remove_reference<_ValueType>::type _Tp; |
| 611 | using _ForwardTp = conditional_t< |
| 612 | is_reference<_ValueType>::value, |
| 613 | _ValueType, |
| 614 | conditional_t<is_move_constructible<_Tp>::value, _Tp, _Tp&> |
| 615 | >; |
| 616 | _Tp * __tmp = _VSTD::any_cast<_Tp>(&__v); |
| 617 | if (__tmp == nullptr) |
| 618 | __libcpp_throw(bad_any_cast()); |
| 619 | return _VSTD::forward<_ForwardTp>(*__tmp); |
| 620 | } |
| 621 | |
| 622 | template <class _ValueType> |
| 623 | inline _LIBCPP_INLINE_VISIBILITY |
| 624 | add_pointer_t<add_const_t<_ValueType>> |
| 625 | any_cast(any const * __any) _NOEXCEPT |
| 626 | { |
| 627 | static_assert(!is_reference<_ValueType>::value, |
| 628 | "_ValueType may not be a reference."); |
| 629 | return _VSTD::any_cast<_ValueType>(const_cast<any *>(__any)); |
| 630 | } |
| 631 | |
| 632 | template <class _ValueType> |
| 633 | add_pointer_t<_ValueType> |
| 634 | any_cast(any * __any) _NOEXCEPT |
| 635 | { |
| 636 | using __any_imp::_Action; |
| 637 | static_assert(!is_reference<_ValueType>::value, |
| 638 | "_ValueType may not be a reference."); |
| 639 | typedef typename add_pointer<_ValueType>::type _ReturnType; |
| 640 | if (__any && __any->__h) { |
| 641 | return static_cast<_ReturnType>( |
| 642 | __any->__call(_Action::_Get, nullptr, |
| 643 | #if !defined(_LIBCPP_NO_RTTI) |
| 644 | &typeid(_ValueType), |
| 645 | #else |
| 646 | nullptr, |
| 647 | #endif |
| 648 | __any_imp::__get_fallback_typeid<_ValueType>() |
| 649 | )); |
| 650 | } |
| 651 | return nullptr; |
| 652 | } |
| 653 | |
| 654 | #endif // _LIBCPP_STD_VER > 14 |
| 655 | |
| 656 | _LIBCPP_END_NAMESPACE_STD |
| 657 | |
| 658 | #endif // _LIBCPP_ANY |