YSTest: ytimer.cpp 源文件

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
81 Timer::Refresh()
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
93 Duration
94 Timer::RefreshDelta()
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
106 Timer::RefreshRemainder()
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
118 Activate(Timer& tmr)
119 {
120 if(tmr.Interval != Duration::zero())
121 tmr.nBase = HighResolutionClock::now();
122 }
123
124 } // namespace Timers;
125
126 } // namespace YSLib;
127
platform::StartTicks
YF_API void StartTicks()
开始 tick 计时。
Definition: Timer.cpp:102
YSLib::Timers::Timer::nBase
TimePoint nBase
时间基点：计时的起点。
Definition: ytimer.h:104
YSLib::UI::Refresh
void Refresh(PaintEventArgs &&) override
刷新：按指定参数绘制界面并更新状态。
YSLib::Timers::Timer::RefreshDelta
Duration RefreshDelta()
刷新：对于非零时间间隔判断有效性并都更新时间基点。
Definition: ytimer.cpp:94
YSLib::Timers::HighResolutionClock::now
static time_point now() ynothrow
Definition: ytimer.h:61
YSLib::Timers::Timer::Activate
virtual nBase YF_API friend void Activate(Timer &)
激活：当时间间隔非零时同步时间基点。
Definition: ytimer.cpp:118
YSLib::Timers::Timer::RefreshRemainder
Duration RefreshRemainder()
刷新：对于非零时间间隔判断有效性并都更新时间基点。
Definition: ytimer.cpp:106
YSLib::Timers::Timer::Interval
Duration Interval
重复刷新有效的最小时间间隔。
Definition: ytimer.h:111
YB_UNLIKELY
#define YB_UNLIKELY(expr)
分支预测提示。
Definition: ydef.h:298
YSLib::Timers::Delay
YF_API void Delay(const TimeSpan &)
阻塞延时。
Definition: ytimer.cpp:63
YSLib::Timers::Timer::Timer
Timer(const Duration &={}, bool=true)
构造：使用时间间隔和激活状态。
Definition: ytimer.cpp:72
platform::GetTicks
YF_API std::uint32_t GetTicks()
取 tick 数。
Definition: Timer.cpp:141
YSLib::Timers::TimePoint
HighResolutionClock::time_point TimePoint
时刻。
Definition: ytimer.h:78
YSLib::Timers::Duration
HighResolutionClock::duration Duration
高精度时间间隔。
Definition: ytimer.h:72
YB_LIKELY
#define YB_LIKELY(expr)
Definition: ydef.h:297
YSLib::Timers::TimeSpan
std::chrono::milliseconds TimeSpan
低精度时间间隔。
Definition: ytimer.h:85
YModules.h
YSLib::Timers::Timer
计时器。
Definition: ytimer.h:100
YAssert
#define YAssert(_expr, _msg)
Definition: cassert.h:73
YSLib::Timers::Activate
void Activate(Timer &tmr)
Definition: ytimer.cpp:118