YSTest: typeinfo.h 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

typeinfo.h
浏览该文件的文档.
1 /*
2 © 2016 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_typeinfo_h_
29 #define YB_INC_ystdex_typeinfo_h_ 1
30
31 #include "base.h" // for noncopyable;
32 #include "type_traits.hpp" // for is_void;
33 #include <typeinfo> // for std::type_info, typeid;
34 #include <functional> // for std::hash;
35
36 namespace ystdex
37 {
38
40
41
46 class YB_API type_id_info : private yimpl(noncopyable)
47 {
48 public:
49 using id_t = size_t;
50
51 private:
52 id_t id;
53
54 yimpl(yconstfn)
55 type_id_info(id_t i = 0)
56 : id(i)
57 {}
58
59 public:
60 yimpl(yconstfn) bool
61 operator==(const type_id_info& rhs) const ynothrow
62 {
63 return id == rhs.id;
64 }
65
66 yimpl(yconstfn) bool
67 operator!=(const type_id_info& rhs) const ynothrow
68 {
69 return id != rhs.id;
70 }
71
72 yimpl(yconstfn) bool
73 before(const type_id_info& rhs) const ynothrow
74 {
75 return id < rhs.id;
76 }
77
78 id_t
79 get() const ynothrow
80 {
81 return id;
82 }
83
84 size_t
85 hash_code() const ynothrow
86 {
87 return std::hash<id_t>()(id);
88 }
89
90 template<typename _type>
91 static const type_id_info&
92 make()
93 {
94 return is_void<_type>() ? xref_void() : xref<remove_cv_t<_type>>();
95 }
96
97 yimpl(yconstfn) const char*
98 name() const ynothrow
99 {
100 return "<unknown>";
101 }
102
103 private:
104 static id_t
105 push()
106 {
107 static id_t id;
108
109 return ++id;
110 }
111
112 template<typename _type>
113 static const type_id_info&
114 xref()
115 {
116 static_assert(is_same<_type, remove_cv_t<_type>>(),
117 "Invalid type found.");
118 static type_id_info info(push());
119
120 return info;
121 }
122
123 static const type_id_info&
124 xref_void()
125 {
126 static type_id_info info;
127
128 return info;
129 }
130 };
131
132
139 #if YB_Use_LightweightTypeID
140 using type_info = type_id_info;
141 #else
142 using std::type_info;
143 #endif
144
145
147 template<typename _type>
148 const type_info&
149 type_id()
150 {
151 #if YB_Use_LightweightTypeID
152 return type_info::make<_type>();
153 #else
154 return typeid(_type);
155 #endif
156 }
158
159 } // namespace ystdex;
160
161 #endif
162
ystdex::type_id_info::xref_void
static const type_id_info & xref_void()
Definition: typeinfo.h:124
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
ystdex::type_id_info::push
static id_t push()
Definition: typeinfo.h:105
NPL::A1::name
ctx const string & name
Definition: NPLA1.h:248
platform::size_t
YF_API size_t
Definition: FileIO.h:1262
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:256
YB_API
#define YB_API
YBase 应用程序编程接口：用于向库文件约定链接。
Definition: ydef.h:566
ystdex::type_id_info::make
static const type_id_info & make()
Definition: typeinfo.h:92
ystdex::type_id_info
类型信息。
Definition: typeinfo.h:46
base.h
基类实用设施。
std::hash
ystdex::observer_ptr 散列支持。
Definition: memory.hpp:1110
ystdex
特性测试宏。
Definition: ydef.h:796
ystdex::type_id_info::id_t
size_t id_t
Definition: typeinfo.h:49
type_traits.hpp
ISO C++ 类型特征扩展。
YSLib::i
return i
Definition: YCoreUtilities.h:176
ystdex::type_id_info::xref
static const type_id_info & xref()
Definition: typeinfo.h:114
ystdex::type_id_info::hash_code
size_t hash_code() const
Definition: typeinfo.h:85
ystdex::type_id
const type_info & type_id()
Definition: typeinfo.h:149
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:654
ystdex::type_id_info::id
id_t id
Definition: typeinfo.h:52
platform::bool
std::FILE *fp bool
Definition: FileIO.h:1187