YSTest: Host.h 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

Host.h
浏览该文件的文档.
1 /*
2 © 2014-2019 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
30 #ifndef YCL_INC_Host_h_
31 #define YCL_INC_Host_h_ 1
32
33 #include "YCLib/YModules.h"
34 #include "YSLib/Core/YModules.h"
35 #include YFM_YCLib_Container // for unordered_map, pair, string_view, string,
36 // ystdex::invoke;
37 #include YFM_YSLib_Core_YException // for YSLib::LoggedEvent;
38 #include YFM_YCLib_Reference // for unique_ptr_from, unique_ptr, observer_ptr,
39 // tidy_ptr, make_observer;
40 #include <system_error> // for std::system_error;
41 #include <cstdio> // for std::FILE;
42 #if !YCL_Win32
43 # include YFM_YCLib_FileIO // for platform::FileDescriptor::Deleter;
44 #else
45 using HANDLE = void*;
47 #endif
48
49 #if YF_Hosted
50 namespace platform_ex
51 {
52
58 class YF_API Exception : public std::system_error
59 {
60 private:
62 YSLib::RecordLevel level = YSLib::Emergent;
63
64 public:
70 YB_NONNULL(3)
71 Exception(std::error_code, const char* = "unknown host exception",
72 YSLib::RecordLevel = YSLib::Emergent);
74 Exception(std::error_code, const std::string&,
75 YSLib::RecordLevel = YSLib::Emergent);
76 Exception(std::error_code, string_view,
77 YSLib::RecordLevel = YSLib::Emergent);
78 YB_NONNULL(4)
79 Exception(int, const std::error_category&, const char*
80 = "unknown host exception", YSLib::RecordLevel = YSLib::Emergent);
82 Exception(int, const std::error_category&, const std::string&,
83 YSLib::RecordLevel = YSLib::Emergent);
84 Exception(int, const std::error_category&, string_view,
85 YSLib::RecordLevel = YSLib::Emergent);
87 DefDeCopyCtor(Exception)
93 ~Exception() override;
94
96 DefGetter(const ynothrow, YSLib::RecordLevel, Level, level)
97 };
98
99
100 # if YCL_Win32
101
106 struct YF_API HandleDelete
107 {
112 using pointer = ::HANDLE;
113
115 void
116 operator()(pointer) const ynothrowv;
117 };
118 # elif YCL_API_Has_unistd_h
119 using HandleDelete = platform::FileDescriptor::Deleter;
121 # else
122 # error "Unsupported platform found."
123 # endif
124
126 using UniqueHandle = unique_ptr_from<HandleDelete>;
127
128
136 class YF_API Semaphore : private ystdex::noncopyable
137 {
138 public:
139 #if YCL_Win32
140 using CountType = long;
141 #else
142 using CountType = unsigned;
143 #endif
144
145 private:
146 #if YCL_Win32
147 using Deleter = HandleDelete;
148 #else
149 class YF_API Deleter
150 {
151 public:
152 using pointer = void*;
153
154 observer_ptr<Semaphore> Referent;
155
160 DefDeCtor(Deleter)
161 void
162 operator()(pointer) const ynothrowv;
163 };
164 #endif
165
166 public:
167 using native_handle_type = typename Deleter::pointer;
168
169 private:
170 #if !YCL_Win32
171 string name{};
172 #endif
173 unique_ptr_from<Deleter> h_sem;
174
175 public:
189 YB_NONNULL(2)
190 Semaphore(const char*, CountType = 1);
191 YB_NONNULL(2)
192 Semaphore(const char16_t*, CountType = 1);
194
196 void
197 lock();
198
200 bool
201 try_lock() ynothrow;
202
207 void
208 unlock() ynothrow;
209
215 PDefH(native_handle_type, native_handle, )
216 ImplRet(h_sem.get())
217 };
218
219
229 YF_API pair<UniqueHandle, UniqueHandle>
230 MakePipe();
231
232
246 # if YCL_Win32
247 YF_API YB_NONNULL(1) string
248 DecodeArg(const char*);
249 inline PDefH(string, DecodeArg, string_view arg)
250 ImplRet(DecodeArg(&arg[0]))
251 # endif
252 template<typename _type
253 #if YCL_Win32
254 , yimpl(typename = ystdex::enable_if_t<
255 !std::is_constructible<string_view, _type>::value>)
256 #endif
257 >
258 yconstfn auto
259 DecodeArg(_type&& arg) -> decltype(yforward(arg))
260 {
261 return yforward(arg);
262 }
263
264 # if YCL_Win32
265 YF_API YB_NONNULL(1) string
266 EncodeArg(const char*);
268 inline PDefH(string, EncodeArg, string_view arg)
269 ImplRet(EncodeArg(arg.data()))
270 # endif
271 template<typename _type
272 #if YCL_Win32
273 , yimpl(typename = ystdex::enable_if_t<
274 !std::is_constructible<string_view, _type>::value>)
275 #endif
276 >
277 yconstfn auto
278 EncodeArg(_type&& arg) -> decltype(yforward(arg))
279 {
280 return yforward(arg);
281 }
283
284
286
287
291 class TerminalData;
292
300 class YF_API Terminal
301 {
302 public:
307 class YF_API Guard final
308 {
309 private:
310 tidy_ptr<Terminal> p_terminal;
311
312 public:
314 template<typename _fCallable, typename... _tParams>
315 Guard(Terminal& te, _fCallable f, _tParams&&... args)
316 : p_terminal(make_observer(&te))
317 {
318 if(te)
319 ystdex::invoke(f, te, yforward(args)...);
320 }
322 DefDeMoveCtor(Guard)
324 ~Guard();
325
327 DefDeMoveAssignment(Guard)
328
329
333 DefCvt(const ynothrow, ystdex::add_ptr_t<
334 std::ios_base&(std::ios_base&)>, ystdex::id<std::ios_base&>())
335 };
336
337 private:
339 unique_ptr<TerminalData> p_data;
340
341 public:
347 Terminal(std::FILE* = stdout);
348 ~Terminal();
349
351 DefBoolNeg(explicit, bool(p_data))
352
353
354
355 bool
357 Clear();
358
363 Guard
364 LockForeColor(std::uint8_t);
366
367 bool
368 RestoreAttributes();
369
370 bool
371 UpdateForeColor(std::uint8_t);
372 };
374
382 YF_API bool
383 UpdateForeColorByLevel(Terminal&, YSLib::RecordLevel);
384
385 } // namespace platform_ex;
386 #endif
387
388 #endif
389
DefDeCtor
#define DefDeCtor(_t)
简单通用成员函数定义。
Definition: YBaseMacro.h:157
ystdex::single_thread::try_lock
int try_lock(_tLock1 &&, _tLock2 &&, _tLocks &&...)
Definition: pseudo_mutex.h:983
NPL::A1::Guard
any Guard
作用域守卫类型。
Definition: NPLA1.h:269
ystdex::invoke
invoke_result_t< _fCallable, _tParams... > invoke(_fCallable &&f, _tParams &&... args)
调用可调用对象。
Definition: invoke.hpp:538
platform_ex::Windows::PDefH
YB_ATTR_nodiscard YB_STATELESS yconstfn PDefH(bool, IsDirectory, FileAttributes attr) ynothrow ImplRet(attr &Directory) YB_ATTR_nodiscard YB_STATELESS inline PDefH(bool
判断 FileAttributes 是否指定目录。
platform_ex
平台扩展命名空间。
Definition: Android.h:46
platform::Descriptions::Emergent
@ Emergent
Definition: YCommon.h:179
platform::Descriptions::RecordLevel
RecordLevel
记录等级。
Definition: YCommon.h:178
ystdex::cpp2017::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:437
DefDeMoveCtor
#define DefDeMoveCtor(_t)
Definition: YBaseMacro.h:167
ystdex::cpp2014::enable_if_t
typename enable_if< _bCond, _type >::type enable_if_t
Definition: meta.hpp:199
YModules.h
YSLib 核心模块配置文件。
platform_ex::YB_NONNULL
YB_ATTR_nodiscard YB_NONNULL(1) inline PDefH(u16string
构造适合表示路径的 char16_t 字符串。
YF_API
#define YF_API
Definition: Platform.h:56
CHRLib::f
f(d, s)
platform_ex::Windows::unsigned
YF_API unsigned
Definition: NLS.h:52
CHRLib::args
YB_ATTR_nodiscard _tParams && args
Definition: CharacterProcessing.h:649
DefBoolNeg
#define DefBoolNeg(_spec,...)
定义表示显式转换 bool 操作符及其否定的 operator! 。
Definition: YBaseMacro.h:228
ystdex::make_observer
observer_ptr< _type > make_observer(_type *p)
Definition: pointer.hpp:405
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 转发对应参数。
Definition: ydef.h:1508
ystdex::cpp2017::data
auto data(_tRange &c) -> decltype(c.data())
Definition: range.hpp:581
std
Definition: optional.h:982
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
NPL::A1::name
const char * name
Definition: NPLA1.h:1876
platform::FileDescriptor::Deleter
删除器。
Definition: FileIO.h:156
CHRLib::char16_t
YB_ATTR_nodiscard YB_PURE char16_t((*c_ptr<< CHAR_BIT)|c_ptr[1])) YB_ATTR_nodiscard YB_PURE inline PDefH(char16_t
取 c_ptr 指向的小端序双字节字符。
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:1219
YSLib::_tParams
YB_ATTR_nodiscard YB_PURE typename _tParams
Definition: ValueNode.h:1294
DefDeCopyCtor
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:162
YModules.h
YCLib 库模块配置文件。
ystdex::id
恒等仿函数。
Definition: functor.hpp:94
DefCvt
#define DefCvt(_q, _t,...)
Definition: YBaseMacro.h:206
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:241
const
const Decapsulate const Decapsulate &y ynothrow ContextNode &ctx const
Definition: NPLA1Forms.cpp:1629
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:413
ystdex::single_thread::lock
void lock(_tLock1 &&, _tLock2 &&, _tLocks &&...)
Definition: pseudo_mutex.h:990
ystdex::add_ptr_t
_type * add_ptr_t
Definition: type_traits.hpp:53
DefDeMoveAssignment
#define DefDeMoveAssignment(_t)
Definition: YBaseMacro.h:191
ystdex::cpp2017::string
basic_string< char > string
Definition: string.hpp:772
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:1199
ystdex::bases::noncopyable
不可复制对象：禁止派生类调用默认原型的复制构造函数和复制赋值操作符。
Definition: base.h:50
platform_ex::ImplRet
YB_ATTR_nodiscard const char16_t *s ImplRet(platform::Nonnull(s)) YB_ATTR_nodiscard inline PDefH(const u16string &
NPL::get
PDefH(void, LiftTermRef, TermNode &term, const TermNode &tm) ImplExpr(NPL ref get()
Definition: NPLA.h:1737
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:1073
YSLib::level
YF_API size_t level
Definition: YException.h:156