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