forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.h
678 lines (604 loc) · 24.8 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef REMOTING_BASE_RESULT_H_
#define REMOTING_BASE_RESULT_H_
#include <type_traits>
#include <utility>
#include "base/check.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
// Result<SuccessType, ErrorType> represents the success or failure of an
// operation, along with either the success value or error details.
//
// It can be convenient to alias Result for specific error types. For example,
// template <SuccessType>
// using PosixResult<SuccessType, int>
//
// PosixResult<size_t> MyRead(int fd, void* buf, size_t count);
// Synopsis:
//
// SuccessType
// ErrorType
// The success and error types of the Result.
//
// Result()
// Only present when the success value is default constructible. Default
// constructs the success value. This is useful for situations like IPC
// deserialization where a default-costructed instance is created and the
// actual value is filled in later. In general, prefer using the
// Result(kSuccessTag) constructor to be explicit.
//
// Result(SuccessTag, Args&&... args)
// Result(ErrorTag, Args&&... args)
// Direct constructs the success or error value, respectively, with the
// provided arguments.
//
// Result(T&& success_value)
// Result(T&& error_value)
// Implicitly constructs either the success or error value. Only callable if
// T is implicitly convertible to SuccessType or ErrorType but not both.
//
// Result(const Result& other)
// Result(Result&& other)
// Copy / move constructors. Only present if both SuccessType and ErrorType
// are copyable (for the first overload) / moveable (for the second).
//
// Result(const Result<S, E>& other)
// Result(Result<S, E>&& other)
// Conversion constructors from a compatible Result. Only callable if S is
// copy (for the first overload) / move (for the second) convertible to
// SuccessType and E is convertible to ErrorType.
//
// Result& operator=(const Result& other)
// Result& operator=(Result&& other)
// Copy / move assignment. If this!=*other, destroys the current value and
// copy / move constructs the current value from other. Only present if both
// SuccessType and ErrorType are copy (for the first overload) / move (for
// the second) constructible.
//
// Result& operator=(const Result<S, E>& other)
// Result& operator=(Result<S, E>&& other)
// Conversion assignment. Only callable if S is copy (for the first overload)
// / move (for the second) convertible to SuccessType and E is copy / move
// convertible to ErrorType.
//
// EmplaceSuccess(Args&&... args)
// EmplaceError(Args&&... args)
// Destroys the current value and direct constructs the success or error value
// with the provided arguments.
//
// Result<NewSuccessType, ErrorType> Map(F&& on_success) const&
// Result<NewSuccessType, ErrorType> Map(F&& on_success) &&
// If is_success(), calls on_success passing the current value, and returns a
// new Result using the return value. If is_error(), the error value is passed
// through to the new result unchanged.
//
// Result<SuccessType, NewErrorType> MapError(F&& on_error) const&
// Result<SuccessType, NewErrorType> MapError(F&& on_error) &&
// If is_error(), calls on_error passing the current value, and returns a new
// Result using the return value. If is_success(), the success value is passed
// through to the new result unchanged.
//
// Result<NewSuccessType, ErrorType> AndThen(F&& on_success) const&
// Result<NewSuccessType, ErrorType> AndThen(F&& on_success) &&
// If is_success(), calls on_success passing the current value, which should
// itself return a Result. That Result is then returned, converting an error
// to ErrorType if necessary. If is_error(), the error value is passed through
// to the new result unchanged.
//
// Result<NewSuccessType, ErrorType> OrElse(F&& on_error) const&
// Result<NewSuccessType, ErrorType> OrElse(F&& on_error) &&
// If is_error(), calls on_error passing the current value, which should
// itself return a Result. That Result is then returned, converting a success
// value to SuccessType if necessary. If is_success(), the success value is
// passed through to the new result unchanged.
//
// R Visit(F&& visitor) const&
// R Visit(F&& visitor) &
// R Visit(F&& visitor) &&
// Calls either success() or error() on the provided visitor depending on the
// state of the result, passing the value of the corresponding state and
// returning the value returned by the visitor. success() and error() must
// return the same type for the overload called. That is
// success(const SuccessType&) must have the same return type as
// error(const ErrorType&), but need not be the same as success(SuccessType&)
// and error(ErrorType&) (if present).
//
// bool is_success() const
// bool is_error() const
// Check whether the Result currently holds a success value or an error.
//
// SuccessType& success()
// const SuccessType& success() const
// Retrieve the success value. Undefined behavior if !is_success().
//
// ErrorType& error()
// const ErrorType& error() const
// Retrieve the error value. Undefined behavior if !is_error().
namespace remoting {
// SuccessTag and ErrorTag are used for constructing a Result in the success
// state or error state, respectively.
class SuccessTag {};
class ErrorTag {};
// Monostate can be used for SuccessType or ErrorType to indicate that there is
// no data for that state. Thus, Result<SomeType, Monostate> is somewhat
// analogous to absl::optional<SomeType>, and Result<Monostate, Monostate> is
// effectively a (2-byte) boolean. Result<Monostate, ErrorType> can be useful
// for cases where an operation can fail, but there is no return value in the
// success case.
// TODO(rkjnsn): Replace with std::monostate once C++17 is allowed.
class Monostate {};
constexpr SuccessTag kSuccessTag = SuccessTag();
constexpr ErrorTag kErrorTag = ErrorTag();
constexpr Monostate kMonostate = Monostate();
namespace internal {
template <typename SuccessType, typename ErrorType>
struct ResultStorage {
// Default constructor.
ResultStorage() : ResultStorage(kSuccessTag) {}
// Direct constructors.
template <typename... Args>
ResultStorage(SuccessTag, Args&&... args) : is_success(true) {
new (std::addressof(success)) SuccessType(std::forward<Args>(args)...);
}
template <typename... Args>
ResultStorage(ErrorTag, Args&&... args) : is_success(false) {
new (std::addressof(error)) ErrorType(std::forward<Args>(args)...);
}
// Copy/move constructors.
ResultStorage(const ResultStorage& other) : is_success(other.is_success) {
if (is_success) {
new (std::addressof(success)) SuccessType(other.success);
} else {
new (std::addressof(error)) ErrorType(other.error);
}
}
ResultStorage(ResultStorage&& other) : is_success(other.is_success) {
if (is_success) {
new (std::addressof(success)) SuccessType(std::move(other.success));
} else {
new (std::addressof(error)) ErrorType(std::move(other.error));
}
}
// Conversion constructors.
template <typename S, typename E>
ResultStorage(const ResultStorage<S, E>& other)
: is_success(other.is_success) {
if (is_success) {
new (std::addressof(success)) SuccessType(other.success);
} else {
new (std::addressof(error)) ErrorType(other.error);
}
}
template <typename S, typename E>
ResultStorage(ResultStorage<S, E>&& other) : is_success(other.is_success) {
if (is_success) {
new (std::addressof(success)) SuccessType(std::move(other.success));
} else {
new (std::addressof(error)) ErrorType(std::move(other.error));
}
}
~ResultStorage() {
if (is_success) {
success.~SuccessType();
} else {
error.~ErrorType();
}
}
// Assignment.
ResultStorage& operator=(const ResultStorage& other) {
if (this == &other) {
return *this;
}
this->~ResultStorage();
new (this) ResultStorage(other);
return *this;
}
ResultStorage& operator=(ResultStorage&& other) {
if (this == &other) {
return *this;
}
this->~ResultStorage();
new (this) ResultStorage(std::move(other));
return *this;
}
union {
SuccessType success;
ErrorType error;
};
bool is_success;
};
// The following structs are helpers to implement constructor/assign-operator
// overloading. Result defines all five as "default", and then inherits from
// these base classes so the compiler will include or omit each constructor or
// operator as appropriate.
template <bool is_default_constructible>
struct DefaultConstructible {
constexpr DefaultConstructible() = default;
constexpr DefaultConstructible(int) {}
};
template <>
struct DefaultConstructible<false> {
constexpr DefaultConstructible() = delete;
constexpr DefaultConstructible(int) {}
};
template <bool is_copy_constructible>
struct CopyConstructible {};
template <>
struct CopyConstructible<false> {
constexpr CopyConstructible() = default;
constexpr CopyConstructible(const CopyConstructible&) = delete;
constexpr CopyConstructible(CopyConstructible&&) = default;
CopyConstructible& operator=(const CopyConstructible&) = default;
CopyConstructible& operator=(CopyConstructible&&) = default;
};
template <bool is_move_constructible>
struct MoveConstructible {};
template <>
struct MoveConstructible<false> {
constexpr MoveConstructible() = default;
constexpr MoveConstructible(const MoveConstructible&) = default;
constexpr MoveConstructible(MoveConstructible&&) = delete;
MoveConstructible& operator=(const MoveConstructible&) = default;
MoveConstructible& operator=(MoveConstructible&&) = default;
};
template <bool is_copy_assignable>
struct CopyAssignable {};
template <>
struct CopyAssignable<false> {
constexpr CopyAssignable() = default;
constexpr CopyAssignable(const CopyAssignable&) = default;
constexpr CopyAssignable(CopyAssignable&&) = default;
CopyAssignable& operator=(const CopyAssignable&) = delete;
CopyAssignable& operator=(CopyAssignable&&) = default;
};
template <bool is_move_assignable>
struct MoveAssignable {};
template <>
struct MoveAssignable<false> {
constexpr MoveAssignable() = default;
constexpr MoveAssignable(const MoveAssignable&) = default;
constexpr MoveAssignable(MoveAssignable&&) = default;
MoveAssignable& operator=(const MoveAssignable&) = default;
MoveAssignable& operator=(MoveAssignable&&) = delete;
};
} // namespace internal
// TODO(rkjnsn): Add [[nodiscard]] once C++17 is allowed.
template <typename SuccessType_, typename ErrorType_>
class Result : public internal::DefaultConstructible<
std::is_default_constructible<SuccessType_>::value>,
public internal::CopyConstructible<
std::is_copy_constructible<SuccessType_>::value &&
std::is_copy_constructible<ErrorType_>::value>,
public internal::MoveConstructible<
std::is_move_constructible<SuccessType_>::value &&
std::is_move_constructible<ErrorType_>::value>,
public internal::CopyAssignable<
std::is_copy_assignable<SuccessType_>::value &&
std::is_copy_assignable<ErrorType_>::value>,
public internal::MoveAssignable<
std::is_move_assignable<SuccessType_>::value &&
std::is_move_assignable<ErrorType_>::value> {
public:
typedef SuccessType_ SuccessType;
typedef ErrorType_ ErrorType;
private:
template <typename T>
struct is_convertible_result : public std::false_type {};
template <typename OtherSuccessType, typename OtherErrorType>
struct is_convertible_result<Result<OtherSuccessType, OtherErrorType>>
: public std::integral_constant<
bool,
std::is_convertible<OtherSuccessType&&, SuccessType>::value &&
std::is_convertible<OtherErrorType&&, ErrorType>::value> {};
typedef internal::DefaultConstructible<
std::is_default_constructible<SuccessType_>::value>
DefaultConstructible;
public:
// Default constructor. Will default construct the success value. This is for
// situations like IPC deserialization where a default-constructed instance is
// created and the actual value is filled in later. In general, prefer using
// Result(kSuccessTag) constructor to be explicit.
Result() = default;
// Direct constructors allow constructing either the SuccessType or ErrorType
// in place. Usage: Result(kSuccessTag, success_type_constructor_args...) or
// Result(kErrorTag, error_type_constructor_args...)
template <typename... Args>
Result(typename std::enable_if<
std::is_constructible<SuccessType, Args...>::value,
SuccessTag>::type,
Args&&... args)
: DefaultConstructible(0),
storage_(kSuccessTag, std::forward<Args>(args)...) {}
template <typename... Args>
Result(
typename std::enable_if<std::is_constructible<ErrorType, Args...>::value,
ErrorTag>::type,
Args&&... args)
: DefaultConstructible(0),
storage_(kErrorTag, std::forward<Args>(args)...) {}
// Allow implicit construction from objects implicitly convertible to
// SuccessType xor ErrorType.
template <typename T,
typename std::enable_if<
std::is_convertible<T&&, SuccessType>::value &&
!std::is_convertible<T&&, ErrorType>::value &&
// Prefer move/copy/conversion to member construction.
!is_convertible_result<typename std::decay<T>::type>::value,
int>::type = 0>
Result(T&& success_value)
: Result(kSuccessTag, std::forward<T>(success_value)) {}
template <typename T,
typename std::enable_if<
!std::is_convertible<T&&, SuccessType>::value &&
std::is_convertible<T&&, ErrorType>::value &&
!is_convertible_result<typename std::decay<T>::type>::value,
int>::type = 0>
Result(T&& error_value) : Result(kErrorTag, std::forward<T>(error_value)) {}
// Copy / move constructors.
Result(const Result& other) = default;
Result(Result&& other) = default;
// Conversion constructors.
template <
typename OtherSuccessType,
typename OtherErrorType,
typename std::enable_if<
std::is_convertible<const OtherSuccessType&, SuccessType>::value &&
std::is_convertible<const OtherErrorType&, ErrorType>::value,
int>::type = 0>
Result(const Result<OtherSuccessType, OtherErrorType>& other)
: DefaultConstructible(0), storage_(other.storage_) {}
template <typename OtherSuccessType,
typename OtherErrorType,
typename std::enable_if<
std::is_convertible<OtherSuccessType&&, SuccessType>::value &&
std::is_convertible<OtherErrorType&&, ErrorType>::value,
int>::type = 0>
Result(Result<OtherSuccessType, OtherErrorType>&& other)
: DefaultConstructible(0), storage_(std::move(other.storage_)) {}
// Assignment.
Result& operator=(const Result& other) = default;
Result& operator=(Result&& other) = default;
// Conversion assignment.
template <
typename OtherSuccessType,
typename OtherErrorType,
typename std::enable_if<
std::is_convertible<const OtherSuccessType&, SuccessType>::value &&
std::is_convertible<const OtherErrorType&, ErrorType>::value,
int>::type = 0>
Result& operator=(const Result<OtherSuccessType, OtherErrorType>& other) {
this->~Result();
new (this) Result(other);
return *this;
}
template <typename OtherSuccessType,
typename OtherErrorType,
typename std::enable_if<
std::is_convertible<OtherSuccessType&&, SuccessType>::value &&
std::is_convertible<OtherErrorType&&, ErrorType>::value,
int>::type = 0>
Result& operator=(Result<OtherSuccessType, OtherErrorType>&& other) {
this->~Result();
new (this) Result(std::move(other));
return *this;
}
// Emplaces new success value in the result and returns a reference to it.
template <typename... Args>
typename std::enable_if<std::is_constructible<SuccessType, Args...>::value,
SuccessType&>::type
EmplaceSuccess(Args&&... args) {
this->~Result();
new (this) Result(kSuccessTag, std::forward<Args>(args)...);
return storage_.success;
}
// Emplaces new error value in the result and returns a reference to it.
template <typename... Args>
typename std::enable_if<std::is_constructible<ErrorType, Args...>::value,
ErrorType&>::type
EmplaceError(Args&&... args) {
this->~Result();
new (this) Result(kErrorTag, std::forward<Args>(args)...);
return storage_.error;
}
// Maps Result<Success, Error> to Result<NewSuccess, Error> by applying the
// provided Success->NewSuccess functor to the success value, if present. If
// this Result contains an error, it will be passed through unchanged and the
// functor will not be called.
template <typename SuccessFunctor>
Result<typename std::result_of<SuccessFunctor && (const SuccessType&)>::type,
ErrorType>
Map(SuccessFunctor&& on_success) const& {
if (storage_.is_success) {
return {kSuccessTag,
std::forward<SuccessFunctor>(on_success)(storage_.success)};
} else {
return {kErrorTag, storage_.error};
}
}
template <typename SuccessFunctor>
Result<typename std::result_of<SuccessFunctor && (SuccessType &&)>::type,
ErrorType>
Map(SuccessFunctor&& on_success) && {
if (storage_.is_success) {
return {kSuccessTag, std::forward<SuccessFunctor>(on_success)(
std::move(storage_.success))};
} else {
return {kErrorTag, std::move(storage_.error)};
}
}
// Maps Result<Success, Error> to Result<Success, NewError> by applying the
// provided Error->NewError functor to the error value, if present. If this
// Result contains a success value, it will be passed through unchanged and
// the functor will not be called.
template <typename ErrorFunctor>
Result<SuccessType,
typename std::result_of<ErrorFunctor && (const ErrorType&)>::type>
MapError(ErrorFunctor&& on_error) const& {
if (storage_.is_success) {
return {kSuccessTag, storage_.success};
} else {
return {kErrorTag, std::forward<ErrorFunctor>(on_error)(storage_.error)};
}
}
template <typename ErrorFunctor>
Result<SuccessType,
typename std::result_of<ErrorFunctor && (ErrorType &&)>::type>
MapError(ErrorFunctor&& on_error) && {
if (storage_.is_success) {
return {kSuccessTag, std::move(storage_.success)};
} else {
return {kErrorTag,
std::forward<ErrorFunctor>(on_error)(std::move(storage_.error))};
}
}
// Maps Result<Success, Error> to Result<NewSuccess, Error> by calling the
// provided Success->Result<NewSuccess, Error> functor with the success value,
// if present. If this Result contains an error, it will be passed through
// unchanged and the functor will not be called.
template <
typename SuccessFunctor,
typename ReturnType =
typename std::result_of<SuccessFunctor && (const SuccessType&)>::type,
typename std::enable_if<
std::is_convertible<typename ReturnType::ErrorType, ErrorType>::value,
int>::type = 0>
Result<typename ReturnType::SuccessType, ErrorType> AndThen(
SuccessFunctor&& on_success) const& {
if (storage_.is_success) {
return std::forward<SuccessFunctor>(on_success)(storage_.success);
} else {
return {kErrorTag, storage_.error};
}
}
template <
typename SuccessFunctor,
typename ReturnType =
typename std::result_of<SuccessFunctor && (SuccessType &&)>::type,
typename std::enable_if<
std::is_convertible<typename ReturnType::ErrorType, ErrorType>::value,
int>::type = 0>
Result<typename ReturnType::SuccessType, ErrorType> AndThen(
SuccessFunctor&& on_success) && {
if (storage_.is_success) {
return std::forward<SuccessFunctor>(on_success)(
std::move(storage_.success));
} else {
return {kErrorTag, std::move(storage_.error)};
}
}
// Maps Result<Success, Error> to Result<Success, NewError> by calling the
// provided Error->Result<Success, NewError> functor with the error value, if
// present. If this Result contains a success value, it will be passed through
// unchanged and the functor will not be called.
template <typename ErrorFunctor,
typename ReturnType = typename std::result_of<
ErrorFunctor && (const ErrorType&)>::type,
typename std::enable_if<
std::is_convertible<typename ReturnType::SuccessType,
SuccessType>::value,
int>::type = 0>
Result<SuccessType, typename ReturnType::ErrorType> OrElse(
ErrorFunctor&& on_error) const& {
if (storage_.is_success) {
return {kSuccessTag, storage_.success};
} else {
return std::forward<ErrorFunctor>(on_error)(storage_.error);
}
}
template <typename ErrorFunctor,
typename ReturnType =
typename std::result_of<ErrorFunctor && (ErrorType &&)>::type,
typename std::enable_if<
std::is_convertible<typename ReturnType::SuccessType,
SuccessType>::value,
int>::type = 0>
Result<SuccessType, typename ReturnType::ErrorType> OrElse(
ErrorFunctor&& on_error) && {
if (storage_.is_success) {
return {kSuccessTag, std::move(storage_.success)};
} else {
return std::forward<ErrorFunctor>(on_error)(std::move(storage_.error));
}
}
// Calls either success() or error() on the provided visitor depending on the
// state of the result, passing the value of the corresponding state.
template <
typename Visitor,
typename SuccessReturn = decltype(
std::declval<Visitor>().success(std::declval<const SuccessType&>())),
typename ErrorReturn = decltype(
std::declval<Visitor>().error(std::declval<const ErrorType&>())),
typename std::enable_if<std::is_same<SuccessReturn, ErrorReturn>::value,
int>::type = 0>
SuccessReturn Visit(Visitor&& visitor) const& {
if (storage_.is_success) {
return std::forward<Visitor>(visitor).success(storage_.success);
} else {
return std::forward<Visitor>(visitor).error(storage_.error);
}
}
template <
typename Visitor,
typename SuccessReturn = decltype(
std::declval<Visitor>().success(std::declval<SuccessType&>())),
typename ErrorReturn =
decltype(std::declval<Visitor>().error(std::declval<ErrorType&>())),
typename std::enable_if<std::is_same<SuccessReturn, ErrorReturn>::value,
int>::type = 0>
SuccessReturn Visit(Visitor&& visitor) & {
if (storage_.is_success) {
return std::forward<Visitor>(visitor).success(storage_.success);
} else {
return std::forward<Visitor>(visitor).error(storage_.error);
}
}
template <
typename Visitor,
typename SuccessReturn = decltype(
std::declval<Visitor>().success(std::declval<SuccessType&&>())),
typename ErrorReturn =
decltype(std::declval<Visitor>().error(std::declval<ErrorType&&>())),
typename std::enable_if<std::is_same<SuccessReturn, ErrorReturn>::value,
int>::type = 0>
SuccessReturn Visit(Visitor&& visitor) && {
if (storage_.is_success) {
return std::forward<Visitor>(visitor).success(
std::move(storage_.success));
} else {
return std::forward<Visitor>(visitor).error(std::move(storage_.error));
}
}
// Accessors
bool is_success() const { return storage_.is_success; }
bool is_error() const { return !storage_.is_success; }
SuccessType& success() {
DCHECK(storage_.is_success);
return storage_.success;
}
const SuccessType& success() const {
DCHECK(storage_.is_success);
return storage_.success;
}
ErrorType& error() {
DCHECK(!storage_.is_success);
return storage_.error;
}
const ErrorType& error() const {
DCHECK(!storage_.is_success);
return storage_.error;
}
// Allow Result to be treated like an Optional that just happens to have more
// details in the error case.
explicit operator bool() const { return storage_.is_success; }
SuccessType& operator*() { return success(); }
const SuccessType& operator*() const { return success(); }
SuccessType* operator->() { return &success(); }
const SuccessType* operator->() const { return &success(); }
private:
internal::ResultStorage<SuccessType, ErrorType> storage_;
template <typename S, typename E>
friend class Result;
};
} // namespace remoting
#endif // REMOTING_BASE_RESULT_H_