YSTest: YCoreUtilities.h 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

YCoreUtilities.h
浏览该文件的文档.
1 /*
2 © 2010-2015 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 std::string, LoggedEvent, string;
33 #include <ystdex/algorithm.hpp> // for ystdex::clamp;
34
35 namespace YSLib
36 {
37
43 template<typename _type>
44 yconstfn YB_STATELESS std::int_fast8_t
45 FetchSign(_type a, _type b = _type(0)) ynothrow
46 {
47 return a < b ? -1 : !(a == b);
48 }
49
59 template<typename _type>
60 yconstfn YB_STATELESS std::int_fast8_t
61 FetchSignFromInterval(_type d, _type a, _type b) ynothrow
62 {
63 return FetchSign(a, d) * FetchSign(d, b);
64 }
65
70 template<typename _type>
71 yconstfn YB_STATELESS _type
72 HalfDifference(_type x, _type y)
73 {
74 return (x - y) / 2;
75 }
76
82 template<typename _type>
83 inline YB_STATELESS bool
84 IsInInterval(_type i, _type b) ynothrow
85 {
86 YAssert(_type(0) < b,
87 "Zero element as lower bound is not less than upper bound.");
88 return !(i < _type(0)) && i < b;
89 }
95 template<typename _type>
96 inline YB_STATELESS bool
97 IsInInterval(_type i, _type a, _type b) ynothrow
98 {
99 YAssert(a < b, "Lower bound is not less than upper bound.");
100 return !(i < a) && i < b;
101 }
102
108 template<typename _type>
109 inline YB_STATELESS bool
110 IsInClosedInterval(_type i, _type b) ynothrow
111 {
112 YAssert(_type(0) < b,
113 "Zero element as lower bound is not less than upper bound.");
114 return !(i < _type(0) || b < i);
115 }
121 template<typename _type>
122 inline YB_STATELESS bool
123 IsInClosedInterval(_type i, _type a, _type b) ynothrow
124 {
125 YAssert(a < b, "Lower bound is not less than upper bound.");
126 return !(i < a || b < i);
127 }
128
134 template<typename _type>
135 inline YB_STATELESS bool
136 IsInOpenInterval(_type i, _type b) ynothrow
137 {
138 YAssert(_type(0) < b,
139 "Zero element as lower bound is not less than upper bound.");
140 return _type(0) < i && i < b;
141 }
147 template<typename _type>
148 inline YB_STATELESS bool
149 IsInOpenInterval(_type i, _type a, _type b) ynothrow
150 {
151 YAssert(a < b, "Lower bound is not less than upper bound.");
152 return a < i && i < b;
153 }
154
165 template<typename _type>
166 YB_PURE YB_NONNULL(2) size_t
167 SwitchInterval(_type v, const _type* a, size_t n) ynothrow
168 {
169 YAssert(n != 0, "Zero length of array found.");
170 YAssert(!(v < Deref(a)), "Value less than lower bound found.");
171
172 size_t i(0);
173
174 while(!(++i == n || v < a[i]))
175 ;
176 return i - 1;
177 }
178
183 template<typename _type>
184 YB_PURE YB_NONNULL(2) size_t
185 SwitchAddedInterval(_type v, const _type* a, size_t n) ynothrow
186 {
187 YAssert(n != 0, "Zero length of array found.");
188 YAssert(!(v < Deref(a)), "Value less than lower bound found.");
189
190 _type s(*a);
191 size_t i(0);
192
193 while(!(++i == n || v < (s += a[i])))
194 ;
195 return i - 1;
196 }
198
205 template<typename _type>
206 void
207 RestrictInClosedInterval(_type& v, const _type& a, const _type& b) ynothrow
208 {
209 v = ystdex::clamp(v, a, b);
210 }
211
218 template<typename _type>
219 void
220 RestrictInInterval(_type& i, const _type& a, const _type& b) ynothrow
221 {
222 YAssert(a < b, "Lower bound is not less than upper bound.");
223 if(i < a)
224 i = a;
225 else if(!(i < b))
226 i = b - 1;
227 }
228
234 template<typename _type>
235 void
236 RestrictUnsignedStrict(_type& u, _type b) ynothrow
237 {
238 static_assert(std::is_unsigned<_type>(), "Invalid type found.");
239
240 if(b < u)
241 u = b;
242 }
243
250 template<typename _type>
251 void
252 RestrictUnsigned(_type& u, unsigned b) ynothrow
253 {
254 YAssert(b != _type(0), "Zero upper bound found.");
255 if(!(u < b))
256 u = b - 1;
257 }
258
264 template<typename _type>
265 inline void
266 RestrictLessEqual(_type& a, _type& b) ynothrow
267 {
268 if(b < a)
269 std::swap(a, b);
270 }
271
272
278 template<typename _tDst, typename _type>
280 inline _tDst
281 CheckScalar(_type val, const std::string& name = {}, RecordLevel lv = Err)
282 {
283 using common_t = ystdex::common_type_t<_tDst, _type>;
284
285 if(YB_UNLIKELY(common_t(val) > common_t(std::numeric_limits<_tDst>::max())))
286 throw LoggedEvent(name + " value out of range.", lv);
287 return _tDst(val);
288 }
289
291 template<typename _tDst, typename _type>
292 inline _tDst
293 CheckNonnegativeScalar(_type val, const std::string& name = {},
294 RecordLevel lv = Err)
295 {
296 if(val < 0)
297 // XXX: Use more specified exception type.
298 throw LoggedEvent("Failed getting nonnegative " + name + " value.", lv);
299 return CheckScalar<_tDst>(val, name, lv);
300 }
301
303 template<typename _tDst, typename _type>
304 inline _tDst
305 CheckPositiveScalar(_type val, const std::string& name = {},
306 RecordLevel lv = Err)
307 {
308 if(!(0 < val))
309 // XXX: Use more specified exception type.
310 throw LoggedEvent("Failed getting positive " + name + " value.", lv);
311 return CheckScalar<_tDst>(val, name, lv);
312 }
314
315
323 template<typename _tOut>
324 inline void
325 ClearSequence(_tOut dst, size_t n) ynothrowv
326 {
327 using _type = ystdex::decay_t<decltype(*dst)>;
328 static_assert(std::is_pod<_type>()
329 || (std::is_nothrow_default_constructible<_type>()
330 && std::is_nothrow_assignable<_type, _type>()),
331 "Invalid type found.");
332
333 std::fill_n(dst, n, _type());
334 }
335
336
344 YF_API YB_NONNULL(2) bool
345 FetchEnvironmentVariable(string&, const char*);
346
347 } // namespace YSLib;
348
349 #endif
350
YSLib::CheckNonnegativeScalar
_tDst CheckNonnegativeScalar(_type val, const std::string &name={}, RecordLevel lv=Err)
检查非负纯量数值在指定类型的范围内。
Definition: YCoreUtilities.h:293
YSLib::lv
RecordLevel lv
Definition: YException.cpp:58
YSLib::Drawing::x
The boundary is out of the const Rect SDst SPos x
Definition: YDraw.h:127
YSLib::HalfDifference
yconstfn YB_STATELESS _type HalfDifference(_type x, _type y)
计算指定类型的差值的一半。
Definition: YCoreUtilities.h:72
YB_STATELESS
#define YB_STATELESS
指示函数或函数模板实例为无状态函数。
Definition: ydef.h:522
YSLib::IsInOpenInterval
YB_STATELESS bool IsInOpenInterval(_type i, _type b) ynothrow
判断 i 是否在开区间 (_type(0), b) 内。
Definition: YCoreUtilities.h:136
platform::containers::string
basic_string< char > string
Definition: Container.h:101
YSLib::RestrictInInterval
void RestrictInInterval(_type &i, const _type &a, const _type &b) ynothrow
约束整数 i 在左闭右开区间 [a, b) 中。
Definition: YCoreUtilities.h:220
YModules.h
YSLib::IsInInterval
YB_STATELESS bool IsInInterval(_type i, _type b) ynothrow
判断 i 是否在左闭右开区间 [_type(0), b) 中。
Definition: YCoreUtilities.h:84
ystdex::swap
void swap(container_adaptor< _tSeqCon > &x, container_adaptor< _tSeqCon > &y)
Definition: container.hpp:179
v
byte v
Definition: CharRenderer.cpp:75
YSLib::FetchEnvironmentVariable
bool FetchEnvironmentVariable(string &res, const char *var)
Definition: YCoreUtilities.cpp:36
NPL::A1::name
ctx const string & name
Definition: NPLA1.h:248
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
YSLib::RestrictInClosedInterval
void RestrictInClosedInterval(_type &v, const _type &a, const _type &b) ynothrow
约束 v 在闭区间 [a, b] 中。
Definition: YCoreUtilities.h:207
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:760
CHRLib::d
_tDst * d
Definition: CharacterProcessing.h:139
YSLib::RecordLevel
YF_API RecordLevel
Definition: YException.h:141
YSLib::FetchSign
yconstfn YB_STATELESS std::int_fast8_t FetchSign(_type a, _type b=_type(0)) ynothrow
符号函数。
Definition: YCoreUtilities.h:45
YB_PURE
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:501
YSLib::size_t
name const ValueNode::Container const string &name ynothrow observer_ptr< ValueNode::Container > const string &name ynothrow observer_ptr< const ValueNode::Container > const string &name ynothrow size_t
Definition: ValueNode.h:639
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:168
ystdex::clamp
const _type & clamp(const _type &v, const _type &lo, const _type &hi, _fComp comp=_fComp())
取约束范围的值。
Definition: algorithm.hpp:565
YB_UNLIKELY
#define YB_UNLIKELY(_expr)
分支预测提示。
Definition: ydef.h:438
YSLib::IsInClosedInterval
YB_STATELESS bool IsInClosedInterval(_type i, _type b) ynothrow
判断 i 是否在闭区间 [_type(0), b] 中。
Definition: YCoreUtilities.h:110
YSLib::RestrictUnsigned
void RestrictUnsigned(_type &u, unsigned b) ynothrow
约束无符号整数 u 在左闭右开区间 [0, b) 中。
Definition: YCoreUtilities.h:252
NPL::dst
YF_API const char src const string const char * dst
Definition: Dependency.h:93
platform::Deref
yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:440
YSLib::RestrictUnsignedStrict
void RestrictUnsignedStrict(_type &u, _type b) ynothrow
约束无符号整数 u 在区间上界 b 内。
Definition: YCoreUtilities.h:236
YSLib::YAssert
YAssert(!(v< Deref(a)),"Value less than lower bound found.")
YSLib::RestrictLessEqual
void RestrictLessEqual(_type &a, _type &b) ynothrow
约束关系：a ≤ b 。
Definition: YCoreUtilities.h:266
ystdex::max
const _type & max(const _type &a, const _type &b, _fComp comp=less< _type >())
取较大的元素。
Definition: algorithm.hpp:543
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
YSLib::i
return i
Definition: YCoreUtilities.h:176
YSLib::YB_NONNULL
YF_API YB_NONNULL(1, 2) ValueNode LoadNPLA1File(const char *disp
载入 NPLA1 配置文件。
YSLib::CheckScalar
_tDst CheckScalar(_type val, const std::string &name={}, RecordLevel lv=Err)
检查纯量数值在指定类型的范围内。
Definition: YCoreUtilities.h:281
YSLib::CheckPositiveScalar
_tDst CheckPositiveScalar(_type val, const std::string &name={}, RecordLevel lv=Err)
检查正纯量数值在指定类型的范围内。
Definition: YCoreUtilities.h:305
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:654
YSLib::a
YB_PURE const _type * a
Definition: YCoreUtilities.h:167
YSLib::FetchSignFromInterval
yconstfn YB_STATELESS std::int_fast8_t FetchSignFromInterval(_type d, _type a, _type b) ynothrow
判断 d 和以 [a, b](a ≤ b) 或 [b, a](a > b) 区间的关系。
Definition: YCoreUtilities.h:61
YSLib::ClearSequence
void ClearSequence(_tOut dst, size_t n) ynothrowv
清除指定的连续对象。
Definition: YCoreUtilities.h:325
YF_API
#define YF_API
Definition: Platform.h:56
platform::Descriptions::Err
Definition: YCommon.h:156
algorithm.hpp
泛型算法。
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
platform::bool
std::FILE *fp bool
Definition: FileIO.h:1187
platform::n
size_t n
Definition: YCLib/FileSystem.cpp:321