This repository has been archived by the owner on May 14, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
maybe.hh
124 lines (103 loc) · 2.24 KB
/
maybe.hh
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
#ifndef MAYBE_HH
#define MAYBE_HH
#include <cassert>
#include <new>
#include <memory>
template<typename T>
struct maybe {
maybe() : is_init(false) {}
maybe(const T &other) : is_init(false) {
new (&memory) T(other);
is_init = true;
}
maybe(T &&other) : is_init(false) {
new (&memory) T(std::move(other));
is_init = true;
}
maybe(const maybe &other) : is_init(false) {
if (other.is_init) {
new (&memory) T(*other.as_ptr());
is_init = other.is_init;
}
}
maybe(maybe &&other) : is_init(false) {
if (other.is_init) {
new (&memory) T(std::move(*other.as_ptr()));
other.is_init = false;
is_init = true;
}
}
maybe &operator=(const maybe &other) {
if (this != &other && other.is_init) {
if (is_init) {
*as_ptr() = *other.as_ptr();
} else {
new (&memory) T(*other.as_ptr());
is_init = true;
}
} else {
clear();
}
return *this;
}
maybe &operator=(maybe &&other) {
assert(this != &other);
if (other.is_init) {
other.is_init = false;
if (is_init) {
*as_ptr() = std::move(*other.as_ptr());
} else {
new (&memory) T(std::move(*other.as_ptr()));
is_init = true;
}
} else {
clear();
}
return *this;
}
~maybe() {
if (is_init) {
as_ptr()->~T();
}
}
explicit operator bool() const {
return is_init;
}
T &operator*() {
assert(is_init);
return *as_ptr();
}
const T &operator*() const {
assert(is_init);
return *as_ptr();
}
T *operator->() {
assert(is_init);
return as_ptr();
}
const T *operator->() const {
assert(is_init);
return as_ptr();
}
T *get() {
return is_init ? as_ptr() : nullptr;
}
const T *get() const {
return is_init ? as_ptr() : nullptr;
}
T const &get_value_or(T const &v) const {
return is_init ? *as_ptr() : v;
}
void clear() {
if (is_init) {
as_ptr()->~T();
is_init = false;
}
}
private:
T *as_ptr() { return reinterpret_cast<T *>(&memory); }
const T *as_ptr() const { return reinterpret_cast<const T *>(&memory); }
typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type memory;
bool is_init;
};
#endif