forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Result.h
320 lines (283 loc) · 9.02 KB
/
Result.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* Copyright 2017-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#pragma once
#include <exception>
#include <stdexcept>
#include <system_error>
#include <type_traits>
namespace watchman {
// To avoid some horrible special casing for the void type in template
// metaprogramming we use Unit to denote an uninteresting value type.
struct Unit {
// Lift void -> Unit if T is void, else T
template <typename T>
struct Lift : std::conditional<std::is_same<T, void>::value, Unit, T> {};
};
// Represents the Result of an operation, and thus can hold either
// a value or an error, or neither. This is similar to the folly::Try
// type and also to the rust Result type. The contained Error type
// can be replaced by an arbitrary error container as a stronger nod
// toward the rust Result type and is useful in situations where
// throwing and catching exceptions is undesirable.
template <typename Value, typename Error = std::exception_ptr>
class Result {
static_assert(
!std::is_reference<Value>::value && !std::is_reference<Error>::value,
"Result may not be used with reference types");
static_assert(
!std::is_same<Value, Error>::value,
"Value and Error must not be the same type");
enum class State { kEMPTY, kVALUE, kERROR };
public:
using value_type = Value;
using error_type = Error;
// Default construct an empty Result
Result() : state_(State::kEMPTY) {}
~Result() {
// value_/error_ is a union, thus manual call of destructors
switch (state_) {
case State::kEMPTY:
break;
case State::kVALUE:
value_.~Value();
break;
case State::kERROR:
error_.~Error();
break;
}
}
// Copy a value into the result
explicit Result(const Value& other) : state_(State::kVALUE), value_(other) {}
// Move in value
explicit Result(Value&& other)
: state_(State::kVALUE), value_(std::move(other)) {}
// Copy in error
explicit Result(const Error& error) : state_(State::kERROR), error_(error) {}
// Move in error
explicit Result(Error&& error)
: state_(State::kERROR), error_(std::move(error)) {}
// Move construct
explicit Result(Result&& other) noexcept : state_(other.state_) {
switch (state_) {
case State::kEMPTY:
break;
case State::kVALUE:
new (&value_) Value(std::move(other.value_));
break;
case State::kERROR:
new (&error_) Error(std::move(other.error_));
break;
}
other.~Result();
other.state_ = State::kEMPTY;
}
// Move assign
Result& operator=(Result&& other) noexcept {
if (&other != this) {
this->~Result();
state_ = other.state_;
switch (state_) {
case State::kEMPTY:
break;
case State::kVALUE:
new (&value_) Value(std::move(other.value_));
break;
case State::kERROR:
new (&error_) Error(std::move(other.error_));
break;
}
other.~Result();
other.state_ = State::kEMPTY;
}
return *this;
}
// Copy construct
Result(const Result& other) {
static_assert(
std::is_copy_constructible<Value>::value &&
std::is_copy_constructible<Error>::value,
"Value and Error must be copyable for "
"Result<Value,Error> to be copyable");
state_ = other.state_;
switch (state_) {
case State::kEMPTY:
break;
case State::kVALUE:
new (&value_) Value(other.value_);
break;
case State::kERROR:
new (&error_) Error(other.error_);
break;
}
}
// Copy assign
Result& operator=(const Result& other) {
static_assert(
std::is_copy_constructible<Value>::value &&
std::is_copy_constructible<Error>::value,
"Value and Error must be copyable for "
"Result<Value,Error> to be copyable");
if (&other != this) {
this->~Result();
state_ = other.state_;
switch (state_) {
case State::kEMPTY:
break;
case State::kVALUE:
new (&value_) Value(other.value_);
break;
case State::kERROR:
new (&error_) Error(other.error_);
break;
}
}
return *this;
}
bool hasValue() const {
return state_ == State::kVALUE;
}
bool hasError() const {
return state_ == State::kERROR;
}
bool empty() const {
return state_ == State::kEMPTY;
}
// If Result does not contain a valid Value, throw
// the Error value. If there is no error value,
// throw a logic error.
// This variant is used when Error is std::exception_ptr.
template <typename E = Error>
typename std::enable_if<std::is_same<E, std::exception_ptr>::value>::type
throwIfError() const {
switch (state_) {
case State::kVALUE:
return;
case State::kEMPTY:
throw std::logic_error("Uninitialized Result");
case State::kERROR:
std::rethrow_exception(error_);
}
}
// If Result does not contain a valid Value, throw a logic error.
// This variant is used when Error is std::error_code.
template <typename E = Error>
typename std::enable_if<std::is_same<E, std::error_code>::value>::type
throwIfError() const {
switch (state_) {
case State::kVALUE:
return;
case State::kEMPTY:
throw std::logic_error("Uninitialized Result");
case State::kERROR:
throw std::system_error(error_);
}
}
// If Result does not contain a valid Value, throw a logic error.
// This variant is used when Error is not std::exception_ptr or
// std::error_code.
template <typename E = Error>
typename std::enable_if<
!std::is_same<E, std::exception_ptr>::value &&
!std::is_same<E, std::error_code>::value>::type
throwIfError() const {
switch (state_) {
case State::kVALUE:
return;
case State::kEMPTY:
throw std::logic_error("Uninitialized Result");
case State::kERROR:
throw std::logic_error("Result holds Error, not Value");
}
}
// Get a mutable reference to the value. If the value is
// not assigned, an exception will be thrown by throwIfError().
Value& value() & {
throwIfError();
return value_;
}
// Get an rvalue reference to the value. If the value is
// not assigned, an exception will be thrown by throwIfError().
Value&& value() && {
throwIfError();
return value_;
}
// Get a const reference to the value. If the value is
// not assigned, an exception will be thrown by throwIfError().
const Value& value() const& {
throwIfError();
return value_;
}
// Throws a logic exception if the result does not contain an Error
void throwIfNotError() {
switch (state_) {
case State::kVALUE:
throw std::logic_error("Result holds Value, not Error");
case State::kEMPTY:
throw std::logic_error("Uninitialized Result");
case State::kERROR:
return;
}
}
// Get a mutable reference to the error. If the error is
// not assigned, an exception will be thrown by throwIfNotError().
Error& error() & {
throwIfNotError();
return error_;
}
// Get an rvalue reference to the error. If the error is
// not assigned, an exception will be thrown by throwIfNotError().
Error&& error() && {
throwIfNotError();
return error_;
}
// Get a const reference to the error. If the error is
// not assigned, an exception will be thrown by throwIfNotError().
const Error& error() const& {
throwIfNotError();
return error_;
}
private:
State state_;
union {
Value value_;
Error error_;
};
};
// Helper for making a Result from a value; auto-deduces the Value type.
// The Error type can be overridden and is listed first because the whole
// point of this is to avoid specifying the Value type.
template <typename Error = std::exception_ptr, typename T>
Result<typename std::decay<T>::type, Error> makeResult(T&& t) {
return Result<typename std::decay<T>::type, Error>(std::forward<T>(t));
}
// Helper for populating a Result with the return value from a lambda.
// If the lambda throws an exception it will be captured into the Result.
// This is the non-void return type flavor.
template <typename Func>
typename std::enable_if<
!std::is_same<typename std::result_of<Func()>::type, void>::value,
Result<typename std::result_of<Func()>::type>>::type
makeResultWith(Func&& func) {
using ResType = typename std::result_of<Func()>::type;
try {
return Result<ResType>(func());
} catch (const std::exception& e) {
return Result<ResType>(std::current_exception());
}
}
// Helper for populating a Result with the return value from a lambda.
// If the lambda throws an exception it will be captured into the Result.
// This is the void return type flavor; it produces Result<Unit>
template <typename Func>
typename std::enable_if<
std::is_same<typename std::result_of<Func()>::type, void>::value,
Result<Unit>>::type
makeResultWith(Func&& func) {
try {
func();
return Result<Unit>(Unit{});
} catch (const std::exception& e) {
return Result<Unit>(std::current_exception());
}
}
}