-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPosixTimespec.hpp
More file actions
173 lines (132 loc) · 5.81 KB
/
PosixTimespec.hpp
File metadata and controls
173 lines (132 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#if !defined POSIX_TIMESPEC_HPP
#define POSIX_TIMESPEC_HPP
#include <istream>
#include <ostream>
#include <sys/types.h>
#include <time.h>
class PosixTimespec
{
public:
friend class PosixTimespec_test;
// Initializes to 0s 0ns
PosixTimespec();
// Saves the proviced timespec internally
explicit PosixTimespec(const timespec& tp);
// Converts to timespec before saving
explicit PosixTimespec(double tp_sec);
// Copy constructor
PosixTimespec(const PosixTimespec&);
// Initializes to tv_sec tv_nsec
PosixTimespec(time_t tv_sec, long tv_nsec);
// For converting PosixTimespecs to other types
operator double() const;
operator timespec() const;
// Does nothing
~PosixTimespec();
// Returns internally-saved timespec
void getTimespec(timespec& tp) const;
// Sets internally-saved timespec
void setTimespec(const timespec& tp);
// Returns current value of timespec seconds field
time_t getSeconds() const;
// Sets the timespec seconds field
void setSeconds(time_t s);
// Returns the timespec nanoseconds field; unlike seconds this field is
// explicitly specified in POSIX as long
long getNanoseconds() const;
// Sets the timespec nanoseconds field; unlike seconds this field is
// explicitly specified in POSIX as long
void setNanoseconds(long ns);
// tp timespec to a double-precision floating point
double toDouble() const;
// Sets self based on provided double-precision floating point number
bool fromDouble(double tp_dbl);
PosixTimespec& operator=(double tp_dbl);
PosixTimespec& operator=(const timespec& tp);
PosixTimespec& operator=(const PosixTimespec&);
// Performs integer addition so no data loss occurs during the addition
// operation itself; tv_nsec overflows and adds to tv_sec as one would
// expect
PosixTimespec& operator+=(double tp_dbl);
PosixTimespec& operator+=(const timespec& tp);
PosixTimespec& operator+=(const PosixTimespec& tp);
// Performs integer subtraction so no data loss occurs during the
// subtraction operation itself; tv_nsec underflows and adds to tv_sec as
// one would expect
PosixTimespec& operator-=(double tp_dbl);
PosixTimespec& operator-=(const timespec& tp);
PosixTimespec& operator-=(const PosixTimespec& tp);
private:
// POSIX-defined
timespec tp;
static const unsigned int nanoseconds_per_second;
};
PosixTimespec operator+(PosixTimespec lhs, const PosixTimespec& rhs);
PosixTimespec operator+(PosixTimespec lhs, const timespec& rhs);
PosixTimespec operator+(timespec lhs, const PosixTimespec& rhs);
PosixTimespec operator+(PosixTimespec lhs, double rhs);
PosixTimespec operator+(double lhs, const PosixTimespec& rhs);
PosixTimespec operator-(PosixTimespec lhs, const PosixTimespec& rhs);
PosixTimespec operator-(PosixTimespec lhs, const timespec& rhs);
PosixTimespec operator-(timespec lhs, const PosixTimespec& rhs);
PosixTimespec operator-(PosixTimespec lhs, double rhs);
PosixTimespec operator-(double lhs, const PosixTimespec& rhs);
bool operator==(const PosixTimespec& lhs, const PosixTimespec& rhs);
bool operator==(const PosixTimespec& lhs, const timespec& rhs);
bool operator==(const timespec& lhs, const PosixTimespec& rhs);
bool operator==(PosixTimespec lhs, double rhs);
bool operator==(double lhs, const PosixTimespec& rhs);
bool operator!=(const PosixTimespec& lhs, const PosixTimespec& rhs);
bool operator!=(const PosixTimespec& lhs, const timespec& rhs);
bool operator!=(const timespec& lhs, const PosixTimespec& rhs);
bool operator!=(PosixTimespec lhs, double rhs);
bool operator!=(double lhs, const PosixTimespec& rhs);
bool operator<(const PosixTimespec& lhs, const PosixTimespec& rhs);
bool operator<(const PosixTimespec& lhs, const timespec& rhs);
bool operator<(const timespec& lhs, const PosixTimespec& rhs);
bool operator<(PosixTimespec lhs, double rhs);
bool operator<(double lhs, const PosixTimespec& rhs);
bool operator>(const PosixTimespec& lhs, const PosixTimespec& rhs);
bool operator>(const PosixTimespec& lhs, const timespec& rhs);
bool operator>(const timespec& lhs, const PosixTimespec& rhs);
bool operator>(PosixTimespec lhs, double rhs);
bool operator>(double lhs, const PosixTimespec& rhs);
bool operator<=(const PosixTimespec& lhs, const PosixTimespec& rhs);
bool operator<=(const PosixTimespec& lhs, const timespec& rhs);
bool operator<=(const timespec& lhs, const PosixTimespec& rhs);
bool operator<=(PosixTimespec lhs, double rhs);
bool operator<=(double lhs, const PosixTimespec& rhs);
bool operator>=(const PosixTimespec& lhs, const PosixTimespec& rhs);
bool operator>=(const PosixTimespec& lhs, const timespec& rhs);
bool operator>=(const timespec& lhs, const PosixTimespec& rhs);
bool operator>=(PosixTimespec lhs, double rhs);
bool operator>=(double lhs, const PosixTimespec& rhs);
// Writes a string representation
std::ostream& operator<<(std::ostream& os, PosixTimespec& posix_timespec);
// Reads a string representation
std::istream& operator>>(std::istream& is, PosixTimespec& posix_timespec);
inline void PosixTimespec::getTimespec(timespec& tp) const
{
tp = this->tp;
}
inline void PosixTimespec::setTimespec(const timespec& tp)
{
this->tp = tp;
}
inline time_t PosixTimespec::getSeconds() const
{
return tp.tv_sec;
}
inline void PosixTimespec::setSeconds(time_t s)
{
this->tp.tv_sec = s;
}
inline long PosixTimespec::getNanoseconds() const
{
return tp.tv_nsec;
}
inline void PosixTimespec::setNanoseconds(long ns)
{
this->tp.tv_nsec = ns;
}
#endif