NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
check_type.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_TYPEINFO_CHECK_TYPE_HPP__
2#define NEFORCE_CORE_TYPEINFO_CHECK_TYPE_HPP__
3
17
18#include <typeinfo>
20NEFORCE_BEGIN_NAMESPACE__
22NEFORCE_BEGIN_INNER__
23
31class output {
32private:
33 bool is_compact_;
34 string& str_;
35
36 template <typename T>
37 static NEFORCE_CONSTEXPR20 bool check_empty(const T&) noexcept {
38 return false;
39 }
40 static NEFORCE_CONSTEXPR20 bool check_empty(const char* value) noexcept { return !value || value[0] == 0; }
41
42 template <typename T>
43 NEFORCE_CONSTEXPR20 void out(const T& value) {
44 if (this->check_empty(value)) {
45 return;
46 }
47 if (!this->is_compact_) {
48 str_ += " ";
49 }
50 this->str_ += to_string(value);
51 this->is_compact_ = false;
52 }
53
54public:
55 NEFORCE_CONSTEXPR20 output(string& str) noexcept :
56 is_compact_(true),
57 str_(str) {}
58
59 NEFORCE_CONSTEXPR20 output& operator()() noexcept { return *this; }
60
61 NEFORCE_CONSTEXPR20 output& compact() noexcept {
62 this->is_compact_ = true;
63 return *this;
64 }
65
66 template <typename T1, typename... T>
67 NEFORCE_CONSTEXPR20 output& operator()(const T1& value, const T&... args) {
68 this->out(value);
69 return operator()(args...);
70 }
71};
72
80template <bool IsStart>
81struct bracket {
82 output& out_;
83
84 NEFORCE_CONSTEXPR20 bracket(output& out, const char* = nullptr) :
85 out_(out) {
86 out_("(").compact();
87 }
88
89 NEFORCE_CONSTEXPR20 ~bracket() { out_.compact()(")"); }
90};
91
92template <>
93struct bracket<false> {
94 NEFORCE_CONSTEXPR20 bracket(output& out, const char* str = nullptr) { out(str); }
95};
96
104template <size_t N = 0>
105struct bound {
106private:
107 template <size_t NN>
108 NEFORCE_CONSTEXPR20 enable_if_t<NN == 0> __bound_dispatch() const {
109 out_("[]");
110 return;
111 }
112
113 template <size_t NN>
114 NEFORCE_CONSTEXPR20 enable_if_t<NN != 0> __bound_dispatch() const {
115 out_("[").compact()(NN).compact()("]");
116 return;
117 }
118
119public:
120 output& out_;
121
122 NEFORCE_CONSTEXPR20 bound(output& out) :
123 out_(out) {}
124
125 NEFORCE_CONSTEXPR20 ~bound() { __bound_dispatch<N>(); }
126};
127
134struct at_destruct {
135 output& out_;
136 const char* str_;
137
138 NEFORCE_CONSTEXPR20 at_destruct(output& out, const char* str = nullptr) noexcept :
139 out_(out),
140 str_(str) {}
141
142 NEFORCE_CONSTEXPR20 ~at_destruct() { out_(str_); }
143
144 NEFORCE_CONSTEXPR20 void set_str(const char* str = nullptr) noexcept { str_ = str; }
145};
146
147
148#ifdef NEFORCE_COMPILER_GNUC
149
155string NEFORCE_API real_symbol_name(string name);
156
157#endif
158
159
168template <typename T, bool IsBase = false>
169struct check {
170 output out_;
171
172 NEFORCE_CONSTEXPR20 check(const output& out) :
173 out_(out) {
174#ifdef NEFORCE_COMPILER_GNUC
176 out_(real_symbol_name(typeid(FinT).name()));
177#else
178 out_(typeid(T).name());
179#endif
180 }
181};
182
188template <typename T, bool IsBase>
189struct check<T[], IsBase> : check<T, true> {
190 using base_t = check<T, true>;
191 using base_t::out_;
192
193 bound<> bound_;
194 bracket<IsBase> bracket_;
195
196 NEFORCE_CONSTEXPR20 check(const output& out) :
197 base_t(out),
198 bound_(out_),
199 bracket_(out_) {}
200};
201
202#define CHECK_TYPE__(OPT) \
203 template <typename T, bool IsBase> \
204 struct check<T OPT, IsBase> : check<T, true> { \
205 using base_t = check<T, true>; \
206 using base_t::out_; \
207 \
208 NEFORCE_CONSTEXPR20 check(const output& out) : \
209 base_t(out) { \
210 out_(#OPT); \
211 } \
212 };
213
214CHECK_TYPE__(const)
215CHECK_TYPE__(volatile)
216CHECK_TYPE__(const volatile)
217CHECK_TYPE__(&)
218CHECK_TYPE__(&&)
219CHECK_TYPE__(*)
220#undef CHECK_TYPE__
221
222
223template <bool IsStart, typename... P>
224struct parameter;
225
234template <bool IsStart, typename P1, typename... P>
235struct parameter<IsStart, P1, P...> {
236 output& out_;
237
238 NEFORCE_CONSTEXPR20 parameter(output& out) noexcept :
239 out_(out) {}
240
241 NEFORCE_CONSTEXPR20 ~parameter() {
242 [this](bracket<IsStart>&&) {
243 check<P1>{out_};
244 parameter<false, P...>{out_.compact()};
245 }(bracket<IsStart>{out_, ","});
246 }
247};
248
253template <bool IsStart>
254struct parameter<IsStart> {
255 output& out_;
256
257 NEFORCE_CONSTEXPR20 parameter(output& out) noexcept :
258 out_(out) {}
259
260 NEFORCE_CONSTEXPR20 ~parameter() { bracket<IsStart>{out_}; }
261};
262
263
264#define CHECK_TYPE_ARRAY__(CV_OPT, BOUND_OPT, ...) \
265 template <typename T, bool IsBase __VA_ARGS__> \
266 struct check<T CV_OPT[BOUND_OPT], IsBase> : check<T CV_OPT, !is_array_v<T>> { \
267 using base_t = check<T CV_OPT, !is_array_v<T>>; \
268 using base_t::out_; \
269 \
270 bound<BOUND_OPT> bound_; \
271 bracket<IsBase> bracket_; \
272 \
273 NEFORCE_CONSTEXPR20 check(const output& out) : \
274 base_t(out), \
275 bound_(out_), \
276 bracket_(out_) {} \
277 };
278
279#define CHECK_TYPE_ARRAY_CV__(BOUND_OPT, ...) \
280 CHECK_TYPE_ARRAY__(, BOUND_OPT, , ##__VA_ARGS__) \
281 CHECK_TYPE_ARRAY__(const, BOUND_OPT, , ##__VA_ARGS__) \
282 CHECK_TYPE_ARRAY__(volatile, BOUND_OPT, , ##__VA_ARGS__) \
283 CHECK_TYPE_ARRAY__(const volatile, BOUND_OPT, , ##__VA_ARGS__)
284
285#ifdef NEFORCE_COMPILER_GNUC
286CHECK_TYPE_ARRAY_CV__(0)
287#endif
288CHECK_TYPE_ARRAY_CV__(N, size_t N)
289CHECK_TYPE_ARRAY__(const, , )
290CHECK_TYPE_ARRAY__(volatile, , )
291CHECK_TYPE_ARRAY__(const volatile, , )
292
293#undef CHECK_TYPE_ARRAY__
294#undef CHECK_TYPE_ARRAY_CV__
295
302template <typename T, bool IsBase, typename... P>
303struct check<T(P...), IsBase> : check<T, true> {
304 using base_t = check<T, true>;
305 using base_t::out_;
306
307 parameter<true, P...> parameter_;
308 bracket<IsBase> bracket_;
309
310 NEFORCE_CONSTEXPR20 check(const output& out) :
311 base_t(out),
312 parameter_(out_),
313 bracket_(out_) {}
314};
315
322template <typename T, bool IsBase, typename C>
323struct check<T C::*, IsBase> : check<T, true> {
324 using base_t = check<T, true>;
325 using base_t::out_;
326
327 NEFORCE_CONSTEXPR20 check(const output& out) :
328 base_t(out) {
329 check<C>{out_};
330 out_.compact()("::*");
331 }
332};
333
341template <typename T, bool IsBase, typename C, typename... P>
342struct check<T (C::*)(P...), IsBase> : check<T(P...), true> {
343 using base_t = check<T(P...), true>;
344 using base_t::out_;
345
346 NEFORCE_CONSTEXPR20 check(const output& out) :
347 base_t(out) {
348 check<C>{out_};
349 out_.compact()("::*");
350 }
351};
352
353#define CHECK_TYPE_MEM_FUNC__(...) \
354 template <typename T, bool IsBase, typename C, typename... P> \
355 struct check<T (C::*)(P...) __VA_ARGS__, IsBase> { \
356 at_destruct cv_; \
357 check<T(P...), true> base_; \
358 output& out_ = base_.out_; \
359 \
360 NEFORCE_CONSTEXPR20 check(const output& out) : \
361 cv_(base_.out_), \
362 base_(out) { \
363 cv_.set_str(#__VA_ARGS__); \
364 check<C>{out_}; \
365 out_.compact()("::*"); \
366 } \
367 };
368
369CHECK_TYPE_MEM_FUNC__(const)
370CHECK_TYPE_MEM_FUNC__(volatile)
371CHECK_TYPE_MEM_FUNC__(const volatile)
372#undef CHECK_TYPE_MEM_FUNC__
373
374NEFORCE_END_INNER__
376
382
388template <typename T>
389NEFORCE_CONSTEXPR20 string check_type() {
390 string str;
391 inner::check<T>{str};
392 return _NEFORCE move(str);
393}
394 // CheckType
396
397NEFORCE_END_NAMESPACE__
398#endif // NEFORCE_CORE_TYPEINFO_CHECK_TYPE_HPP__
NEFORCE_CONSTEXPR20 string check_type()
检查类型并返回可读字符串
typename remove_function_qualifiers< T >::type remove_function_qualifiers_t
remove_function_qualifiers的便捷别名
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
NEFORCE_ALWAYS_INLINE_INLINE bool name(char *buffer, size_t size)
获取当前线程名称
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
NEFORCE_CONSTEXPR20 string to_string(const CharT &c)
将字符转换为普通字符串
数值类型包装类