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