OSVR-Core
TimeValueC.h
Go to the documentation of this file.
1 
14 /*
15 // Copyright 2014 Sensics, Inc.
16 //
17 // Licensed under the Apache License, Version 2.0 (the "License");
18 // you may not use this file except in compliance with the License.
19 // You may obtain a copy of the License at
20 //
21 // http://www.apache.org/licenses/LICENSE-2.0
22 //
23 // Unless required by applicable law or agreed to in writing, software
24 // distributed under the License is distributed on an "AS IS" BASIS,
25 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 // See the License for the specific language governing permissions and
27 // limitations under the License.
28 */
29 
30 #ifndef INCLUDED_TimeValueC_h_GUID_A02C6917_124D_4CB3_E63E_07F2DA7144E9
31 #define INCLUDED_TimeValueC_h_GUID_A02C6917_124D_4CB3_E63E_07F2DA7144E9
32 
33 /* Internal Includes */
34 #include <osvr/Util/APIBaseC.h>
36 #include <osvr/Util/BoolC.h>
37 #include <osvr/Util/Export.h>
38 #include <osvr/Util/PlatformConfig.h>
39 #include <osvr/Util/StdInt.h>
40 
41 /* Library/third-party includes */
42 /* none */
43 
44 /* Standard includes */
45 /* none */
46 
47 OSVR_EXTERN_C_BEGIN
48 
66 typedef int64_t OSVR_TimeValue_Seconds;
70 
81 typedef struct OSVR_TimeValue {
87 
88 #ifdef OSVR_HAVE_STRUCT_TIMEVAL
89 
90 OSVR_UTIL_EXPORT void osvrTimeValueGetNow(OSVR_OUT OSVR_TimeValue *dest)
91  OSVR_FUNC_NONNULL((1));
92 
93 struct timeval; /* forward declaration */
94 
103 OSVR_UTIL_EXPORT void
104 osvrTimeValueToStructTimeval(OSVR_OUT struct timeval *dest,
105  OSVR_IN_PTR const OSVR_TimeValue *src)
106  OSVR_FUNC_NONNULL((1, 2));
107 
117 OSVR_UTIL_EXPORT void
118 osvrStructTimevalToTimeValue(OSVR_OUT OSVR_TimeValue *dest,
119  OSVR_IN_PTR const struct timeval *src)
120  OSVR_FUNC_NONNULL((1, 2));
121 #endif
122 
130 OSVR_UTIL_EXPORT void osvrTimeValueNormalize(OSVR_INOUT_PTR OSVR_TimeValue *tv)
131  OSVR_FUNC_NONNULL((1));
132 
142 OSVR_UTIL_EXPORT void osvrTimeValueSum(OSVR_INOUT_PTR OSVR_TimeValue *tvA,
143  OSVR_IN_PTR const OSVR_TimeValue *tvB)
144  OSVR_FUNC_NONNULL((1, 2));
145 
158 OSVR_UTIL_EXPORT void
160  OSVR_IN_PTR const OSVR_TimeValue *tvB)
161  OSVR_FUNC_NONNULL((1, 2));
162 
169 OSVR_UTIL_EXPORT int osvrTimeValueCmp(OSVR_IN_PTR const OSVR_TimeValue *tvA,
170  OSVR_IN_PTR const OSVR_TimeValue *tvB)
171  OSVR_FUNC_NONNULL((1, 2));
172 
173 OSVR_EXTERN_C_END
174 
184 OSVR_INLINE double
186  OSVR_IN_PTR const OSVR_TimeValue *tvB) {
187  OSVR_TimeValue A = *tvA;
188  osvrTimeValueDifference(&A, tvB);
189  double dt = A.seconds + A.microseconds / 1000000.0;
190  return dt;
191 }
192 
194 OSVR_INLINE OSVR_CBool
196  OSVR_IN_PTR const OSVR_TimeValue *tvB) {
197  if (!tvA || !tvB) {
198  return OSVR_FALSE;
199  }
200  return ((tvA->seconds > tvB->seconds) ||
201  ((tvA->seconds == tvB->seconds) &&
202  (tvA->microseconds > tvB->microseconds)))
203  ? OSVR_TRUE
204  : OSVR_FALSE;
205 }
206 
207 #ifdef __cplusplus
208 
209 #include <cassert>
210 #include <cmath>
211 
213 inline bool osvrTimeValueIsNormalized(const OSVR_TimeValue &tv) {
214 #ifdef _LIBCPP_VERSION
215  // libcxx has only floating-point abs and has several, so it makes this
216  // ambiguous otherwise
217  return std::abs(double(tv.microseconds)) < 1000000 &&
218 #else
219  return (std::abs(tv.microseconds) < 1000000) &&
220 #endif
221  (
222  // zeros can be paired with a positive or negative
223  (tv.seconds == 0) || (tv.microseconds == 0) ||
224  // if both non-zero, then both must have same sign.
225  ((tv.seconds > 0) == (tv.microseconds > 0)));
226 }
227 
229 inline bool osvrTimeValueGreater(const OSVR_TimeValue &tvA,
230  const OSVR_TimeValue &tvB) {
231  assert(osvrTimeValueIsNormalized(tvA) &&
232  "First timevalue argument to comparison was not normalized!");
233  assert(osvrTimeValueIsNormalized(tvB) &&
234  "Second timevalue argument to comparison was not normalized!");
235  return (tvA.seconds > tvB.seconds) ||
236  (tvA.seconds == tvB.seconds && tvA.microseconds > tvB.microseconds);
237 }
238 
240 inline bool operator>(const OSVR_TimeValue &tvA, const OSVR_TimeValue &tvB) {
241  return osvrTimeValueGreater(tvA, tvB);
242 }
243 
245 inline bool operator<(const OSVR_TimeValue &tvA, const OSVR_TimeValue &tvB) {
246  // Change the order of arguments before forwarding.
247  return osvrTimeValueGreater(tvB, tvA);
248 }
249 
251 inline bool operator==(const OSVR_TimeValue &tvA, const OSVR_TimeValue &tvB) {
252  assert(
253  osvrTimeValueIsNormalized(tvA) &&
254  "First timevalue argument to equality comparison was not normalized!");
255  assert(
256  osvrTimeValueIsNormalized(tvB) &&
257  "Second timevalue argument to equality comparison was not normalized!");
258  return (tvA.seconds == tvB.seconds) &&
259  (tvA.microseconds == tvB.microseconds);
260 }
262 inline bool operator!=(const OSVR_TimeValue &tvA, const OSVR_TimeValue &tvB) {
263  assert(osvrTimeValueIsNormalized(tvA) && "First timevalue argument to "
264  "inequality comparison was not "
265  "normalized!");
266  assert(osvrTimeValueIsNormalized(tvB) && "Second timevalue argument to "
267  "inequality comparison was not "
268  "normalized!");
269  return (tvA.seconds != tvB.seconds) ||
270  (tvA.microseconds != tvB.microseconds);
271 }
272 #endif
273 
276 #endif
OSVR_UTIL_EXPORT void OSVR_UTIL_EXPORT void OSVR_UTIL_EXPORT int osvrTimeValueCmp(OSVR_IN_PTR const OSVR_TimeValue *tvA, OSVR_IN_PTR const OSVR_TimeValue *tvB) OSVR_FUNC_NONNULL((1
Compares two time values (assumed to be normalized), returning the same values as strcmp...
uint8_t OSVR_CBool
A pre-C99-safe bool type.
Definition: BoolC.h:50
#define OSVR_FALSE
Canonical "false" value for OSVR_CBool.
Definition: BoolC.h:54
#define OSVR_IN_PTR
Indicates a required pointer (non-null) function parameter that serves only as input.
Definition: AnnotationMacrosC.h:108
Header providing a C-safe "bool" type, because we can&#39;t depend on Visual Studio providing proper C99 ...
Header wrapping the C99 standard stdint header.
OSVR_UTIL_EXPORT void OSVR_UTIL_EXPORT void OSVR_UTIL_EXPORT int OSVR_EXTERN_C_END OSVR_INLINE double osvrTimeValueDurationSeconds(OSVR_IN_PTR const OSVR_TimeValue *tvA, OSVR_IN_PTR const OSVR_TimeValue *tvB)
Compute the difference between the two time values, returning the duration as a double-precision floa...
Definition: TimeValueC.h:185
int32_t OSVR_TimeValue_Microseconds
The signed integer type storing the microseconds in a struct OSVR_TimeValue.
Definition: TimeValueC.h:69
#define OSVR_FUNC_NONNULL(X)
Indicates the parameter(s) that must be non-null.
Definition: AnnotationMacrosC.h:202
#define OSVR_TRUE
Canonical "true" value for OSVR_CBool.
Definition: BoolC.h:52
#define OSVR_OUT
Indicates a required function parameter that serves only as output.
Definition: AnnotationMacrosC.h:143
OSVR_UTIL_EXPORT void OSVR_UTIL_EXPORT void osvrTimeValueDifference(OSVR_INOUT_PTR OSVR_TimeValue *tvA, OSVR_IN_PTR const OSVR_TimeValue *tvB) OSVR_FUNC_NONNULL((1
Computes the difference between two time values, replacing the first with the result.
OSVR_UTIL_EXPORT void osvrTimeValueNormalize(OSVR_INOUT_PTR OSVR_TimeValue *tv) OSVR_FUNC_NONNULL((1))
"Normalizes" a time value so that the absolute number of microseconds is less than 1...
Definition: TimeValueC.cpp:46
int64_t OSVR_TimeValue_Seconds
The signed integer type storing the seconds in a struct OSVR_TimeValue.
Definition: TimeValueC.h:66
#define OSVR_INOUT_PTR
Indicates a required pointer (non-null) function parameter that is both read and written to...
Definition: AnnotationMacrosC.h:178
Header providing basic C macros for defining API headers.
OSVR_INLINE OSVR_CBool osvrTimeValueGreater(OSVR_IN_PTR const OSVR_TimeValue *tvA, OSVR_IN_PTR const OSVR_TimeValue *tvB)
True if A is later than B.
Definition: TimeValueC.h:195
OSVR_TimeValue_Microseconds microseconds
Microseconds portion of the time value.
Definition: TimeValueC.h:85
Header containing macros for source-level annotation.
OSVR_UTIL_EXPORT void osvrTimeValueSum(OSVR_INOUT_PTR OSVR_TimeValue *tvA, OSVR_IN_PTR const OSVR_TimeValue *tvB) OSVR_FUNC_NONNULL((1
Sums two time values, replacing the first with the result.
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
struct OSVR_TimeValue OSVR_TimeValue
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
OSVR_TimeValue_Seconds seconds
Seconds portion of the time value.
Definition: TimeValueC.h:83