28 #ifndef YB_INC_ystdex_any_h_
29 #define YB_INC_ystdex_any_h_ 1
61 template<
typename _tPOD = aligned_storage_t<sizeof(
void*)>>
64 static_assert(is_pod<_tPOD>::value,
"Non-POD underlying type found.");
75 template<
typename _type,
86 template<
typename _type,
91 access<remove_reference_t<_type>>() =
yforward(x);
106 template<
typename _type>
110 return *
static_cast<_type*
>(
access());
112 template<
typename _type>
116 return *
static_cast<const _type*
>(
access());
133 template<
typename _type>
139 template<
typename _type>
141 operator _type&()
const
143 return *
static_cast<_type*
>(&*
this);
149 return const_cast<void*
>(
ptr);
174 clone()
const override = 0;
177 virtual const std::type_info&
187 template<typename _type>
190 static_assert(is_object<_type>::value,
"Non-object type found.");
191 static_assert(!(is_const<_type>::value || is_volatile<_type>::value),
192 "Cv-qualified type found.");
212 : held(std::move(value))
225 return std::addressof(held);
229 const std::type_info&
232 return typeid(_type);
243 template<
typename _type>
246 static_assert(is_object<_type>::value,
"Invalid type found.");
292 const std::type_info&
295 return p_held ?
typeid(_type) :
typeid(
void);
335 template<
typename _type,
bool _bStoredLocally =
sizeof(_type)
343 using value_type = _type;
344 using local_storage = integral_constant<bool, _bStoredLocally>;
349 return const_cast<value_type*
>(_bStoredLocally ? std::addressof(
350 s.
access<value_type>()) : s.access<const value_type*>());
359 return *get_pointer(s);
366 new(d.access()) value_type(s.access<value_type>());
372 d =
new value_type(*s.access<value_type*>());
380 d.access<value_type>().~value_type();
385 delete d.access<value_type*>();
388 template<
typename _tValue>
392 init_impl(d,
yforward(x), local_storage());
399 template<
typename _tValue>
403 new(d.access()) value_type(
yforward(x));
405 template<
typename _tValue>
421 d = &
typeid(value_type);
427 copy(d, s, local_storage());
430 uninit(d, local_storage());
436 d =
static_cast<holder*
>(
nullptr);
446 template<
typename _type>
447 class ref_handler :
public value_handler<_type*>
450 using value_type = _type;
451 using base = value_handler<value_type*>;
457 return base::get_reference(s);
465 return *get_pointer(s);
469 init(
any_storage& d, std::reference_wrapper<value_type> x)
471 base::init(d, std::addressof(x.get()));
480 d = &
typeid(value_type);
486 base::manage(d, s, op);
496 template<
typename _tHolder>
497 class holder_handler :
public value_handler<_tHolder>
499 static_assert(is_convertible<_tHolder&, holder&>::value,
500 "Invalid holder type found.");
503 using value_type =
typename _tHolder::value_type;
504 using base = value_handler<_tHolder>;
505 using local_storage =
typename base::local_storage;
510 return static_cast<value_type*
>(base::get_pointer(s)->_tHolder::get());
516 init(
any_storage& d, std::unique_ptr<_tHolder> p, true_type)
518 new(d.access()) _tHolder(std::move(*p));
522 init(
any_storage& d, std::unique_ptr<_tHolder> p, false_type)
532 init(d, std::move(p), local_storage());
537 base::init(d, std::move(x));
539 template<
typename... _tParams>
552 d = &
typeid(value_type);
558 base::copy(d, s, local_storage());
561 base::uninit(d, local_storage());
564 d = &
typeid(_tHolder);
567 d =
static_cast<holder*
>(base::get_pointer(s));
597 : storage(), manager()
600 template<
typename _type, yimpl(
typename = exclude_self_ctor_t<any, _type>)>
604 any_ops::value_handler<typename remove_rcv<_type>::type>::init(storage,
609 template<
typename _type>
610 any(std::reference_wrapper<_type> x)
611 : manager(any_ops::ref_handler<_type>::manage)
613 any_ops::ref_handler<_type>::init(storage, x);
619 template<
typename _tHolder>
621 : manager(any_ops::holder_handler<_tHolder>::manage)
623 any_ops::holder_handler<_tHolder>::init(storage, std::move(p));
625 template<
typename _type>
627 : manager(any_ops::holder_handler<any_ops::value_holder<typename
643 template<
typename _type>
704 template<typename _type>
708 return type() ==
typeid(_type) ? static_cast<_type*>(
get()) :
nullptr;
710 template<
typename _type>
714 return type() ==
typeid(_type)
715 ? static_cast<const _type*>(
get()) :
nullptr;
720 const std::type_info&
754 from_name(
"unknown"), to_name(
"unknown")
756 bad_any_cast(
const std::type_info& from_type,
const std::type_info& to_type)
758 from_name(from_type.
name()), to_name(to_type.
name())
777 return "Failed conversion: any_cast.";
793 template<
typename _tPo
inter>
799 template<
typename _tPo
inter>
811 template<
typename _tValue>
819 return static_cast<_tValue
>(*tmp);
821 template<
typename _tValue>
823 any_cast(
const any& x)
829 return static_cast<_tValue
>(*tmp);
842 template<
typename _type>
847 return static_cast<_type*
>(p->
get());
850 template<
typename _type>
852 unsafe_any_cast(
const any* p)
855 return static_cast<const _type*
>(p->
get());
867 template<
typename... _tParams>
pointer_holder(pointer_holder &&h)
pod_storage< non_aggregate_pod > any_storage
const void * const_object_ptr
pod_storage & operator=(_type &&x)
value_holder(const _type &value)
typename remove_reference< _type >::type remove_reference_t
yconstfn const string _tParams && args
virtual const char * what() const override
any(std::reference_wrapper< _type > x)
bad_any_cast(const std::type_info &from_type, const std::type_info &to_type)
const class ystdex::nullptr_t nullptr
const volatile void * const_volatile_object_ptr
virtual ~pointer_holder()
intnon_aggregate_pod::* member_object_pointer
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
_tPointer any_cast(any *p)
动态泛型转换。
typename remove_pointer< _type >::type remove_pointer_t
void swap(any &x, any &y)
交换对象。
value_holder(_type &&value)
转移构造。
pointer_holder * clone() const override
const _type * target() const
value_holder * clone() const override
void * operator&() const volatile
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
_type * unsafe_any_cast(any *p)
非安全动态泛型转换。
const std::type_info & type() const
yconstfn const string & name
any & operator=(any &&a)
转移赋值:使用复制和交换。
#define yalignof(_type)
指定特定类型的对齐。
any_ops::any_storage storage
typename remove_cv< _type >::type remove_cv_t
any(any_ops::holder_tag, std::unique_ptr< _tHolder > p)
构造:使用指定持有者。
#define YB_API
YBase 应用程序编程接口:用于向库文件约定链接。
#define yconstraint
约束:接口语义。
const std::type_info & type() const override
volatile void * volatile_object_ptr
#define YB_PURE
指示函数或函数模板实例为纯函数。
const void * access() const
#define yconstfn
指定编译时常量函数。
enable_if_t<!is_same< _tClass &, remove_rcv_t< _tParam > & >::value, _type > exclude_self_ctor_t
移除选择类类型的特定重载避免构造模板和复制/转移构造函数冲突。
byte data[sizeof(underlying)]
const std::type_info & type() const override
any & operator=(const _type &x)
any & operator=(const any &a)
复制赋值:使用复制和交换。
pseudo_output & operator=(_tParams &&...)
any(_type &&x, any_ops::holder_tag)
any_ops::any_manager manager
void(non_aggregate_pod::* member_function_pointer)()
#define ynoexcept(...)
YSLib 无异常抛出保证:指定特定的异常规范。
const _type & access() const
pointer_holder(const pointer_holder &h)
pointer_holder(_type *value)
void(*)(any_storage &, const any_storage &, op_code) any_manager
移除可能被 cv-qualifier 修饰的引用类型。
const volatile void * ptr
const char * from() const