YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ytimer.cpp
浏览该文件的文档.
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 #include "YSLib/Service/YModules.h"
29 #include YFM_YSLib_Service_YTimer
30 
31 namespace YSLib
32 {
33 
34 namespace Timers
35 {
36 
37 namespace
38 {
39 
40 bool NotInitialized(true);
41 
43 void
44 InitClock()
45 {
46  if(YB_UNLIKELY(NotInitialized))
47  {
48  StartTicks();
49  NotInitialized = {};
50  }
51 }
52 
53 
54 inline TimePoint
55 GetTickPoint()
56 {
57  return TimePoint(TimeSpan(GetTicks()));
58 }
59 
60 } // unnamed namespace;
61 
62 void
63 Delay(const TimeSpan& ms)
64 {
65  const auto end(GetTickPoint() + ms);
66 
67  while(GetTickPoint() < end)
68  ;
69 }
70 
71 
72 Timer::Timer(const Duration& i, bool b)
73  : nBase(), Interval(i)
74 {
75  InitClock();
76  if(b)
77  Activate(*this);
78 }
79 
80 bool
82 {
83  YAssert(Interval != Duration::zero(), "Zero interval found.");
84 
85  const auto tick(HighResolutionClock::now());
86 
87  if(YB_LIKELY(tick < nBase + Interval))
88  return false;
89  nBase = tick - (tick - nBase) % Interval;
90  return true;
91 }
92 
95 {
96  YAssert(Interval != Duration::zero(), "Zero interval found.");
97  const auto tick(HighResolutionClock::now());
98  const auto delta(tick - nBase);
99 
100  if(YB_UNLIKELY(!(tick < nBase + Interval)))
101  nBase = tick - delta % Interval;
102  return delta;
103 }
104 
105 Duration
107 {
108  YAssert(Interval != Duration::zero(), "Zero interval found.");
109  const auto tick(HighResolutionClock::now());
110  const auto remainder((tick - nBase) % Interval);
111 
112  if(YB_UNLIKELY(!(tick < nBase + Interval)))
113  nBase = tick - remainder;
114  return remainder;
115 }
116 
117 void
119 {
120  if(tmr.Interval != Duration::zero())
122 }
123 
124 } // namespace Timers;
125 
126 } // namespace YSLib;
127 
YF_API void StartTicks()
开始 tick 计时。
Definition: Timer.cpp:102
TimePoint nBase
时间基点:计时的起点。
Definition: ytimer.h:104
void Refresh(PaintEventArgs &&) override
刷新:按指定参数绘制界面并更新状态。
Duration RefreshDelta()
刷新:对于非零时间间隔判断有效性并都更新时间基点。
Definition: ytimer.cpp:94
static time_point now() ynothrow
Definition: ytimer.h:61
virtual nBase YF_API friend void Activate(Timer &)
激活:当时间间隔非零时同步时间基点。
Definition: ytimer.cpp:118
Duration RefreshRemainder()
刷新:对于非零时间间隔判断有效性并都更新时间基点。
Definition: ytimer.cpp:106
Duration Interval
重复刷新有效的最小时间间隔。
Definition: ytimer.h:111
#define YB_UNLIKELY(expr)
分支预测提示。
Definition: ydef.h:298
YF_API void Delay(const TimeSpan &)
阻塞延时。
Definition: ytimer.cpp:63
Timer(const Duration &={}, bool=true)
构造:使用时间间隔和激活状态。
Definition: ytimer.cpp:72
YF_API std::uint32_t GetTicks()
取 tick 数。
Definition: Timer.cpp:141
HighResolutionClock::time_point TimePoint
时刻。
Definition: ytimer.h:78
HighResolutionClock::duration Duration
高精度时间间隔。
Definition: ytimer.h:72
#define YB_LIKELY(expr)
Definition: ydef.h:297
std::chrono::milliseconds TimeSpan
低精度时间间隔。
Definition: ytimer.h:85
计时器。
Definition: ytimer.h:100
#define YAssert(_expr, _msg)
Definition: cassert.h:73
void Activate(Timer &tmr)
Definition: ytimer.cpp:118