- morning panick victory
[The-Artvertiser.git] / artvertiser / FProfiler / FTime.h
blobb6e7fa38a3efd2527e00be8738fcb80e228881ad
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 OSX
22 #include <mach/mach_time.h>
23 #else
24 #include <time.h>
25 #endif
26 #include <stdio.h>
28 class FTime
30 public:
31 FTime() {
32 #ifdef OSX
33 time = 0;
34 #else
35 time.tv_sec = 0; time.tv_nsec = 0;
36 #endif
37 last_update_time = 0.1f; };
38 ~FTime() { };
39 FTime( const FTime& other ) { Copy(other); }
41 // set us to the current time
42 void SetNow()
44 #ifdef OSX
45 time = mach_absolute_time();
46 #else
47 // clock_gettime(CLOCK_MONOTONIC, &ts); // Works on FreeBSD
48 clock_gettime(CLOCK_REALTIME, &time); // Works on Linux
49 #endif
52 // set us to the given time in seconds
53 void SetSeconds( double seconds );
55 // update our time to the current time, returning
56 // delta time in seconds as a float
57 double Update();
59 // get the last delta time returned by Update()
60 double GetLastUpdateTime() const { return last_update_time; }
62 // return milliseconds
63 double ToMillis() const;
64 // return seconds
65 double ToSeconds() const { return ToMillis() / 1000.0; }
68 // calculate difference
69 FTime operator-( const FTime& other ) const
71 // copy self
72 FTime t = *this;
73 // invoke operator -=
74 t -= other;
75 return t;
77 FTime& operator -= (const FTime& other )
79 #ifdef OSX
80 time -= other.time;
81 #else
82 //assert( false && "broken code, please fix ");
83 if ( time.tv_nsec < other.time.tv_nsec )
85 // will underflow
87 //printf("underflow: %10li:%10li - %10li:%10li = ", time.tv_sec, time.tv_nsec, other.time.tv_sec, other.time.tv_nsec );
88 time.tv_sec -= other.time.tv_sec + 1;
89 time.tv_nsec = 1e9 - (other.time.tv_nsec-time.tv_nsec);
90 //printf(" %10li:%10li\n", time.tv_sec, time.tv_nsec );
92 else
94 time.tv_sec -= other.time.tv_sec;
95 time.tv_nsec -= other.time.tv_nsec;
97 #endif
98 return *this;
101 // assignment
102 FTime& operator= (const FTime& other ) { Copy(other); return *this; }
104 // equality
105 bool operator== (const FTime& other ) const {
106 #ifdef OSX
107 assert( false && "implement me" );
108 #else
109 return time.tv_sec == other.time.tv_sec && time.tv_nsec == other.time.tv_nsec;
110 #endif
113 // compare
114 bool operator< (const FTime& other) const {
115 #ifdef OSX
116 assert(false&&"implement me" );
117 return false;
118 #else
119 /*bool res =*/return (time.tv_sec < other.time.tv_sec) ||
120 (time.tv_sec == other.time.tv_sec && time.tv_nsec < other.time.tv_nsec );
122 printf("%f < %f: %s\n", ToSeconds(), other.ToSeconds(), res?"yes":"no");
123 return res;*/
124 #endif
127 private:
129 void Copy( const FTime& other )
131 if ( this != &other )
133 time = other.time;
138 #ifdef OSX
139 uint64_t time;
140 #else
141 timespec time;
142 #endif
143 double last_update_time;
147 //inline bool operator< (const FTime& a, const FTime& b) { return a.operator<(b); }
149 #endif