YSTest: Host.h 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

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

- 文件列表
- 文件成员

Host.h
浏览该文件的文档.
1 /*
2 © 2014-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
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
51 namespace platform_ex
52 {
53
59 class YF_API Exception : public std::system_error
60 {
61 private:
63 YSLib::RecordLevel level = YSLib::Emergent;
64
65 public:
71 YB_NONNULL(3)
72 Exception(std::error_code, const char* = "unknown host exception",
73 YSLib::RecordLevel = YSLib::Emergent);
74 Exception(std::error_code, string_view,
75 YSLib::RecordLevel = YSLib::Emergent);
76 YB_NONNULL(4)
77 Exception(int, const std::error_category&, const char*
78 = "unknown host exception", YSLib::RecordLevel = YSLib::Emergent);
79 Exception(int, const std::error_category&, string_view,
80 YSLib::RecordLevel = YSLib::Emergent);
82 DefDeCopyCtor(Exception)
88 ~Exception() override;
89
91 DefGetter(const ynothrow, YSLib::RecordLevel, Level, level)
92 };
93
94
96
97
102 YF_API YB_NONNULL(1) int
103 upclose(std::FILE*) ynothrowv;
104
106
107
115 YF_API YB_NONNULL(1, 2) std::FILE*
117 upopen(const char* filename, const char* mode) ynothrowv;
119 YF_API YB_NONNULL(1, 2) std::FILE*
120 upopen(const char16_t* filename, const char16_t* mode) ynothrowv;
122
123
124
132 YF_API YB_NONNULL(1, 2) void
133 SetEnvironmentVariable(const char*, const char*);
134
135
136 # if YCL_Win32
137
142 struct YF_API HandleDelete
143 {
148 using pointer = ::HANDLE;
149
151 void
152 operator()(pointer) const ynothrowv;
153 };
154 # elif YCL_API_Has_unistd_h
155 using HandleDelete = platform::FileDescriptor::Deleter;
157 # else
158 # error "Unsupported platform found."
159 # endif
160
162 using UniqueHandle = unique_ptr_from<HandleDelete>;
163
164
172 class YF_API Semaphore : private ystdex::noncopyable
173 {
174 public:
175 #if YCL_Win32
176 using CountType = long;
177 #else
178 using CountType = unsigned;
179 #endif
180
181 private:
182 #if YCL_Win32
183 using Deleter = HandleDelete;
184 #else
185 class YF_API Deleter
186 {
187 public:
188 using pointer = void*;
189
190 observer_ptr<Semaphore> Referent;
191
196 DefDeCtor(Deleter)
197 void
198 operator()(pointer) const ynothrowv;
199 };
200 #endif
201
202 public:
203 using native_handle_type = typename Deleter::pointer;
204
205 private:
206 #if !YCL_Win32
207 string name{};
208 #endif
209 unique_ptr_from<Deleter> h_sem;
210
211 public:
225 YB_NONNULL(2)
226 Semaphore(const char*, CountType = 1);
227 YB_NONNULL(2)
228 Semaphore(const char16_t*, CountType = 1);
230
232 void
233 lock();
234
236 bool
237 try_lock() ynothrow;
238
243 void
244 unlock() ynothrow;
245
251 PDefH(native_handle_type, native_handle, )
252 ImplRet(h_sem.get())
253 };
254
255
257
258 yconstexpr const size_t DefaultCommandBufferSize(yimpl(4096));
260
271 YF_API YB_NONNULL(1) pair<string, int>
272 FetchCommandOutput(const char*, size_t = DefaultCommandBufferSize);
273
274
276 using CommandCache = unordered_map<string, string>;
277
282 YF_API YSLib::locked_ptr<CommandCache>
283 LockCommandCache();
284
286
287 YF_API const string&
289 FetchCachedCommandResult(const string&, size_t = DefaultCommandBufferSize);
290
292 inline PDefH(string, FetchCachedCommandString, const string& cmd,
293 size_t buf_size = DefaultCommandBufferSize)
294 ImplRet(ystdex::trail(string(FetchCachedCommandResult(cmd, buf_size))))
296
297
307 YF_API pair<UniqueHandle, UniqueHandle>
308 MakePipe();
309
310
324 # if YCL_Win32
325 YF_API YB_NONNULL(1) string
326 DecodeArg(const char*);
327 inline PDefH(string, DecodeArg, string_view arg)
328 ImplRet(DecodeArg(&arg[0]))
329 # endif
330 template<typename _type
331 #if YCL_Win32
332 , yimpl(typename = ystdex::enable_if_t<
333 !std::is_constructible<string_view, _type&&>::value>)
334 #endif
335 >
336 yconstfn auto
337 DecodeArg(_type&& arg) -> decltype(yforward(arg))
338 {
339 return yforward(arg);
340 }
341
342 # if YCL_Win32
343 YF_API YB_NONNULL(1) string
344 EncodeArg(const char*);
346 inline PDefH(string, EncodeArg, string_view arg)
347 ImplRet(EncodeArg(arg.data()))
348 # endif
349 template<typename _type
350 #if YCL_Win32
351 , yimpl(typename = ystdex::enable_if_t<!std::is_constructible<string_view,
352 _type&&>::value>)
353 #endif
354 >
355 yconstfn auto
356 EncodeArg(_type&& arg) -> decltype(yforward(arg))
357 {
358 return yforward(arg);
359 }
361
362
363 # if !YCL_Android
364
366
370 class TerminalData;
371
379 class YF_API Terminal
380 {
381 public:
386 class YF_API Guard final
387 {
388 private:
389 tidy_ptr<Terminal> p_terminal;
390
391 public:
393 template<typename _fCallable, typename... _tParams>
394 Guard(Terminal& te, _fCallable f, _tParams&&... args)
395 : p_terminal(make_observer(&te))
396 {
397 if(te)
398 ystdex::invoke(f, te, yforward(args)...);
399 }
401 DefDeMoveCtor(Guard)
403 ~Guard();
404
406 DefDeMoveAssignment(Guard)
407
408
412 DefCvt(const ynothrow, ystdex::add_ptr_t<
413 std::ios_base&(std::ios_base&)>, ystdex::id<std::ios_base&>())
414 };
415
416 private:
418 unique_ptr<TerminalData> p_data;
419
420 public:
426 Terminal(std::FILE* = stdout);
427 ~Terminal();
428
430 DefBoolNeg(explicit, bool(p_data))
431
433
434 bool
436 Clear();
437
442 Guard
443 LockForeColor(std::uint8_t);
445
446 bool
447 RestoreAttributes();
448
449 bool
450 UpdateForeColor(std::uint8_t);
451 };
453
461 YF_API bool
462 UpdateForeColorByLevel(Terminal&, YSLib::RecordLevel);
463 # endif
464
465 } // namespace platform_ex;
466
467 #endif
468
469 #endif
470
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
DefBoolNeg
#define DefBoolNeg(_spec,...)
定义表示显式转换 bool 操作符及其否定的 operator! 。
Definition: YBaseMacro.h:228
ystdex::make_observer
observer_ptr< _type > make_observer(_type *p)
Definition: pointer.hpp:326
CHRLib::f
f(d, s)
ystdex::add_ptr_t
_type * add_ptr_t
Definition: type_traits.hpp:1021
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:262
DefDeMoveCtor
#define DefDeMoveCtor(_t)
Definition: YBaseMacro.h:167
platform::Descriptions::Emergent
Definition: YCommon.h:163
ystdex::cpp2014::enable_if_t
typename enable_if< _bCond, _type >::type enable_if_t
Definition: type_traits.hpp:343
CHRLib::args
_tParams && args
Definition: CharacterProcessing.h:538
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:679
ystdex::data
auto data(_tRange &c) -> decltype(c.data())
Definition: range.hpp:280
NPL::Guard
ystdex::any Guard
作用域守护类型。
Definition: NPLA.h:958
platform_ex::Windows::unsigned
YF_API unsigned
Definition: NLS.h:52
ystdex::trail
_tCon && trail(_tCon &&con, const typename decay_t< _tCon >::value_type &value={})
保证非空容器以参数指定的元素结尾。
Definition: container.hpp:858
platform::Descriptions::RecordLevel
RecordLevel
记录等级。
Definition: YCommon.h:161
YSLib::level
YF_API size_t level
Definition: YException.h:142
DefDeMoveAssignment
#define DefDeMoveAssignment(_t)
Definition: YBaseMacro.h:191
DefDeCtor
#define DefDeCtor(_t)
简单通用成员函数定义。
Definition: YBaseMacro.h:157
platform_ex::ImplRet
const char16_t *s ImplRet(platform::Nonnull(s)) inline PDefH(const u16string &
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:766
ystdex
特性测试宏。
Definition: ydef.h:822
YF_API
#define YF_API
Definition: Platform.h:56
YModules.h
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:786
platform_ex::YB_NONNULL
YB_NONNULL(1) PDefH(u16string
构造适合表示路径的 char16_t 字符串。
std
Definition: memory.hpp:1183
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:241
platform::FileDescriptor::Deleter
删除器。
Definition: FileIO.h:153
DefCvt
#define DefCvt(_q, _t,...)
Definition: YBaseMacro.h:206
ystdex::locked_ptr
threading::locked_ptr< _type, _tMutex, _tLock > locked_ptr
独占所有权的锁定指针：默认使用标准库互斥量和锁。
Definition: concurrency.h:61
YModules.h
DefDeCopyCtor
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:162
ystdex::single_thread::try_lock
int try_lock(_tLock1 &&, _tLock2 &&, _tLocks &&...)
Definition: pseudo_mutex.h:983
platform_ex
平台扩展命名空间。
Definition: Android.h:45
YSLib::UI::name
yconstfn const string & name
Definition: Loader.h:130
ystdex::noncopyable
不可复制对象：禁止派生类调用默认原型的复制构造函数和复制赋值操作符。
Definition: base.h:41
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:995
ystdex::invoke
result_of_t< _fCallable &&(_tParams &&...)> invoke(_fCallable &&f, _tParams &&...args)
调用可调用对象。
Definition: functional.hpp:339
ystdex::single_thread::lock
void lock(_tLock1 &&, _tLock2 &&, _tLocks &&...)
Definition: pseudo_mutex.h:990
CHRLib::char16_t
char16_t((*c_ptr<< CHAR_BIT)|c_ptr[1])) inline PDefH(char16_t
取 c_ptr 指向的小端序双字节字符。
ystdex::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:415
ystdex::details::get
get())
Definition: cast.hpp:102