-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathoptional.h
More file actions
161 lines (119 loc) · 2.84 KB
/
optional.h
File metadata and controls
161 lines (119 loc) · 2.84 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
#pragma once
#include "allscale/utils/assert.h"
#include <ostream>
namespace allscale {
namespace utils {
template<typename T>
class optional {
// true, if the object is valid, false otherwise
bool valid;
// the stored object, if present
char obj[sizeof(T)];
public:
optional() : valid(false) {};
optional(const optional& other) : valid(other.valid) {
if (valid) new (obj) T(other.asRef());
}
optional(optional&& other) : valid(other.valid) {
if (valid) new (obj) T(std::move(other.asRef()));
other.discard();
}
optional(const T& val) : valid(true) {
new (obj) T(val);
}
optional(T&& val) : valid(true) {
new (obj) T(std::move(val));
};
~optional() {
if (valid) asPtr()->~T();
}
// Operator:
explicit operator bool() const {
return valid;
}
T& operator*() {
return asRef();
}
const T& operator*() const {
return asRef();
}
optional& operator=(const optional& other) {
if (this == &other) return *this;
// discard old state
discard();
// if the other is representing none => done
if (!other.valid) return *this;
// copy other state
if (!valid) {
valid = true;
new (obj) T(other.asRef());
} else {
asRef() = other.asRef();
}
// done
return *this;
}
optional& operator=(optional&& other) {
if (this == &other) return *this;
// discard old state
discard();
// if the other is representing none => done
if (!other.valid) return *this;
// copy other state
if (!valid) {
valid = true;
new (obj) T(std::move(other.asRef()));
} else {
asRef() = std::move(other.asRef());
}
// invalidate other
other.discard();
// done
return *this;
}
bool operator==(const optional& other) const {
return (!valid && !other.valid) || (valid && other.valid && asRef() == other.asRef());
}
bool operator!=(const optional& other) const {
return !(*this == other);
}
bool operator<(const optional& other) const {
if (!valid) return other.valid;
if (!other.valid) return false;
return asRef() < other.asRef();
}
friend std::ostream& operator<<(std::ostream& out, const optional& opt) {
if (!bool(opt)) return out << "Nothing";
return out << "Just(" << *opt << ")";
}
private:
T* asPtr() {
return reinterpret_cast<T*>((void*)&obj[0]);
}
const T* asPtr() const {
return reinterpret_cast<const T*>((void*)&obj[0]);
}
T& asRef() {
assert_true(valid);
return *asPtr();
}
const T& asRef() const {
assert_true(valid);
return *asPtr();
}
void discard() {
if (!valid) return;
asPtr()->~T();
valid = false;
}
};
template<typename T>
optional<T> make_optional(const T& val) {
return { val };
}
template<typename T>
optional<T> make_optional(T&& val) {
return { std::move(val) };
}
} // end namespace utils
} // end namespace allscale