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