Imported Upstream version 3.2.2
[debian/gnuradio] / omnithread / gnuradio / omni_time.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 #ifndef INCLUDED_OMNI_TIME_H
22 #define INCLUDED_OMNI_TIME_H
23
24 struct omni_time {
25   long int d_secs;      // seconds.
26   long int d_nsecs;     // nanoseconds.  Always in [0, 1e9-1]
27
28   omni_time() : d_secs(0), d_nsecs(0) {}
29   omni_time(long secs, long nanosecs=0) : d_secs(secs), d_nsecs(nanosecs) {}
30
31   // N.B., this only makes sense for differences between times.
32   // Double doesn't have enough bits to precisely represent an absolute time.
33   omni_time(double secs);
34
35   // N.B. This only makes sense for differences between times.
36   // Double doesn't have enough bits to precisely represent an absolute time.
37   double double_time() const { return (double)d_secs + d_nsecs * 1e-9; }
38
39   /*!
40    * \brief Return an absolute time suitable for use with
41    * schedule_one_shot_timeout & schedule_periodic_timeout
42    *
43    * The return value is the current time plus the given relative offset.
44    */
45   static omni_time time(const omni_time &relative_offset = omni_time());
46 };
47
48
49 inline static bool
50 operator<(const omni_time &x, const omni_time &y)
51 {
52   return ((x.d_secs < y.d_secs)
53           || (x.d_secs == y.d_secs && x.d_nsecs < y.d_nsecs));
54 }
55
56 inline static bool
57 operator>(const omni_time &x, const omni_time &y)
58 {
59   return ((x.d_secs > y.d_secs)
60           || (x.d_secs == y.d_secs && x.d_nsecs > y.d_nsecs));
61 }
62
63 inline static bool
64 operator>=(const omni_time &x, const omni_time &y)
65 {
66   return ((x.d_secs > y.d_secs)
67           || (x.d_secs == y.d_secs && x.d_nsecs >= y.d_nsecs));
68 }
69
70 inline static bool
71 operator<=(const omni_time &x, const omni_time &y)
72 {
73   return ((x.d_secs < y.d_secs)
74           || (x.d_secs == y.d_secs && x.d_nsecs <= y.d_nsecs));
75 }
76
77 inline static bool
78 operator==(const omni_time &x, const omni_time &y)
79 {
80   return (x.d_secs == y.d_secs && x.d_nsecs == y.d_nsecs);
81 }
82
83
84 omni_time operator+(const omni_time &x, const omni_time &y);
85 omni_time operator+(const omni_time &x, double y);
86 omni_time operator-(const omni_time &x, const omni_time &y);
87 omni_time operator-(const omni_time &x, double y);
88
89 #endif /* INCLUDED_OMNI_TIME_H */