Stable Tag: Copying everything over
[synfig.git] / ETL / tags / stable / ETL / _clock_gettimeofday.h
1 /*! ========================================================================
2 ** Extended Template and Library
3 ** gettimeofday() Clock Description Implementation
4 ** $Id$
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 **
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
12 **
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ** General Public License for more details.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** This is an internal header file, included by other ETL headers.
21 ** You should not attempt to use it directly.
22 **
23 ** ========================================================================= */
24
25 /* === S T A R T =========================================================== */
26
27 #ifndef __ETL__CLOCK_GETTIMEOFDAY_H
28 #define __ETL__CLOCK_GETTIMEOFDAY_H
29
30 /* === H E A D E R S ======================================================= */
31
32 #include <sys/time.h>
33 #include <cmath>
34
35 /* === M A C R O S ========================================================= */
36
37 /* === T Y P E D E F S ===================================================== */
38
39 /* === C L A S S E S & S T R U C T S ======================================= */
40
41 _ETL_BEGIN_NAMESPACE
42
43 class clock_desc_gettimeofday
44 {
45 public:
46         typedef double value_type;
47
48         inline static bool realtime()
49         { return true; }
50
51         inline static bool proctime()
52         { return false; }
53
54         inline static value_type
55         one_second()
56         { return 1.0f; }
57
58         inline static value_type precision()
59         { return one_second()/(value_type)1000000.0f; }
60
61         inline static const char *description()
62         { return "UNIX gettimeofday()"; };
63
64 protected:
65         class timestamp : public timeval
66         {
67                 timestamp(int sec, int usec)
68                 { tv_sec=sec; tv_usec=usec; }
69
70                 friend class clock_desc_gettimeofday;
71         public:
72                 timestamp() { }
73
74
75                 inline timestamp operator-(const timestamp &rhs)const
76                 {
77                         timestamp ret;
78                         ret.tv_usec=tv_usec-rhs.tv_usec;
79
80                         if(ret.tv_usec<0)
81                         {
82                                 ret.tv_sec=tv_sec-rhs.tv_sec-1;
83                                 ret.tv_usec+=1000000;
84                         }
85                         else
86                                 ret.tv_sec=tv_sec-rhs.tv_sec;
87                         return ret;
88                 }
89
90                 inline timestamp operator+(timestamp rhs)const
91                 {
92                         rhs.tv_usec+=tv_usec;
93
94                         if(rhs.tv_usec>1000000)
95                         {
96                                 rhs.tv_sec+=tv_sec+1;
97                                 rhs.tv_usec-=1000000;
98                         }
99                         else
100                                 rhs.tv_sec+=tv_sec;
101                         return rhs;
102                 }
103
104                 inline bool operator<(const timestamp &rhs)const
105                 { return tv_sec<rhs.tv_sec || tv_usec<rhs.tv_usec; }
106
107                 inline bool operator==(const timestamp &rhs)const
108                 { return tv_usec==rhs.tv_usec && tv_sec==rhs.tv_sec; }
109
110                 inline bool operator!=(const timestamp &rhs)const
111                 { return tv_usec!=rhs.tv_usec || tv_sec!=rhs.tv_sec; }
112         };
113
114         static void
115         get_current_time(timestamp &x)
116         { gettimeofday(&x,NULL);}
117
118         static timestamp
119         get_current_time()
120         { timestamp ret; get_current_time(ret); return ret; }
121
122         static value_type
123         timestamp_to_seconds(const timestamp &x)
124         { return (value_type)x.tv_sec + precision()*x.tv_usec; }
125
126         static timestamp
127         seconds_to_timestamp(const value_type &x)
128         { return timestamp((int)floor(x), (int)((x-floor(x))/precision()+0.5)); }
129 };
130
131 _ETL_END_NAMESPACE
132
133 /* === E N D =============================================================== */
134
135 #endif
136