Updated Copyright year to 2013
[getmangos.git] / src / shared / Timer.h
blob2e46b518cd95d2138abb6fe9f460204fa04d64a4
1 /*
2 * Copyright (C) 2005-2013 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef MANGOS_TIMER_H
20 #define MANGOS_TIMER_H
22 #include "Common.h"
23 #include <ace/OS_NS_sys_time.h>
25 class WorldTimer
27 public:
29 // get current server time
30 static uint32 getMSTime();
32 // get time difference between two timestamps
33 static inline uint32 getMSTimeDiff(const uint32& oldMSTime, const uint32& newMSTime)
35 if (oldMSTime > newMSTime)
37 const uint32 diff_1 = (uint32(0xFFFFFFFF) - oldMSTime) + newMSTime;
38 const uint32 diff_2 = oldMSTime - newMSTime;
40 return std::min(diff_1, diff_2);
43 return newMSTime - oldMSTime;
46 // get last world tick time
47 static MANGOS_DLL_SPEC uint32 tickTime();
48 // get previous world tick time
49 static MANGOS_DLL_SPEC uint32 tickPrevTime();
50 // tick world timer
51 static MANGOS_DLL_SPEC uint32 tick();
53 private:
54 WorldTimer();
55 WorldTimer(const WorldTimer&);
57 // analogue to getMSTime() but it persists m_SystemTickTime
58 static uint32 getMSTime_internal(bool savetime = false);
60 static MANGOS_DLL_SPEC uint32 m_iTime;
61 static MANGOS_DLL_SPEC uint32 m_iPrevTime;
64 class IntervalTimer
66 public:
67 IntervalTimer() : _interval(0), _current(0) {}
69 void Update(time_t diff)
71 _current += diff;
72 if (_current < 0)
73 _current = 0;
75 bool Passed() const { return _current >= _interval; }
76 void Reset()
78 if (_current >= _interval)
79 _current -= _interval;
82 void SetCurrent(time_t current) { _current = current; }
83 void SetInterval(time_t interval) { _interval = interval; }
84 time_t GetInterval() const { return _interval; }
85 time_t GetCurrent() const { return _current; }
87 private:
88 time_t _interval;
89 time_t _current;
92 class ShortIntervalTimer
94 public:
95 ShortIntervalTimer() : _interval(0), _current(0) {}
97 void Update(uint32 diff)
99 _current += diff;
102 bool Passed() const { return _current >= _interval; }
103 void Reset()
105 if (_current >= _interval)
106 _current -= _interval;
109 void SetCurrent(uint32 current) { _current = current; }
110 void SetInterval(uint32 interval) { _interval = interval; }
111 uint32 GetInterval() const { return _interval; }
112 uint32 GetCurrent() const { return _current; }
114 private:
115 uint32 _interval;
116 uint32 _current;
119 struct TimeTracker
121 public:
122 TimeTracker(time_t expiry) : i_expiryTime(expiry) {}
123 void Update(time_t diff) { i_expiryTime -= diff; }
124 bool Passed() const { return (i_expiryTime <= 0); }
125 void Reset(time_t interval) { i_expiryTime = interval; }
126 time_t GetExpiry() const { return i_expiryTime; }
128 private:
129 time_t i_expiryTime;
132 struct ShortTimeTracker
134 public:
135 ShortTimeTracker(int32 expiry = 0) : i_expiryTime(expiry) {}
136 void Update(int32 diff) { i_expiryTime -= diff; }
137 bool Passed() const { return (i_expiryTime <= 0); }
138 void Reset(int32 interval) { i_expiryTime = interval; }
139 int32 GetExpiry() const { return i_expiryTime; }
141 private:
142 int32 i_expiryTime;
145 #endif