YSTest: YCommon.h 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

YCommon.h
浏览该文件的文档.
1 /*
2 © 2009-2016, 2018-2020 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 YCL_INC_ycommon_h_
29 #define YCL_INC_ycommon_h_ 1
30
31 #include "YModules.h"
32 #include YFM_YCLib_Platform
33 #include <ystdex/functional.hpp> // for ystdex::decay_t,
34 // ystdex::invoke_result_t, ystdex::retry_on_cond, yfsig;
35 #include <ystdex/cassert.h> // yconstraint, yassume for other headers;
36 #include <cerrno> // for errno, ENOMEM;
37 #include <ystdex/exception.h> // for ystdex::throw_error;
38 #include <cstring> // for std::strerror;
39 #include <ystdex/cwctype.h> // for ystdex::isprint, ystdex::iswprint;
40 #include <ystdex/cstring.h> // for ystdex::uchar_t, ystdex::replace_cast;
41 #include YFM_YBaseMacro // for TryRet, CatchIgnore;
42 #include <exception> // for std::bad_alloc;
43
45 namespace platform
46 {
47
53 using ID = std::uintmax_t;
55
57 template<ID _vN>
58 using MetaID = std::integral_constant<ID, _vN>;
59
61 struct IDTagBase
62 {};
63
64 #if YB_IMPL_CLANGPP
65 # pragma GCC diagnostic push
66 # pragma GCC diagnostic ignored "-Wweak-vtables"
67 #endif
68
70 struct HostedIDTagBase : virtual IDTagBase
71 {};
72
74 template<ID _vN>
75 struct IDTag : virtual IDTagBase, virtual MetaID<_vN>
76 {};
77
79 #define YCL_IDTag_Spec(_n, _base) \
80 template<> \
81 struct IDTag<YF_Platform_##_n> : virtual _base, \
82 virtual MetaID<YF_Platform_##_n> \
83 {};
84
86 #define YCL_IDTag_SpecHost(_n) \
87 YCL_IDTag_Spec(_n, HostedIDTagBase)
88
90 #define YCL_IDTag_SpecOnBase(_n, _b) \
91 YCL_IDTag_Spec(_n, IDTag<YF_Platform_##_b>)
92
93 YCL_IDTag_SpecHost(Win32)
94 YCL_IDTag_SpecOnBase(Win64, Win32)
95 YCL_IDTag_SpecOnBase(MinGW32, Win32)
96 YCL_IDTag_SpecOnBase(MinGW64, Win64)
97 YCL_IDTag_SpecHost(Linux)
98 YCL_IDTag_SpecOnBase(Linux_x86, Linux)
99 YCL_IDTag_SpecOnBase(Linux_x64, Linux)
100 YCL_IDTag_SpecOnBase(Android, Linux)
101 YCL_IDTag_SpecOnBase(Android_ARM, Android)
102
104 template<ID... _vN>
105 struct IDTagSet : virtual IDTag<_vN>...
106 {};
108
114 #define YCL_Tag_constfn inline
115
116 #if YB_IMPL_CLANGPP
117 # pragma GCC diagnostic pop
118 #endif
119
127 // XXX: See $2019-01 @ %Documentation::Workflow.
128 #if YB_IMPL_GNUCPP >= 60000
129 # define YCL_DefPlatformFwdTmpl(_n, _fn) \
130 YB_Diag_Push \
131 YB_Diag_Ignore(suggest-attribute=const) \
132 YB_Diag_Ignore(suggest-attribute=pure) \
133 DefFwdTmplAuto(_n, _fn(platform::IDTag<YF_Platform>(), yforward(args)...)) \
134 YB_Diag_Pop
135 #else
136 # define YCL_DefPlatformFwdTmpl(_n, _fn) \
137 DefFwdTmplAuto(_n, _fn(platform::IDTag<YF_Platform>(), yforward(args)...))
138 #endif
139
140
141
148 #define YCL_CheckDecl_t(_fn) CheckDecl##_fn##_t
149 #define YCL_DeclCheck_t(_fn, _call) \
150 template<typename... _tParams> \
151 using YCL_CheckDecl_t(_fn) \
152 = decltype(_call(std::declval<_tParams>()...));
153
154
155
162 YB_NORETURN YF_API void
163 terminate() ynothrow;
164
165
170 namespace Descriptions
171 {
172
177 enum RecordLevel : std::uint8_t
178 {
179 Emergent = 0x00,
180 Alert = 0x20,
181 Critical = 0x40,
182 Err = 0x60,
183 Warning = 0x80,
184 Notice = 0xA0,
185 Informative = 0xC0,
186 Debug = 0xE0
187 };
188
189 } // namespace Descriptions;
190
191
193
194
196
201 #define YCL_Raise_SysE(_t, _msg, _sig) \
202 do \
203 { \
204 const auto err_(errno); \
205 \
206 ystdex::throw_error<_t>(err_, \
207 platform::ComposeMessageWithSignature(_msg YPP_Comma _sig)); \
208 }while(false)
209
211 #define YCL_RaiseZ_SysE(_t, _expr, _msg, _sig) \
212 do \
213 { \
214 const auto err_(_expr); \
215 \
216 if(err_ != decltype(err_)()) \
217 ystdex::throw_error<_t>(err_, \
218 platform::ComposeMessageWithSignature(_msg YPP_Comma _sig)); \
219 }while(false)
220
221
226 #define YCL_Trace_SysE(_lv, _fn, _sig) \
227 do \
228 { \
229 const int err_(errno); \
230 \
231 YTraceDe(_lv, "Failed calling " #_fn " @ %s with error %d: %s.", \
232 _sig, err_, std::strerror(err_)); \
233 }while(false)
234
248 #define YCL_WrapCall_CAPI(_t, _fn, ...) \
249 [&](const char* sig_) YB_NONNULL(2){ \
250 const auto res_(_fn(__VA_ARGS__)); \
251 \
252 if(YB_UNLIKELY(res_ < decltype(res_)())) \
253 YCL_Raise_SysE(_t, #_fn, sig_); \
254 return res_; \
255 }
256
257 #define YCL_Call_CAPI(_t, _fn, _sig, ...) \
258 YCL_WrapCall_CAPI(_t, _fn, __VA_ARGS__)(_sig)
259
260 #define YCL_CallF_CAPI(_t, _fn, ...) YCL_Call_CAPI(_t, _fn, yfsig, __VA_ARGS__)
261
262
269 #define YCL_TraceWrapCall_CAPI(_fn, ...) \
270 [&](const char* sig_) YB_NONNULL(2){ \
271 const auto res_(_fn(__VA_ARGS__)); \
272 \
273 if(YB_UNLIKELY(res_ < decltype(res_)())) \
274 YCL_Trace_SysE(platform::Descriptions::Warning, _fn, sig_); \
275 return res_; \
276 }
277
278 #define YCL_TraceCall_CAPI(_fn, _sig, ...) \
279 YCL_TraceWrapCall_CAPI(_fn, __VA_ARGS__)(_sig)
280
281 #define YCL_TraceCallF_CAPI(_fn, ...) \
282 YCL_TraceCall_CAPI(_fn, yfsig, __VA_ARGS__)
283
284
285
286
287
295 YB_STATELESS inline PDefH(bool, IsPrint, char c)
296 ImplRet(ystdex::isprint(c))
297 YB_STATELESS inline PDefH(bool, IsPrint, wchar_t c)
298 ImplRet(ystdex::iswprint(c))
299 template<typename _tChar>
300 YB_STATELESS bool
301 IsPrint(_tChar c)
302 {
303 return platform::IsPrint(wchar_t(c));
304 }
306
307
313 inline PDefH(ystdex::uchar_t*, ucast, wchar_t* p) ynothrow
314 ImplRet(ystdex::replace_cast<ystdex::uchar_t*>(p))
315 inline PDefH(const ystdex::uchar_t*, ucast, const wchar_t* p) ynothrow
316 ImplRet(ystdex::replace_cast<const ystdex::uchar_t*>(p))
317 template<typename _tChar>
318 _tChar*
319 ucast(_tChar* p) ynothrow
320 {
321 return p;
322 }
323
324 inline PDefH(wchar_t*, wcast, ystdex::uchar_t* p) ynothrow
325 ImplRet(ystdex::replace_cast<wchar_t*>(p))
326 inline PDefH(const wchar_t*, wcast, const ystdex::uchar_t* p) ynothrow
327 ImplRet(ystdex::replace_cast<const wchar_t*>(p))
328 template<typename _tChar>
329 _tChar*
330 wcast(_tChar* p) ynothrow
331 {
332 return p;
333 }
335
336
341 template<typename _func, typename... _tParams>
342 ystdex::invoke_result_t<_func, _tParams...>
343 CallNothrow(const ystdex::invoke_result_t<_func, _tParams...>& v, _func f,
344 _tParams&&... args) ynothrowv
345 {
346 TryRet(f(yforward(args)...))
347 CatchExpr(std::bad_alloc&, errno = ENOMEM)
348 CatchIgnore(...)
349 return v;
350 }
351
356 template<typename _func, typename _tErrorRef,
357 typename _tError = ystdex::decay_t<_tErrorRef>,
358 typename _type = ystdex::invoke_result_t<_func&>>
359 _type
360 RetryOnError(_func f, _tErrorRef&& err, _tError e = _tError())
361 {
362 return ystdex::retry_on_cond([&](_type res){
363 return res < _type() && _tError(err) == e;
364 }, f);
365 }
366
371 template<typename _func, typename _type = ystdex::invoke_result_t<_func&>>
372 inline _type
373 RetryOnInterrupted(_func f)
374 {
375 return RetryOnError(f, errno, EINTR);
376 }
377
378
386 YF_API int
387 uspawn(const char*);
388
395 YF_API int
396 usystem(const char*);
397
398
407 #if YCL_DS
408 YB_NORETURN
409 #endif
410 YF_API YB_NONNULL(1, 2) void
411 SetEnvironmentVariable(const char*, const char*);
412
413
415
416
420 enum class SystemOption
421 {
427 PageSize,
429
430 MaxSemaphoreNumber,
433 MaxSemaphoreValue,
435 MaxSymlinkLoop
437 };
438
439
461 YF_API
462 #if YCL_DS
463 YB_STATELESS
464 #endif
465 size_t
466 FetchLimit(SystemOption) ynothrow;
468
469 } // namespace platform;
470
475 namespace platform_ex
476 {
477
478 #if YCL_DS
479
486 YF_API bool
487 AllowSleep(bool);
488 #endif
489
490 } // namespace platform_ex;
491
492 #endif
493
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:112
platform::MetaID
std::integral_constant< ID, _vN > MetaID
YF_Platform_* 宏替换值的对应的元类型。
Definition: YCommon.h:58
CatchIgnore
#define CatchIgnore(_ex)
指定异常处理器忽略指定异常。
Definition: YBaseMacro.h:133
ystdex::isprint
bool isprint(char c)
区域无关的 std::isprint 。
Definition: cctype.h:115
CHRLib::c
c
Definition: CharacterProcessing.h:84
platform::SystemOption::PageSize
@ PageSize
内存页面字节数。
platform::RetryOnInterrupted
_type RetryOnInterrupted(_func f)
循环可能被中断的操作。
Definition: YCommon.h:373
cstring.h
ISO C 标准字符串扩展。
platform::usystem
YF_API int usystem(const char *)
执行 UTF-8 字符串的环境命令。
Definition: YCommon.cpp:91
platform_ex
平台扩展命名空间。
Definition: Android.h:46
platform::SystemOption::MaxSemaphoreValue
@ MaxSemaphoreValue
可具有的信号量的最大值。
platform::Descriptions::Emergent
@ Emergent
Definition: YCommon.h:179
ystdex::retry_on_cond
invoke_result_t< _fCallable, _tParams... > retry_on_cond(_fCond cond, _fCallable &&f, _tParams &&... args)
循环重复调用：代替直接使用 do-while 语句以避免过多引入作用域外的变量。
Definition: functional.hpp:554
ystdex::uchar_t
cond_t< is_trivially_replaceable< wchar_t, char16_t >, char16_t, cond_t< is_trivially_replaceable< wchar_t, char32_t >, char32_t, void > > uchar_t
指定和 wchar_t 互相替换存储的非 char 内建字符类型。
Definition: cstring.h:118
platform::Descriptions::RecordLevel
RecordLevel
记录等级。
Definition: YCommon.h:178
platform::HostedIDTagBase
宿主平台标识的公共标记类型：指定任意宿主平台。
Definition: YCommon.h:71
YF_API
#define YF_API
Definition: Platform.h:56
platform::FetchLimit
YF_API size_t FetchLimit(SystemOption) ynothrow
取限制配置。
Definition: YCommon.cpp:121
CHRLib::f
f(d, s)
YCL_IDTag_SpecHost
#define YCL_IDTag_SpecHost(_n)
指定宿主平台的 IDTag 特化。
Definition: YCommon.h:86
platform::SetEnvironmentVariable
void SetEnvironmentVariable(const char *envname, const char *envval)
Definition: YCommon.cpp:102
platform::terminate
YB_NORETURN YF_API void terminate() ynothrow
终止函数。
Definition: YCommon.cpp:48
CHRLib::args
YB_ATTR_nodiscard _tParams && args
Definition: CharacterProcessing.h:649
ystdex::cpp2014::decay_t
typename decay< _type >::type decay_t
Definition: meta.hpp:196
platform::IDTagSet
取平台标识的整数标记集合类型。
Definition: YCommon.h:106
platform::Descriptions::Alert
@ Alert
Definition: YCommon.h:180
platform::Descriptions::Critical
@ Critical
Definition: YCommon.h:181
platform::ImplRet
YB_ATTR_nodiscard const char *s ImplRet(Nonnull(s)) YB_ATTR_nodiscard inline PDefH(const string &
YB_NORETURN
#define YB_NORETURN
指定无返回值函数。
Definition: ydef.h:877
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 转发对应参数。
Definition: ydef.h:1508
platform::ynothrow
const _tString &path ynothrow
Definition: YCLib/FileSystem.h:453
platform::SystemOption
SystemOption
系统配置选项。
Definition: YCommon.h:421
ystdex::iswprint
bool iswprint(wchar_t)
区域无关的 std::isprint 实现。
Definition: cwctype.cpp:34
platform::Descriptions::Notice
@ Notice
Definition: YCommon.h:184
YB_STATELESS
#define YB_STATELESS
指示函数或函数模板实例为无状态函数。
Definition: ydef.h:941
platform::Descriptions::Informative
@ Informative
Definition: YCommon.h:185
cwctype.h
ISO C 宽字符分类操作扩展。
functional.hpp
函数和可调用对象。
platform::Descriptions::Err
@ Err
Definition: YCommon.h:182
platform::Descriptions::Warning
@ Warning
Definition: YCommon.h:183
platform::YB_NONNULL
YB_NONNULL(2) YB_PURE inline PDefH(std
组合消息和函数签名字符串。
Definition: Debug.h:560
ystdex::cpp2017::invoke_result_t
_t< invoke_result< _fCallable, _tParams... > > invoke_result_t
Definition: invoke.hpp:393
CatchExpr
#define CatchExpr(_ex,...)
指定表达式作为异常处理器。
Definition: YBaseMacro.h:129
YCL_IDTag_SpecOnBase
#define YCL_IDTag_SpecOnBase(_n, _b)
指定基于特定平台的 IDTag 特化。
Definition: YCommon.h:90
platform
默认平台命名空间。
Definition: Container.h:56
platform::IDTagBase
平台标识的公共标记类型：指定任意平台。
Definition: YCommon.h:62
exception.h
标准库异常扩展接口。
YModules.h
YSLib 用户界面模块配置文件。
v
byte v
Definition: CharRenderer.cpp:76
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
platform::IDTag
平台标识的整数标记类型。
Definition: YCommon.h:76
YSLib::_tParams
YB_ATTR_nodiscard YB_PURE typename _tParams
Definition: ValueNode.h:1294
platform::SystemOption::MaxSemaphoreNumber
@ MaxSemaphoreNumber
一个进程中可具有的信号量的最大数量。
platform::PDefH
YB_ATTR_nodiscard PDefH(string, MakePathString, u16string_view sv) ImplRet(MakePathString(sv.data())) using FileNodeID
文件节点标识类型。
platform::ynothrowv
YB_ATTR_nodiscard YF_API int amode ynothrowv
Definition: FileIO.h:550
platform::uspawn
YF_API int uspawn(const char *)
执行 UTF-8 字符串的环境命令。
Definition: YCommon.cpp:59
platform::p
return p p
Definition: YCLib/FileSystem.h:209
const
const Decapsulate const Decapsulate &y ynothrow ContextNode &ctx const
Definition: NPLA1Forms.cpp:1629
platform::ID
std::uintmax_t ID
YF_Platform_* 宏替换值的公共类型。
Definition: YCommon.h:54
cassert.h
ISO C 断言/调试跟踪扩展。
TryRet
#define TryRet(...)
指定表达式在 try 块中求值并返回。
Definition: YBaseMacro.h:124
platform::Descriptions::Debug
@ Debug
Definition: YCommon.h:186
platform::SystemOption::MaxSymlinkLoop
@ MaxSymlinkLoop
在路径解析中符号链接可被可靠遍历的最大次数。