1#ifndef MSTL_CORE_UTILITY_ANY_HPP__
2#define MSTL_CORE_UTILITY_ANY_HPP__
12#include <initializer_list>
22struct any_cast_true_tag {};
23struct any_cast_false_tag {};
32template <
typename T,
typename U>
33const T* __any_cast_aux_dispatch_impl(
const _MSTL any* value, any_cast_true_tag)
noexcept;
73 union storage_internal {
74 storage_internal() =
default;
75 storage_internal(
const storage_internal&) =
delete;
76 storage_internal& operator =(
const storage_internal&) =
delete;
104 const std::type_info* type_ptr_;
115 template <
typename T>
116 struct internal_manage {
123 static void manage(any_operation op,
const any* value, ArgT* arg);
131 template <
typename... Args>
132 static void create(storage_internal& storage, Args&&... args) {
133 void* ptr = &storage.buffer_;
142 static T* access(
const storage_internal& storage) {
143 const void* ptr = &storage.buffer_;
144 return static_cast<T*
>(
const_cast<void*
>(ptr));
155 template <
typename T>
156 struct external_manage {
163 static void manage(any_operation op,
const any* value, ArgT* arg);
171 template <
typename... Args>
172 static void create(storage_internal& storage, Args&&... args) {
181 static T* access(
const storage_internal& storage) {
182 return static_cast<T*
>(storage.ptr_);
192 template <
typename T>
194 is_nothrow_move_constructible_v<T> &&
195 sizeof(T) <=
sizeof(storage_internal) &&
196 alignof(T) <=
alignof(storage_internal)
197 , internal_manage<T>, external_manage<T>>;
199 using manage_func = void (*)(any_operation,
const any*, ArgT*);
201 manage_func manage_ =
nullptr;
202 storage_internal storage_{};
204 template <
typename T,
typename U>
205 friend const T*
_INNER __any_cast_aux_dispatch_impl(
const any* value,
_INNER any_cast_true_tag)
noexcept;
214 template <
typename T,
typename... Args,
typename Manager = manage_t<T>>
215 void try_emplace(Args&&... args) {
218 manage_ = &Manager::manage;
230 template <
typename T,
typename U,
typename... Args,
typename Manager = manage_t<T>>
231 void try_emplace(std::initializer_list<U> ilist, Args&&... args) {
234 manage_ = &Manager::manage;
255 any& operator =(
const any& other) {
264 any(
any&& other)
noexcept;
271 any& operator =(
any&& other)
noexcept;
278 template <
typename T,
typename VT = decay_t<T>,
typename Manager = manage_t<VT>,
279 enable_if_t<is_copy_constructible_v<VT> && !is_same_v<inplace_construct_tag, VT> && !is_same_v<VT, any>,
int> = 0>
280 explicit any(T&& value) : manage_(&Manager::manage) {
290 template <
typename T,
typename VT = decay_t<T>,
291 enable_if_t<!is_same_v<VT, any> && is_copy_constructible_v<VT>,
int> = 0>
292 any& operator =(T&& value) {
303 template <
typename T,
typename... Args,
typename VT =
decay_t<T>,
typename Manager = manage_t<VT>,
317 template <
typename T,
typename U,
typename... Args,
typename VT =
decay_t<T>,
typename Manager = manage_t<VT>,
337 template <
typename T,
typename... Args,
typename DT =
decay_t<T>,
339 const DT& emplace(Args&&... args) {
341 return *manage_t<DT>::access(storage_);
353 template <
typename T,
typename U,
typename... Args,
typename DT =
decay_t<T>,
355 const DT& emplace(std::initializer_list<U> ilist, Args&&... args) {
357 return *manage_t<DT>::access(storage_);
363 void reset()
noexcept {
365 manage_(DESTROY,
this,
nullptr);
374 MSTL_NODISCARD
bool has_value()
const noexcept {
375 return manage_ !=
nullptr;
382 MSTL_NODISCARD
const std::type_info& type()
const noexcept;
398template <
typename T,
typename... Args,
413template <
typename T,
typename U,
typename... Args,
422template <
typename T,
typename U>
423const T* __any_cast_aux_dispatch_impl(
const any* value, any_cast_true_tag)
noexcept {
424 if (value->manage_ == &any::manage_t<U>::manage || value->type() ==
typeid(T))
425 return static_cast<const T*
>(any::manage_t<U>::access(value->storage_));
429template <
typename T,
typename U>
430const T* __any_cast_aux_dispatch_impl(
const any*, any_cast_false_tag)
noexcept {
434template <
typename T,
typename U>
435const T* __any_cast_aux_dispatch(
const any* value)
noexcept {
437 (is_same_v<decay_t<U>, U> || is_copy_constructible_v<U>),
438 any_cast_true_tag, any_cast_false_tag
440 return _INNER __any_cast_aux_dispatch_impl<T, U>(value, tag{});
443template <
typename T, enable_if_t<is_
object_v<T>,
int> = 0>
444const T* __any_cast_aux(
const any* value)
noexcept {
446 return __any_cast_aux_dispatch<T, remove_cv_t<T>>(value);
450template <
typename T, enable_if_t<!is_
object_v<T>,
int> = 0>
451const T* __any_cast_aux(
const any*)
noexcept {
466 return _INNER __any_cast_aux<T>(value);
477 return const_cast<T*
>(
any_cast<T>(
const_cast<const any*
>(value)));
495 "type T must be valid to cast from any.");
499 return static_cast<T
>(*ptr);
506void any::internal_manage<T>::manage(
const any_operation op,
const any* value, ArgT* arg) {
507 auto ptr =
reinterpret_cast<const T*
>(&value->storage_.buffer_);
510 arg->obj_ptr_ =
const_cast<T*
>(ptr);
513 case GET_TYPE_INFO: {
514 arg->type_ptr_ = &
typeid(T);
518 ::new(&arg->any_ptr_->storage_.buffer_) T(*ptr);
519 arg->any_ptr_->manage_ = value->manage_;
527 ::new(&arg->any_ptr_->storage_.buffer_) T(
_MSTL move(*const_cast<T*>(ptr)));
529 arg->any_ptr_->manage_ = value->manage_;
530 const_cast<
any*>(value)->manage_ =
nullptr;
540void any::external_manage<T>::manage(
const any_operation op,
const any* value, ArgT* arg) {
541 auto ptr =
static_cast<const T*
>(value->storage_.ptr_);
544 arg->obj_ptr_ =
const_cast<T*
>(ptr);
547 case GET_TYPE_INFO: {
548 arg->type_ptr_ = &
typeid(T);
552 arg->any_ptr_->storage_.ptr_ = ::new T(*ptr);
553 arg->any_ptr_->manage_ = value->manage_;
561 arg->any_ptr_->storage_.ptr_ = value->storage_.ptr_;
562 arg->any_ptr_->manage_ = value->manage_;
563 const_cast<any*
>(value)->manage_ =
nullptr;
typename aligned_storage< Len, Align >::type aligned_storage_t
aligned_storage的便捷别名
const T * any_cast(const any *value) noexcept
从any对象转换常量值
any make_any(Args &&... args)
创建any对象
MSTL_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
#define MSTL_ERROR_BUILD_DERIVED_CLASS(THIS, BASE, INFO)
构建可派生的异常类宏
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_INNER__
结束inner命名空间
#define _INNER
inner命名空间前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
#define MSTL_BEGIN_INNER__
开始inner命名空间
typename remove_cvref< T >::type remove_cvref_t
remove_cvref的便捷别名
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素
void swap()=delete
删除无参数的swap重载
typename decay< T >::type decay_t
decay的便捷别名
typename conditional< Test, T1, T2 >::type conditional_t
conditional的便捷别名
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名