-
Notifications
You must be signed in to change notification settings - Fork 1k
/
result.h
432 lines (357 loc) · 11.7 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//
// Copyright (c) YugaByte, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations
// under the License.
//
//
#pragma once
#include <string>
#include <type_traits>
#include "yb/gutil/dynamic_annotations.h"
#include "yb/util/status.h"
#include "yb/util/tostring.h"
namespace yb {
template<class TValue>
struct ResultTraits {
typedef TValue ValueType;
typedef TValue Stored;
typedef const TValue* ConstPointer;
typedef TValue* Pointer;
typedef const TValue& ConstReference;
typedef TValue&& RValueReference;
static const TValue& ToStored(const TValue& value) { return value; }
static void Destroy(Stored* value) { value->~TValue(); }
static Stored* GetPtr(Stored* value) { return value; }
static const Stored* GetPtr(const Stored* value) { return value; }
};
template<class TValue>
struct ResultTraits<TValue&> {
typedef TValue& ValueType;
typedef TValue* Stored;
typedef const TValue* ConstPointer;
typedef TValue* Pointer;
typedef const TValue& ConstReference;
typedef Pointer&& RValueReference;
static TValue* ToStored(TValue& value) { return &value; } // NOLINT
static void Destroy(Stored* value) {}
static TValue* GetPtr(const Stored* value) { return *value; }
};
void StatusCheck(bool);
template<class TValue>
class NODISCARD_CLASS Result {
public:
using Traits = ResultTraits<TValue>;
using ValueType = typename Traits::ValueType;
Result(const Result& rhs) : success_(rhs.success_) {
if (success_) {
new (&value_) typename Traits::Stored(rhs.value_);
} else {
new (&status_) Status(rhs.status_);
}
}
template<class UValue, class = std::enable_if_t<std::is_convertible<UValue, TValue>::value>>
Result(const Result<UValue>& rhs) : success_(rhs.success_) {
if (success_) {
new (&value_) typename Traits::Stored(rhs.value_);
} else {
new (&status_) Status(rhs.status_);
}
}
Result(Result&& rhs) : success_(rhs.success_) {
if (success_) {
new (&value_) typename Traits::Stored(std::move(rhs.value_));
} else {
new (&status_) Status(std::move(rhs.status_));
}
}
// Forbid creation from Status::OK as value must be explicitly specified in case status is OK
Result(const Status::OK&) = delete; // NOLINT
Result(Status::OK&&) = delete; // NOLINT
Result(const Status& status) : success_(false), status_(status) { // NOLINT
StatusCheck(!status_.ok());
}
Result(Status&& status) : success_(false), status_(std::move(status)) { // NOLINT
StatusCheck(!status_.ok());
}
Result(const TValue& value) : success_(true), value_(Traits::ToStored(value)) {} // NOLINT
template <class UValue,
class = std::enable_if_t<std::is_convertible<const UValue&, const TValue&>::value>>
Result(const UValue& value) // NOLINT
: success_(true), value_(Traits::ToStored(value)) {}
Result(typename Traits::RValueReference value) // NOLINT
: success_(true), value_(std::move(value)) {}
template <class UValue, class = std::enable_if_t<std::is_convertible<UValue&&, TValue&&>::value>>
Result(UValue&& value) : success_(true), value_(std::move(value)) {} // NOLINT
Result& operator=(const Result& rhs) {
if (&rhs == this) {
return *this;
}
this->~Result();
return *new (this) Result(rhs);
}
Result& operator=(Result&& rhs) {
if (&rhs == this) {
return *this;
}
this->~Result();
return *new (this) Result(std::move(rhs));
}
template<class UValue, class = std::enable_if_t<std::is_convertible<UValue, TValue>::value>>
Result& operator=(const Result<UValue>& rhs) {
this->~Result();
return *new (this) Result(rhs);
}
Result& operator=(const Status& status) {
StatusCheck(!status.ok());
this->~Result();
return *new (this) Result(status);
}
Result& operator=(Status&& status) {
StatusCheck(!status.ok());
this->~Result();
return *new (this) Result(std::move(status));
}
Result& operator=(const TValue& value) {
this->~Result();
return *new (this) Result(value);
}
template <class UValue, class = std::enable_if_t<std::is_convertible<UValue&&, TValue&&>::value>>
Result& operator=(UValue&& value) {
this->~Result();
return *new (this) Result(std::move(value));
}
MUST_USE_RESULT explicit operator bool() const {
return ok();
}
MUST_USE_RESULT bool operator!() const {
return !ok();
}
MUST_USE_RESULT bool ok() const {
#ifndef NDEBUG
ANNOTATE_IGNORE_WRITES_BEGIN();
success_checked_ = true;
ANNOTATE_IGNORE_WRITES_END();
#endif
return success_;
}
const Status& status() const& {
#ifndef NDEBUG
StatusCheck(ANNOTATE_UNPROTECTED_READ(success_checked_));
#endif
StatusCheck(!success_);
return status_;
}
Status& status() & {
#ifndef NDEBUG
StatusCheck(ANNOTATE_UNPROTECTED_READ(success_checked_));
#endif
StatusCheck(!success_);
return status_;
}
Status&& status() && {
#ifndef NDEBUG
StatusCheck(ANNOTATE_UNPROTECTED_READ(success_checked_));
#endif
StatusCheck(!success_);
return std::move(status_);
}
auto& get() const { return *get_ptr(); }
auto& operator*() const& { return *get_ptr(); }
auto& operator*() & { return *get_ptr(); }
TValue&& operator*() && {
#ifndef NDEBUG
StatusCheck(ANNOTATE_UNPROTECTED_READ(success_checked_));
#endif
StatusCheck(success_);
return value_;
}
auto operator->() const { return get_ptr(); }
auto operator->() { return get_ptr(); }
auto get_ptr() const {
#ifndef NDEBUG
StatusCheck(ANNOTATE_UNPROTECTED_READ(success_checked_));
#endif
StatusCheck(success_);
return Traits::GetPtr(&value_);
}
auto get_ptr() {
#ifndef NDEBUG
StatusCheck(ANNOTATE_UNPROTECTED_READ(success_checked_));
#endif
StatusCheck(success_);
return Traits::GetPtr(&value_);
}
Status MoveTo(typename Traits::Pointer value) {
if (!ok()) {
return status();
}
*value = std::move(**this);
return Status::OK();
}
std::string ToString() const {
return ok() ? AsString(**this) : status().ToString();
}
~Result() {
if (success_) {
Traits::Destroy(&value_);
} else {
status_.~Status();
}
}
private:
bool success_;
#ifndef NDEBUG
mutable bool success_checked_ = false;
#endif
union {
Status status_;
typename Traits::Stored value_;
};
template <class UValue> friend class Result;
};
// Specify Result<bool> to avoid confusion with operator bool and operator!.
template<>
Result<bool>::operator bool() const = delete;
template<>
bool Result<bool>::operator!() const = delete;
template<class TValue>
Status&& MoveStatus(Result<TValue>&& result) {
return std::move(result.status());
}
template<class TValue>
const Status& MoveStatus(const Result<TValue>& result) {
return result.status();
}
template<class TValue>
inline std::string StatusToString(const Result<TValue>& result) {
return result.status().ToString();
}
template<class TValue>
std::ostream& operator<<(std::ostream& out, const Result<TValue>& result) {
return result.ok() ? out << *result : out << result.status();
}
template<class Functor>
class ResultToStatusAdaptor {
public:
explicit ResultToStatusAdaptor(const Functor& functor) : functor_(functor) {}
template <class Output, class... Args>
Status operator()(Output* output, Args&&... args) {
auto result = functor_(std::forward<Args>(args)...);
RETURN_NOT_OK(result);
*output = std::move(*result);
return Status::OK();
}
private:
Functor functor_;
};
template<class Functor>
ResultToStatusAdaptor<Functor> ResultToStatus(const Functor& functor) {
return ResultToStatusAdaptor<Functor>(functor);
}
template<class TValue>
Status ResultToStatus(const Result<TValue>& result) {
return result.ok() ? Status::OK() : result.status();
}
template<class TValue>
TValue ResultToValue(const Result<TValue>& result, const TValue& value_for_error) {
return result.ok() ? *result : value_for_error;
}
template<class TValue>
TValue ResultToValue(Result<TValue>&& result, const TValue& value_for_error) {
return result.ok() ? std::move(*result) : value_for_error;
}
template<class TValue>
TValue ResultToValue(const Result<TValue>& result, TValue&& value_for_error) {
return result.ok() ? *result : std::move(value_for_error);
}
template<class TValue>
TValue ResultToValue(Result<TValue>&& result, TValue&& value_for_error) {
return result.ok() ? std::move(*result) : std::move(value_for_error);
}
/*
* GNU statement expression extension forces to return value and not rvalue reference.
* As a result VERIFY_RESULT or similar helpers will call move or copy constructor of T even
* for Result<T&>/Result<const T&>
* To avoid this undesirable behavior for Result<T&>/Result<const T&>, the std::reference_wrapper<T>
* is returned from statement.
* The following functions help implement this strategy.
*/
template<class T>
T&& WrapMove(Result<T>&& result) {
return std::move(*result);
}
template<class T>
std::reference_wrapper<T> WrapMove(Result<T&>&& result) {
return std::reference_wrapper<T>(*result);
}
template<class T>
Result<T> Copy(const Result<T>& src) {
return src;
}
template<class T>
struct IsNonConstResultRvalue : std::false_type {};
template<class T>
struct IsNonConstResultRvalue<Result<T>&&> : std::true_type {};
#define RESULT_CHECKER_HELPER(expr, checker) \
__extension__ ({ \
auto&& __result = (expr); \
static_assert(yb::IsNonConstResultRvalue<decltype(__result)>::value, \
"only non-const Result<T> rvalue reference is allowed"); \
checker; \
WrapMove(std::move(__result)); })
// Returns if result is not ok, extracts result value in case of success.
#define VERIFY_RESULT(expr) \
RESULT_CHECKER_HELPER(expr, RETURN_NOT_OK(__result))
// Returns if result is not ok, extracts result value in case of success.
#define VERIFY_RESULT_OR_SET_CODE(expr, code) \
RESULT_CHECKER_HELPER(expr, RETURN_NOT_OK_SET_CODE(__result, code))
// Helper version of VERIFY_RESULT which returns reference instead of std::reference_wrapper.
#define VERIFY_RESULT_REF(expr) \
VERIFY_RESULT(expr).get()
// If expr's result is not ok, returns the error status prepended with provided message.
// If expr's result is ok returns wrapped value.
#define VERIFY_RESULT_PREPEND(expr, message) \
RESULT_CHECKER_HELPER(expr, RETURN_NOT_OK_PREPEND(__result, message))
template<class T>
T&& OptionalWrapMove(Result<T>&& result) {
return std::move(*result);
}
template<class T>
T&& OptionalWrapMove(T&& result) {
return std::move(result);
}
template<class T>
bool OptionalResultIsOk(const Result<T>& result) {
return result.ok();
}
template<class T>
constexpr bool OptionalResultIsOk(const T& result) {
return true;
}
template<class TValue>
Status&& OptionalMoveStatus(Result<TValue>&& result) {
return std::move(result.status());
}
template<class TValue>
Status OptionalMoveStatus(TValue&& value) {
return Status::OK();
}
// When expr type is Result, then it works as VERIFY_RESULT. Otherwise, just returns expr.
// Could be used in templates to work with functions that returns Result in some instantiations,
// and plain value in others.
#define OPTIONAL_VERIFY_RESULT(expr) \
__extension__ ({ \
auto&& __result = (expr); \
if (!::yb::OptionalResultIsOk(__result)) { \
return ::yb::OptionalMoveStatus(std::move(__result)); \
} \
::yb::OptionalWrapMove(std::move(__result)); })
} // namespace yb