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