YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ycutil.h
浏览该文件的文档.
1 /*
2  © 2010-2014 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_ycutil_h_
29 #define YSL_INC_Core_ycutil_h_ 1
30 
31 #include "YModules.h"
32 #include YFM_YSLib_Core_YException // for YSLib::LoggedEvent;
33 
34 namespace YSLib
35 {
36 
44 template<typename _type, typename _tStrict, typename _tWeak>
46 {
48  std::is_convertible<_type, _tStrict>::value, _tStrict, _tWeak>;
49 
50  static inline Result
51  Cast(_type o)
52  {
53  return Result(o);
54  }
55 };
56 
57 
65 template<typename _type, typename _tStrict>
66 struct SelectConvertible : MoreConvertible<_type, _tStrict, _type>
67 {
69 };
70 
71 
76 template<typename _type>
77 yconstfn _type
79 {
80  return _type(0);
81 }
82 
89 FetchSign(int a, int b = 0) ynothrow
90 {
91  return a < b ? -1 : !(a == b);
92 }
98 template<typename _type>
100 FetchSign(const _type& a, const _type& b = FetchZero<_type>()) ynothrow
101 {
102  return a < b ? -1 : !(a == b);
103 }
104 
113 yconstfn int
114 FetchSignFromInterval(int d, int a, int b) ynothrow
115 {
116  return FetchSign(a, d) * FetchSign(d, b);
117 }
126 template<typename _type>
127 yconstfn int
128 FetchSignFromInterval(const _type& d, const _type& a, const _type& b) ynothrow
129 {
130  return FetchSign(a, d) * FetchSign(d, b);
131 }
132 
138 template<typename _type>
139 inline bool
140 IsInInterval(_type i, _type b) ynothrow
141 {
142  YAssert(FetchZero<_type>() < b,
143  "Zero element as lower bound is not less than upper bound.");
144  return !(i < FetchZero<_type>()) && i < b;
145 }
151 template<typename _type>
152 inline bool
153 IsInInterval(_type i, _type a, _type b) ynothrow
154 {
155  YAssert(a < b, "Lower bound is not less than upper bound.");
156  return !(i < a) && i < b;
157 }
158 
164 template<typename _type>
165 inline bool
166 IsInClosedInterval(_type i, _type b) ynothrow
167 {
168  YAssert(FetchZero<_type>() < b,
169  "Zero element as lower bound is not less than upper bound.");
170  return !(i < FetchZero<_type>() || b < i);
171 }
177 template<typename _type>
178 inline bool
179 IsInClosedInterval(_type i, _type a, _type b) ynothrow
180 {
181  YAssert(a < b, "Lower bound is not less than upper bound.");
182  return !(i < a || b < i);
183 }
184 
190 template<typename _type>
191 inline bool
192 IsInOpenInterval(_type i, _type b) ynothrow
193 {
194  YAssert(FetchZero<_type>() < b,
195  "Zero element as lower bound is not less than upper bound.");
196  return FetchZero<_type>() < i && i < b;
197 }
203 template<typename _type>
204 inline bool
205 IsInOpenInterval(_type i, _type a, _type b) ynothrow
206 {
207  YAssert(a < b,
208  "Lower bound is not less than upper bound.");
209  return a < i && i < b;
210 }
211 
219 template<typename _type>
220 size_t
221 SwitchInterval(_type v, const _type* a, size_t n) ynothrow
222 {
223  YAssert(a, "Null array pointer found."),
224  YAssert(n != 0, "Zero length of array found.");
225  YAssert(!(v < *a), "Value less than lower bound found.");
226 
227  size_t i(0);
228 
229  while(!(++i == n || v < a[i]))
230  ;
231  return i - 1;
232 }
233 
242 template<typename _type>
243 size_t
244 SwitchAddedInterval(_type v, const _type* a, size_t n) ynothrow
245 {
246  YAssert(a, "Null array pointer found."),
247  YAssert(n != 0, "Zero length of array found.");
248  YAssert(!(v < *a), "Value less than lower bound found.");
249 
250  _type s(*a);
251  size_t i(0);
252 
253  while(!(++i == n || v < (s += a[i])))
254  ;
255  return i - 1;
256 }
257 
264 template<typename _type>
265 void
266 RestrictInClosedInterval(_type& v, const _type& a, const _type& b) ynothrow
267 {
268  YAssert(!(b < a), "Upper bound is less than lower bound.");
269  if(v < a)
270  v = a;
271  else if(b < v)
272  v = b;
273 }
274 
281 template<typename _type>
282 void
283 RestrictInInterval(_type& i, int a, int b) ynothrow
284 {
285  YAssert(a < b, "Lower bound is not less than upper bound.");
286  if(i < a)
287  i = a;
288  else if(!(i < b))
289  i = b - 1;
290 }
291 
297 template<typename _type>
298 void
299 RestrictUnsignedStrict(_type& u, unsigned b) ynothrow
300 {
301  if(b < u)
302  u = b;
303 }
304 
311 template<typename _type>
312 void
313 RestrictUnsigned(_type& u, unsigned b) ynothrow
314 {
315  YAssert(b != FetchZero<_type>(), "Zero upper bound found.");
316  if(!(u < b))
317  u = b - 1;
318 }
319 
325 template<typename _type>
326 inline void
327 RestrictLessEqual(_type& a, _type& b) ynothrow
328 {
329  if(b < a)
330  std::swap(a, b);
331 }
332 
333 
342 template<typename _tOut>
343 inline void
344 ClearSequence(_tOut dst, size_t n) ynothrow
345 {
347  static_assert(std::is_pod<_type>::value
348  || (std::is_nothrow_default_constructible<_type>::value
349  && std::is_nothrow_assignable<_type, _type>::value),
350  "Invalid type found.");
351 
352  if(YB_LIKELY(dst && n))
353  std::fill_n(dst, n, _type());
354 }
355 
356 
362 {
366  template<typename _type>
367  inline void
368  operator()(_type* _ptr) ynothrow
369  {
370  delete _ptr;
371  }
372 };
373 
374 
381 {
385  template<typename _type>
386  inline void
387  operator()(const _type& _pr) ynothrow
388  {
389  delete _pr.second;
390  }
391 };
392 
393 
394 #ifdef YSL_USE_MEMORY_DEBUG
395 
400 struct delete_obj_debug
401 {
405  template<typename _type>
406  inline void
407  operator()(_type* _ptr) ynothrow
408  {
409  ydelete(_ptr);
410  }
411 };
412 
413 
419 struct delete_second_mem_debug
420 {
424  template<typename _type>
425  inline void
426  operator()(const _type& _pr) ynothrow
427  {
428  ydelete(_pr.second);
429  }
430 };
431 
432 
433 # define delete_obj delete_obj_debug
434 # define delete_second_mem delete_second_mem_debug
435 
436 #else
437 
438 # define delete_obj delete_obj_ndebug
439 # define delete_second_mem delete_second_mem_ndebug
440 
441 #endif
442 
448 {
452  template<typename _tPointer>
453  inline void
454  operator()(_tPointer& _ptr) ynothrow
455  {
456  reset(_ptr);
457  }
458 };
459 
460 
465 template<typename _type>
466 yconstfn auto
467 CloneNonpolymorphic(const _type& p) -> decltype(&*p)
468 {
469  return new typename ystdex::remove_reference_t<decltype(*p)>(*p);
470 }
471 
477 template<class _type>
478 auto
479 ClonePolymorphic(const _type& p) -> decltype(&*p)
480 {
481  static_assert(std::is_polymorphic<ystdex::remove_reference_t<decltype(*p)>>
482  ::value, "Non-polymorphic class type found.");
483 
484  return p->clone();
485 }
486 
487 } // namespace YSLib;
488 
489 #endif
490 
delete 第二成员仿函数。
Definition: ycutil.h:380
typename MoreConvertible< _type, _tStrict, _type >::Result Result
Definition: ycutil.h:68
void operator()(const _type &_pr) ynothrow
删除第二成员指向的对象。
Definition: ycutil.h:387
void RestrictInClosedInterval(_type &v, const _type &a, const _type &b) ynothrow
约束 v 在闭区间 [a, b] 中。
Definition: ycutil.h:266
typename remove_reference< _type >::type remove_reference_t
Definition: type_op.hpp:234
auto ClonePolymorphic(const _type &p) -> decltype(&*p)
使用 clone 成员函数复制指定指针指向的多态类类型对象。
Definition: ycutil.h:479
typename conditional< _bCond, _type, _type2 >::type conditional_t
Definition: type_op.hpp:277
bool IsInOpenInterval(_type i, _type b) ynothrow
判断 i 是否在开区间 (FetchZero<_type>(), b) 内。
Definition: ycutil.h:192
void ClearSequence(_tOut dst, size_t n) ynothrow
清除指定的连续对象。
Definition: ycutil.h:344
static Result Cast(_type o)
Definition: ycutil.h:51
转换类型选择。
Definition: ycutil.h:45
yconstfn int FetchSignFromInterval(int d, int a, int b) ynothrow
判断整数 d 和以 [a, b](a ≤ b) 或 [b, a](a > b) 区间的关系。
Definition: ycutil.h:114
void swap(any &x, any &y)
交换对象。
Definition: any.h:729
void operator()(_type *_ptr) ynothrow
删除指针指向的对象。
Definition: ycutil.h:368
delete 仿函数。
Definition: ycutil.h:361
带置空指针操作的 delete 仿函数。
Definition: ycutil.h:447
void RestrictUnsigned(_type &u, unsigned b) ynothrow
约束无符号整数 u 在左闭右开区间 [0, b) 中。
Definition: ycutil.h:313
size_t SwitchAddedInterval(_type v, const _type *a, size_t n) ynothrow
计算满足指定的值 v 在区间 [b(i), b(i + 1)) 内的最小的 i ; 其中 b(i) 是 a[i] 前 i 项的和。 ...
Definition: ycutil.h:244
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
yconstfn _type FetchZero() ynothrow
取指定类型的零元素。
Definition: ycutil.h:78
void RestrictUnsignedStrict(_type &u, unsigned b) ynothrow
约束无符号整数 u 在区间上界 b 内。
Definition: ycutil.h:299
yconstfn s8 FetchSign(int a, int b=0) ynothrow
整数类型符号函数。
Definition: ycutil.h:89
std::int8_t s8
Definition: yadaptor.h:71
void RestrictLessEqual(_type &a, _type &b) ynothrow
约束关系:a ≤ b 。
Definition: ycutil.h:327
ystdex::conditional_t< std::is_convertible< _type, _tStrict >::value, _tStrict, _tWeak > Result
Definition: ycutil.h:48
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
#define ydelete
Definition: ynew.h:207
void operator()(_tPointer &_ptr) ynothrow
删除指针指向的对象,并置指针为空值。
Definition: ycutil.h:454
void RestrictInInterval(_type &i, int a, int b) ynothrow
约束整数 i 在左闭右开区间 [a, b) 中。
Definition: ycutil.h:283
#define YB_LIKELY(expr)
Definition: ydef.h:297
转换类型选择。
Definition: ycutil.h:66
bool IsInInterval(_type i, _type b) ynothrow
判断 i 是否在左闭右开区间 [FetchZero<_type>(), b) 中。
Definition: ycutil.h:140
size_t SwitchInterval(_type v, const _type *a, size_t n) ynothrow
计算满足指定的值 v 在区间 [a[i], a[i + 1]) 内最小的 i 。
Definition: ycutil.h:221
bool IsInClosedInterval(_type i, _type b) ynothrow
判断 i 是否在闭区间 [FetchZero<_type>(), b] 中。
Definition: ycutil.h:166
bool reset(_type *&p) ynothrow
Definition: yref.hpp:69
yconstfn auto CloneNonpolymorphic(const _type &p) -> decltype(&*p)
使用 new 复制指定指针指向的对象。
Definition: ycutil.h:467
byte v
#define YAssert(_expr, _msg)
Definition: cassert.h:73