-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimingBase.hpp
More file actions
138 lines (117 loc) · 2.81 KB
/
Copy pathTimingBase.hpp
File metadata and controls
138 lines (117 loc) · 2.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
#ifndef TIMINGBASE_HPP
#define TIMINGBASE_HPP
#include "Timing.hpp"
namespace Timing
{
template<typename T>
class TimingBase
{
public:
TimingBase()
{
sec_ = 0UL;
nsec_ = 0UL;
}
explicit TimingBase(uint32_t sec, uint32_t nanosec)
{
sec_ = sec;
nsec_ = nanosec;
}
virtual uint32_t nanosec()
{
return nsec_;
}
virtual uint32_t sec()
{
return sec_;
}
static T from_microsecs(uint32_t microseconds)
{
return T(microseconds / MiS, static_cast<uint32_t>((microseconds % MiS) * MS));
}
static T from_millisecs(uint32_t milliseconds)
{
return T(milliseconds / MS, static_cast<uint32_t>((milliseconds % MS) * MiS));
}
static T from_secs(double seconds)
{
uint32_t int_secs = static_cast<uint32_t>(seconds);
uint32_t nanos = static_cast<uint32_t>((seconds - int_secs) * NS);
return T(int_secs, nanos);
}
static T from_minutes(double minutes)
{
return T::from_secs((minutes * 60));
}
static T from_hours(double hours)
{
return T::from_minutes((hours * 60));
}
int compare(const T& that) const
{
int ret;
if(sec_ >= that.sec_ && (sec_ > that.sec_ || nsec_ > that.nsec_))
{
ret = 1;
}
else if(sec_ <= that.sec_ && (sec_ < that.sec_ || nsec_ < that.nsec_))
{
ret = -1;
}
else
{
ret = 0;
}
return ret;
}
template<class O>
bool operator >(const O& that) const
{
return sec_ >= that.sec_ && (sec_ > that.sec_ || nsec_ > that.nsec_);
}
template<class O>
bool operator >=(const O& that)
{
return sec() >= that.sec() && nanosec() >= that.nanosec();
}
template<class O>
bool operator ==(const O& that) const
{
return sec_ == that.sec_ && nsec_ == that.nsec_;
}
template<class O>
bool operator <=(const O& that) const
{
return sec_ <= that.sec_ && nsec_ <= that.nsec_;
}
template<class O>
bool operator <(const O& that) const
{
return sec_ <= that.sec_ && (sec_ < that.sec_ || nsec_ < that.nsec_);
}
uint32_t to_millisecs() const
{
return (static_cast<uint32_t>(sec_) * MS) + (nsec_ / MiS);
}
uint32_t to_microsecs() const
{
return (static_cast<uint32_t>(sec_) * MiS) + (nsec_ / MS);
}
double to_secs() const
{
return static_cast<double>(sec_) + (static_cast<double>(nsec_) / NS);
}
double to_minutes() const
{
return static_cast<double>(this->to_secs() / 60);
}
double to_hours() const
{
return static_cast<double>(this->to_minutes() / 60);
}
protected:
uint32_t sec_;
uint32_t nsec_;
};
}
#endif