-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpected.h
More file actions
215 lines (175 loc) · 5.93 KB
/
expected.h
File metadata and controls
215 lines (175 loc) · 5.93 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#ifndef PROTOTYPE_EXPECTED_H
#define PROTOTYPE_EXPECTED_H
#include <functional>
#include <stdexcept>
#include <utility>
namespace expect {
template <typename T>
struct expected;
template <typename F, typename... Args, std::enable_if_t<std::is_invocable_v<F, Args...>, int> = 0, typename R = std::invoke_result_t<F, Args...>>
auto captured_invoke(F&& f, Args&&... args) noexcept
-> expected<R>;
namespace detail {
template <typename T>
struct expected_base {
template <typename F, std::enable_if_t<std::is_invocable_v<F, expected<T>&>, int> = 0>
auto map(F&& f) & noexcept -> expected<std::invoke_result_t<F, expected<T>&>> {
return captured_invoke(std::forward<F>(f), self());
}
template <typename F, std::enable_if_t<std::is_invocable_v<F, expected<T> const&>, int> = 0>
auto map(F&& f) const& noexcept
-> expected<std::invoke_result_t<F, expected<T> const&>> {
return captured_invoke(std::forward<F>(f), self());
}
template <typename F, std::enable_if_t<std::is_invocable_v<F, expected<T>&&>, int> = 0>
auto map(F&& f) && noexcept -> expected<std::invoke_result_t<F, expected<T>&&>> {
return captured_invoke(std::forward<F>(f), std::move(self()));
}
void rethrow_error() const {
if (self().has_error()) {
std::rethrow_exception(self().error());
}
}
template <typename E, typename F, std::enable_if_t<std::is_invocable_v<F, E const&>, int> = 0>
auto catch_error(F&& f) -> std::optional<std::invoke_result_t<F, E const&>> {
try {
self().rethrow_error();
} catch (E const& e) {
return std::invoke(std::forward<F>(f), e);
} catch (...) {
}
return std::nullopt;
}
explicit operator bool() const noexcept { return self().has_error(); }
private:
[[nodiscard]] expected<T>& self() & { return static_cast<expected<T>*>(this); }
[[nodiscard]] expected<T> const& self() const& {
return *static_cast<expected<T> const*>(this);
}
};
} // namespace detail
template <typename T>
struct expected : detail::expected_base<T> {
static_assert(!std::is_void_v<T> && !std::is_reference_v<T>);
expected() = delete;
expected(std::exception_ptr p)
: _exception(std::move(p)), _has_value(false) {}
expected(T t) : _value(std::move(t)), _has_value(true) {}
template <typename... Ts, std::enable_if_t<std::is_constructible_v<T, Ts...>, int> = 0>
explicit expected(std::in_place_t, Ts&&... ts)
: _value(std::forward<Ts>(ts)...), _has_value(true) {}
template <typename U = T, std::enable_if_t<std::is_move_constructible_v<U>, int> = 0>
expected(expected&& o) noexcept(std::is_nothrow_move_constructible_v<U>)
: _has_value(o._has_value) {
if (o._has_value) {
new (&this->_value) T(std::move(o._value));
} else {
new (&this->_exception) std::exception_ptr(std::move(o._exception));
}
}
~expected() {
if (_has_value) {
_value.~T();
} else {
_exception.~exception_ptr();
}
}
expected(expected const&) = delete;
expected(expected&&) noexcept = default;
expected& operator=(expected const&) = delete;
expected& operator=(expected&&) noexcept = default;
T& unwrap() & {
if (_has_value) {
return _value;
}
std::rethrow_exception(_exception);
}
T const& unwrap() const& {
if (_has_value) {
return _value;
}
std::rethrow_exception(_exception);
}
T&& unwrap() && {
if (_has_value) {
return std::move(_value);
}
std::rethrow_exception(_exception);
}
[[nodiscard]] std::exception_ptr error() const {
if (has_error()) {
return _exception;
}
return nullptr;
}
T* operator->() { return &unwrap(); }
T const* operator->() const { return &unwrap(); }
[[nodiscard]] bool has_value() const noexcept { return _has_value; }
[[nodiscard]] bool has_error() const noexcept { return !has_value(); }
template <typename F, std::enable_if_t<std::is_invocable_v<F, T&>, int> = 0>
auto map_value(F&& f) & noexcept -> expected<std::invoke_result_t<F, T&>> {
if (has_error()) {
return _exception;
}
return captured_invoke(std::forward<F>(f), _value);
}
template <typename F, std::enable_if_t<std::is_invocable_v<F, T const&>, int> = 0>
auto map_value(F&& f) const& noexcept
-> expected<std::invoke_result_t<F, T const&>> {
if (has_error()) {
return _exception;
}
return captured_invoke(std::forward<F>(f), _value);
}
template <typename F, std::enable_if_t<std::is_invocable_v<F, T&&>, int> = 0>
auto map_value(F&& f) && noexcept -> expected<std::invoke_result_t<F, T&&>> {
if (has_error()) {
return std::move(_exception);
}
return captured_invoke(std::forward<F>(f), std::move(_value));
}
private:
union {
T _value;
std::exception_ptr _exception;
};
const bool _has_value;
};
template <>
struct expected<void> : detail::expected_base<void> {
expected() = default;
expected(std::exception_ptr p) : _exception(std::move(p)) {}
explicit expected(std::in_place_t) : _exception(nullptr) {}
~expected() = default;
expected(expected const&) = delete;
expected(expected&&) noexcept = default;
expected& operator=(expected const&) = delete;
expected& operator=(expected&&) noexcept = default;
[[nodiscard]] bool has_error() const noexcept {
return _exception != nullptr;
}
[[nodiscard]] std::exception_ptr error() const {
if (has_error()) {
return _exception;
}
return nullptr;
}
private:
std::exception_ptr _exception = nullptr;
};
template <typename F, typename... Args, std::enable_if_t<std::is_invocable_v<F, Args...>, int>, typename R>
auto captured_invoke(F&& f, Args&&... args) noexcept
-> expected<R> {
try {
if constexpr (std::is_void_v<R>) {
std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
return {};
} else {
return std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
}
} catch (...) {
return std::current_exception();
}
}
} // namespace expect
#endif // PROTOTYPE_EXPECTED_H