YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
any.h
浏览该文件的文档.
1 /*
2  © 2011-2014 FrankHB.
3 
4  This file is part of the YSLib project, and may only be used,
5  modified, and distributed under the terms of the YSLib project
6  license, LICENSE.TXT. By continuing to use, modify, or distribute
7  this file you indicate that you have read the license and
8  understand and accept it fully.
9 */
10 
28 #ifndef YB_INC_ystdex_any_h_
29 #define YB_INC_ystdex_any_h_ 1
30 
31 #include "utility.hpp"
32 #include <memory> // for std::addressof, std::unique_ptr;
33 #include <typeinfo> // for typeid, std::bad_cast;
34 
35 namespace ystdex
36 {
37 
38 /*
39 \brief 用于表示任意不需要复制存储的非聚集 POD 类型。
40 \note POD 和聚集类型的含义参考 ISO C++11 。
41 \since build 352
42 \todo 确定整数和枚举类型的必要性。
43 */
45 {
46  void* object_ptr;
47  const void* const_object_ptr;
48  volatile void* volatile_object_ptr;
49  const volatile void* const_volatile_object_ptr;
50  void(*function_ptr)();
53 };
54 
55 
56 /*
57 \brief 任意 POD 类型存储。
58 \note POD 的含义参考 ISO C++11 。
59 \since build 351
60 */
61 template<typename _tPOD = aligned_storage_t<sizeof(void*)>>
63 {
64  static_assert(is_pod<_tPOD>::value, "Non-POD underlying type found.");
65 
66  using underlying = _tPOD;
67 
69  byte data[sizeof(underlying)];
70 
72 
73  pod_storage() = default;
75  template<typename _type,
77  pod_storage(_type&& x)
78  {
80  }
81 
82  /*
83  \note 为避免类型错误,需要确定类型时应使用显式使用 access 指定类型赋值。
84  \since build 454
85  */
86  template<typename _type,
89  operator=(_type&& x)
90  {
91  access<remove_reference_t<_type>>() = yforward(x);
92  return *this;
93  }
95 
96  YB_PURE void*
98  {
99  return &data[0];
100  }
101  yconstfn YB_PURE const void*
102  access() const
103  {
104  return &data[0];
105  }
106  template<typename _type>
107  YB_PURE _type&
109  {
110  return *static_cast<_type*>(access());
111  }
112  template<typename _type>
113  yconstfn YB_PURE const _type&
114  access() const
115  {
116  return *static_cast<const _type*>(access());
117  }
118 };
119 
120 
127 class void_ref
128 {
129 private:
130  const volatile void* ptr;
131 
132 public:
133  template<typename _type>
134  yconstfn
135  void_ref(_type& obj)
136  : ptr(&obj)
137  {}
138 
139  template<typename _type>
141  operator _type&() const
142  {
143  return *static_cast<_type*>(&*this);
144  }
145 
146  YB_PURE void*
147  operator&() const volatile
148  {
149  return const_cast<void*>(ptr);
150  }
151 };
152 
153 
155 namespace any_ops
156 {
157 
162 class YB_API holder : public cloneable
163 {
164 public:
165  virtual
167  {}
168 
170  virtual void*
171  get() const = 0;
172 
173  virtual holder*
174  clone() const override = 0;
175 
177  virtual const std::type_info&
178  type() const ynothrow = 0;
179 };
180 
181 
187 template<typename _type>
188 class value_holder : public holder
189 {
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.");
193 
194 public:
196  using value_type = _type;
197 
198 protected:
200  mutable _type held;
201 
202 public:
203  value_holder(const _type& value)
204  : held(value)
205  {}
211  value_holder(_type&& value) ynoexcept(ynoexcept(_type(std::move(value))))
212  : held(std::move(value))
213  {}
214 
215  value_holder*
216  clone() const override
217  {
218  return new value_holder(held);
219  }
220 
222  void*
223  get() const override
224  {
225  return std::addressof(held);
226  }
227 
229  const std::type_info&
230  type() const ynothrow override
231  {
232  return typeid(_type);
233  }
234 };
235 
236 
243 template<typename _type>
244 class pointer_holder : public holder
245 {
246  static_assert(is_object<_type>::value, "Invalid type found.");
247 
248 public:
250  using value_type = _type;
251 
252 protected:
254  _type* p_held;
255 
256 public:
257  pointer_holder(_type* value)
258  : p_held(value)
259  {}
261 
263  : pointer_holder(h.p_held ? new _type(*h.p_held) : nullptr)
264  {}
266  : p_held(h.p_held)
267  {
268  h.p_held = {};
269  }
271  virtual
274  {
275  delete p_held;
276  }
277 
279  clone() const override
280  {
281  return new pointer_holder(*this);
282  }
283 
285  void*
286  get() const override
287  {
288  return p_held;
289  }
290 
292  const std::type_info&
293  type() const ynothrow override
294  {
295  return p_held ? typeid(_type) : typeid(void);
296  }
297 };
298 
299 
301 using op_code = std::uint32_t;
302 
305 {
316 };
317 
318 
320 
322 using any_manager = void(*)(any_storage&, const any_storage&, op_code);
323 
324 
327 {};
329 
330 
335 template<typename _type, bool _bStoredLocally = sizeof(_type)
336  <= sizeof(any_storage) && yalignof(_type) <= yalignof(any_storage)
337  && yalignof(any_storage) % yalignof(_type) == 0>
338 class value_handler
339 {
340 public:
342 
343  using value_type = _type;
344  using local_storage = integral_constant<bool, _bStoredLocally>;
345 
346  static value_type*
347  get_pointer(const any_storage& s)
348  {
349  return const_cast<value_type*>(_bStoredLocally ? std::addressof(
350  s.access<value_type>()) : s.access<const value_type*>());
351  }
353 
355  static value_type&
356  get_reference(const any_storage& s)
357  {
358  yassume(get_pointer(s));
359  return *get_pointer(s);
360  }
361 
363  static void
364  copy(any_storage& d, const any_storage& s, true_type)
365  {
366  new(d.access()) value_type(s.access<value_type>());
367  }
369  static void
370  copy(any_storage& d, const any_storage& s, false_type)
371  {
372  d = new value_type(*s.access<value_type*>());
373  }
374 
376 
377  static void
378  uninit(any_storage& d, true_type)
379  {
380  d.access<value_type>().~value_type();
381  }
382  static void
383  uninit(any_storage& d, false_type)
384  {
385  delete d.access<value_type*>();
386  }
387 
388  template<typename _tValue>
389  static void
390  init(any_storage& d, _tValue&& x)
391  {
392  init_impl(d, yforward(x), local_storage());
393  }
395 
396 private:
398 
399  template<typename _tValue>
400  static void
401  init_impl(any_storage& d, _tValue&& x, true_type)
402  {
403  new(d.access()) value_type(yforward(x));
404  }
405  template<typename _tValue>
406  static void
407  init_impl(any_storage& d, _tValue&& x, false_type)
408  {
409  d = new value_type(yforward(x));
410  }
412 
413 public:
415  static void
416  manage(any_storage& d, const any_storage& s, op_code op)
417  {
418  switch(op)
419  {
420  case get_type:
421  d = &typeid(value_type);
422  break;
423  case get_ptr:
424  d = get_pointer(s);
425  break;
426  case clone:
427  copy(d, s, local_storage());
428  break;
429  case destroy:
430  uninit(d, local_storage());
431  break;
432  case get_holder_type:
433  d = &typeid(void);
434  break;
435  case get_holder_ptr:
436  d = static_cast<holder*>(nullptr);
437  }
438  }
439 };
440 
441 
446 template<typename _type>
447 class ref_handler : public value_handler<_type*>
448 {
449 public:
450  using value_type = _type;
451  using base = value_handler<value_type*>;
452 
454  static value_type*
455  get_pointer(const any_storage& s)
456  {
457  return base::get_reference(s);
458  }
459 
461  static value_type&
462  get_reference(const any_storage& s)
463  {
464  yassume(get_pointer(s));
465  return *get_pointer(s);
466  }
467 
468  static void
469  init(any_storage& d, std::reference_wrapper<value_type> x)
470  {
471  base::init(d, std::addressof(x.get()));
472  }
473 
474  static void
475  manage(any_storage& d, const any_storage& s, op_code op)
476  {
477  switch(op)
478  {
479  case get_type:
480  d = &typeid(value_type);
481  break;
482  case get_ptr:
483  d = get_pointer(s);
484  break;
485  default:
486  base::manage(d, s, op);
487  }
488  }
489 };
490 
491 
496 template<typename _tHolder>
497 class holder_handler : public value_handler<_tHolder>
498 {
499  static_assert(is_convertible<_tHolder&, holder&>::value,
500  "Invalid holder type found.");
501 
502 public:
503  using value_type = typename _tHolder::value_type;
504  using base = value_handler<_tHolder>;
505  using local_storage = typename base::local_storage;
506 
507  static value_type*
508  get_pointer(const any_storage& s)
509  {
510  return static_cast<value_type*>(base::get_pointer(s)->_tHolder::get());
511  }
512 
513 private:
515  static void
516  init(any_storage& d, std::unique_ptr<_tHolder> p, true_type)
517  {
518  new(d.access()) _tHolder(std::move(*p));
519  }
521  static void
522  init(any_storage& d, std::unique_ptr<_tHolder> p, false_type)
523  {
524  d = p.release();
525  }
526 
527 public:
529  static void
530  init(any_storage& d, std::unique_ptr<_tHolder> p)
531  {
532  init(d, std::move(p), local_storage());
533  }
534  static void
535  init(any_storage& d, _tHolder&& x)
536  {
537  base::init(d, std::move(x));
538  }
539  template<typename... _tParams>
540  static void
541  init(any_storage& d, _tParams&&... args)
542  {
543  init(d, _tHolder(yforward(args)...));
544  }
545 
546  static void
547  manage(any_storage& d, const any_storage& s, op_code op)
548  {
549  switch(op)
550  {
551  case get_type:
552  d = &typeid(value_type);
553  break;
554  case get_ptr:
555  d = get_pointer(s);
556  break;
557  case clone:
558  base::copy(d, s, local_storage());
559  break;
560  case destroy:
561  base::uninit(d, local_storage());
562  break;
563  case get_holder_type:
564  d = &typeid(_tHolder);
565  break;
566  case get_holder_ptr:
567  d = static_cast<holder*>(base::get_pointer(s));
568  }
569  }
570 };
571 
572 } // namespace any_ops;
573 
574 
584 class YB_API any
585 {
586 protected:
588 
592 
593 public:
595  yconstfn
597  : storage(), manager()
598  {}
600  template<typename _type, yimpl(typename = exclude_self_ctor_t<any, _type>)>
601  any(_type&& x)
602  : manager(any_ops::value_handler<remove_reference_t<_type>>::manage)
603  {
604  any_ops::value_handler<typename remove_rcv<_type>::type>::init(storage,
605  yforward(x));
606  }
608 
609  template<typename _type>
610  any(std::reference_wrapper<_type> x)
611  : manager(any_ops::ref_handler<_type>::manage)
612  {
613  any_ops::ref_handler<_type>::init(storage, x);
614  }
619  template<typename _tHolder>
620  any(any_ops::holder_tag, std::unique_ptr<_tHolder> p)
621  : manager(any_ops::holder_handler<_tHolder>::manage)
622  {
623  any_ops::holder_handler<_tHolder>::init(storage, std::move(p));
624  }
625  template<typename _type>
627  : manager(any_ops::holder_handler<any_ops::value_holder<typename
628  remove_rcv<_type>::type>>::manage)
629  {
630  any_ops::holder_handler<any_ops::value_holder<
631  remove_cv_t<_type>>>::init(storage, yforward(x));
632  }
634  any(const any&);
636  : any()
637  {
638  a.swap(*this);
639  }
641  ~any();
642 
643  template<typename _type>
644  any&
645  operator=(const _type& x)
646  {
647  any(x).swap(*this);
648  return *this;
649  }
654  any&
655  operator=(const any& a)
656  {
657  any(a).swap(*this);
658  return *this;
659  }
664  any&
666  {
667  any(std::move(a)).swap(*this);
668  return *this;
669  }
670 
671  bool
673  {
674  return empty();
675  }
676 
677  explicit
678  operator bool() const ynothrow
679  {
680  return !empty();
681  }
682 
683  bool
684  empty() const ynothrow
685  {
686  return !manager;
687  }
688 
690  void*
691  get() const ynothrow;
692 
694  get_holder() const;
695 
696  void
697  clear() ynothrow;
698 
699  void
700  swap(any& a) ynothrow;
701 
703 
704  template<typename _type>
705  _type*
706  target() ynothrow
707  {
708  return type() == typeid(_type) ? static_cast<_type*>(get()) : nullptr;
709  }
710  template<typename _type>
711  const _type*
712  target() const ynothrow
713  {
714  return type() == typeid(_type)
715  ? static_cast<const _type*>(get()) : nullptr;
716  }
718 
720  const std::type_info&
721  type() const ynothrow;
722 };
723 
728 inline void
730 {
731  x.swap(y);
732 }
733 
734 
741 class bad_any_cast : public std::bad_cast
742 {
743 private:
745  const char* from_name;
747  const char* to_name;
748 
749 public:
751 
753  : std::bad_cast(),
754  from_name("unknown"), to_name("unknown")
755  {};
756  bad_any_cast(const std::type_info& from_type, const std::type_info& to_type)
757  : std::bad_cast(),
758  from_name(from_type.name()), to_name(to_type.name())
759  {}
760 
761  const char*
762  from() const ynothrow
763  {
764  return from_name;
765  }
766 
767  const char*
768  to() const ynothrow
769  {
770  return to_name;
771  }
773 
774  virtual const char*
775  what() const ynothrow override
776  {
777  return "Failed conversion: any_cast.";
778  }
779 };
780 
781 
792 
793 template<typename _tPointer>
794 inline _tPointer
796 {
797  return p ? p->target<remove_pointer_t<_tPointer>>() : nullptr;
798 }
799 template<typename _tPointer>
800 inline _tPointer
801 any_cast(const any* p) ynothrow
802 {
803  return p ? p->target<remove_pointer_t<_tPointer>>() : nullptr;
804 }
806 
811 template<typename _tValue>
812 inline _tValue
814 {
815  const auto tmp(any_cast<remove_reference_t<_tValue>*>(&x));
816 
817  if(!tmp)
818  throw bad_any_cast(x.type(), typeid(_tValue));
819  return static_cast<_tValue>(*tmp);
820 }
821 template<typename _tValue>
822 _tValue
823 any_cast(const any& x)
824 {
825  const auto tmp(any_cast<remove_reference_t<_tValue>*>(&x));
826 
827  if(!tmp)
828  throw bad_any_cast(x.type(), typeid(_tValue));
829  return static_cast<_tValue>(*tmp);
830 }
832 
833 
842 template<typename _type>
843 inline _type*
845 {
846  yconstraint(p);
847  return static_cast<_type*>(p->get());
848 }
849 
850 template<typename _type>
851 inline const _type*
852 unsafe_any_cast(const any* p)
853 {
854  yconstraint(p);
855  return static_cast<const _type*>(p->get());
856 }
858 
859 
866 {
867  template<typename... _tParams>
868  inline pseudo_output&
869  operator=(_tParams&&...)
870  {
871  return *this;
872  }
873 };
874 
875 } // namespace ystdex;
876 
877 #endif
878 
实用设施。
基于类型擦除的动态泛型对象。
Definition: any.h:584
_tPOD underlying
Definition: any.h:66
动态泛型转换失败异常。
Definition: any.h:741
pointer_holder(pointer_holder &&h)
Definition: any.h:265
const char * to() const
Definition: any.h:768
pod_storage< non_aggregate_pod > any_storage
Definition: any.h:321
const void * const_object_ptr
Definition: any.h:47
pod_storage & operator=(_type &&x)
Definition: any.h:89
任意对象引用类型。
Definition: any.h:127
value_holder(const _type &value)
Definition: any.h:203
typename remove_reference< _type >::type remove_reference_t
Definition: type_op.hpp:234
值类型动态泛型持有者。
Definition: any.h:188
yconstfn const string _tParams && args
Definition: Loader.h:111
virtual const char * what() const override
Definition: any.h:775
underlying object
Definition: any.h:68
any(std::reference_wrapper< _type > x)
Definition: any.h:610
bad_any_cast(const std::type_info &from_type, const std::type_info &to_type)
Definition: any.h:756
void * access()
Definition: any.h:97
unsigned char byte
字节类型。
Definition: ydef.h:555
const class ystdex::nullptr_t nullptr
const volatile void * const_volatile_object_ptr
Definition: any.h:49
pod_storage(_type &&x)
Definition: any.h:77
可动态复制的抽象基类。
Definition: utility.hpp:142
抽象动态泛型持有者接口。
Definition: any.h:162
指针类型动态泛型持有者。
Definition: any.h:244
intnon_aggregate_pod::* member_object_pointer
Definition: any.h:51
void(* function_ptr)()
Definition: any.h:50
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
_tPointer any_cast(any *p)
动态泛型转换。
Definition: any.h:795
typename remove_pointer< _type >::type remove_pointer_t
Definition: type_op.hpp:255
#define yimpl(...)
实现标签。
Definition: ydef.h:177
any(any &&a)
Definition: any.h:635
const char * to_name
Definition: any.h:747
_type & access()
Definition: any.h:108
void swap(any &x, any &y)
交换对象。
Definition: any.h:729
void_ref(_type &obj)
Definition: any.h:135
value_holder(_type &&value)
转移构造。
Definition: any.h:211
pointer_holder * clone() const override
Definition: any.h:279
const _type * target() const
Definition: any.h:712
value_holder * clone() const override
Definition: any.h:216
void * operator&() const volatile
Definition: any.h:147
#define yassume
假定:环境语义。
Definition: cassert.h:58
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
_type * unsafe_any_cast(any *p)
非安全动态泛型转换。
Definition: any.h:844
const std::type_info & type() const
Definition: any.cpp:92
yconstfn const string & name
Definition: Loader.h:110
any & operator=(any &&a)
转移赋值:使用复制和交换。
Definition: any.h:665
#define yalignof(_type)
指定特定类型的对齐。
Definition: ydef.h:442
any_ops::any_storage storage
Definition: any.h:589
typename remove_cv< _type >::type remove_cv_t
Definition: type_op.hpp:222
any(any_ops::holder_tag, std::unique_ptr< _tHolder > p)
构造:使用指定持有者。
Definition: any.h:620
#define YB_API
YBase 应用程序编程接口:用于向库文件约定链接。
Definition: ydef.h:391
#define yconstraint
约束:接口语义。
Definition: cassert.h:47
void * get() const
Definition: any.cpp:49
const std::type_info & type() const override
Definition: any.h:230
volatile void * volatile_object_ptr
Definition: any.h:48
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:331
_tValue any_cast(any &x)
Definition: any.h:813
const void * access() const
Definition: any.h:102
void swap(any &a)
Definition: any.cpp:85
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
enable_if_t<!is_same< _tClass &, remove_rcv_t< _tParam > & >::value, _type > exclude_self_ctor_t
移除选择类类型的特定重载避免构造模板和复制/转移构造函数冲突。
Definition: type_op.hpp:766
byte data[sizeof(underlying)]
Definition: any.h:69
bool operator!() const
Definition: any.h:672
std::uint32_t op_code
Definition: any.h:301
const std::type_info & type() const override
Definition: any.h:293
const char * from_name
Definition: any.h:745
any & operator=(const _type &x)
Definition: any.h:645
any & operator=(const any &a)
复制赋值:使用复制和交换。
Definition: any.h:655
pseudo_output & operator=(_tParams &&...)
Definition: any.h:869
bool empty() const
Definition: any.h:684
any(_type &&x, any_ops::holder_tag)
Definition: any.h:626
any_ops::any_manager manager
Definition: any.h:590
void(non_aggregate_pod::* member_function_pointer)()
Definition: any.h:52
#define ynoexcept(...)
YSLib 无异常抛出保证:指定特定的异常规范。
Definition: ydef.h:526
const _type & access() const
Definition: any.h:114
pointer_holder(const pointer_holder &h)
Definition: any.h:262
pointer_holder(_type *value)
Definition: any.h:257
使用持有者标记。
Definition: any.h:326
void(*)(any_storage &, const any_storage &, op_code) any_manager
Definition: any.h:322
any()
Definition: any.h:596
any(_type &&x)
Definition: any.h:601
移除可能被 cv-qualifier 修饰的引用类型。
Definition: type_op.hpp:657
const volatile void * ptr
Definition: any.h:130
伪输出对象。
Definition: any.h:865
virtual ~holder()
Definition: any.h:166
const char * from() const
Definition: any.h:762