-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.cpp
More file actions
164 lines (131 loc) · 4.94 KB
/
Copy pathClock.cpp
File metadata and controls
164 lines (131 loc) · 4.94 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
#include "../include/Clock.hpp"
//============================================================================================================
//Class Clock
//============================================================================================================
Clock::TimeStamp Clock::get_current_time()
{
return TimeStamp();
}
//============================================================================================================
//Class Clock::Duration
//============================================================================================================
Clock::Duration::Duration() :
m_duration(std::chrono::microseconds(0))
{}
Clock::Duration::Duration(const std::chrono::duration<long, std::micro>& arg) :
m_duration(arg)
{}
const Clock::Duration& Clock::Duration::operator+=(const Duration &d) {
this->m_duration += d.m_duration;
return *this;
}
const Clock::Duration& Clock::Duration::operator-=(const Duration &d) {
this->m_duration -= d.m_duration;
return *this;
}
const Clock::Duration& Clock::Duration::operator*=(const int rhs) {
this->m_duration = m_duration * rhs;
return *this;
}
const Clock::Duration& Clock::Duration::operator/=(const int rhs) {
this->m_duration = m_duration / rhs;
return *this;
}
bool Clock::Duration::operator==(const Duration & rhs) const {
return this->m_duration == rhs.m_duration;
}
bool Clock::Duration::operator!=(const Duration & rhs) const {
return !(this->m_duration == rhs.m_duration);
}
bool Clock::Duration::operator<(const Duration & rhs) const {
return this->m_duration < rhs.m_duration;
}
bool Clock::Duration::operator<=(const Duration & rhs) const {
return this->m_duration <= rhs.m_duration;
}
bool Clock::Duration::operator>(const Duration & rhs) const {
return this->m_duration > rhs.m_duration;
}
bool Clock::Duration::operator>=(const Duration & rhs) const {
return this->m_duration >= rhs.m_duration;
}
Clock::Duration Clock::Duration::operator+(const Clock::Duration & rhs) const {
return this->m_duration + rhs.m_duration;
}
Clock::Duration Clock::Duration::operator-(const Clock::Duration & rhs) const {
return this->m_duration - rhs.m_duration;
}
Clock::Duration Clock::Duration::operator*(const int rhs) const {
return this->m_duration * rhs;
}
Clock::Duration Clock::Duration::operator/(const int rhs) const {
return this->m_duration / rhs;
}
//============================================================================================================
//Class Clock::TimeStamp
//============================================================================================================
Clock::TimeStamp::TimeStamp() :
m_time_stamp(std::chrono::high_resolution_clock::now())
{}
Clock::TimeStamp::TimeStamp
(const std::chrono::time_point<std::chrono::high_resolution_clock>& arg) :
m_time_stamp(arg)
{}
bool Clock::TimeStamp::operator==(const Clock::TimeStamp& rhs) const {
return this->m_time_stamp == rhs.m_time_stamp;
}
bool Clock::TimeStamp::operator!=(const Clock::TimeStamp& rhs) const {
return !(this->m_time_stamp == rhs.m_time_stamp);
}
bool Clock::TimeStamp::operator<(const Clock::TimeStamp& rhs) const {
return this->m_time_stamp < rhs.m_time_stamp;
}
bool Clock::TimeStamp::operator<=(const Clock::TimeStamp& rhs) const {
return this->m_time_stamp <= rhs.m_time_stamp;
}
bool Clock::TimeStamp::operator>(const Clock::TimeStamp& rhs) const {
return this->m_time_stamp > rhs.m_time_stamp;
}
bool Clock::TimeStamp::operator>=(const Clock::TimeStamp& rhs) const {
return this->m_time_stamp >= rhs.m_time_stamp;
}
Clock::Duration Clock::TimeStamp::operator-
(const Clock::TimeStamp& rhs) const {
return Duration(std::chrono::duration_cast<std::chrono::microseconds>
(this->m_time_stamp - rhs.m_time_stamp));
}
//============================================================================================================
//Friend functies
//============================================================================================================
Clock::TimeStamp& operator+=
(Clock::TimeStamp& lhs, const Clock::Duration& d) {
lhs.m_time_stamp = lhs.m_time_stamp + d.m_duration;
return lhs;
}
Clock::TimeStamp& operator-=
(Clock::TimeStamp& lhs, const Clock::Duration& d) {
lhs.m_time_stamp = lhs.m_time_stamp - d.m_duration;
return lhs;
}
Clock::TimeStamp operator+
(const Clock::TimeStamp& lhs, const Clock::Duration& rhs) {
return lhs.m_time_stamp + rhs.m_duration;
}
Clock::TimeStamp operator-
(const Clock::TimeStamp& lhs, const Clock::Duration& rhs) {
return lhs.m_time_stamp - rhs.m_duration;
}
Clock::TimeStamp operator+
(const Clock::Duration& lhs, const Clock::TimeStamp& rhs) {
return rhs.m_time_stamp + lhs.m_duration;
}
std::ostream & operator<<(std::ostream & s, const Clock::Duration & rhs) {
s << rhs.m_duration.count();
return s;
}
std::ostream & operator<<(std::ostream & s, const Clock::TimeStamp &rhs) {
std::chrono::microseconds ms =
std::chrono::duration_cast<std::chrono::microseconds>(rhs.m_time_stamp.time_since_epoch());
s << ms.count();
return s;
}