2016-08-11 11:13:11 +08:00
|
|
|
// -*- C++ -*-
|
2021-11-18 05:25:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2016-08-11 11:13:11 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-08-11 11:13:11 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP_ANY
|
|
|
|
#define _LIBCPP_ANY
|
|
|
|
|
|
|
|
/*
|
|
|
|
any synopsis
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
|
|
|
|
class bad_any_cast : public bad_cast
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual const char* what() const noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
class any
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
// 6.3.1 any construct/destruct
|
|
|
|
any() noexcept;
|
|
|
|
|
|
|
|
any(const any& other);
|
|
|
|
any(any&& other) noexcept;
|
|
|
|
|
|
|
|
template <class ValueType>
|
|
|
|
any(ValueType&& value);
|
|
|
|
|
|
|
|
~any();
|
|
|
|
|
|
|
|
// 6.3.2 any assignments
|
|
|
|
any& operator=(const any& rhs);
|
|
|
|
any& operator=(any&& rhs) noexcept;
|
|
|
|
|
|
|
|
template <class ValueType>
|
|
|
|
any& operator=(ValueType&& rhs);
|
|
|
|
|
|
|
|
// 6.3.3 any modifiers
|
2017-04-13 06:51:27 +08:00
|
|
|
template <class ValueType, class... Args>
|
|
|
|
decay_t<ValueType>& emplace(Args&&... args);
|
|
|
|
template <class ValueType, class U, class... Args>
|
|
|
|
decay_t<ValueType>& emplace(initializer_list<U>, Args&&...);
|
2016-08-11 11:13:11 +08:00
|
|
|
void reset() noexcept;
|
|
|
|
void swap(any& rhs) noexcept;
|
|
|
|
|
|
|
|
// 6.3.4 any observers
|
|
|
|
bool has_value() const noexcept;
|
|
|
|
const type_info& type() const noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 6.4 Non-member functions
|
|
|
|
void swap(any& x, any& y) noexcept;
|
|
|
|
|
|
|
|
template <class T, class ...Args>
|
|
|
|
any make_any(Args&& ...args);
|
|
|
|
template <class T, class U, class ...Args>
|
|
|
|
any make_any(initializer_list<U>, Args&& ...args);
|
|
|
|
|
|
|
|
template<class ValueType>
|
|
|
|
ValueType any_cast(const any& operand);
|
|
|
|
template<class ValueType>
|
|
|
|
ValueType any_cast(any& operand);
|
|
|
|
template<class ValueType>
|
|
|
|
ValueType any_cast(any&& operand);
|
|
|
|
|
|
|
|
template<class ValueType>
|
|
|
|
const ValueType* any_cast(const any* operand) noexcept;
|
|
|
|
template<class ValueType>
|
|
|
|
ValueType* any_cast(any* operand) noexcept;
|
|
|
|
|
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2022-03-26 00:55:36 +08:00
|
|
|
#include <__assert> // all public C++ headers provide the assertion handler
|
2020-11-05 04:01:25 +08:00
|
|
|
#include <__availability>
|
2021-05-19 23:57:04 +08:00
|
|
|
#include <__config>
|
2021-06-05 10:47:47 +08:00
|
|
|
#include <__utility/forward.h>
|
2022-03-06 02:17:07 +08:00
|
|
|
#include <__utility/in_place.h>
|
|
|
|
#include <__utility/move.h>
|
2022-03-04 02:39:12 +08:00
|
|
|
#include <__utility/unreachable.h>
|
2021-05-19 23:57:04 +08:00
|
|
|
#include <cstdlib>
|
2022-06-11 01:53:10 +08:00
|
|
|
#include <initializer_list>
|
2022-09-03 10:35:10 +08:00
|
|
|
#include <memory>
|
2016-08-11 11:13:11 +08:00
|
|
|
#include <type_traits>
|
2021-05-19 23:57:04 +08:00
|
|
|
#include <typeinfo>
|
2018-09-13 03:41:40 +08:00
|
|
|
#include <version>
|
2016-08-11 11:13:11 +08:00
|
|
|
|
2022-08-20 16:34:26 +08:00
|
|
|
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
|
|
|
|
# include <chrono> // IGNORE-CYCLE due to <format>
|
[libc++] Re-add transitive includes that had been removed since LLVM 14
This commit re-adds transitive includes that had been removed by
4cd04d1687f1, c36870c8e79c, a83f4b9cda57, 1458458b558d, 2e2f3158c604,
and 489637e66dd3. This should cover almost all the includes that had
been removed since LLVM 14 and that would contribute to breaking user
code when releasing LLVM 15.
It is possible to disable the inclusion of these headers by defining
_LIBCPP_REMOVE_TRANSITIVE_INCLUDES. The intent is that vendors will
enable that macro and start fixing downstream issues immediately. We
can then remove the macro (and the transitive includes) by default in
a future release. That way, we will break users only once by removing
transitive includes in bulk instead of doing it bit by bit a every
release, which is more disruptive for users.
Note 1: The set of headers to re-add was found by re-generating the
transitive include test on a checkout of release/14.x, which
provided the list of all transitive includes we used to provide.
Note 2: Several includes of <vector>, <optional>, <array> and <unordered_map>
have been added in this commit. These transitive inclusions were
added when we implemented boyer_moore_searcher in <functional>.
Note 3: This is a best effort patch to try and resolve downstream breakage
caused since branching LLVM 14. I wasn't able to perfectly mirror
transitive includes in LLVM 14 for a few headers, so I added a
release note explaining it. To summarize, adding boyer_moore_searcher
created a bunch of circular dependencies, so we have to break
backwards compatibility in a few cases.
Differential Revision: https://reviews.llvm.org/D128661
2022-06-28 03:53:41 +08:00
|
|
|
#endif
|
|
|
|
|
2016-08-11 11:13:11 +08:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-02 09:16:40 +08:00
|
|
|
# pragma GCC system_header
|
2016-08-11 11:13:11 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace std {
|
2018-11-19 23:37:04 +08:00
|
|
|
class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_ANY_CAST bad_any_cast : public bad_cast
|
2016-08-11 11:13:11 +08:00
|
|
|
{
|
|
|
|
public:
|
2022-08-24 08:14:29 +08:00
|
|
|
const char* what() const _NOEXCEPT override;
|
2016-08-11 11:13:11 +08:00
|
|
|
};
|
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
|
|
|
#if _LIBCPP_STD_VER > 14
|
|
|
|
|
2018-07-12 07:14:33 +08:00
|
|
|
_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
|
2018-11-19 23:37:04 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
|
2016-08-26 01:47:09 +08:00
|
|
|
void __throw_bad_any_cast()
|
|
|
|
{
|
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
|
|
|
throw bad_any_cast();
|
|
|
|
#else
|
2018-08-04 06:36:53 +08:00
|
|
|
_VSTD::abort();
|
2016-08-26 01:47:09 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-08-11 11:13:11 +08:00
|
|
|
// Forward declarations
|
2017-01-05 07:56:00 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS any;
|
2016-08-11 11:13:11 +08:00
|
|
|
|
|
|
|
template <class _ValueType>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
add_pointer_t<add_const_t<_ValueType>>
|
|
|
|
any_cast(any const *) _NOEXCEPT;
|
|
|
|
|
|
|
|
template <class _ValueType>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
add_pointer_t<_ValueType> any_cast(any *) _NOEXCEPT;
|
|
|
|
|
|
|
|
namespace __any_imp
|
|
|
|
{
|
|
|
|
using _Buffer = aligned_storage_t<3*sizeof(void*), alignment_of<void*>::value>;
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
using _IsSmallObject = integral_constant<bool
|
|
|
|
, sizeof(_Tp) <= sizeof(_Buffer)
|
|
|
|
&& alignment_of<_Buffer>::value
|
|
|
|
% alignment_of<_Tp>::value == 0
|
|
|
|
&& is_nothrow_move_constructible<_Tp>::value
|
|
|
|
>;
|
|
|
|
|
|
|
|
enum class _Action {
|
|
|
|
_Destroy,
|
|
|
|
_Copy,
|
|
|
|
_Move,
|
|
|
|
_Get,
|
|
|
|
_TypeInfo
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Tp> struct _SmallHandler;
|
|
|
|
template <class _Tp> struct _LargeHandler;
|
|
|
|
|
|
|
|
template <class _Tp>
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS __unique_typeinfo { static constexpr int __id = 0; };
|
2016-08-11 11:13:11 +08:00
|
|
|
template <class _Tp> constexpr int __unique_typeinfo<_Tp>::__id;
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
constexpr const void* __get_fallback_typeid() {
|
2020-07-15 23:26:20 +08:00
|
|
|
return &__unique_typeinfo<remove_cv_t<remove_reference_t<_Tp>>>::__id;
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
bool __compare_typeid(type_info const* __id, const void* __fallback_id)
|
|
|
|
{
|
|
|
|
#if !defined(_LIBCPP_NO_RTTI)
|
|
|
|
if (__id && *__id == typeid(_Tp))
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
if (!__id && __fallback_id == __any_imp::__get_fallback_typeid<_Tp>())
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
using _Handler = conditional_t<
|
|
|
|
_IsSmallObject<_Tp>::value, _SmallHandler<_Tp>, _LargeHandler<_Tp>>;
|
|
|
|
|
|
|
|
} // namespace __any_imp
|
|
|
|
|
2017-01-05 07:56:00 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS any
|
2016-08-11 11:13:11 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// construct/destruct
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2022-09-02 22:19:07 +08:00
|
|
|
constexpr any() _NOEXCEPT : __h_(nullptr) {}
|
2016-08-11 11:13:11 +08:00
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2022-09-02 22:19:07 +08:00
|
|
|
any(any const & __other) : __h_(nullptr)
|
2016-08-11 11:13:11 +08:00
|
|
|
{
|
2022-09-02 22:19:07 +08:00
|
|
|
if (__other.__h_) __other.__call(_Action::_Copy, this);
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2022-09-02 22:19:07 +08:00
|
|
|
any(any && __other) _NOEXCEPT : __h_(nullptr)
|
2016-08-11 11:13:11 +08:00
|
|
|
{
|
2022-09-02 22:19:07 +08:00
|
|
|
if (__other.__h_) __other.__call(_Action::_Move, this);
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
class _ValueType
|
2016-10-08 05:27:45 +08:00
|
|
|
, class _Tp = decay_t<_ValueType>
|
2016-08-11 11:13:11 +08:00
|
|
|
, class = enable_if_t<
|
2016-10-08 05:27:45 +08:00
|
|
|
!is_same<_Tp, any>::value &&
|
2016-11-18 03:24:04 +08:00
|
|
|
!__is_inplace_type<_ValueType>::value &&
|
2016-10-08 05:27:45 +08:00
|
|
|
is_copy_constructible<_Tp>::value>
|
2016-08-11 11:13:11 +08:00
|
|
|
>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
any(_ValueType && __value);
|
|
|
|
|
2016-10-08 05:27:45 +08:00
|
|
|
template <class _ValueType, class ..._Args,
|
|
|
|
class _Tp = decay_t<_ValueType>,
|
2016-08-11 11:13:11 +08:00
|
|
|
class = enable_if_t<
|
|
|
|
is_constructible<_Tp, _Args...>::value &&
|
|
|
|
is_copy_constructible<_Tp>::value
|
|
|
|
>
|
|
|
|
>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2016-10-08 05:27:45 +08:00
|
|
|
explicit any(in_place_type_t<_ValueType>, _Args&&... __args);
|
2016-08-11 11:13:11 +08:00
|
|
|
|
2016-10-08 05:27:45 +08:00
|
|
|
template <class _ValueType, class _Up, class ..._Args,
|
|
|
|
class _Tp = decay_t<_ValueType>,
|
2016-08-11 11:13:11 +08:00
|
|
|
class = enable_if_t<
|
|
|
|
is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
|
|
|
|
is_copy_constructible<_Tp>::value>
|
|
|
|
>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2016-10-08 05:27:45 +08:00
|
|
|
explicit any(in_place_type_t<_ValueType>, initializer_list<_Up>, _Args&&... __args);
|
2016-08-11 11:13:11 +08:00
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
~any() { this->reset(); }
|
|
|
|
|
|
|
|
// assignments
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
any & operator=(any const & __rhs) {
|
|
|
|
any(__rhs).swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
any & operator=(any && __rhs) _NOEXCEPT {
|
|
|
|
any(_VSTD::move(__rhs)).swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
class _ValueType
|
2016-10-08 05:27:45 +08:00
|
|
|
, class _Tp = decay_t<_ValueType>
|
2016-08-11 11:13:11 +08:00
|
|
|
, class = enable_if_t<
|
2016-10-08 05:27:45 +08:00
|
|
|
!is_same<_Tp, any>::value
|
2016-10-16 10:51:50 +08:00
|
|
|
&& is_copy_constructible<_Tp>::value>
|
2016-08-11 11:13:11 +08:00
|
|
|
>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
any & operator=(_ValueType && __rhs);
|
|
|
|
|
2016-10-08 05:27:45 +08:00
|
|
|
template <class _ValueType, class ..._Args,
|
|
|
|
class _Tp = decay_t<_ValueType>,
|
2016-08-11 11:13:11 +08:00
|
|
|
class = enable_if_t<
|
|
|
|
is_constructible<_Tp, _Args...>::value &&
|
|
|
|
is_copy_constructible<_Tp>::value>
|
|
|
|
>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2022-07-09 00:17:26 +08:00
|
|
|
_Tp& emplace(_Args&&...);
|
2016-08-11 11:13:11 +08:00
|
|
|
|
2016-10-08 05:27:45 +08:00
|
|
|
template <class _ValueType, class _Up, class ..._Args,
|
|
|
|
class _Tp = decay_t<_ValueType>,
|
2016-08-11 11:13:11 +08:00
|
|
|
class = enable_if_t<
|
|
|
|
is_constructible<_Tp, initializer_list<_Up>&, _Args...>::value &&
|
|
|
|
is_copy_constructible<_Tp>::value>
|
|
|
|
>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-04-13 06:51:27 +08:00
|
|
|
_Tp& emplace(initializer_list<_Up>, _Args&&...);
|
2016-08-11 11:13:11 +08:00
|
|
|
|
|
|
|
// 6.3.3 any modifiers
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2022-09-02 22:19:07 +08:00
|
|
|
void reset() _NOEXCEPT { if (__h_) this->__call(_Action::_Destroy); }
|
2016-08-11 11:13:11 +08:00
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
void swap(any & __rhs) _NOEXCEPT;
|
|
|
|
|
|
|
|
// 6.3.4 any observers
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2022-09-02 22:19:07 +08:00
|
|
|
bool has_value() const _NOEXCEPT { return __h_ != nullptr; }
|
2016-08-11 11:13:11 +08:00
|
|
|
|
|
|
|
#if !defined(_LIBCPP_NO_RTTI)
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
const type_info & type() const _NOEXCEPT {
|
2022-09-02 22:19:07 +08:00
|
|
|
if (__h_) {
|
2016-08-11 11:13:11 +08:00
|
|
|
return *static_cast<type_info const *>(this->__call(_Action::_TypeInfo));
|
|
|
|
} else {
|
|
|
|
return typeid(void);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef __any_imp::_Action _Action;
|
|
|
|
using _HandleFuncPtr = void* (*)(_Action, any const *, any *, const type_info *,
|
|
|
|
const void* __fallback_info);
|
|
|
|
|
|
|
|
union _Storage {
|
|
|
|
constexpr _Storage() : __ptr(nullptr) {}
|
|
|
|
void * __ptr;
|
|
|
|
__any_imp::_Buffer __buf;
|
|
|
|
};
|
|
|
|
|
2018-07-12 07:14:33 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2016-08-11 11:13:11 +08:00
|
|
|
void * __call(_Action __a, any * __other = nullptr,
|
|
|
|
type_info const * __info = nullptr,
|
|
|
|
const void* __fallback_info = nullptr) const
|
|
|
|
{
|
2022-09-02 22:19:07 +08:00
|
|
|
return __h_(__a, this, __other, __info, __fallback_info);
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
2018-07-12 07:14:33 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2016-08-11 11:13:11 +08:00
|
|
|
void * __call(_Action __a, any * __other = nullptr,
|
|
|
|
type_info const * __info = nullptr,
|
|
|
|
const void* __fallback_info = nullptr)
|
|
|
|
{
|
2022-09-02 22:19:07 +08:00
|
|
|
return __h_(__a, this, __other, __info, __fallback_info);
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class>
|
|
|
|
friend struct __any_imp::_SmallHandler;
|
|
|
|
template <class>
|
|
|
|
friend struct __any_imp::_LargeHandler;
|
|
|
|
|
|
|
|
template <class _ValueType>
|
|
|
|
friend add_pointer_t<add_const_t<_ValueType>>
|
|
|
|
any_cast(any const *) _NOEXCEPT;
|
|
|
|
|
|
|
|
template <class _ValueType>
|
|
|
|
friend add_pointer_t<_ValueType>
|
|
|
|
any_cast(any *) _NOEXCEPT;
|
|
|
|
|
2022-09-02 22:19:07 +08:00
|
|
|
_HandleFuncPtr __h_ = nullptr;
|
|
|
|
_Storage __s_;
|
2016-08-11 11:13:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace __any_imp
|
|
|
|
{
|
|
|
|
template <class _Tp>
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS _SmallHandler
|
2016-08-11 11:13:11 +08:00
|
|
|
{
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
static void* __handle(_Action __act, any const * __this, any * __other,
|
|
|
|
type_info const * __info, const void* __fallback_info)
|
|
|
|
{
|
|
|
|
switch (__act)
|
|
|
|
{
|
|
|
|
case _Action::_Destroy:
|
|
|
|
__destroy(const_cast<any &>(*__this));
|
|
|
|
return nullptr;
|
|
|
|
case _Action::_Copy:
|
|
|
|
__copy(*__this, *__other);
|
|
|
|
return nullptr;
|
|
|
|
case _Action::_Move:
|
|
|
|
__move(const_cast<any &>(*__this), *__other);
|
|
|
|
return nullptr;
|
|
|
|
case _Action::_Get:
|
|
|
|
return __get(const_cast<any &>(*__this), __info, __fallback_info);
|
|
|
|
case _Action::_TypeInfo:
|
|
|
|
return __type_info();
|
|
|
|
}
|
2022-03-04 02:39:12 +08:00
|
|
|
__libcpp_unreachable();
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ..._Args>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-04-13 06:51:27 +08:00
|
|
|
static _Tp& __create(any & __dest, _Args&&... __args) {
|
2020-09-15 21:56:03 +08:00
|
|
|
typedef allocator<_Tp> _Alloc;
|
|
|
|
typedef allocator_traits<_Alloc> _ATraits;
|
|
|
|
_Alloc __a;
|
2022-09-02 22:19:07 +08:00
|
|
|
_Tp * __ret = static_cast<_Tp*>(static_cast<void*>(&__dest.__s_.__buf));
|
2020-09-15 21:56:03 +08:00
|
|
|
_ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...);
|
2022-09-02 22:19:07 +08:00
|
|
|
__dest.__h_ = &_SmallHandler::__handle;
|
2017-04-13 06:51:27 +08:00
|
|
|
return *__ret;
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
static void __destroy(any & __this) {
|
2020-09-15 21:56:03 +08:00
|
|
|
typedef allocator<_Tp> _Alloc;
|
|
|
|
typedef allocator_traits<_Alloc> _ATraits;
|
|
|
|
_Alloc __a;
|
2022-09-02 22:19:07 +08:00
|
|
|
_Tp * __p = static_cast<_Tp *>(static_cast<void*>(&__this.__s_.__buf));
|
2020-09-15 21:56:03 +08:00
|
|
|
_ATraits::destroy(__a, __p);
|
2022-09-02 22:19:07 +08:00
|
|
|
__this.__h_ = nullptr;
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
static void __copy(any const & __this, any & __dest) {
|
|
|
|
_SmallHandler::__create(__dest, *static_cast<_Tp const *>(
|
2022-09-02 22:19:07 +08:00
|
|
|
static_cast<void const *>(&__this.__s_.__buf)));
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
static void __move(any & __this, any & __dest) {
|
|
|
|
_SmallHandler::__create(__dest, _VSTD::move(
|
2022-09-02 22:19:07 +08:00
|
|
|
*static_cast<_Tp*>(static_cast<void*>(&__this.__s_.__buf))));
|
2016-08-11 11:13:11 +08:00
|
|
|
__destroy(__this);
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
static void* __get(any & __this,
|
|
|
|
type_info const * __info,
|
|
|
|
const void* __fallback_id)
|
|
|
|
{
|
|
|
|
if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_id))
|
2022-09-02 22:19:07 +08:00
|
|
|
return static_cast<void*>(&__this.__s_.__buf);
|
2016-08-11 11:13:11 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
static void* __type_info()
|
|
|
|
{
|
|
|
|
#if !defined(_LIBCPP_NO_RTTI)
|
|
|
|
return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
|
|
|
|
#else
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Tp>
|
2017-01-05 07:56:00 +08:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS _LargeHandler
|
2016-08-11 11:13:11 +08:00
|
|
|
{
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
static void* __handle(_Action __act, any const * __this,
|
|
|
|
any * __other, type_info const * __info,
|
|
|
|
void const* __fallback_info)
|
|
|
|
{
|
|
|
|
switch (__act)
|
|
|
|
{
|
|
|
|
case _Action::_Destroy:
|
|
|
|
__destroy(const_cast<any &>(*__this));
|
|
|
|
return nullptr;
|
|
|
|
case _Action::_Copy:
|
|
|
|
__copy(*__this, *__other);
|
|
|
|
return nullptr;
|
|
|
|
case _Action::_Move:
|
|
|
|
__move(const_cast<any &>(*__this), *__other);
|
|
|
|
return nullptr;
|
|
|
|
case _Action::_Get:
|
|
|
|
return __get(const_cast<any &>(*__this), __info, __fallback_info);
|
|
|
|
case _Action::_TypeInfo:
|
|
|
|
return __type_info();
|
|
|
|
}
|
2022-03-04 02:39:12 +08:00
|
|
|
__libcpp_unreachable();
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ..._Args>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-04-13 06:51:27 +08:00
|
|
|
static _Tp& __create(any & __dest, _Args&&... __args) {
|
2016-08-11 11:13:11 +08:00
|
|
|
typedef allocator<_Tp> _Alloc;
|
2020-09-15 21:56:03 +08:00
|
|
|
typedef allocator_traits<_Alloc> _ATraits;
|
2016-08-11 11:13:11 +08:00
|
|
|
typedef __allocator_destructor<_Alloc> _Dp;
|
|
|
|
_Alloc __a;
|
2020-09-15 21:56:03 +08:00
|
|
|
unique_ptr<_Tp, _Dp> __hold(_ATraits::allocate(__a, 1), _Dp(__a, 1));
|
|
|
|
_Tp * __ret = __hold.get();
|
|
|
|
_ATraits::construct(__a, __ret, _VSTD::forward<_Args>(__args)...);
|
2022-09-02 22:19:07 +08:00
|
|
|
__dest.__s_.__ptr = __hold.release();
|
|
|
|
__dest.__h_ = &_LargeHandler::__handle;
|
2017-04-13 06:51:27 +08:00
|
|
|
return *__ret;
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
static void __destroy(any & __this){
|
2020-09-15 21:56:03 +08:00
|
|
|
typedef allocator<_Tp> _Alloc;
|
|
|
|
typedef allocator_traits<_Alloc> _ATraits;
|
|
|
|
_Alloc __a;
|
2022-09-02 22:19:07 +08:00
|
|
|
_Tp * __p = static_cast<_Tp *>(__this.__s_.__ptr);
|
2020-09-15 21:56:03 +08:00
|
|
|
_ATraits::destroy(__a, __p);
|
|
|
|
_ATraits::deallocate(__a, __p, 1);
|
2022-09-02 22:19:07 +08:00
|
|
|
__this.__h_ = nullptr;
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
static void __copy(any const & __this, any & __dest) {
|
2022-09-02 22:19:07 +08:00
|
|
|
_LargeHandler::__create(__dest, *static_cast<_Tp const *>(__this.__s_.__ptr));
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
static void __move(any & __this, any & __dest) {
|
2022-09-02 22:19:07 +08:00
|
|
|
__dest.__s_.__ptr = __this.__s_.__ptr;
|
|
|
|
__dest.__h_ = &_LargeHandler::__handle;
|
|
|
|
__this.__h_ = nullptr;
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
static void* __get(any & __this, type_info const * __info,
|
|
|
|
void const* __fallback_info)
|
|
|
|
{
|
|
|
|
if (__any_imp::__compare_typeid<_Tp>(__info, __fallback_info))
|
2022-09-02 22:19:07 +08:00
|
|
|
return static_cast<void*>(__this.__s_.__ptr);
|
2016-08-11 11:13:11 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
static void* __type_info()
|
|
|
|
{
|
|
|
|
#if !defined(_LIBCPP_NO_RTTI)
|
|
|
|
return const_cast<void*>(static_cast<void const *>(&typeid(_Tp)));
|
|
|
|
#else
|
|
|
|
return nullptr;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace __any_imp
|
|
|
|
|
|
|
|
|
2016-10-08 05:27:45 +08:00
|
|
|
template <class _ValueType, class _Tp, class>
|
2022-09-02 22:19:07 +08:00
|
|
|
any::any(_ValueType && __v) : __h_(nullptr)
|
2016-08-11 11:13:11 +08:00
|
|
|
{
|
2016-10-08 05:27:45 +08:00
|
|
|
__any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_ValueType>(__v));
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
2016-10-08 05:27:45 +08:00
|
|
|
template <class _ValueType, class ..._Args, class _Tp, class>
|
|
|
|
any::any(in_place_type_t<_ValueType>, _Args&&... __args) {
|
|
|
|
__any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
|
2019-06-20 00:33:28 +08:00
|
|
|
}
|
2016-08-11 11:13:11 +08:00
|
|
|
|
2016-10-08 05:27:45 +08:00
|
|
|
template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
|
|
|
|
any::any(in_place_type_t<_ValueType>, initializer_list<_Up> __il, _Args&&... __args) {
|
|
|
|
__any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
2016-10-08 05:27:45 +08:00
|
|
|
template <class _ValueType, class, class>
|
2016-08-11 11:13:11 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
any & any::operator=(_ValueType && __v)
|
|
|
|
{
|
|
|
|
any(_VSTD::forward<_ValueType>(__v)).swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-10-08 05:27:45 +08:00
|
|
|
template <class _ValueType, class ..._Args, class _Tp, class>
|
2016-08-11 11:13:11 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2017-04-13 06:51:27 +08:00
|
|
|
_Tp& any::emplace(_Args&&... __args) {
|
2016-08-11 11:13:11 +08:00
|
|
|
reset();
|
2017-04-13 06:51:27 +08:00
|
|
|
return __any_imp::_Handler<_Tp>::__create(*this, _VSTD::forward<_Args>(__args)...);
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
2016-10-08 05:27:45 +08:00
|
|
|
template <class _ValueType, class _Up, class ..._Args, class _Tp, class>
|
2016-08-11 11:13:11 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2017-04-13 06:51:27 +08:00
|
|
|
_Tp& any::emplace(initializer_list<_Up> __il, _Args&&... __args) {
|
2016-08-11 11:13:11 +08:00
|
|
|
reset();
|
2017-04-13 06:51:27 +08:00
|
|
|
return __any_imp::_Handler<_Tp>::__create(*this, __il, _VSTD::forward<_Args>(__args)...);
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void any::swap(any & __rhs) _NOEXCEPT
|
|
|
|
{
|
2016-10-08 05:27:45 +08:00
|
|
|
if (this == &__rhs)
|
|
|
|
return;
|
2022-09-02 22:19:07 +08:00
|
|
|
if (__h_ && __rhs.__h_) {
|
2016-08-11 11:13:11 +08:00
|
|
|
any __tmp;
|
|
|
|
__rhs.__call(_Action::_Move, &__tmp);
|
|
|
|
this->__call(_Action::_Move, &__rhs);
|
|
|
|
__tmp.__call(_Action::_Move, this);
|
|
|
|
}
|
2022-09-02 22:19:07 +08:00
|
|
|
else if (__h_) {
|
2016-08-11 11:13:11 +08:00
|
|
|
this->__call(_Action::_Move, &__rhs);
|
|
|
|
}
|
2022-09-02 22:19:07 +08:00
|
|
|
else if (__rhs.__h_) {
|
2016-08-11 11:13:11 +08:00
|
|
|
__rhs.__call(_Action::_Move, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 6.4 Non-member functions
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
void swap(any & __lhs, any & __rhs) _NOEXCEPT
|
|
|
|
{
|
|
|
|
__lhs.swap(__rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp, class ..._Args>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
any make_any(_Args&&... __args) {
|
2016-11-18 03:24:04 +08:00
|
|
|
return any(in_place_type<_Tp>, _VSTD::forward<_Args>(__args)...);
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp, class _Up, class ..._Args>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
any make_any(initializer_list<_Up> __il, _Args&&... __args) {
|
2016-11-18 03:24:04 +08:00
|
|
|
return any(in_place_type<_Tp>, __il, _VSTD::forward<_Args>(__args)...);
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _ValueType>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2018-11-19 23:37:04 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
|
2016-08-11 11:13:11 +08:00
|
|
|
_ValueType any_cast(any const & __v)
|
|
|
|
{
|
2016-10-08 05:27:45 +08:00
|
|
|
using _RawValueType = __uncvref_t<_ValueType>;
|
|
|
|
static_assert(is_constructible<_ValueType, _RawValueType const &>::value,
|
2016-10-16 09:43:43 +08:00
|
|
|
"ValueType is required to be a const lvalue reference "
|
|
|
|
"or a CopyConstructible type");
|
2016-10-08 05:27:45 +08:00
|
|
|
auto __tmp = _VSTD::any_cast<add_const_t<_RawValueType>>(&__v);
|
2016-08-11 11:13:11 +08:00
|
|
|
if (__tmp == nullptr)
|
2016-08-26 01:47:09 +08:00
|
|
|
__throw_bad_any_cast();
|
2016-10-08 05:27:45 +08:00
|
|
|
return static_cast<_ValueType>(*__tmp);
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _ValueType>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2018-11-19 23:37:04 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
|
2016-08-11 11:13:11 +08:00
|
|
|
_ValueType any_cast(any & __v)
|
|
|
|
{
|
2016-10-08 05:27:45 +08:00
|
|
|
using _RawValueType = __uncvref_t<_ValueType>;
|
|
|
|
static_assert(is_constructible<_ValueType, _RawValueType &>::value,
|
2016-10-16 09:43:43 +08:00
|
|
|
"ValueType is required to be an lvalue reference "
|
|
|
|
"or a CopyConstructible type");
|
2016-10-08 05:27:45 +08:00
|
|
|
auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
|
2016-08-11 11:13:11 +08:00
|
|
|
if (__tmp == nullptr)
|
2016-08-26 01:47:09 +08:00
|
|
|
__throw_bad_any_cast();
|
2016-10-08 05:27:45 +08:00
|
|
|
return static_cast<_ValueType>(*__tmp);
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _ValueType>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2018-11-19 23:37:04 +08:00
|
|
|
_LIBCPP_AVAILABILITY_THROW_BAD_ANY_CAST
|
2016-08-11 11:13:11 +08:00
|
|
|
_ValueType any_cast(any && __v)
|
|
|
|
{
|
2016-10-08 05:27:45 +08:00
|
|
|
using _RawValueType = __uncvref_t<_ValueType>;
|
|
|
|
static_assert(is_constructible<_ValueType, _RawValueType>::value,
|
2016-10-16 09:43:43 +08:00
|
|
|
"ValueType is required to be an rvalue reference "
|
|
|
|
"or a CopyConstructible type");
|
2016-10-08 05:27:45 +08:00
|
|
|
auto __tmp = _VSTD::any_cast<_RawValueType>(&__v);
|
2016-08-11 11:13:11 +08:00
|
|
|
if (__tmp == nullptr)
|
2016-08-26 01:47:09 +08:00
|
|
|
__throw_bad_any_cast();
|
2016-10-08 05:27:45 +08:00
|
|
|
return static_cast<_ValueType>(_VSTD::move(*__tmp));
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _ValueType>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
add_pointer_t<add_const_t<_ValueType>>
|
|
|
|
any_cast(any const * __any) _NOEXCEPT
|
|
|
|
{
|
|
|
|
static_assert(!is_reference<_ValueType>::value,
|
|
|
|
"_ValueType may not be a reference.");
|
|
|
|
return _VSTD::any_cast<_ValueType>(const_cast<any *>(__any));
|
|
|
|
}
|
|
|
|
|
2016-10-16 19:56:38 +08:00
|
|
|
template <class _RetType>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
_RetType __pointer_or_func_cast(void* __p, /*IsFunction*/false_type) noexcept {
|
|
|
|
return static_cast<_RetType>(__p);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _RetType>
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
_RetType __pointer_or_func_cast(void*, /*IsFunction*/true_type) noexcept {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-08-11 11:13:11 +08:00
|
|
|
template <class _ValueType>
|
2022-05-27 03:24:59 +08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2016-08-11 11:13:11 +08:00
|
|
|
add_pointer_t<_ValueType>
|
|
|
|
any_cast(any * __any) _NOEXCEPT
|
|
|
|
{
|
|
|
|
using __any_imp::_Action;
|
|
|
|
static_assert(!is_reference<_ValueType>::value,
|
|
|
|
"_ValueType may not be a reference.");
|
|
|
|
typedef typename add_pointer<_ValueType>::type _ReturnType;
|
2022-09-02 22:19:07 +08:00
|
|
|
if (__any && __any->__h_) {
|
2016-10-16 19:56:38 +08:00
|
|
|
void *__p = __any->__call(_Action::_Get, nullptr,
|
2016-08-11 11:13:11 +08:00
|
|
|
#if !defined(_LIBCPP_NO_RTTI)
|
|
|
|
&typeid(_ValueType),
|
|
|
|
#else
|
|
|
|
nullptr,
|
|
|
|
#endif
|
2016-10-16 19:56:38 +08:00
|
|
|
__any_imp::__get_fallback_typeid<_ValueType>());
|
|
|
|
return _VSTD::__pointer_or_func_cast<_ReturnType>(
|
|
|
|
__p, is_function<_ValueType>{});
|
2016-08-11 11:13:11 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // _LIBCPP_STD_VER > 14
|
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
|
|
|
#endif // _LIBCPP_ANY
|