-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelementary_datatypes.h
179 lines (140 loc) · 4.55 KB
/
elementary_datatypes.h
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
174
175
176
177
178
179
//
// Created by bdev on 11/4/19.
//
#ifndef PLC_EMULATOR__ELEMENTARY_DATATYPES_H_
#define PLC_EMULATOR__ELEMENTARY_DATATYPES_H_
namespace plc_emulator {
struct BaseType {
BaseType &operator+(const BaseType &rhs) {
return *this;
}
BaseType &operator-(const BaseType &rhs) {
return *this;
}
BaseType &operator/(const BaseType &rhs) {
return *this;
}
BaseType &operator%(const BaseType &rhs) {
return *this;
}
BaseType &operator*(const BaseType &rhs) {
return *this;
}
BaseType &operator&(const BaseType &rhs) {
return *this;
}
BaseType &operator|(const BaseType &rhs) {
return *this;
}
friend bool operator>(const BaseType &lhs, const BaseType &rhs) {
return false;
}
friend bool operator>=(const BaseType &lhs, const BaseType &rhs) {
return false;
}
friend bool operator==(const BaseType &lhs, const BaseType &rhs) {
return false;
}
friend bool operator<(const BaseType &lhs, const BaseType &rhs) {
return false;
}
friend bool operator<=(const BaseType &lhs, const BaseType &rhs) {
return false;
}
};
struct DateType : BaseType {
int year;
int month;
int day;
friend bool operator>(const DateType &lhs, const DateType &rhs) {
return lhs.year > rhs.year ||
(lhs.year == rhs.year && lhs.month > rhs.month) ||
(lhs.year == rhs.year && lhs.month == rhs.month
&& lhs.day > rhs.day);
}
friend bool operator>=(const DateType &lhs, const DateType &rhs) {
return lhs.year > rhs.year ||
(lhs.year == rhs.year && lhs.month > rhs.month) ||
(lhs.year == rhs.year && lhs.month == rhs.month
&& lhs.day >= rhs.day);
}
friend bool operator==(const DateType &lhs, const DateType &rhs) {
return (lhs.year == rhs.year && lhs.month == rhs.month
&& lhs.day == rhs.day);
}
friend bool operator<(const DateType &lhs, const DateType &rhs) {
return lhs.year < rhs.year ||
(lhs.year == rhs.year && lhs.month < rhs.month) ||
(lhs.year == rhs.year && lhs.month == rhs.month
&& lhs.day < rhs.day);
}
friend bool operator<=(const DateType &lhs, const DateType &rhs) {
return lhs.year < rhs.year ||
(lhs.year == rhs.year && lhs.month < rhs.month) ||
(lhs.year == rhs.year && lhs.month == rhs.month
&& lhs.day <= rhs.day);
}
};
typedef DateType Date;
struct TimeType : BaseType {
int hour;
int minute;
int second;
[[nodiscard]] int TotalSeconds() const {
return hour * 3600 + minute * 60 + second;
}
TimeType &operator+(const TimeType &rhs) {
int total_seconds = this->TotalSeconds() + rhs.TotalSeconds();
hour = total_seconds / 3600;
minute = (total_seconds % 3600) / 60;
second = total_seconds % 60;
return *this;
}
TimeType &operator-(const TimeType &rhs) {
int total_seconds = this->TotalSeconds() - rhs.TotalSeconds();
hour = total_seconds / 3600;
minute = (total_seconds % 3600) / 60;
second = total_seconds % 60;
return *this;
}
friend bool operator>(const TimeType &lhs, const TimeType &rhs) {
return lhs.TotalSeconds() > rhs.TotalSeconds();
}
friend bool operator>=(const TimeType &lhs, const TimeType &rhs) {
return lhs.TotalSeconds() >= rhs.TotalSeconds();
}
friend bool operator==(const TimeType &lhs, const TimeType &rhs) {
return lhs.TotalSeconds() == rhs.TotalSeconds();
}
friend bool operator<(const TimeType &lhs, const TimeType &rhs) {
return lhs.TotalSeconds() < rhs.TotalSeconds();
}
friend bool operator<=(const TimeType &lhs, const TimeType &rhs) {
return lhs.TotalSeconds() <= rhs.TotalSeconds();
}
};
typedef TimeType Time;
struct DateTimeType : BaseType {
Date date;
Time time;
friend bool operator>(const DateTimeType &lhs, const DateTimeType &rhs) {
return lhs.date > rhs.date || (lhs.date == rhs.date && lhs.time > rhs.time);
}
friend bool operator>=(const DateTimeType &lhs, const DateTimeType &rhs) {
return lhs.date > rhs.date
|| (lhs.date == rhs.date && lhs.time >= rhs.time);
}
friend bool operator==(const DateTimeType &lhs, const DateTimeType &rhs) {
return (lhs.date == rhs.date && lhs.time == rhs.time);
}
friend bool operator<(const DateTimeType &lhs, const DateTimeType &rhs) {
return lhs.date < rhs.date || (lhs.date == rhs.date && lhs.time < rhs.time);
}
friend bool operator<=(const DateTimeType &lhs, const DateTimeType &rhs) {
return lhs.date < rhs.date
|| (lhs.date == rhs.date && lhs.time <= rhs.time);
}
};
typedef DateTimeType DateTime;
}
#endif //PLC_EMULATOR__ELEMENTARY_DATATYPES_H_