More gracefully handle type mismatch errors when doing playlist things (just ignore...
[ardour.git] / libs / glibmm2 / glib / glibmm / timeval.h
1 // -*- c++ -*-
2 #ifndef _GLIBMM_TIMEVAL_H
3 #define _GLIBMM_TIMEVAL_H
4
5 /* $Id: timeval.h 2 2003-01-07 16:59:16Z murrayc $ */
6
7 /* timeval.h
8  *
9  * Copyright (C) 2002 The gtkmm Development Team
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the Free
23  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #include <glib/gtypes.h>
27 #include <glib/gtimer.h>
28
29 namespace Glib
30 {
31
32 /** Glib::TimeVal is a wrapper around the glib structure GTimeVal.
33  * The glib structure GTimeVal itself is equivalent to struct timeval,
34  * which is returned by the gettimeofday() UNIX call. Additionally
35  * this wrapper provides an assortment of time manipulation functions.
36  */
37 struct TimeVal : public GTimeVal
38 {
39   inline TimeVal();
40   inline TimeVal(long seconds, long microseconds);
41
42   inline TimeVal(const GTimeVal& gtimeval);
43   inline TimeVal& operator=(const GTimeVal& gtimeval);
44
45   /** Assigns the current time to the TimeVal instance.
46    * Equivalent to the UNIX gettimeofday() function, but is portable and 
47    * works also on Win32. 
48    */
49   void assign_current_time();
50
51   void add(const TimeVal& rhs);
52   void subtract(const TimeVal& rhs);
53   void add_seconds(long seconds);
54   void subtract_seconds(long seconds);
55   void add_milliseconds(long milliseconds);
56   void subtract_milliseconds(long milliseconds);
57   void add_microseconds(long microseconds);
58   void subtract_microseconds(long microseconds);
59
60   inline TimeVal& operator+=(const TimeVal& gtimeval);
61   inline TimeVal& operator-=(const TimeVal& gtimeval);
62   inline TimeVal& operator+=(long seconds);
63   inline TimeVal& operator-=(long seconds);
64
65   /** Returns a double representation of the time interval.
66    * This member function converts the time interval, that is
67    * internally stored as two long values for seconds and microseconds,
68    * to a double representation, whose unit is seconds.
69    */
70   inline double as_double() const;
71
72   inline bool negative() const;
73
74   /** Checks whether the stored time interval is positive.
75    * Returns true if the stored time / time interval is positive.
76    */
77   inline bool valid() const;
78 };
79
80 inline
81 TimeVal::TimeVal()
82 {
83   tv_sec  = 0;
84   tv_usec = 0;
85 }
86
87 inline
88 TimeVal::TimeVal(long seconds, long microseconds)
89 {
90   tv_sec  = seconds;
91   tv_usec = microseconds;
92 }
93
94 inline
95 TimeVal::TimeVal(const GTimeVal& gtimeval)
96 {
97   tv_sec  = gtimeval.tv_sec;
98   tv_usec = gtimeval.tv_usec;
99 }
100
101 inline
102 TimeVal& TimeVal::operator=(const GTimeVal& gtimeval)
103 {
104   tv_sec  = gtimeval.tv_sec;
105   tv_usec = gtimeval.tv_usec;
106   return *this;
107 }
108
109 inline
110 TimeVal& TimeVal::operator+=(const TimeVal& gtimeval)
111 {
112   add(gtimeval);
113
114   return *this;
115 }
116
117 inline
118 TimeVal& TimeVal::operator-=(const TimeVal& gtimeval)
119 {
120   subtract(gtimeval);
121
122   return *this;
123 }
124
125 inline
126 TimeVal& TimeVal::operator+=(long seconds)
127 {
128   add_seconds(seconds);
129
130   return *this;
131 }
132
133 inline
134 TimeVal& TimeVal::operator-=(long seconds)
135 {
136   subtract_seconds(seconds);
137
138   return *this;
139 }
140
141 inline
142 double TimeVal::as_double() const
143 {
144   return tv_sec + ((double) tv_usec / (double) G_USEC_PER_SEC);
145 }
146
147 inline
148 bool TimeVal::negative() const
149 {
150   return (tv_sec < 0);
151 }
152
153 inline
154 bool TimeVal::valid() const
155 {
156   return (tv_usec >= 0 && tv_usec < G_USEC_PER_SEC);
157 }
158
159 /** @relates Glib::TimeVal */
160 inline
161 TimeVal operator+(const TimeVal& lhs, const TimeVal& rhs)
162 { return TimeVal(lhs) += rhs; }
163
164 /** @relates Glib::TimeVal */
165 inline 
166 TimeVal operator+(const TimeVal& lhs, long seconds)
167 { return TimeVal(lhs) += seconds; }
168
169 /** @relates Glib::TimeVal */
170 inline 
171 TimeVal operator-(const TimeVal& lhs, const TimeVal& rhs)
172 { return TimeVal(lhs) -= rhs; }
173
174 /** @relates Glib::TimeVal */
175 inline 
176 TimeVal operator-(const TimeVal& lhs, long seconds)
177 { return TimeVal(lhs) -= seconds; }
178
179
180 /** @relates Glib::TimeVal */
181 inline
182 bool operator==(const TimeVal& lhs, const TimeVal& rhs)
183 {
184   return (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec == rhs.tv_usec);
185 }
186
187 /** @relates Glib::TimeVal */
188 inline
189 bool operator!=(const TimeVal& lhs, const TimeVal& rhs)
190 {
191   return (lhs.tv_sec != rhs.tv_sec || lhs.tv_usec != rhs.tv_usec);
192 }
193
194 /** @relates Glib::TimeVal */
195 inline
196 bool operator<(const TimeVal& lhs, const TimeVal& rhs)
197 {
198   return ((lhs.tv_sec < rhs.tv_sec) ||
199           (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec < rhs.tv_usec));
200 }
201
202 /** @relates Glib::TimeVal */
203 inline
204 bool operator>(const TimeVal& lhs, const TimeVal& rhs)
205 {
206   return ((lhs.tv_sec > rhs.tv_sec) ||
207           (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec > rhs.tv_usec));
208 }
209
210 /** @relates Glib::TimeVal */
211 inline
212 bool operator<=(const TimeVal& lhs, const TimeVal& rhs)
213 {
214   return ((lhs.tv_sec < rhs.tv_sec) ||
215           (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec <= rhs.tv_usec));
216 }
217
218 /** @relates Glib::TimeVal */
219 inline
220 bool operator>=(const TimeVal& lhs, const TimeVal& rhs)
221 {
222   return ((lhs.tv_sec > rhs.tv_sec) ||
223           (lhs.tv_sec == rhs.tv_sec && lhs.tv_usec >= rhs.tv_usec));
224 }
225
226 } // namespace Glib
227
228
229 #endif /* _GLIBMM_TIMEVAL_H */
230
231
232
233