YSTest: any.h 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

any.h
浏览该文件的文档.
1 /*
2 © 2011-2017 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
31 #ifndef YB_INC_ystdex_any_h_
32 #define YB_INC_ystdex_any_h_ 1
33
34 #include "typeinfo.h" // for "typeinfo.h", type_info, exclude_self_t,
35 // ystdex::type_id, is_nothrow_move_constructible, and_, bool_, enable_if_t,
36 // remove_reference_t, cond_t, std::bad_cast, decay_t, _t, yconstraint;
37 #include "utility.hpp" // "utility.hpp", for boxed_value, std::addressof,
38 // std::unique_ptr, ystdex::clone_monomorphic_ptr, standard_layout_storage,
39 // aligned_storage_t, is_aligned_storable, ystdex::pvoid, default_init_t;
40 #include "exception.h" // for throw_invalid_construction;
41 #include "ref.hpp" // for is_reference_wrapper, unwrap_reference_t;
42 #include <initializer_list> // for std::initializer_list;
43
44 namespace ystdex
45 {
46
53 namespace any_ops
54 {
55
57 template<typename>
58 struct with_handler_t
59 {};
60
61
66 class YB_API holder
67 {
68 public:
70
71 holder() = default;
72 holder(const holder&) = default;
74 virtual
75 ~holder();
77
79 virtual void*
80 get() const = 0;
81
83 virtual const type_info&
84 type() const ynothrow = 0;
85 };
86
92 inline bool
93 hold_same(const holder& x, const holder& y)
94 {
95 return x.get() == y.get();
96 }
97
98
104 template<typename _type>
105 class value_holder : protected boxed_value<_type>, public holder
106 {
107 static_assert(is_object<_type>(), "Non-object type found.");
108 static_assert(!is_cv<_type>(), "Cv-qualified type found.");
109
110 public:
112 using value_type = _type;
113
115
116 value_holder() = default;
117 template<typename _tParam,
118 yimpl(typename = exclude_self_t<value_holder, _tParam>)>
119 value_holder(_tParam&& arg)
120 ynoexcept(is_nothrow_constructible<_type, _tParam&&>())
121 : boxed_value<_type>(yforward(arg))
122 {}
123 using boxed_value<_type>::boxed_value;
125
127 value_holder(const value_holder&) = default;
128 value_holder(value_holder&&) = default;
129
130 value_holder&
131 operator=(const value_holder&) = default;
132 value_holder&
133 operator=(value_holder&&) = default;
135
137 void*
138 get() const override
139 {
140 return std::addressof(this->value);
141 }
142
144 const type_info&
145 type() const ynothrow override
146 {
147 return ystdex::type_id<_type>();
148 }
149 };
150
151
160 template<typename _type, class _tPointer = std::unique_ptr<_type>>
161 class pointer_holder : public holder
162 {
164 static_assert(is_object<_type>(), "Invalid type found.");
165
166 public:
168 using value_type = _type;
169 using holder_pointer = _tPointer;
170 using pointer = typename holder_pointer::pointer;
171
172 protected:
173 holder_pointer p_held;
174
175 public:
177 pointer_holder(pointer value)
178 : p_held(value)
179 {}
181
182 pointer_holder(const pointer_holder& h)
183 : pointer_holder(ystdex::clone_monomorphic_ptr(h.p_held))
184 {}
185 pointer_holder(pointer_holder&&) = default;
187
188 pointer_holder&
189 operator=(const pointer_holder&) = default;
190 pointer_holder&
191 operator=(pointer_holder&&) = default;
192
194 void*
195 get() const override
196 {
197 return p_held.get();
198 }
199
201 const type_info&
202 type() const ynothrow override
203 {
204 return p_held ? ystdex::type_id<_type>() : ystdex::type_id<void>();
205 }
206 };
207
208
218 using op_code = std::uint32_t;
219
224 enum base_op : op_code
225 {
227 no_op,
229 get_type,
231 get_ptr,
233 clone,
235 destroy,
237 get_holder_type,
239 get_holder_ptr,
241 end_base_op
242 };
243
244
246 using any_storage = standard_layout_storage<aligned_storage_t<sizeof(void*),
247 yalignof(void*)>>;
249 using any_manager = void(*)(any_storage&, any_storage&, op_code);
250
255 template<class _tHandler, typename... _tParams>
256 any_manager
257 construct(any_storage& storage, _tParams&&... args)
258 {
259 _tHandler::init(storage, yforward(args)...);
260 return _tHandler::manage;
261 }
262
263
268 yconstexpr const struct use_holder_t{} use_holder{};
269
270
275 template<typename _type,
276 bool _bStoredLocally = and_<is_nothrow_move_constructible<_type>,
277 is_aligned_storable<any_storage, _type>>::value>
278 class value_handler
279 {
280 public:
282
283 using value_type = _type;
284 using local_storage = bool_<_bStoredLocally>;
286
288
289 static void
290 copy(any_storage& d, const any_storage& s)
291 {
292 try_init(is_copy_constructible<value_type>(), local_storage(), d,
293 get_reference(s));
294 }
295
296 static void
297 dispose(any_storage& d) ynothrowv
298 {
299 dispose_impl(local_storage(), d);
300 }
301
302 private:
303 static void
304 dispose_impl(false_, any_storage& d) ynothrowv
305 {
306 delete d.access<value_type*>();
307 }
308 static void
309 dispose_impl(true_, any_storage& d) ynothrowv
310 {
311 d.destroy<value_type>();
312 }
314
315 public:
317
318 static value_type*
319 get_pointer(any_storage& s)
320 {
321 return get_pointer_impl(local_storage(), s);
322 }
323 static const value_type*
324 get_pointer(const any_storage& s)
325 {
326 return get_pointer_impl(local_storage(), s);
327 }
328
329 private:
331
332 static value_type*
333 get_pointer_impl(false_, any_storage& s)
334 {
335 return s.access<value_type*>();
336 }
337 static const value_type*
338 get_pointer_impl(false_, const any_storage& s)
339 {
340 return s.access<const value_type*>();
341 }
342 static value_type*
343 get_pointer_impl(true_, any_storage& s)
344 {
345 return std::addressof(get_reference_impl(true_(), s));
346 }
347 static const value_type*
348 get_pointer_impl(true_, const any_storage& s)
349 {
350 return std::addressof(get_reference_impl(true_(), s));
351 }
353
354 public:
355 static value_type&
356 get_reference(any_storage& s)
357 {
358 return get_reference_impl(local_storage(), s);
359 }
360 static const value_type&
361 get_reference(const any_storage& s)
362 {
363 return get_reference_impl(local_storage(), s);
364 }
365
366 private:
368
369 static value_type&
370 get_reference_impl(false_, any_storage& s)
371 {
372 const auto p(get_pointer_impl(false_(), s));
373
374 yassume(p);
375 return *p;
376 }
377 static const value_type&
378 get_reference_impl(false_, const any_storage& s)
379 {
380 const auto p(get_pointer_impl(false_(), s));
381
382 yassume(p);
383 return *p;
384 }
385 static value_type&
386 get_reference_impl(true_, any_storage& s)
387 {
388 return s.access<value_type>();
389 }
390 static const value_type&
391 get_reference_impl(true_, const any_storage& s)
392 {
393 return s.access<const value_type>();
394 }
396
397
398 public:
400 template<typename... _tParams>
401 static void
402 init(any_storage& d, _tParams&&... args)
403 {
404 init_impl(local_storage(), d, yforward(args)...);
405 }
406
407 private:
409 template<typename... _tParams>
410 static YB_ATTR(always_inline) void
411 init_impl(false_, any_storage& d, _tParams&&... args)
412 {
413 d.construct<value_type*>(new value_type(yforward(args)...));
414 }
416 template<typename... _tParams>
417 static YB_ATTR(always_inline) void
418 init_impl(true_, any_storage& d, _tParams&&... args)
419 {
420 d.construct<value_type>(yforward(args)...);
421 }
422
423 public:
425 static void
426 manage(any_storage& d, any_storage& s, op_code op)
427 {
428 switch(op)
429 {
430 case get_type:
431 d = &ystdex::type_id<value_type>();
432 break;
433 case get_ptr:
434 d = ystdex::pvoid(get_pointer(s));
435 break;
436 case clone:
437 copy(d, s);
438 break;
439 case destroy:
440 dispose(d);
441 break;
442 case get_holder_type:
443 d = &ystdex::type_id<void>();
444 break;
445 case get_holder_ptr:
446 d = static_cast< holder*>(nullptr);
447 }
448 }
449
450 private:
452 template<typename... _tParams>
453 YB_NORETURN static YB_ATTR(always_inline) void
454 try_init(false_, _tParams&&...)
455 {
456 throw_invalid_construction();
457 }
459 template<class _bInPlace, typename... _tParams>
460 static YB_ATTR(always_inline) void
461 try_init(true_, _bInPlace b, any_storage& d, _tParams&&... args)
462 {
463 init_impl(b, d, yforward(args)...);
464 }
465 };
466
467
472 template<typename _type>
473 class ref_handler : public value_handler<_type*>
474 {
475 public:
476 using value_type = _type;
477 using base = value_handler<value_type*>;
478
480 static value_type*
481 get_pointer(any_storage& s)
482 {
483 return base::get_reference(s);
484 }
485
487 static value_type&
488 get_reference(any_storage& s)
489 {
490 yassume(get_pointer(s));
491 return *get_pointer(s);
492 }
493
495 template<typename _tWrapper,
496 yimpl(typename = enable_if_t<is_reference_wrapper<_tWrapper>::value>)>
497 static auto
498 init(any_storage& d, _tWrapper x)
499 -> decltype(base::init(d, std::addressof(x.get())))
500 {
501 base::init(d, std::addressof(x.get()));
502 }
503
505 static void
506 manage(any_storage& d, any_storage& s, op_code op)
507 {
508 switch(op)
509 {
510 case get_type:
511 d = &ystdex::type_id<value_type>();
512 break;
513 case get_ptr:
514 d = ystdex::pvoid(get_pointer(s));
515 break;
516 default:
517 base::manage(d, s, op);
518 }
519 }
520 };
521
522
527 template<typename _tHolder>
528 class holder_handler : public value_handler<_tHolder>
529 {
530 static_assert(is_convertible<_tHolder&, holder&>(),
531 "Invalid holder type found.");
532
533 public:
534 using base = value_handler<_tHolder>;
535
537 static _tHolder*
538 get_holder_pointer(any_storage& s)
539 {
540 return base::get_pointer(s);
541 }
542
543 private:
545 static void
546 init(false_, any_storage& d, std::unique_ptr<_tHolder> p)
547 {
548 d.construct<_tHolder*>(p.release());
549 }
551 static void
552 init(true_, any_storage& d, std::unique_ptr<_tHolder> p)
553 {
554 d.construct<_tHolder>(std::move(*p));
555 }
556
557 public:
559 static void
560 init(any_storage& d, std::unique_ptr<_tHolder> p)
561 {
562 init(typename base::local_storage(), d, std::move(p));
563 }
565 using base::init;
566
568 static void
569 manage(any_storage& d, any_storage& s, op_code op)
570 {
571 switch(op)
572 {
573 case get_type:
574 d = &get_holder_pointer(s)->type();
575 break;
576 case get_ptr:
577 d = ystdex::pvoid(get_holder_pointer(s)->get());
578 break;
579 case get_holder_type:
580 d = &ystdex::type_id<_tHolder>();
581 break;
582 case get_holder_ptr:
583 d = static_cast< holder*>(get_holder_pointer(s));
584 break;
585 default:
586 base::manage(d, s, op);
587 }
588 }
589 };
590
591
597 template<typename _type>
598 struct wrap_handler
599 {
600 using value_type = remove_reference_t<unwrap_reference_t<_type>>;
601 using type = cond_t<is_reference_wrapper<_type>, ref_handler<value_type>,
602 value_handler<value_type>>;
603 };
604
605 } // namespace any_ops;
606
607
615 template<typename _type>
616 using is_any_cast_dest = or_<is_reference<_type>, is_copy_constructible<_type>>;
617
618
628 class YB_API bad_any_cast : public std::bad_cast
629 {
630 private:
632 lref<const type_info> from_ti, to_ti;
633
634 public:
636
637 bad_any_cast()
638 : std::bad_cast(),
639 from_ti(ystdex::type_id<void>()), to_ti(ystdex::type_id<void>())
640 {}
642 bad_any_cast(const type_info& from_, const type_info& to_)
643 : std::bad_cast(),
644 from_ti(from_), to_ti(to_)
645 {}
647 bad_any_cast(const bad_any_cast&) = default;
652 ~bad_any_cast() override;
653
655
656 YB_ATTR_returns_nonnull const char*
657 from() const ynothrow;
658
660 const type_info&
661 from_type() const ynothrow
662 {
663 return from_ti.get();
664 }
665
666 YB_ATTR_returns_nonnull const char*
667 to() const ynothrow;
668
670 const type_info&
671 to_type() const ynothrow
672 {
673 return to_ti.get();
674 }
676
677
678 virtual YB_ATTR_returns_nonnull const char*
679 what() const ynothrow override;
680 };
681
682
684 namespace details
685 {
686
687 struct any_base
688 {
690 mutable any_ops::any_storage storage{};
691 any_ops::any_manager manager{};
692
693 any_base() = default;
694 template<class _tHandler, typename... _tParams>
695 inline
696 any_base(any_ops::with_handler_t<_tHandler>, _tParams&&... args)
697 : manager(any_ops::construct<_tHandler>(storage, yforward(args)...))
698 {}
699
700 protected:
702 any_base(const any_base& a)
703 : manager(a.manager)
704 {}
706 ~any_base() = default;
707
708 public:
709 YB_API any_ops::any_storage&
710 call(any_ops::any_storage&, any_ops::op_code) const;
711
712 YB_API void
713 clear() ynothrowv;
714
715 void
716 copy(const any_base&);
717
718 void
719 destroy() ynothrowv;
720
722 bool
723 has_value() const ynothrow
724 {
725 return manager;
726 }
727
729
730 YB_API void*
731 get() const ynothrowv;
732
733 YB_API any_ops::holder*
734 get_holder() const;
735
737 any_ops::any_storage&
738 get_storage() const
739 {
740 return storage;
741 }
742
743 YB_API void
744 swap(any_base&) ynothrow;
745
746 template<typename _type>
747 _type*
748 target() ynothrowv
749 {
750 return type() == ystdex::type_id<_type>() ? static_cast<_type*>(get())
751 : nullptr;
752 }
753 template<typename _type>
754 const _type*
755 target() const ynothrowv
756 {
757 return type() == ystdex::type_id<_type>()
758 ? static_cast<const _type*>(get()) : nullptr;
759 }
760
761 YB_API const type_info&
762 type() const ynothrowv;
763
765 template<typename _type, typename... _tParams>
766 inline _type
767 unchecked_access(any_ops::op_code op, _tParams&&... args) const
768 {
769 any_ops::any_storage t;
770 const auto gd(t.pun<_type>(yforward(args)...));
771
772 return unchecked_access<_type>(t, op);
773 }
775 template<typename _type>
776 inline _type
777 unchecked_access(default_init_t, any_ops::op_code op) const
778 {
779 any_ops::any_storage t;
780 const auto gd(t.pun_default<_type>());
781
782 return unchecked_access<_type>(t, op);
783 }
785 template<typename _type>
786 inline _type
787 unchecked_access(any_ops::any_storage& t, any_ops::op_code op) const
788 {
789 return call(t, op).access<_type>();
790 }
792 };
793
794
795 template<class _tAny>
796 struct any_emplace
797 {
798 template<typename _type, typename... _tParams>
799 void
800 emplace(_tParams&&... args)
801 {
802 emplace_with_handler<any_ops::value_handler<decay_t<_type>>>(
803 yforward(args)...);
804 }
806 template<typename _type, typename _tOther, typename... _tParams>
807 void
808 emplace(std::initializer_list<_tOther> il, _tParams&&... args)
809 {
810 emplace_with_handler<any_ops::value_handler<decay_t<_type>>>(
811 il, yforward(args)...);
812 }
813 template<typename _tHolder, typename... _tParams>
814 void
815 emplace(any_ops::use_holder_t, _tParams&&... args)
816 {
817 emplace_with_handler<any_ops::holder_handler<decay_t<_tHolder>>>(
818 yforward(args)...);
819 }
820
821 template<typename _tHandler, typename... _tParams>
822 void
823 emplace_with_handler(_tParams&&... args)
824 {
825 auto& a(static_cast<_tAny&>(*this));
826
827 a.reset();
828 a.manager = any_ops::construct<decay_t<_tHandler>>(a.storage,
829 yforward(args)...);
830 }
831 };
832
833 } // namespace details;
834
835
845 class YB_API any : private details::any_base, private details::any_emplace<any>
846 {
848 friend details::any_emplace<any>;
849
850 public:
852 yconstfn
853 any() ynothrow = default;
859 template<typename _type, yimpl(typename = exclude_self_t<any, _type>)>
860 inline
861 any(_type&& x)
862 : any(any_ops::with_handler_t<
863 _t<any_ops::wrap_handler<decay_t<_type>>>>(), yforward(x))
864 {}
866
867 template<typename _type, typename... _tParams>
869 inline
870 any(in_place_type_t<_type>, _tParams&&... args)
871 : any(any_ops::with_handler_t<any_ops::value_handler<_type>>(),
872 yforward(args)...)
873 {}
879 template<typename _tHolder>
880 inline
881 any(any_ops::use_holder_t, std::unique_ptr<_tHolder> p)
882 : any(any_ops::with_handler_t<any_ops::holder_handler<_tHolder>>(),
883 std::move(p))
884 {}
885 template<typename _tHolder>
886 inline
887 any(any_ops::use_holder_t, _tHolder&& h)
888 : any(any_ops::with_handler_t<
889 any_ops::holder_handler<decay_t<_tHolder>>>(), yforward(h))
890 {}
892 template<typename _tHolder, typename... _tParams>
893 inline
894 any(any_ops::use_holder_t, in_place_type_t<_tHolder>, _tParams&&... args)
895 : any(any_ops::with_handler_t<any_ops::holder_handler<_tHolder>>(),
896 yforward(args)...)
897 {}
899 template<typename _type>
900 inline
901 any(_type&& x, any_ops::use_holder_t)
902 : any(any_ops::with_handler_t<any_ops::holder_handler<
903 any_ops::value_holder<decay_t<_type>>>>(), yforward(x))
904 {}
906 template<class _tHandler, typename... _tParams>
907 inline
908 any(any_ops::with_handler_t<_tHandler> t, _tParams&&... args)
909 : any_base(t, yforward(args)...)
910 {}
912 any(const any&);
913 any(any&& a) ynothrow
914 : any()
915 {
916 a.swap(*this);
917 }
919 ~any();
920
922 template<typename _type, yimpl(typename = exclude_self_t<any, _type>)>
923 any&
924 operator=(_type&& x)
925 {
926 any(yforward(x)).swap(*this);
927 return *this;
928 }
933 any&
934 operator=(const any& a)
935 {
936 any(a).swap(*this);
937 return *this;
938 }
943 any&
944 operator=(any&& a) ynothrow
945 {
946 any(std::move(a)).swap(*this);
947 return *this;
948 }
949
951 using any_base::has_value;
952
954
955 void*
957 get() const ynothrow
958 {
959 return manager ? unchecked_get() : nullptr;
960 }
961
962 any_ops::holder*
963 get_holder() const
964 {
965 return manager ? unchecked_get_holder() : nullptr;
966 }
968
969 protected:
975 using any_base::get_storage;
976
977 using any_base::call;
979
980 public:
982 void
983 reset() ynothrow;
984
990 using any_emplace<any>::emplace;
991
992 using any_emplace<any>::emplace_with_handler;
994
995 void
996 swap(any& a) ynothrow
997 {
998 any_base::swap(a);
999 }
1000
1008 template<typename _type>
1009 _type*
1010 target() ynothrow
1011 {
1012 return manager ? any_base::template target<_type>() : nullptr;
1013 }
1014 template<typename _type>
1015 const _type*
1016 target() const ynothrow
1017 {
1018 return manager ? any_base::template target<_type>() : nullptr;
1019 }
1021
1023 const type_info&
1024 type() const ynothrow
1025 {
1026 return manager ? unchecked_type() : ystdex::type_id<void>();
1027 }
1028
1035 protected:
1037 using any_base::unchecked_access;
1038
1039 public:
1041 void*
1042 unchecked_get() const ynothrowv
1043 {
1044 return any_base::get();
1045 }
1046
1048 any_ops::holder*
1049 unchecked_get_holder() const
1050 {
1051 return any_base::get_holder();
1052 }
1053
1058 const type_info&
1059 unchecked_type() const ynothrowv
1060 {
1061 return any_base::type();
1062 }
1064 };
1065
1067
1068
1070
1074 inline void
1075 swap(any& x, any& y) ynothrow
1076 {
1077 x.swap(y);
1078 }
1079
1087 template<typename _type, typename... _tParams>
1088 any
1089 make_any(_tParams&&... args)
1090 {
1091 return any(in_place<_type>, yforward(args)...);
1092 }
1093 template<typename _type, typename _tOther, typename... _tParams>
1094 any
1095 make_any(std::initializer_list<_tOther> il, _tParams&&... args)
1096 {
1097 return any(in_place<_type>, il, yforward(args)...);
1098 }
1100
1111
1112 template<typename _type>
1113 inline _type*
1114 any_cast(any* p) ynothrow
1115 {
1116 return p ? p->target<_type>() : nullptr;
1117 }
1118 template<typename _type>
1119 inline const _type*
1120 any_cast(const any* p) ynothrow
1121 {
1122 return p ? p->target<_type>() : nullptr;
1123 }
1125
1130 template<typename _tValue>
1131 _tValue
1132 any_cast(any& x)
1133 {
1134 static_assert(is_any_cast_dest<_tValue>(),
1135 "Invalid cast destination type found.");
1136
1137 if(const auto p = x.template target<remove_reference_t<_tValue>>())
1138 return static_cast<_tValue>(*p);
1139 throw bad_any_cast(x.type(), ystdex::type_id<_tValue>());
1140 }
1141 template<typename _tValue>
1142 _tValue
1143 any_cast(const any& x)
1144 {
1145 static_assert(is_any_cast_dest<_tValue>(),
1146 "Invalid cast destination type found.");
1147
1148 if(const auto p = x.template target<const remove_reference_t<_tValue>>())
1149 return static_cast<_tValue>(*p);
1150 throw bad_any_cast(x.type(), ystdex::type_id<_tValue>());
1151 }
1153 template<typename _tValue>
1154 _tValue
1155 any_cast(any&& x)
1156 {
1157 static_assert(is_any_cast_dest<_tValue>(),
1158 "Invalid cast destination type found.");
1159
1160 if(const auto p = x.template target<remove_reference_t<_tValue>>())
1161 return static_cast<_tValue>(*p);
1162 throw bad_any_cast(x.type(), ystdex::type_id<_tValue>());
1163 }
1165
1166
1167
1169
1170
1174 inline bool
1175 hold_same(const any& x, const any& y)
1176 {
1177 return x.get() == y.get();
1178 }
1189 template<typename _type>
1190 inline _type*
1191 unchecked_any_cast(any* p) ynothrowv
1192 {
1193 yconstraint(p && p->has_value()
1194 && p->unchecked_type() == ystdex::type_id<_type>());
1195 return static_cast<_type*>(p->unchecked_get());
1196 }
1197
1202 template<typename _type>
1203 inline const _type*
1204 unchecked_any_cast(const any* p) ynothrowv
1205 {
1206 yconstraint(p && p->has_value()
1207 && p->unchecked_type() == ystdex::type_id<const _type>());
1208 return static_cast<const _type*>(p->unchecked_get());
1209 }
1211
1218 template<typename _type>
1220 inline _type*
1221 unsafe_any_cast(any* p) ynothrowv
1222 {
1223 yconstraint(p && p->type() == ystdex::type_id<_type>());
1224 return static_cast<_type*>(p->get());
1225 }
1226
1228 template<typename _type>
1229 inline const _type*
1230 unsafe_any_cast(const any* p) ynothrowv
1231 {
1232 yconstraint(p && p->type() == ystdex::type_id<const _type>());
1233 return static_cast<const _type*>(p->get());
1234 }
1236
1237
1238
1239
1245 class YB_API void_ref_any
1246 {
1247 private:
1248 mutable any data;
1249
1250 public:
1251 void_ref_any() = default;
1252 template<typename _type,
1253 yimpl(typename = exclude_self_t<void_ref_any, _type>)>
1254 void_ref_any(_type&& x)
1255 : void_ref_any(yforward(x), is_convertible<_type&&, void_ref>())
1256 {}
1257
1258 private:
1259 template<typename _type>
1260 void_ref_any(_type&& x, true_)
1261 : data(void_ref(yforward(x)))
1262 {}
1263 template<typename _type>
1264 void_ref_any(_type&& x, false_)
1265 : data(yforward(x))
1266 {}
1267
1268 public:
1269 void_ref_any(const void_ref_any&) = default;
1270 void_ref_any(void_ref_any&&) = default;
1271
1272 void_ref_any&
1273 operator=(const void_ref_any&) = default;
1274 void_ref_any&
1275 operator=(void_ref_any&&) = default;
1276
1278 template<typename _type,
1279 yimpl(typename = exclude_self_t<void_ref_any, _type>)>
1280 YB_PURE
1281 operator _type() const
1282 {
1283 if(data.type() == ystdex::type_id<void_ref>())
1284 return *ystdex::unchecked_any_cast<void_ref>(&data);
1285 return std::move(*ystdex::unchecked_any_cast<_type>(&data));
1286 }
1287 };
1288
1289 } // namespace ystdex;
1290
1291 #endif
1292
ystdex::any_ops::holder_handler
动态泛型持有者处理器。
Definition: any.h:528
ystdex::any_ops::value_holder::type
const type_info & type() const override
Definition: any.h:145
ystdex::details::any_emplace
Definition: any.h:796
ystdex::void_ref_any
Definition: any.h:1245
ystdex::boxed_value
包装类类型的值的对象。
Definition: utility.hpp:252
ystdex::reset
bool reset(std::unique_ptr< _type, _tDeleter > &p)
Definition: memory.hpp:708
ystdex::bad_any_cast::bad_any_cast
bad_any_cast(const type_info &from_, const type_info &to_)
Definition: any.h:642
ystdex::any_ops::with_handler_t
Definition: any.h:58
ystdex::any::unsafe_any_cast
_type * unsafe_any_cast(any *p)
非安全动态泛型转换。
Definition: any.h:1221
ystdex::any::any
any(any_ops::use_holder_t, std::unique_ptr< _tHolder > p)
构造：使用指定持有者。
Definition: any.h:881
ystdex::default_init_t
默认初始化标记。
Definition: placement.hpp:48
ystdex::cond_t
conditional_t< _tCond::value, _tThen, _tElse > cond_t
条件判断。
Definition: type_traits.hpp:738
ystdex::any::any
any(_type &&x)
Definition: any.h:861
ystdex::true_
bool_< true > true_
Definition: type_traits.hpp:391
ystdex::any_ops::value_holder::value_holder
value_holder()=default
ystdex::any::any_cast
_tValue any_cast(any &x)
Definition: any.h:1132
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
ystdex::bad_any_cast
动态泛型转换失败异常。
Definition: any.h:628
ystdex::any_ops::ref_handler::manage
static void manage(any_storage &d, any_storage &s, op_code op)
Definition: any.h:506
ystdex::any_ops::value_handler< _tHolder >::local_storage
bool_< and_< is_nothrow_move_constructible< _tHolder >, is_aligned_storable< any_storage, _tHolder >>::value > local_storage
Definition: any.h:284
ystdex::any_ops::no_op
Definition: any.h:227
ystdex::details::any_base::any_base
any_base(const any_base &a)
Definition: any.h:702
ystdex::any::unchecked_type
const type_info & unchecked_type() const
取包含对象的类型。
Definition: any.h:1059
ystdex::void_ref_any::void_ref_any
void_ref_any(_type &&x)
Definition: any.h:1254
ystdex::any_ops::holder::hold_same
bool hold_same(const holder &x, const holder &y)
判断是否持有相同对象。
Definition: any.h:93
ystdex::details::any_base::any_base
any_base(any_ops::with_handler_t< _tHandler >, _tParams &&...args)
Definition: any.h:696
yalignof
#define yalignof(_type)
指定特定类型的对齐。
Definition: ydef.h:660
ystdex::any_ops::pointer_holder::pointer_holder
pointer_holder(const pointer_holder &h)
Definition: any.h:182
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
NPL::x
const TermNode &term ynothrow const TermNode &term ynothrow const TermNode &term ynothrow TermNode &term const TermNode &term ValueNode &node const ValueNode &node const _type & x
Definition: SContext.h:94
ystdex::any_ops::op_code
std::uint32_t op_code
操作码。
Definition: any.h:218
ystdex::any_ops::holder
抽象动态泛型持有者接口。
Definition: any.h:66
ystdex::any::any
any(in_place_type_t< _type >, _tParams &&...args)
Definition: any.h:870
ystdex::any_ops::pointer_holder
指针类型动态泛型持有者。
Definition: any.h:161
ystdex::false_
bool_< false > false_
Definition: type_traits.hpp:392
ystdex::standard_layout_storage::pun
pun_ptr< _type > pun(_tParams &&...args)
Definition: type_pun.hpp:389
ystdex::any::unchecked_get_holder
any_ops::holder * unchecked_get_holder() const
取持有者指针。
Definition: any.h:1049
ystdex::any_ops::ref_handler::value_type
_type value_type
Definition: any.h:476
ystdex::or_
Definition: type_traits.hpp:438
ystdex::any_ops::get_type
Definition: any.h:229
ystdex::any::swap
void swap(any &a)
Definition: any.h:996
ystdex::any_ops::ref_handler
动态泛型引用处理器。
Definition: any.h:473
ystdex::any_ops::value_handler::get_pointer_impl
static value_type * get_pointer_impl(false_, any_storage &s)
Definition: any.h:333
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:262
ystdex::standard_layout_storage
任意标准布局类型对象提供空间的存储。
Definition: type_pun.hpp:307
ystdex::details::any_base::target
const _type * target() const
Definition: any.h:755
ystdex::is_aligned_storable
判断是否可对齐存储。
Definition: type_pun.hpp:72
ystdex::any_ops::value_handler::get_reference_impl
static const value_type & get_reference_impl(false_, const any_storage &s)
Definition: any.h:378
ystdex::is_cv
判断指定类型是否为 const 或 volatile 类型。
Definition: type_traits.hpp:882
ystdex::cpp2014::enable_if_t
typename enable_if< _bCond, _type >::type enable_if_t
Definition: type_traits.hpp:343
ystdex::any_ops::construct
any_manager construct(any_storage &storage, _tParams &&...args)
使用指定处理器初始化存储。
Definition: any.h:257
ystdex::any::any
any(any_ops::use_holder_t, in_place_type_t< _tHolder >, _tParams &&...args)
Definition: any.h:894
ystdex::any_ops::pointer_holder::operator=
pointer_holder & operator=(const pointer_holder &)=default
CHRLib::args
_tParams && args
Definition: CharacterProcessing.h:538
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:679
ystdex::any_ops::pointer_holder::type
const type_info & type() const override
Definition: any.h:202
ystdex::any_ops::pointer_holder::value_type
_type value_type
Definition: any.h:168
ystdex::any_ops::holder::get
virtual void * get() const =0
ystdex::any::operator=
any & operator=(any &&a)
转移赋值：使用复制和交换。
Definition: any.h:944
ystdex::any_ops::value_holder::operator=
value_holder & operator=(const value_holder &)=default
ystdex::data
auto data(_tRange &c) -> decltype(c.data())
Definition: range.hpp:280
ystdex::any::unchecked_any_cast
const _type * unchecked_any_cast(const any *p)
Definition: any.h:1204
ystdex::any_ops::value_handler::init_impl
static void init_impl(false_, any_storage &d, _tParams &&...args)
Definition: any.h:411
ystdex::any::get
void * get() const
Definition: any.h:957
ystdex::any_ops::value_handler::get_pointer_impl
static value_type * get_pointer_impl(true_, any_storage &s)
Definition: any.h:343
ystdex::any_ops::value_handler::dispose
static void dispose(any_storage &d)
Definition: any.h:297
ystdex::any::operator=
any & operator=(const any &a)
复制赋值：使用复制和交换。
Definition: any.h:934
ystdex::any_ops::base_op
base_op
基本操作码。
Definition: any.h:224
ystdex::any_ops::value_handler::try_init
static void try_init(false_, _tParams &&...)
Definition: any.h:454
YB_PURE
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:527
ystdex::is_reference_wrapper
判断模板参数指定的类型是否。
Definition: ref.hpp:161
ystdex::any::target
_type * target()
取目标指针。
Definition: any.h:1010
ystdex::any::unchecked_any_cast
_type * unchecked_any_cast(any *p)
未检查的动态泛型转换。
Definition: any.h:1191
YB_ATTR
#define YB_ATTR(...)
GNU 风格属性。
Definition: ydef.h:353
ystdex::lref< const type_info >
ystdex::any_ops::wrap_handler
根据类型选择引用或值处理器。
Definition: any.h:598
ystdex::any_ops::value_handler::get_reference
static const value_type & get_reference(const any_storage &s)
Definition: any.h:361
ystdex::any_ops::ref_handler::get_pointer
static value_type * get_pointer(any_storage &s)
Definition: any.h:481
ystdex::any_ops::value_holder::value_holder
value_holder(_tParam &&arg)
Definition: any.h:119
ystdex::cpp2014::aligned_storage_t
typename aligned_storage< _vLen, _vAlign >::type aligned_storage_t
Definition: type_traits.hpp:331
YB_ATTR_returns_nonnull
#define YB_ATTR_returns_nonnull
指示返回非空属性。
Definition: ydef.h:413
ystdex::any::any_cast
_type * any_cast(any *p)
动态泛型转换。
Definition: any.h:1114
ystdex::any::type
const type_info & type() const
Definition: any.h:1024
YSLib::a
YB_PURE const _type * a
Definition: YCoreUtilities.h:167
ystdex::any_ops::value_handler::get_pointer
static const value_type * get_pointer(const any_storage &s)
Definition: any.h:324
ystdex::any
基于类型擦除的动态泛型对象。
Definition: any.h:845
ystdex::any::unchecked_get
void * unchecked_get() const
取包含对象的指针。
Definition: any.h:1042
ystdex::any_ops::value_handler::get_reference_impl
static value_type & get_reference_impl(false_, any_storage &s)
Definition: any.h:370
ystdex::standard_layout_storage::access
void * access()
Definition: type_pun.hpp:335
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:766
ystdex::any_ops::destroy
Definition: any.h:235
ystdex::cpp2014::decay_t
typename decay< _type >::type decay_t
Definition: type_traits.hpp:340
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
ystdex
特性测试宏。
Definition: ydef.h:822
ystdex::any_ops::value_handler::get_pointer_impl
static const value_type * get_pointer_impl(true_, const any_storage &s)
Definition: any.h:348
ystdex::lref::get
_type & get() const
Definition: ref.hpp:108
ystdex::any::any
any(any_ops::with_handler_t< _tHandler > t, _tParams &&...args)
Definition: any.h:908
ystdex::any::any_cast
_tValue any_cast(any &&x)
Definition: any.h:1155
ystdex::any_ops::clone
Definition: any.h:233
ystdex::any_ops::any_storage
standard_layout_storage< aligned_storage_t< sizeof(void *), std::alignment_of< void * >::value >> any_storage
Definition: any.h:247
ystdex::details::any_emplace::emplace
void emplace(_tParams &&...args)
Definition: any.h:800
ref.hpp
引用包装。
ystdex::any_ops::value_handler::manage
static void manage(any_storage &d, any_storage &s, op_code op)
Definition: any.h:426
ystdex::any_ops::value_handler::copy
static void copy(any_storage &d, const any_storage &s)
Definition: any.h:290
ystdex::_t
typename _type::type _t
嵌套类型别名。
Definition: type_traits.hpp:97
ystdex::any_ops::pointer_holder::pointer_holder
pointer_holder(pointer value)
取得所有权。
Definition: any.h:177
ystdex::any_ops::get_holder_ptr
Definition: any.h:239
exception.h
ystdex::any_ops::value_handler::get_pointer_impl
static const value_type * get_pointer_impl(false_, const any_storage &s)
Definition: any.h:338
ystdex::any_ops::get_holder_type
Definition: any.h:237
ystdex::any::unsafe_any_cast
const _type * unsafe_any_cast(const any *p)
Definition: any.h:1230
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:786
ystdex::any_ops::value_holder
值类型动态泛型持有者。
Definition: any.h:105
ystdex::any_ops::holder_handler::manage
static void manage(any_storage &d, any_storage &s, op_code op)
Definition: any.h:569
ystdex::void_ref
任意对象引用类型。
Definition: ref.hpp:275
ystdex::any_ops::pointer_holder::get
void * get() const override
Definition: any.h:195
ystdex::clone_monomorphic_ptr
auto clone_monomorphic_ptr(const _tPointer &p) -> decltype(ystdex::clone_monomorphic(*p))
若指针非空，使用 new 复制指针指向的对象。
Definition: memory.hpp:1091
ystdex::any_ops::value_handler< _tHolder >::value_type
_tHolder value_type
Definition: any.h:283
ystdex::standard_layout_storage::construct
_type * construct(_tParams &&...args)
Definition: type_pun.hpp:371
ystdex::any_ops::value_holder::value_type
_type value_type
Definition: any.h:112
ystdex::void_ref_any::void_ref_any
void_ref_any(_type &&x, true_)
Definition: any.h:1260
ystdex::swap
void swap(container_adaptor< _tSeqCon > &x, container_adaptor< _tSeqCon > &y)
Definition: container.hpp:179
ystdex::any::swap
void swap(any &x, any &y)
交换对象。
Definition: any.h:1075
ystdex::exclude_self_t
enable_if_t<!is_same< _tClass &, decay_t< _tParam > & >::value, _type > exclude_self_t
移除选择类类型的特定重载避免构造模板和复制/转移特殊成员函数冲突。
Definition: type_traits.hpp:748
ystdex::standard_layout_storage::pun_default
pun_ptr< _type > pun_default()
Definition: type_pun.hpp:397
ystdex::destroy
void destroy(_tFwd first, _tFwd last)
Definition: placement.hpp:235
std
Definition: memory.hpp:1183
ystdex::any::any
any(_type &&x, any_ops::use_holder_t)
Definition: any.h:901
ystdex::any_ops::wrap_handler::type
cond_t< is_reference_wrapper< _type >, ref_handler< value_type >, value_handler< value_type >> type
Definition: any.h:602
ystdex::type_id
const type_info & type_id()
Definition: typeinfo.h:149
ystdex::in_place_type_t
in_place_tag(&)(empty_base< _type >) in_place_type_t
原地类型标记模板。
Definition: placement.hpp:73
ystdex::any_ops::use_holder
const struct ystdex::any_ops::use_holder_t use_holder
ystdex::details::any_emplace::emplace_with_handler
void emplace_with_handler(_tParams &&...args)
Definition: any.h:823
ystdex::void_ref_any::data
any data
Definition: any.h:1248
typeinfo.h
ISO C++ 类型信息扩展。
YB_NORETURN
#define YB_NORETURN
指定无返回值函数。
Definition: ydef.h:504
ystdex::any_ops::value_handler
动态泛型对象处理器。
Definition: any.h:278
ystdex::throw_invalid_construction
void throw_invalid_construction()
Definition: ystdex/exception.cpp:49
utility.hpp
实用设施。
ystdex::any_ops::value_handler::init
static void init(any_storage &d, _tParams &&...args)
Definition: any.h:402
ystdex::any_ops::wrap_handler::value_type
remove_reference_t< unwrap_reference_t< _type >> value_type
Definition: any.h:600
ystdex::any_ops::value_handler::get_reference
static value_type & get_reference(any_storage &s)
Definition: any.h:356
ystdex::construct
void construct(_tIter i, _tParams &&...args)
使用指定参数在迭代器指定的位置以指定参数初始化构造对象。
Definition: placement.hpp:178
ystdex::pvoid
enable_if_t< or_< is_object< _type >, is_void< _type > >::value, void * > pvoid(_type *p)
取 void* 类型的指针。
Definition: addressof.hpp:101
ystdex::any_ops::pointer_holder::p_held
holder_pointer p_held
Definition: any.h:173
ystdex::any_ops::pointer_holder::holder_pointer
_tPointer holder_pointer
Definition: any.h:169
ystdex::any_ops::get_ptr
Definition: any.h:231
ystdex::any_ops::end_base_op
Definition: any.h:241
ystdex::bad_any_cast::to_ti
lref< const type_info > to_ti
Definition: any.h:632
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:995
ystdex::any_ops::ref_handler::init
static auto init(any_storage &d, _tWrapper x) -> decltype(base::init(d, std::addressof(x.get())))
Definition: any.h:498
yassume
#define yassume(_expr)
假定：环境语义。
Definition: cassert.h:105
ystdex::details::any_emplace::emplace
void emplace(any_ops::use_holder_t, _tParams &&...args)
Definition: any.h:815
ystdex::any::any
any(any_ops::use_holder_t, _tHolder &&h)
Definition: any.h:887
ystdex::any_ops::value_handler::dispose_impl
static void dispose_impl(true_, any_storage &d)
Definition: any.h:309
ystdex::any_ops::value_handler::get_pointer
static value_type * get_pointer(any_storage &s)
Definition: any.h:319
ystdex::void_ref_any::void_ref_any
void_ref_any(_type &&x, false_)
Definition: any.h:1264
ystdex::any_ops::holder_handler::init
static void init(any_storage &d, std::unique_ptr< _tHolder > p)
Definition: any.h:560
ystdex::details::any_base::unchecked_access
_type unchecked_access(any_ops::any_storage &t, any_ops::op_code op) const
Definition: any.h:787
CHRLib::d
_tDst * d
Definition: CharacterProcessing.h:139
ystdex::any_ops::any_manager
void(*)(any_storage &, any_storage &, op_code) any_manager
Definition: any.h:249
ystdex::any::hold_same
bool hold_same(const any &x, const any &y)
判断是否持有相同对象。
Definition: any.h:1175
ystdex::details::any_emplace::emplace
void emplace(std::initializer_list< _tOther > il, _tParams &&...args)
Definition: any.h:808
ystdex::any_ops::pointer_holder::pointer
typename holder_pointer::pointer pointer
Definition: any.h:170
YB_API
#define YB_API
YBase 应用程序编程接口：用于向库文件约定链接。
Definition: ydef.h:592
ystdex::any::make_any
any make_any(_tParams &&...args)
创建 any 对象。
Definition: any.h:1089
ystdex::any_ops::value_handler::get_reference_impl
static value_type & get_reference_impl(true_, any_storage &s)
Definition: any.h:386
ystdex::any::any
any(any &&a)
Definition: any.h:913
ystdex::any_ops::ref_handler::get_reference
static value_type & get_reference(any_storage &s)
Definition: any.h:488
yconstraint
#define yconstraint(_expr)
约束：接口语义。
Definition: cassert.h:88
ystdex::any_ops::holder_handler::init
static void init(false_, any_storage &d, std::unique_ptr< _tHolder > p)
Definition: any.h:546
ystdex::any_ops::holder_handler::init
static void init(true_, any_storage &d, std::unique_ptr< _tHolder > p)
Definition: any.h:552
ystdex::and_
逻辑操作元函数。
Definition: type_traits.hpp:420
ystdex::any::get_holder
any_ops::holder * get_holder() const
Definition: any.h:963
ystdex::any::operator=
any & operator=(_type &&x)
Definition: any.h:924
ystdex::any_ops::use_holder_t
使用持有者标记。
Definition: any.h:268
ystdex::details::any_base::unchecked_access
_type unchecked_access(default_init_t, any_ops::op_code op) const
Definition: any.h:777
ystdex::boxed_value::value
_type value
Definition: utility.hpp:255
ystdex::any_ops::value_handler::get_reference_impl
static const value_type & get_reference_impl(true_, const any_storage &s)
Definition: any.h:391
ystdex::bad_any_cast::bad_any_cast
bad_any_cast()
Definition: any.h:637
ystdex::details::any_base::get_storage
any_ops::any_storage & get_storage() const
Definition: any.h:738
ystdex::details::any_base
Definition: any.h:687
ystdex::any::target
const _type * target() const
Definition: any.h:1016
ynoexcept
#define ynoexcept(...)
YSLib 无异常抛出保证：指定特定的异常规范。
Definition: ydef.h:746
ystdex::any_ops::holder_handler::get_holder_pointer
static _tHolder * get_holder_pointer(any_storage &s)
Definition: any.h:538
ystdex::any_ops::value_handler::dispose_impl
static void dispose_impl(false_, any_storage &d)
Definition: any.h:304
ystdex::bool_
std::integral_constant< bool, _vInt > bool_
Definition: type_traits.hpp:375
CHRLib::p
const auto p(d)
ystdex::details::get
get())
Definition: cast.hpp:102