New repo for repo.or.cz
[The-Artvertiser.git] / artvertiser / FProfiler / FTime.h
blobbda9ab47d8eccfc974737df3db4ba8811e3cc59e
1 /*
2 Copyright 2007, 2008, 2009, 2010 Damian Stewart <damian@frey.co.nz>.
3 Distributed under the terms of the GNU General Public License v3.
5 This file is part of The Artvertiser.
7 The Artvertiser is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 The Artvertiser is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with The Artvertiser. If not, see <http://www.gnu.org/licenses/>.
21 #ifndef _FTIME_H
22 #define _FTIME_H
24 /** FTime
26 implements a high resolution timer
28 can store either a high-precision relative time or a high-precision absolute time
32 #ifdef __APPLE__
33 #define OSX
34 #endif
36 #ifdef OSX
37 #include <mach/mach_time.h>
38 #else
39 #include <time.h>
40 #endif
41 #include <stdio.h>
43 #include <assert.h>
45 class FTime
47 public:
48 FTime() {
49 #ifdef OSX
50 time = 0;
51 #else
52 time.tv_sec = 0; time.tv_nsec = 0;
53 #endif
54 last_update_time = 0.1f; };
55 ~FTime() { };
56 FTime( const FTime& other ) { Copy(other); }
58 // set us to the current time
59 void SetNow()
61 #ifdef OSX
62 time = mach_absolute_time();
63 #else
64 // clock_gettime(CLOCK_MONOTONIC, &ts); // Works on FreeBSD
65 clock_gettime(CLOCK_REALTIME, &time); // Works on Linux
66 #endif
69 // set us to the given time in seconds
70 void SetSeconds( double seconds );
72 // update our time to the current time, returning
73 // delta time in seconds as a float
74 double Update();
76 // get the last delta time returned by Update()
77 double GetLastUpdateTime() const { return last_update_time; }
79 // return milliseconds
80 double ToMillis() const;
81 // return seconds
82 double ToSeconds() const { return ToMillis() / 1000.0; }
85 // calculate difference
86 FTime operator-( const FTime& other ) const
88 // copy self
89 FTime t = *this;
90 // invoke operator -=
91 t -= other;
92 return t;
94 FTime& operator -= (const FTime& other )
96 #ifdef OSX
97 time -= other.time;
98 #else
99 //assert( false && "broken code, please fix ");
100 if ( time.tv_nsec < other.time.tv_nsec )
102 // will underflow
104 //printf("underflow: %10li:%10li - %10li:%10li = ", time.tv_sec, time.tv_nsec, other.time.tv_sec, other.time.tv_nsec );
105 time.tv_sec -= other.time.tv_sec + 1;
106 time.tv_nsec = 1e9 - (other.time.tv_nsec-time.tv_nsec);
107 //printf(" %10li:%10li\n", time.tv_sec, time.tv_nsec );
109 else
111 time.tv_sec -= other.time.tv_sec;
112 time.tv_nsec -= other.time.tv_nsec;
114 #endif
115 return *this;
118 // assignment
119 FTime& operator= (const FTime& other ) { Copy(other); return *this; }
121 // equality
122 bool operator== (const FTime& other ) const {
123 #ifdef OSX
124 return ( time == other.time );
125 #else
126 return time.tv_sec == other.time.tv_sec && time.tv_nsec == other.time.tv_nsec;
127 #endif
130 // compare
131 bool operator< (const FTime& other) const {
132 #ifdef OSX
133 return ( time < other.time );
134 #else
135 /*bool res =*/return (time.tv_sec < other.time.tv_sec) ||
136 (time.tv_sec == other.time.tv_sec && time.tv_nsec < other.time.tv_nsec );
138 printf("%f < %f: %s\n", ToSeconds(), other.ToSeconds(), res?"yes":"no");
139 return res;*/
140 #endif
143 private:
145 void Copy( const FTime& other )
147 if ( this != &other )
149 time = other.time;
154 #ifdef OSX
155 uint64_t time;
156 #else
157 timespec time;
158 #endif
159 double last_update_time;
163 //inline bool operator< (const FTime& a, const FTime& b) { return a.operator<(b); }
165 #endif