YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ytimer.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 
33 #ifndef YSL_INC_SERVICE_YTIMER_H_
34 #define YSL_INC_SERVICE_YTIMER_H_ 1
35 
36 #include "../Core/yobject.h"
37 #include "../Adaptor/ycont.h"
38 #include "../Core/ycounter.hpp"
39 #include <chrono>
40 
42 
44 
45 
50 {
51 public:
52  typedef std::chrono::nanoseconds duration;
53  typedef duration::rep rep;
54  typedef duration::period period;
55  typedef std::chrono::time_point<HighResolutionClock, duration> time_point;
56 
57  static yconstexpr bool is_steady = false;
58 
60  static time_point
61  now() ynothrow;
62 };
63 
64 inline HighResolutionClock::time_point
65 HighResolutionClock::now() ynothrow
66 {
67  return time_point(std::chrono::nanoseconds(GetHighResolutionTicks()));
68 }
69 
70 
77 
83 
89 typedef std::chrono::milliseconds TimeSpan;
90 
91 
96 YF_API void
97 Delay(const TimeSpan&);
98 
99 
105 class YF_API Timer : private noncopyable, protected GMCounter<Timer>
106 {
107 public:
108  typedef map<u32, Timer*> TimerMap;
109 
110 protected:
112 
113  TimePoint nBase;
114  Duration nInterval;
115 
116 public:
121  explicit
122  Timer(const Duration& = {}, bool = false);
127  ~Timer();
128 
133  bool
134  IsActive() const;
135 
136  DefGetter(const ynothrow, TimePoint, BaseTick, nBase)
137  DefGetter(const ynothrow, Duration, Interval, nInterval)
138 
143  void
144  SetInterval(const TimeSpan& i)
145  {
146  SetInterval(static_cast<const Duration&>(i));
147  }
152  void
153  SetInterval(const Duration&);
154 
159  void
160  Delay(const Duration& d)
161  {
162  nBase += d;
163  }
164 
168  bool
169  Refresh();
170 
174  static bool
175  RefreshAll();
176 
180  void
181  Reset()
182  {
183  nBase = TimePoint();
184  }
185 
189  static void
190  ResetAll();
191 
195  YF_API friend void
196  Activate(Timer&);
197 
201  YF_API friend void
202  Deactivate(Timer&);
203 };
204 
205 inline
206 Timer::~Timer()
207 {
208  Deactivate(*this);
209 }
210 
211 YSL_END_NAMESPACE(Timers)
212 
213 YSL_END
214 
215 #endif
216