compiles and seems to run; not properly tested
[The-Artvertiser.git] / artvertiser / FProfiler / FTime.h
blob37837738a69d6a0ea58fc6636d75b8afac4581d4
1 /*
2 * FTime.h
3 * F
5 * Created by damian on 21/03/07.
6 * Copyright 2007 __MyCompanyName__. All rights reserved.
8 */
10 #ifndef _FTIME_H
11 #define _FTIME_H
13 /** FTime
15 implements a high resolution timer
17 can store either a high-precision relative time or a high-precision absolute time
21 #ifdef __APPLE__
22 #define OSX
23 #endif
25 #ifdef OSX
26 #include <mach/mach_time.h>
27 #else
28 #include <time.h>
29 #endif
30 #include <stdio.h>
32 #include <assert.h>
34 class FTime
36 public:
37 FTime() {
38 #ifdef OSX
39 time = 0;
40 #else
41 time.tv_sec = 0; time.tv_nsec = 0;
42 #endif
43 last_update_time = 0.1f; };
44 ~FTime() { };
45 FTime( const FTime& other ) { Copy(other); }
47 // set us to the current time
48 void SetNow()
50 #ifdef OSX
51 time = mach_absolute_time();
52 #else
53 // clock_gettime(CLOCK_MONOTONIC, &ts); // Works on FreeBSD
54 clock_gettime(CLOCK_REALTIME, &time); // Works on Linux
55 #endif
58 // set us to the given time in seconds
59 void SetSeconds( double seconds );
61 // update our time to the current time, returning
62 // delta time in seconds as a float
63 double Update();
65 // get the last delta time returned by Update()
66 double GetLastUpdateTime() const { return last_update_time; }
68 // return milliseconds
69 double ToMillis() const;
70 // return seconds
71 double ToSeconds() const { return ToMillis() / 1000.0; }
74 // calculate difference
75 FTime operator-( const FTime& other ) const
77 // copy self
78 FTime t = *this;
79 // invoke operator -=
80 t -= other;
81 return t;
83 FTime& operator -= (const FTime& other )
85 #ifdef OSX
86 time -= other.time;
87 #else
88 //assert( false && "broken code, please fix ");
89 if ( time.tv_nsec < other.time.tv_nsec )
91 // will underflow
93 //printf("underflow: %10li:%10li - %10li:%10li = ", time.tv_sec, time.tv_nsec, other.time.tv_sec, other.time.tv_nsec );
94 time.tv_sec -= other.time.tv_sec + 1;
95 time.tv_nsec = 1e9 - (other.time.tv_nsec-time.tv_nsec);
96 //printf(" %10li:%10li\n", time.tv_sec, time.tv_nsec );
98 else
100 time.tv_sec -= other.time.tv_sec;
101 time.tv_nsec -= other.time.tv_nsec;
103 #endif
104 return *this;
107 // assignment
108 FTime& operator= (const FTime& other ) { Copy(other); return *this; }
110 // equality
111 bool operator== (const FTime& other ) const {
112 #ifdef OSX
113 assert( false && "implement me" );
114 #else
115 return time.tv_sec == other.time.tv_sec && time.tv_nsec == other.time.tv_nsec;
116 #endif
119 // compare
120 bool operator< (const FTime& other) const {
121 #ifdef OSX
122 assert(false&&"implement me" );
123 return false;
124 #else
125 /*bool res =*/return (time.tv_sec < other.time.tv_sec) ||
126 (time.tv_sec == other.time.tv_sec && time.tv_nsec < other.time.tv_nsec );
128 printf("%f < %f: %s\n", ToSeconds(), other.ToSeconds(), res?"yes":"no");
129 return res;*/
130 #endif
133 private:
135 void Copy( const FTime& other )
137 if ( this != &other )
139 time = other.time;
144 #ifdef OSX
145 uint64_t time;
146 #else
147 timespec time;
148 #endif
149 double last_update_time;
153 //inline bool operator< (const FTime& a, const FTime& b) { return a.operator<(b); }
155 #endif