YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ycutil.h
浏览该文件的文档.
1 /*
2  Copyright (C) by Franksoft 2010 - 2012.
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 "ysdef.h"
32 
34 
42 template<typename _type, typename _tStrict, typename _tWeak>
44 {
45  typedef typename std::conditional<std::is_convertible<_type,
46  _tStrict>::value, _tStrict, _tWeak>::Result Result;
47 
48  static inline Result
49  Cast(_type o)
50  {
51  return Result(o);
52  }
53 };
54 
55 
63 template<typename _type, typename _tStrict>
64 struct SelectConvertible : MoreConvertible<_type, _tStrict, _type>
65 {
68 };
69 
70 
75 yconstfn int
77 {
78  return 0;
79 }
84 template<typename _type>
85 yconstfn _type
87 {
88  return _type(0);
89 }
90 
97 FetchSign(int a, int b = 0) ynothrow
98 {
99  return a < b ? -1 : !(a == b);
100 }
106 template<typename _type>
107 yconstfn s8
108 FetchSign(const _type& a, const _type& b = FetchZero<_type>()) ynothrow
109 {
110  return a < b ? -1 : !(a == b);
111 }
112 
121 yconstfn int
122 FetchSignFromInterval(int d, int a, int b) ynothrow
123 {
124  return FetchSign(a, d) * FetchSign(d, b);
125 }
134 template<typename _type>
135 yconstfn int
136 FetchSignFromInterval(const _type& d, const _type& a, const _type& b) ynothrow
137 {
138  return FetchSign(a, d) * FetchSign(d, b);
139 }
140 
146 template<typename _type>
147 inline bool
148 IsInInterval(_type i, _type b) ynothrow
149 {
150  YAssert(FetchZero<_type>() < b,
151  "Zero element as lower bound is not less than upper bound.");
152 
153  return !(i < FetchZero<_type>()) && i < b;
154 }
160 template<typename _type>
161 inline bool
162 IsInInterval(_type i, _type a, _type b) ynothrow
163 {
164  YAssert(a < b, "Lower bound is not less than upper bound.");
165 
166  return !(i < a) && i < b;
167 }
168 
174 template<typename _type>
175 inline bool
176 IsInOpenInterval(_type i, _type b) ynothrow
177 {
178  YAssert(FetchZero<_type>() < b,
179  "Zero element as lower bound is not less than upper bound.");
180 
181  return i > FetchZero<_type>() && i < b;
182 }
188 template<typename _type>
189 inline bool
190 IsInOpenInterval(_type i, _type a, _type b) ynothrow
191 {
192  YAssert(a < b,
193  "Lower bound is not less than upper bound.");
194 
195  return i > a && i < b;
196 }
197 
205 template<typename _type>
206 size_t
207 SwitchInterval(_type v, const _type* a, size_t n) ynothrow
208 {
209  YAssert(a, "Null array pointer found."),
210  YAssert(n != 0, "Zero length of array found.");
211  YAssert(!(v < *a), "Value less than lower bound found.");
212 
213  size_t i(0);
214 
215  while(!(++i == n || v < a[i]))
216  ;
217  return i - 1;
218 }
219 
228 template<typename _type>
229 size_t
230 SwitchAddedInterval(_type v, const _type* a, size_t n) ynothrow
231 {
232  YAssert(a, "Null array pointer found."),
233  YAssert(n != 0, "Zero length of array found.");
234  YAssert(!(v < *a), "Value less than lower bound found.");
235 
236  _type s(*a);
237  size_t i(0);
238 
239  while(!(++i == n || v < (s += a[i])))
240  ;
241  return i - 1;
242 }
243 
250 template<typename _type>
251 void
252 RestrictInClosedInterval(_type& i, int a, int b) ynothrow
253 {
254  YAssert(!(b < a), "Upper bound is less than lower bound.");
255 
256  if(i < a)
257  i = a;
258  else if(b < i)
259  i = b;
260 }
261 
268 template<typename _type>
269 void
270 RestrictInInterval(_type& i, int a, int b) ynothrow
271 {
272  YAssert(a < b, "Lower bound is not less than upper bound.");
273 
274  if(i < a)
275  i = a;
276  else if(!(i < b))
277  i = b - 1;
278 }
279 
285 template<typename _type>
286 void
287 RestrictUnsignedStrict(_type& u, unsigned b) ynothrow
288 {
289  if(b < u)
290  u = b;
291 }
292 
299 template<typename _type>
300 void
301 RestrictUnsigned(_type& u, unsigned b) ynothrow
302 {
303  YAssert(b != FetchZero<_type>(), "Zero upper bound found.");
304 
305  if(!(u < b))
306  u = b - 1;
307 }
308 
314 template<typename _type>
315 inline void
316 RestrictLessEqual(_type& a, _type& b) ynothrow
317 {
318  if(b < a)
319  std::swap(a, b);
320 }
321 
322 
331 template<typename _tOut>
332 inline void
333 ClearSequence(_tOut dst, size_t n) ynothrow
334 {
335  typedef typename std::remove_reference<decltype(*dst)>::type _type;
336 
337  static_assert(std::is_pod<_type>::value
338  || (std::is_nothrow_default_constructible<_type>::value
339  && std::is_nothrow_assignable<_type, _type>::value),
340  "Invalid type found.");
341 
342  if(YB_LIKELY(dst && n))
343  std::fill_n(dst, n, _type());
344 }
345 
346 
352 {
356  template<typename _type>
357  inline void
358  operator()(_type* _ptr) ynothrow
359  {
360  delete _ptr;
361  }
362 };
363 
364 
371 {
375  template<typename _type>
376  inline void
377  operator()(const _type& _pr) ynothrow
378  {
379  delete _pr.second;
380  }
381 };
382 
383 
384 #ifdef YSL_USE_MEMORY_DEBUG
385 
391 {
395  template<typename _type>
396  inline void
397  operator()(_type* _ptr) ynothrow
398  {
399  ydelete(_ptr);
400  }
401 };
402 
403 
410 {
414  template<typename _type>
415  inline void
416  operator()(const _type& _pr) ynothrow
417  {
418  ydelete(_pr.second);
419  }
420 };
421 
422 
423 # define delete_obj delete_obj_debug
424 # define delete_second_mem delete_second_mem_debug
425 
426 #else
427 
428 # define delete_obj delete_obj_ndebug
429 # define delete_second_mem delete_second_mem_ndebug
430 
431 #endif
432 
438 {
442  template<typename _tPointer>
443  inline void
444  operator()(_tPointer& _ptr) ynothrow
445  {
446  reset(_ptr);
447  }
448 };
449 
450 
455 template<typename _type>
456 yconstfn auto
457 CloneNonpolymorphic(const _type& p) -> decltype(&*p)
458 {
459  return new typename std::remove_reference<decltype(*p)>::type(*p);
460 }
461 
467 template<class _type>
468 auto
469 ClonePolymorphic(const _type& p) -> decltype(&*p)
470 {
471  static_assert(std::is_polymorphic<typename
472  std::remove_reference<decltype(*p)>::type>::value,
473  "Non-polymorphic class type found.");
474 
475  return p->Clone();
476 }
477 
478 YSL_END
479 
480 #endif
481