YSTest: YCoreUtilities.h 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

YCoreUtilities.h
浏览该文件的文档.
1 /*
2 © 2010-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 YSL_INC_Core_YCoreUtilities_h_
29 #define YSL_INC_Core_YCoreUtilities_h_ 1
30
31 #include "YModules.h"
32 #include YFM_YSLib_Core_YException // for LoggedEvent, string, function,
33 // string_view;
34 #include <ystdex/algorithm.hpp> // for ystdex::clamp;
35
36 namespace YSLib
37 {
38
44 template<typename _type>
45 YB_STATELESS yconstfn std::int_fast8_t
46 FetchSign(_type a, _type b = _type(0)) ynothrow
47 {
48 return a < b ? -1 : !(a == b);
49 }
50
60 template<typename _type>
61 YB_STATELESS yconstfn std::int_fast8_t
62 FetchSignFromInterval(_type d, _type a, _type b) ynothrow
63 {
64 return FetchSign(a, d) * FetchSign(d, b);
65 }
66
71 template<typename _type>
72 YB_STATELESS yconstfn _type
73 HalfDifference(_type x, _type y)
74 {
75 return (x - y) / 2;
76 }
77
83 template<typename _type>
84 YB_STATELESS inline bool
85 IsInInterval(_type i, _type b) ynothrow
86 {
87 YAssert(_type(0) < b,
88 "Zero element as lower bound is not less than upper bound.");
89 return !(i < _type(0)) && i < b;
90 }
96 template<typename _type>
97 YB_STATELESS inline bool
98 IsInInterval(_type i, _type a, _type b) ynothrow
99 {
100 YAssert(a < b, "Lower bound is not less than upper bound.");
101 return !(i < a) && i < b;
102 }
103
109 template<typename _type>
110 YB_STATELESS inline bool
111 IsInClosedInterval(_type i, _type b) ynothrow
112 {
113 YAssert(_type(0) < b,
114 "Zero element as lower bound is not less than upper bound.");
115 return !(i < _type(0) || b < i);
116 }
122 template<typename _type>
123 YB_STATELESS inline bool
124 IsInClosedInterval(_type i, _type a, _type b) ynothrow
125 {
126 YAssert(a < b, "Lower bound is not less than upper bound.");
127 return !(i < a || b < i);
128 }
129
135 template<typename _type>
136 YB_STATELESS inline bool
137 IsInOpenInterval(_type i, _type b) ynothrow
138 {
139 YAssert(_type(0) < b,
140 "Zero element as lower bound is not less than upper bound.");
141 return _type(0) < i && i < b;
142 }
148 template<typename _type>
149 YB_STATELESS inline bool
150 IsInOpenInterval(_type i, _type a, _type b) ynothrow
151 {
152 YAssert(a < b, "Lower bound is not less than upper bound.");
153 return a < i && i < b;
154 }
155
166 template<typename _type>
167 YB_PURE YB_NONNULL(2) size_t
168 SwitchInterval(_type v, const _type* a, size_t n) ynothrow
169 {
170 YAssert(n != 0, "Zero length of array found.");
171 YAssert(!(v < Deref(a)), "Value less than lower bound found.");
172
173 size_t i(0);
174
175 while(!(++i == n || v < a[i]))
176 ;
177 return i - 1;
178 }
179
184 template<typename _type>
185 YB_PURE YB_NONNULL(2) size_t
186 SwitchAddedInterval(_type v, const _type* a, size_t n) ynothrow
187 {
188 YAssert(n != 0, "Zero length of array found.");
189 YAssert(!(v < Deref(a)), "Value less than lower bound found.");
190
191 _type s(*a);
192 size_t i(0);
193
194 while(!(++i == n || v < (s += a[i])))
195 ;
196 return i - 1;
197 }
199
206 template<typename _type>
207 void
208 RestrictInClosedInterval(_type& v, const _type& a, const _type& b) ynothrow
209 {
210 v = ystdex::clamp(v, a, b);
211 }
212
219 template<typename _type>
220 void
221 RestrictInInterval(_type& i, const _type& a, const _type& b) ynothrow
222 {
223 YAssert(a < b, "Lower bound is not less than upper bound.");
224 if(i < a)
225 i = a;
226 else if(!(i < b))
227 i = b - 1;
228 }
229
235 template<typename _type>
236 void
237 RestrictUnsignedStrict(_type& u, _type b) ynothrow
238 {
239 static_assert(std::is_unsigned<_type>(), "Invalid type found.");
240
241 if(b < u)
242 u = b;
243 }
244
251 template<typename _type>
252 void
253 RestrictUnsigned(_type& u, unsigned b) ynothrow
254 {
255 YAssert(b != _type(0), "Zero upper bound found.");
256 if(!(u < b))
257 u = b - 1;
258 }
259
265 template<typename _type>
266 inline void
267 RestrictLessEqual(_type& a, _type& b) ynothrow
268 {
269 if(b < a)
270 std::swap(a, b);
271 }
272
273
282 template<typename _tDst, typename _type>
284 inline _tDst
285 CheckLowerBound(_type val, const string& name = {}, RecordLevel lv = Err)
286 {
287 using namespace ystdex;
288 // XXX: See WG21 N3387.
289 // TODO: Add and use safe %common_arithmetic_type interface instead?
290 using common_t = typename ystdex::common_int_type<_tDst, _type>::type;
291
292 if(!(common_t(val) < common_t(std::numeric_limits<_tDst>::min())))
293 return _tDst(val);
294 throw
295 LoggedEvent("Value of '" + name + "' is less than lower boundary.", lv);
296 }
297
299 template<typename _tDst, typename _type>
300 inline _tDst
301 CheckUpperBound(_type val, const string& name = {}, RecordLevel lv = Err)
302 {
303 using namespace ystdex;
304 // XXX: See WG21 N3387.
305 // TODO: Add and use safe %common_arithmetic_type interface instead?
306 // TODO: Add direct integer rank comparison?
307 using common_t = typename ystdex::common_int_type<_tDst, _type>::type;
308
309 if((std::is_signed<common_t>() && std::is_unsigned<_tDst>()
310 && ystdex::integer_width<common_t>() <= ystdex::integer_width<_tDst>())
311 || !(common_t(std::numeric_limits<common_t>::max()) < common_t(val)))
312 return _tDst(val);
313 throw LoggedEvent("Value of '" + name + "' is greater than upper boundary.",
314 lv);
315 }
316
318 template<typename _tDst, typename _type>
319 inline _tDst
320 CheckArithmetic(_type val, const string& name = {}, RecordLevel lv = Err)
321 {
322 return
323 CheckUpperBound<_tDst>(CheckLowerBound<_tDst>(val, name, lv), name, lv);
324 }
325
327 template<typename _tDst, typename _type>
328 inline _tDst
329 CheckNonnegative(_type val, const string& name = {}, RecordLevel lv = Err)
330 {
331 if(val < 0)
332 // XXX: Use more specified exception type.
333 throw LoggedEvent("Failed getting nonnegative " + name + " value.", lv);
334 return CheckUpperBound<_tDst>(val, name, lv);
335 }
336
338 template<typename _tDst, typename _type>
339 inline _tDst
340 CheckPositive(_type val, const string& name = {}, RecordLevel lv = Err)
341 {
342 if(!(0 < val))
343 // XXX: Use more specified exception type.
344 throw LoggedEvent("Failed getting positive " + name + " value.", lv);
345 return CheckUpperBound<_tDst>(val, name, lv);
346 }
348
349
357 template<typename _tOut>
358 inline void
359 ClearSequence(_tOut dst, size_t n) ynothrowv
360 {
361 using _type = ystdex::decay_t<decltype(*dst)>;
362 static_assert(std::is_trivial<_type>()
363 || (std::is_nothrow_default_constructible<_type>()
364 && std::is_nothrow_assignable<_type, _type>()), "Invalid type found.");
365
366 std::fill_n(dst, n, _type());
367 }
368
369
376 YF_API void
377 PerformKeyAction(function<void()>, const char*, const char*, string_view);
378
379
386 class YF_API ArgumentsVector final
387 {
388 public:
389 vector<string> Arguments{};
390
401 void
402 Reset(int, char*[]);
403 };
404
411 YF_API locked_ptr<ArgumentsVector, recursive_mutex>
412 LockCommandArguments();
413
414
422 YF_API YB_NONNULL(2) bool
423 FetchEnvironmentVariable(string&, const char*);
424
425
427
428 yconstexpr const size_t DefaultCommandBufferSize(yimpl(4096));
430
442 YF_API YB_NONNULL(1) pair<string, int>
443 FetchCommandOutput(const char*, size_t = DefaultCommandBufferSize);
444
445
447 using CommandCache = unordered_map<string, string>;
448
453 YF_API locked_ptr<CommandCache>
454 LockCommandCache();
456
462 YF_API const string&
463 FetchCachedCommandResult(string_view, size_t = DefaultCommandBufferSize);
464
465 } // namespace YSLib;
466
467 #endif
468
YSLib::ClearSequence
void ClearSequence(_tOut dst, size_t n) ynothrowv
清除指定的连续对象。
Definition: YCoreUtilities.h:359
YSLib::RestrictInInterval
void RestrictInInterval(_type &i, const _type &a, const _type &b) ynothrow
约束整数 i 在左闭右开区间 [a, b) 中。
Definition: YCoreUtilities.h:221
YSLib::RestrictInClosedInterval
void RestrictInClosedInterval(_type &v, const _type &a, const _type &b) ynothrow
约束 v 在闭区间 [a, b] 中。
Definition: YCoreUtilities.h:208
x
const EncapsulationBase & x
Definition: NPLA1Forms.cpp:1536
ystdex::cpp2017::swap
void swap(optional< _type > &x, optional< _type > &y)
Definition: optional.h:888
YSLib::FetchSignFromInterval
YB_STATELESS yconstfn std::int_fast8_t FetchSignFromInterval(_type d, _type a, _type b) ynothrow
判断 d 和以 [a, b](a ≤ b) 或 [b, a](a > b) 区间的关系。
Definition: YCoreUtilities.h:62
YSLib::RestrictLessEqual
void RestrictLessEqual(_type &a, _type &b) ynothrow
约束关系：a ≤ b 。
Definition: YCoreUtilities.h:267
YSLib::CheckUpperBound
_tDst CheckUpperBound(_type val, const string &name={}, RecordLevel lv=Err)
检查算术类型数值不大于指定类型的上界。
Definition: YCoreUtilities.h:301
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:1072
YB_PURE
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:919
YSLib::IsInOpenInterval
YB_STATELESS bool IsInOpenInterval(_type i, _type b) ynothrow
判断 i 是否在开区间 (_type(0), b) 内。
Definition: YCoreUtilities.h:137
ystdex::cpp2014::min
const _type & min(const _type &a, const _type &b, _fComp comp=less< _type >())
取较小的元素。
Definition: algorithm.hpp:614
ystdex::cpp2017::basic_string_view
Definition: string_view.hpp:95
YSLib::LockCommandCache
YF_API locked_ptr< CommandCache > LockCommandCache()
锁定命令执行缓冲区。
Definition: YCoreUtilities.cpp:114
ystdex::cpp2017::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:437
YSLib::FetchCachedCommandResult
YF_API const string & FetchCachedCommandResult(string_view, size_t=DefaultCommandBufferSize)
取缓冲的命令执行结果。
Definition: YCoreUtilities.cpp:123
CHRLib::d
_tDst * d
Definition: CharacterProcessing.h:142
ystdex::cpp2014::max
const _type & max(const _type &a, const _type &b, _fComp comp=less< _type >())
取较大的元素。
Definition: algorithm.hpp:635
YSLib::PerformKeyAction
YF_API void PerformKeyAction(function< void()>, const char *, const char *, string_view)
执行关键动作。
Definition: YCoreUtilities.cpp:38
YF_API
#define YF_API
Definition: Platform.h:56
YSLib::CheckArithmetic
_tDst CheckArithmetic(_type val, const string &name={}, RecordLevel lv=Err)
检查算术类型数值在指定类型的范围内。
Definition: YCoreUtilities.h:320
YSLib::CommandCache
unordered_map< string, string > CommandCache
命令和命令执行结果的缓冲区类型。
Definition: YCoreUtilities.h:447
YSLib::FetchCommandOutput
YF_API int FetchCommandOutput(const char *, size_t=DefaultCommandBufferSize)
Definition: YCoreUtilities.cpp:89
ystdex::integer_width
取指定整数类型的位宽度。
Definition: cstdint.hpp:104
YSLib::locked_ptr
unique_ptr< _type, unlock_delete< _tMutex, _tLock > > locked_ptr
独占所有权的锁定指针：使用线程模型对应的互斥量和锁以及 unique_ptr 。
Definition: YShellDefinition.h:108
YSLib::CheckNonnegative
_tDst CheckNonnegative(_type val, const string &name={}, RecordLevel lv=Err)
检查非负算术类型数值在指定类型的范围内。
Definition: YCoreUtilities.h:329
ystdex::cpp2014::decay_t
typename decay< _type >::type decay_t
Definition: meta.hpp:196
YSLib::lv
const char RecordLevel lv
Definition: YException.cpp:61
YSLib::FetchEnvironmentVariable
bool FetchEnvironmentVariable(string &res, const char *var)
Definition: YCoreUtilities.cpp:77
YSLib::YAssert
YAssert(!(v< Deref(a)), "Value less than lower bound found.")
YB_STATELESS
#define YB_STATELESS
指示函数或函数模板实例为无状态函数。
Definition: ydef.h:941
YSLib::ArgumentsVector
参数字符串向量。
Definition: YCoreUtilities.h:387
platform::containers::unordered_map
std::unordered_map< _tKey, _tMapped, _fHash, _fEqual, _tAlloc > unordered_map
Definition: Container.h:133
YSLib::yimpl
YF_API yimpl(GUIApplication &) FetchGlobalInstance()
取全局应用程序实例。
YSLib::i
return i
Definition: YCoreUtilities.h:177
platform::Descriptions::Err
@ Err
Definition: YCommon.h:182
platform::containers::vector
std::vector< _type, _tAlloc > vector
Definition: Container.h:102
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
YSLib::HalfDifference
YB_STATELESS yconstfn _type HalfDifference(_type x, _type y)
计算指定类型的差值的一半。
Definition: YCoreUtilities.h:73
YSLib::RestrictUnsigned
void RestrictUnsigned(_type &u, unsigned b) ynothrow
约束无符号整数 u 在左闭右开区间 [0, b) 中。
Definition: YCoreUtilities.h:253
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:143
YModules.h
YSLib 用户界面模块配置文件。
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:169
YSLib::RecordLevel
YF_API const char RecordLevel
Definition: YException.h:148
ystdex::cpp2017::clamp
const _type & clamp(const _type &v, const _type &lo, const _type &hi, _fComp comp)
Definition: algorithm.hpp:674
YSLib::IsInInterval
YB_STATELESS bool IsInInterval(_type i, _type b) ynothrow
判断 i 是否在左闭右开区间 [_type(0), b) 中。
Definition: YCoreUtilities.h:85
NPL::A1::name
const char * name
Definition: NPLA1.h:1876
ystdex::locked_ptr
threading::locked_ptr< _type, _tMutex, _tLock > locked_ptr
独占所有权的锁定指针：默认使用标准库互斥量和锁。
Definition: concurrency.h:61
platform::Deref
YB_PURE yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:547
YSLib::IsInClosedInterval
YB_STATELESS bool IsInClosedInterval(_type i, _type b) ynothrow
判断 i 是否在闭区间 [_type(0), b] 中。
Definition: YCoreUtilities.h:111
v
byte v
Definition: CharRenderer.cpp:76
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:1219
YSLib::CheckLowerBound
_tDst CheckLowerBound(_type val, const string &name={}, RecordLevel lv=Err)
检查算术类型数值不小于指定类型的下界。
Definition: YCoreUtilities.h:285
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
YSLib::RestrictUnsignedStrict
void RestrictUnsignedStrict(_type &u, _type b) ynothrow
约束无符号整数 u 在区间上界 b 内。
Definition: YCoreUtilities.h:237
YSLib::YB_NONNULL
YB_ATTR_nodiscard YF_API YB_NONNULL(1, 2) ValueNode LoadNPLA1File(const char *disp
载入 NPLA1 配置文件。
YSLib::DefaultCommandBufferSize
YF_API const char *yconstexpr const size_t DefaultCommandBufferSize(yimpl(4096))
默认命令缓冲区大小。
ystdex::common_int_type
公共整数类型。
Definition: cstdint.hpp:221
YSLib::size_t
YF_API const char size_t
Definition: YException.h:149
const
const Decapsulate const Decapsulate &y ynothrow ContextNode &ctx const
Definition: NPLA1Forms.cpp:1629
platform::bool
YB_ATTR_nodiscard std::FILE *fp bool
Definition: FileIO.h:1271
NPL::dst
YF_API const char src const string const char * dst
Definition: Dependency.h:96
YSLib::CheckPositive
_tDst CheckPositive(_type val, const string &name={}, RecordLevel lv=Err)
检查正算术类型数值在指定类型的范围内。
Definition: YCoreUtilities.h:340
YSLib::LockCommandArguments
locked_ptr< ArgumentsVector, recursive_mutex > LockCommandArguments()
Definition: YCoreUtilities.cpp:67
YSLib::FetchSign
YB_STATELESS yconstfn std::int_fast8_t FetchSign(_type a, _type b=_type(0)) ynothrow
符号函数。
Definition: YCoreUtilities.h:46
platform_ex::Reset
unsigned long Reset(COMPtr< _iCOM > &ptr) ynothrow
Definition: Win32/include/YCLib/COM.h:406
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:1073
YSLib::a
YB_PURE const _type * a
Definition: YCoreUtilities.h:168
algorithm.hpp
泛型算法。