TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Timer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TRINITY_TIMER_H
20 #define TRINITY_TIMER_H
21 
22 #include <chrono>
23 
24 inline uint32 getMSTime()
25 {
26  using namespace std::chrono;
27 
28  static const system_clock::time_point ApplicationStartTime = system_clock::now();
29 
30  return uint32(duration_cast<milliseconds>(system_clock::now() - ApplicationStartTime).count());
31 }
32 
33 inline uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
34 {
35  // getMSTime() have limited data range and this is case when it overflow in this tick
36  if (oldMSTime > newMSTime)
37  return (0xFFFFFFFF - oldMSTime) + newMSTime;
38  else
39  return newMSTime - oldMSTime;
40 }
41 
42 inline uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
43 {
44  return getMSTimeDiff(oldMSTime, getMSTime());
45 }
46 
48 {
49 public:
50 
52  : _interval(0), _current(0)
53  {
54  }
55 
56  void Update(time_t diff)
57  {
58  _current += diff;
59  if (_current < 0)
60  _current = 0;
61  }
62 
63  bool Passed()
64  {
65  return _current >= _interval;
66  }
67 
68  void Reset()
69  {
70  if (_current >= _interval)
72  }
73 
74  void SetCurrent(time_t current)
75  {
76  _current = current;
77  }
78 
79  void SetInterval(time_t interval)
80  {
81  _interval = interval;
82  }
83 
84  time_t GetInterval() const
85  {
86  return _interval;
87  }
88 
89  time_t GetCurrent() const
90  {
91  return _current;
92  }
93 
94 private:
95 
96  time_t _interval;
97  time_t _current;
98 };
99 
101 {
102 public:
103 
104  TimeTracker(time_t expiry)
105  : i_expiryTime(expiry)
106  {
107  }
108 
109  void Update(time_t diff)
110  {
111  i_expiryTime -= diff;
112  }
113 
114  bool Passed() const
115  {
116  return i_expiryTime <= 0;
117  }
118 
119  void Reset(time_t interval)
120  {
121  i_expiryTime = interval;
122  }
123 
124  time_t GetExpiry() const
125  {
126  return i_expiryTime;
127  }
128 
129 private:
130 
131  time_t i_expiryTime;
132 };
133 
135 {
136 public:
137 
139  : i_expiryTime(expiry)
140  {
141  }
142 
143  void Update(int32 diff)
144  {
145  i_expiryTime -= diff;
146  }
147 
148  bool Passed() const
149  {
150  return i_expiryTime <= 0;
151  }
152 
153  void Reset(uint32 interval)
154  {
155  i_expiryTime = interval;
156  }
157 
158  int32 GetExpiry() const
159  {
160  return i_expiryTime;
161  }
162 
163 private:
164 
166 };
167 
169 {
170 public:
171 
172  PeriodicTimer(int32 period, int32 start_time)
173  : i_period(period), i_expireTime(start_time)
174  {
175  }
176 
177  bool Update(const uint32 diff)
178  {
179  if ((i_expireTime -= diff) > 0)
180  return false;
181 
182  i_expireTime += i_period > int32(diff) ? i_period : diff;
183  return true;
184  }
185 
186  void SetPeriodic(int32 period, int32 start_time)
187  {
188  i_expireTime = start_time;
189  i_period = period;
190  }
191 
192  // Tracker interface
193  void TUpdate(int32 diff) { i_expireTime -= diff; }
194  bool TPassed() const { return i_expireTime <= 0; }
195  void TReset(int32 diff, int32 period) { i_expireTime += period > diff ? period : diff; }
196 
197 private:
198 
201 };
202 
203 #endif
bool Update(const uint32 diff)
Definition: Timer.h:177
void TUpdate(int32 diff)
Definition: Timer.h:193
int32 i_period
Definition: Timer.h:199
uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
Definition: Timer.h:33
IntervalTimer()
Definition: Timer.h:51
uint32 getMSTime()
Definition: Timer.h:24
time_t _current
Definition: Timer.h:97
TimeTracker(time_t expiry)
Definition: Timer.h:104
Definition: Timer.h:134
void Reset(time_t interval)
Definition: Timer.h:119
time_t i_expiryTime
Definition: Timer.h:131
bool Passed()
Definition: Timer.h:63
void Update(int32 diff)
Definition: Timer.h:143
void SetInterval(time_t interval)
Definition: Timer.h:79
void SetPeriodic(int32 period, int32 start_time)
Definition: Timer.h:186
int32 i_expireTime
Definition: Timer.h:200
Definition: Timer.h:168
int32_t int32
Definition: Define.h:146
void Update(time_t diff)
Definition: Timer.h:109
uint32_t uint32
Definition: Define.h:150
void Reset()
Definition: Timer.h:68
int32 i_expiryTime
Definition: Timer.h:165
void TReset(int32 diff, int32 period)
Definition: Timer.h:195
void Reset(uint32 interval)
Definition: Timer.h:153
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:42
Definition: Timer.h:100
void Update(time_t diff)
Definition: Timer.h:56
int32_t int32
Definition: g3dmath.h:167
time_t GetCurrent() const
Definition: Timer.h:89
bool Passed() const
Definition: Timer.h:114
uint32_t uint32
Definition: g3dmath.h:168
void SetCurrent(time_t current)
Definition: Timer.h:74
time_t GetExpiry() const
Definition: Timer.h:124
TimeTrackerSmall(uint32 expiry=0)
Definition: Timer.h:138
time_t _interval
Definition: Timer.h:96
time_t GetInterval() const
Definition: Timer.h:84
PeriodicTimer(int32 period, int32 start_time)
Definition: Timer.h:172
bool TPassed() const
Definition: Timer.h:194
Definition: Timer.h:47
int32 GetExpiry() const
Definition: Timer.h:158
bool Passed() const
Definition: Timer.h:148