YSTest: Host.h 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

Host.h
浏览该文件的文档.
1 /*
2 © 2014-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
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 #include YFM_YSLib_Core_YException // for YSLib::LoggedEvent;
37 #include YFM_YCLib_Reference // for unique_ptr;
38 #include <system_error> // for std::system_error;
39 #if !YCL_Win32
40 # include YFM_YCLib_FileIO // for platform::FileDescriptor::Deleter;
41 #else
42 using HANDLE = void*;
44 #endif
45
46 #if YF_Hosted
47
48 namespace platform_ex
49 {
50
56 class YF_API Exception : public std::system_error
57 {
58 private:
60 YSLib::RecordLevel level = YSLib::Emergent;
61
62 public:
68 YB_NONNULL(3)
69 Exception(std::error_code, const char* = "unknown host exception",
70 YSLib::RecordLevel = YSLib::Emergent);
71 Exception(std::error_code, string_view,
72 YSLib::RecordLevel = YSLib::Emergent);
73 YB_NONNULL(4)
74 Exception(int, const std::error_category&, const char*
75 = "unknown host exception", YSLib::RecordLevel = YSLib::Emergent);
76 Exception(int, const std::error_category&, string_view,
77 YSLib::RecordLevel = YSLib::Emergent);
79 DefDeCopyCtor(Exception)
85 ~Exception() override;
86
88 DefGetter(const ynothrow, YSLib::RecordLevel, Level, level)
89 };
90
91
92 # if YCL_Win32
93
97 struct YF_API HandleDelete
98 {
103 using pointer = ::HANDLE;
104
105 void
106 operator()(pointer) const ynothrow;
107 };
108 # elif YCL_API_Has_unistd_h
109 using HandleDelete = platform::FileDescriptor::Deleter;
111 # else
112 # error "Unsupported platform found."
113 # endif
114
116 using UniqueHandle = unique_ptr_from<HandleDelete>;
117
118
120
121 yconstexpr const size_t DefaultCommandBufferSize(yimpl(4096));
123
133 YF_API YB_NONNULL(1) string
134 FetchCommandOutput(const char*, size_t = DefaultCommandBufferSize);
135
136
138 using CommandCache = unordered_map<string, string>;
139
144 YF_API YSLib::locked_ptr<CommandCache>
145 LockCommandCache();
146
148
149 YF_API const string&
151 FetchCachedCommandResult(const string&, size_t = DefaultCommandBufferSize);
152
154 inline PDefH(string, FetchCachedCommandString, const string& cmd,
155 size_t buf_size = DefaultCommandBufferSize)
156 ImplRet(ystdex::trail(string(FetchCachedCommandResult(cmd, buf_size))))
158
159
165 YF_API pair<UniqueHandle, UniqueHandle>
166 MakePipe();
167
168
179 # if YCL_Win32
180 YF_API YB_NONNULL(1) string
181 DecodeArg(const char*);
182 inline PDefH(string, DecodeArg, string_view arg)
183 ImplRet(DecodeArg(&arg[0]))
184 # endif
185 template<typename _type
186 #if YCL_Win32
187 , yimpl(typename = ystdex::enable_if_t<
188 !std::is_constructible<string_view, _type&&>::value>)
189 #endif
190 >
191 yconstfn auto
192 DecodeArg(_type&& arg) -> decltype(yforward(arg))
193 {
194 return yforward(arg);
195 }
196
197 # if YCL_Win32
198 YF_API YB_NONNULL(1) string
199 EncodeArg(const char*);
200 inline PDefH(string, EncodeArg, const string& arg)
201 ImplRet(EncodeArg(&arg[0]))
202 # endif
203 template<typename _type
204 #if YCL_Win32
205 , yimpl(typename = ystdex::enable_if_t<!std::is_constructible<string,
206 _type&&>::value>)
207 #endif
208 >
209 yconstfn auto
210 EncodeArg(_type&& arg) -> decltype(yforward(arg))
211 {
212 return yforward(arg);
213 }
215
216
217 # if !YCL_Android
218
220
224 class TerminalData;
225
231 class YF_API Terminal
232 {
233 public:
238 class YF_API Guard final
239 {
240 private:
241 Terminal& terminal;
242
243 public:
244 template<typename _func>
245 Guard(Terminal& term, _func f)
246 : terminal(term)
247 {
248 if(term)
249 f(term);
250 }
252 ~Guard();
253 };
254
255 private:
257 unique_ptr<TerminalData> p_term;
258
259 public:
265 Terminal(std::FILE* = stdout);
266 ~Terminal();
267
269 DefBoolNeg(explicit, bool(p_term))
270
271 bool
272 RestoreAttributes();
273
274 bool
275 UpdateForeColor(std::uint8_t);
276 };
278
286 YF_API bool
287 UpdateForeColorByLevel(Terminal&, YSLib::RecordLevel);
288 # endif
289
290 } // namespace platform_ex;
291
292 #endif
293
294 #endif
295
NPL::term
const TermNode &term ValueNode &node const ValueNode &node const TermNode & term
Definition: SContext.h:72
ystdex::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:415
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:653
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
platform::containers::string
basic_string< char > string
Definition: Container.h:101
YModules.h
ystdex::cpp2014::enable_if_t
typename enable_if< _bCond, _type >::type enable_if_t
Definition: type_traits.hpp:350
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:240
platform::Descriptions::Emergent
Definition: YCommon.h:153
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:256
ystdex::trail
_tCon && trail(_tCon &&con, const typename decay_t< _tCon >::value_type &value={})
保证非空容器以参数指定的元素结尾。
Definition: container.hpp:854
std
Definition: memory.hpp:1100
DefDeCopyCtor
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:161
platform_ex
平台扩展命名空间。
Definition: Android.h:45
NPL::Guard
ystdex::any Guard
作用域守护类型。
Definition: NPLA.h:546
ystdex
特性测试宏。
Definition: ydef.h:796
YModules.h
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:654
YSLib::level
YF_API size_t level
Definition: YException.h:141
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:969
DefBoolNeg
#define DefBoolNeg(_spec,...)
定义表示显式转换 bool 操作符及其否定的 operator! 。
Definition: YBaseMacro.h:227
ystdex::locked_ptr
threading::locked_ptr< _type, _tMutex, _tLock > locked_ptr
独占所有权的锁定指针：默认使用标准库互斥量和锁。
Definition: concurrency.h:62
platform::Descriptions::RecordLevel
RecordLevel
记录等级。
Definition: YCommon.h:151
YF_API
#define YF_API
Definition: Platform.h:56
YSLib::IO::f
const string &path const String &path _func f
Definition: YSLib/Service/FileSystem.h:430
platform::bool
std::FILE *fp bool
Definition: FileIO.h:1187
platform::FileDescriptor::Deleter
删除器。
Definition: FileIO.h:178