-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNullable.h
80 lines (63 loc) · 2 KB
/
Nullable.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
#pragma once
template <typename T>
class Nullable {
public:
Nullable(T val) : value(val) {}
Nullable() {
value = GetNullValue();
}
void SetNull() {
value = GetNullValue();
}
Nullable<T> operator+(const Nullable<T>& other) const {
if (!HasValue() || !other.HasValue())
return Nullable<T>();
else return Nullable<T>(value + other.value);
}
Nullable<T> operator+(const T& other) const {
return operator+(Nullable<T>(other));
}
bool operator==(const Nullable<T>& other) {
return value == other.value;
}
bool operator!=(const Nullable<T>& other) {
return !(operator==(other));
}
bool operator<=(const Nullable<T>& other) {
if (!HasValue())
return true;
else return value <= other.value;
}
bool operator<=(const T& other) {
return operator<=(Nullable<T>(other));
}
bool operator>=(const Nullable<T>& other) {
if (!HasValue())
return false;
else return value >= other.value;
}
bool operator>=(const T& other) {
return operator>=(Nullable<T>(other));
}
template <typename U>
friend std::ostream& operator<< (std::ostream & os, const Nullable<U> & obj);
Nullable<T> Max(const Nullable<T>& other) const {
if (!HasValue())
return other;
else if (!other.HasValue())
return *this;
else return Nullable<T>(std::max(value, other.value));
}
Nullable<T> Min(const Nullable<T>& other) const {
if (!HasValue())
return other;
else if (!other.HasValue())
return *this;
else return Nullable<T>(std::min(value, other.value));
}
bool HasValue() const { return value != GetNullValue(); }
T GetValue() const { return value; }
private:
static T GetNullValue() { return std::numeric_limits<T>::max(); }
T value;
};