-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpz_cxx_future.h
158 lines (132 loc) · 3.2 KB
/
pz_cxx_future.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
/*
* PZ C++ future library functions.
* vim: ts=4 sw=4 et
*
* Copyright (C) Plasma Team
* Distributed under the terms of the MIT license, see ../LICENSE.code
*
* This file contains library code that has been added to a more recent C++
* version than the one we've standardised on (C++11) or features that we
* might reasonably expect to be added to a future version. If/when we move
* to a newer standard we can delete entries here and update code as
* necessary.
*/
#ifndef PZ_CXX_FUTURE_H
#define PZ_CXX_FUTURE_H
#include <string>
#include <functional>
/*
* C++17 libraries don't seem to be on my dev system,
* other people might also be missing them. So just implement this
* ourselves.
*/
template <typename T>
class Optional
{
private:
bool m_present = false;
/*
* AlaskanEmily suggested this trick, allocate space for T here and use
* placement new below so that T's without default constructors can be
* used.
*/
static_assert(sizeof(T) >= 1, "T must have non-zero size");
alignas(alignof(T)) char m_data[sizeof(T)] = {0};
public:
constexpr Optional() {}
// Implicit constructor
Optional(const T & val) {
set(val);
}
Optional(T && val) : m_present(true) {
value() = std::move(val);
}
Optional(const Optional & other) {
if (other.hasValue()) {
set(other.value());
}
}
Optional(Optional && other) {
if (other.hasValue()) {
set(other.release());
}
}
~Optional() {
clear();
}
Optional & operator=(const Optional & other) {
if (this != &other) {
if (other.hasValue()) {
set(other.value());
} else {
clear();
}
}
return *this;
}
Optional & operator=(Optional && other) {
if (this != &other) {
if (other.hasValue()) {
set(other.release());
} else {
clear();
}
}
return *this;
}
static constexpr Optional Nothing() {
return Optional();
}
bool hasValue() const {
return m_present;
}
void set(const T & val) {
clear();
new (m_data) T(val);
m_present = true;
}
void set(T && val) {
clear();
raw() = std::move(val);
m_present = true;
}
T & value() {
assert(m_present);
return raw();
}
const T & value() const {
assert(m_present);
return raw();
}
T && release() {
assert(m_present);
m_present = false;
return std::move(raw());
}
void clear() {
if (m_present) {
raw().~T();
}
m_present = false;
}
private:
// Access the storage as the correct type without an assertion.
T & raw() {
return reinterpret_cast<T &>(m_data);
}
const T & raw() const {
return reinterpret_cast<const T &>(m_data);
}
};
class ScopeExit
{
public:
explicit ScopeExit(std::function<void()> && f) : m_f(f) {}
~ScopeExit()
{
m_f();
}
private:
std::function<void()> m_f;
};
#endif // ! PZ_CXX_FUTURE_H