1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6
7 This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25
26 ===========================================================================
27 */
28
29 #ifndef __TIMER_H__
30 #define __TIMER_H__
31
32 #include "idlib/containers/StrList.h"
33 #include "idlib/Lib.h"
34 #include "sys/sys_public.h"
35
36 /*
37 ===============================================================================
38
39 Clock tick counter. Should only be used for profiling.
40
41 ===============================================================================
42 */
43
44 class idTimer {
45 public:
46 idTimer( void );
47 idTimer( unsigned int ms );
48 ~idTimer( void );
49
50 idTimer operator+( const idTimer &t ) const;
51 idTimer operator-( const idTimer &t ) const;
52 idTimer & operator+=( const idTimer &t );
53 idTimer & operator-=( const idTimer &t );
54
55 void Start( void );
56 void Stop( void );
57 void Clear( void );
58 unsigned int Milliseconds( void ) const;
59
60 private:
61 enum {
62 TS_STARTED,
63 TS_STOPPED
64 } state;
65 unsigned int start;
66 unsigned int ms;
67 };
68
69 /*
70 =================
71 idTimer::idTimer
72 =================
73 */
idTimer(void)74 ID_INLINE idTimer::idTimer( void ) {
75 state = TS_STOPPED;
76 ms = 0;
77 }
78
79 /*
80 =================
81 idTimer::idTimer
82 =================
83 */
idTimer(unsigned int _ms)84 ID_INLINE idTimer::idTimer( unsigned int _ms ) {
85 state = TS_STOPPED;
86 ms = _ms;
87 }
88
89 /*
90 =================
91 idTimer::~idTimer
92 =================
93 */
~idTimer(void)94 ID_INLINE idTimer::~idTimer( void ) {
95 }
96
97 /*
98 =================
99 idTimer::operator+
100 =================
101 */
102 ID_INLINE idTimer idTimer::operator+( const idTimer &t ) const {
103 assert( state == TS_STOPPED && t.state == TS_STOPPED );
104 return idTimer( ms + t.ms );
105 }
106
107 /*
108 =================
109 idTimer::operator-
110 =================
111 */
112 ID_INLINE idTimer idTimer::operator-( const idTimer &t ) const {
113 assert( state == TS_STOPPED && t.state == TS_STOPPED );
114 return idTimer( ms - t.ms );
115 }
116
117 /*
118 =================
119 idTimer::operator+=
120 =================
121 */
122 ID_INLINE idTimer &idTimer::operator+=( const idTimer &t ) {
123 assert( state == TS_STOPPED && t.state == TS_STOPPED );
124 ms += t.ms;
125 return *this;
126 }
127
128 /*
129 =================
130 idTimer::operator-=
131 =================
132 */
133 ID_INLINE idTimer &idTimer::operator-=( const idTimer &t ) {
134 assert( state == TS_STOPPED && t.state == TS_STOPPED );
135 ms -= t.ms;
136 return *this;
137 }
138
139 /*
140 =================
141 idTimer::Start
142 =================
143 */
Start(void)144 ID_INLINE void idTimer::Start( void ) {
145 assert( state == TS_STOPPED );
146 state = TS_STARTED;
147 start = idLib::sys->GetMilliseconds();
148 }
149
150 /*
151 =================
152 idTimer::Stop
153 =================
154 */
Stop(void)155 ID_INLINE void idTimer::Stop( void ) {
156 assert( state == TS_STARTED );
157 ms += idLib::sys->GetMilliseconds() - start;
158 state = TS_STOPPED;
159 }
160
161 /*
162 =================
163 idTimer::Clear
164 =================
165 */
Clear(void)166 ID_INLINE void idTimer::Clear( void ) {
167 ms = 0;
168 }
169
170 /*
171 =================
172 idTimer::Milliseconds
173 =================
174 */
Milliseconds(void)175 ID_INLINE unsigned int idTimer::Milliseconds( void ) const {
176 assert( state == TS_STOPPED );
177 return ms;
178 }
179
180
181 /*
182 ===============================================================================
183
184 Report of multiple named timers.
185
186 ===============================================================================
187 */
188
189 class idTimerReport {
190 public:
191 idTimerReport( void );
192 ~idTimerReport( void );
193
194 void SetReportName( const char *name );
195 int AddReport( const char *name );
196 void Clear( void );
197 void Reset( void );
198 void PrintReport( void );
199 void AddTime( const char *name, idTimer *time );
200
201 private:
202 idList<idTimer*>timers;
203 idStrList names;
204 idStr reportName;
205 };
206
207 #endif /* !__TIMER_H__ */
208