]> git.mxchange.org Git - simgear.git/blob - simgear/timing/timestamp.hxx
Doxygen.
[simgear.git] / simgear / timing / timestamp.hxx
1 /**
2  * \file timestamp.hxx
3  * Provides a class for managing a timestamp (seconds & milliseconds.)
4  */
5
6 // Written by Curtis Olson, started December 1998.
7 //
8 // Copyright (C) 1998  Curtis L. Olson  - curt@flightgear.org
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 //
24 // $Id$
25
26
27 #ifndef _TIMESTAMP_HXX
28 #define _TIMESTAMP_HXX
29
30
31 #ifndef __cplusplus                                                          
32 # error This library requires C++
33 #endif                                   
34
35
36 #ifdef HAVE_CONFIG_H
37 #  include <config.h>
38 #endif
39
40 #ifdef HAVE_WINDOWS_H
41 #  include <windows.h>
42 #endif
43
44 #include <simgear/compiler.h>
45
46 #ifdef SG_HAVE_STD_INCLUDES
47 #  include <ctime>
48 #else
49 #  include <time.h>
50 #endif
51
52 #ifdef HAVE_SYS_TIMEB_H
53 #  include <sys/timeb.h> // for ftime() and struct timeb
54 #endif
55 #ifdef HAVE_UNISTD_H
56 #  include <unistd.h>    // for gettimeofday()
57 #endif
58 #ifdef HAVE_SYS_TIME_H
59 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
60 #endif
61
62 // -dw- want to use metrowerks time.h
63 #ifdef macintosh
64 #  include <time.h>
65 #  include <timer.h>
66 #endif
67
68 #ifdef WIN32
69 #  include <windows.h>
70 #  if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
71 #    define NEAR /* */
72 #    define FAR  /* */
73 #  endif
74 #  include <mmsystem.h>
75 #endif
76
77 // MSVC++ 6.0 kuldge - Need forward declaration of friends.
78 class SGTimeStamp;
79 SGTimeStamp operator + (const SGTimeStamp& t, const long& m);
80 long operator - (const SGTimeStamp& a, const SGTimeStamp& b);
81
82 /**
83  * The SGTimeStamp class allows you to mark and compare time stamps
84  * with microsecond accuracy (if your system has support for this
85  * level of accuracy.)
86  *
87  * The SGTimeStamp is useful for tracking the elapsed time of various
88  * events in your program. You can also use it to keep constistant
89  * motion across varying frame rates.
90  */
91
92 class SGTimeStamp {
93
94 private:
95
96     long seconds;
97     long usec;
98
99 public:
100
101     /** Default constructor */
102     SGTimeStamp();
103
104     /**
105      * This creates an instance of the SGTimeStamp object. When
106      * calling the constructor you may provide initial seconds an
107      * microseconds values.
108      * @param s initial seconds value
109      * @param m initial microseconds value
110      */
111     SGTimeStamp( const long s, const long m );
112     ~SGTimeStamp();
113
114     /** Update stored time to current time (seconds and microseconds) */
115     void stamp();
116
117     /** Compare two time stamps for equality */
118     SGTimeStamp& operator = ( const SGTimeStamp& t );
119
120     /**
121      * Increment the saved time by the specified number of microseconds
122      * @param t time stamp
123      * @param m microseconds increment
124      * @return new time stamp
125      */
126     friend SGTimeStamp operator + (const SGTimeStamp& t, const long& m);
127
128     /**
129      * Subtract two time stamps returning the difference in microseconds.
130      * @param a timestamp 1
131      * @param b timestame 2
132      * @return difference in microseconds
133      */
134     friend long operator - (const SGTimeStamp& a, const SGTimeStamp& b);
135
136     /** @return the saved seconds of this time stamp */
137     inline long get_seconds() const { return seconds; }
138
139     /** @return the saved microseconds of this time stamp */
140     inline long get_usec() const { return usec; }
141 };
142
143 inline SGTimeStamp::SGTimeStamp() {
144 }
145
146 inline SGTimeStamp::SGTimeStamp( const long s, const long u ) {
147     seconds = s;
148     usec = u;
149 }
150
151 inline SGTimeStamp::~SGTimeStamp() {
152 }
153
154 inline SGTimeStamp& SGTimeStamp::operator = (const SGTimeStamp& t)
155 {
156     seconds = t.seconds;
157     usec = t.usec;
158     return *this;
159 }
160
161 inline void SGTimeStamp::stamp() {
162 #if defined( WIN32 )
163     unsigned int t;
164     t = timeGetTime();
165     seconds = 0;
166     usec =  t * 1000;
167 #elif defined( HAVE_GETTIMEOFDAY )
168     struct timeval current;
169     struct timezone tz;
170     // sg_timestamp currtime;
171     gettimeofday(&current, &tz);
172     seconds = current.tv_sec;
173     usec = current.tv_usec;
174 #elif defined( HAVE_GETLOCALTIME )
175     SYSTEMTIME current;
176     GetLocalTime(&current);
177     seconds = current.wSecond;
178     usec = current.wMilliseconds * 1000;
179 #elif defined( HAVE_FTIME )
180     struct timeb current;
181     ftime(&current);
182     seconds = current.time;
183     usec = current.millitm * 1000;
184 // -dw- uses time manager
185 #elif defined( macintosh )
186     UnsignedWide ms;
187     Microseconds(&ms);
188         
189     seconds = ms.lo / 1000000;
190     usec = ms.lo - ( seconds * 1000000 );
191 #else
192 # error Port me
193 #endif
194 }
195
196 // increment the time stamp by the number of microseconds (usec)
197 inline SGTimeStamp operator + (const SGTimeStamp& t, const long& m) {
198 #ifdef WIN32
199     return SGTimeStamp( 0, t.usec + m );
200 #else
201     return SGTimeStamp( t.seconds + ( t.usec + m ) / 1000000,
202                         ( t.usec + m ) % 1000000 );
203 #endif
204 }
205
206 // difference between time stamps in microseconds (usec)
207 inline long operator - (const SGTimeStamp& a, const SGTimeStamp& b)
208 {
209 #if defined( WIN32 )
210     return a.usec - b.usec;
211 #else
212     return 1000000 * (a.seconds - b.seconds) + (a.usec - b.usec);
213 #endif
214 }
215
216
217 #endif // _TIMESTAMP_HXX
218
219