-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.hpp
733 lines (614 loc) · 20.3 KB
/
result.hpp
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
* result.hpp
* Copyright© 2017 rsw0x
*
* Distributed under terms of the MPLv2 license.
*/
#ifndef RESULT_HPP_7LRAEJZ5
#define RESULT_HPP_7LRAEJZ5
#pragma push_macro("LIKELY")
#undef LIKELY
#pragma push_macro("UNLIKELY")
#undef UNLIKELY
#ifdef __GNUC__
#define LIKELY(x) __builtin_expect(static_cast<bool>(x), true)
#define UNLIKELY(x) __builtin_expect(static_cast<bool>(x), false)
#else
#define LIKELY(x) static_cast<bool>(x)
#define UNLIKELY(x) static_cast<bool>(x)
#endif
#pragma push_macro("REQUIRES")
#undef REQUIRES
#define REQUIRES(...) \
details::unique_t<__LINE__>::type = details::unique_t<__LINE__>::type::e, \
bool hiddenBool__ = true, std::enable_if_t < hiddenBool__ && (__VA_ARGS__), \
int > = 0
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <new>
#include <type_traits>
#include <utility>
namespace util {
template<typename T, typename E>
struct Result;
namespace details {
template<int N>
struct unique_t {
enum class type { e };
};
template<typename T>
struct is_result_impl : std::false_type {};
template<typename T, typename E>
struct is_result_impl<util::Result<T, E>> : std::true_type {};
template<typename T>
constexpr bool is_result = is_result_impl<T>::value;
template<typename T>
struct result_flatten {
using type = T;
//? Bad idea?
using Ok_T = T;
};
template<typename T, typename E>
struct result_flatten<util::Result<T, E>> {
// TODO: handle nested results deeper than two levels.
using Ok_T = typename result_flatten<T>::type;
using Err_T = typename result_flatten<E>::type;
};
template<typename F>
struct apply_traits {
using ret_t = std::result_of_t<F>;
using flatten_t = typename result_flatten<ret_t>::Ok_T;
static constexpr bool is_result = details::is_result<ret_t>;
};
template<class...>
using void_t = void;
template<typename Expr, typename Enabler = void>
struct isCallableImpl : std::false_type {};
template<typename F, typename... Args>
struct isCallableImpl<F(Args...), void_t<std::result_of_t<F(Args...)>>>
: std::true_type {};
template<typename Expr>
constexpr bool isCallable = isCallableImpl<Expr>::value;
template<typename T>
struct OkWrapper {
T&& contents;
OkWrapper(OkWrapper&&) = delete;
OkWrapper(const OkWrapper&) = delete;
OkWrapper(T&& v)
: contents(std::forward<T>(v)) {
}
};
template<typename E>
struct ErrWrapper {
ErrWrapper(ErrWrapper&&) = delete;
ErrWrapper(const ErrWrapper&) = delete;
E&& contents;
ErrWrapper(E&& v)
: contents(std::forward<E>(v)) {
}
};
struct EmptyWrapper {};
#ifdef __GNUC__
[[noreturn]] inline void unreachable() {
__builtin_unreachable();
}
#else
inline void unreachable() {
}
#endif
struct ok_tag {};
struct err_tag {};
// Used as the 'invalid' state
struct dummy_t {};
enum class ValidityState : char { invalid = 0, err = 1, ok = 2 };
template<typename T>
struct result_wrap_t {
private:
T contents;
public:
template<typename U>
result_wrap_t(U&& rval)
: contents(std::forward<U>(rval)) {
}
T& get() {
return contents;
}
const T& get() const {
return contents;
}
};
// replace T& with std::reference_wrapper<T>
template<typename T>
struct result_wrap_t<T&> {
private:
std::reference_wrapper<T> contents;
public:
result_wrap_t(T& lval)
: contents(lval) {
}
result_wrap_t(T&&) = delete;
T& get() {
return contents.get();
}
const T& get() const {
return contents.get();
}
};
// TODO: need to move a lot of logic from Result to a middle layer so
// BaseResult can be overloaded for constexpr cases.
template<typename T, typename E>
struct BaseResult {
union contents_t {
result_wrap_t<T> val;
result_wrap_t<E> err;
//`null` state
dummy_t dummy_;
explicit constexpr contents_t(dummy_t) noexcept
: dummy_() {
}
explicit contents_t(T&& v, ok_tag)
: val(std::forward<T>(v)) {
}
explicit contents_t(E&& e, err_tag)
: err(std::forward<E>(e)) {
}
~contents_t() {
}
} contents;
ValidityState validityState_ = ValidityState::invalid;
explicit BaseResult()
: contents(dummy_t{})
, validityState_(ValidityState::invalid) {
}
explicit constexpr BaseResult(dummy_t) noexcept
: contents(dummy_t{})
, validityState_(ValidityState::invalid) {
}
explicit constexpr BaseResult(details::EmptyWrapper) noexcept
: contents(E{}, err_tag{})
, validityState_(ValidityState::err) {
}
~BaseResult() {
destruct();
}
void destruct() {
try {
switch (validityState_) {
case ValidityState::ok:
// clang seems to not accept the decltype dtor syntax...
using val_dtor_t = decltype(contents.val);
contents.val.~val_dtor_t();
break;
case ValidityState::err:
using err_dtor_t = decltype(contents.err);
contents.err.~err_dtor_t();
break;
case ValidityState::invalid:
break;
}
validityState_ = ValidityState::invalid;
} catch (...) {
validityState_ = ValidityState::invalid;
throw;
}
}
};
} // namespace details
// Lightweight wrapper just meant for return type deduction.
template<typename T>
details::OkWrapper<T> Ok(T&& val) {
static_assert(sizeof(details::OkWrapper<T>) == sizeof(void*), "");
return {std::forward<T>(val)};
}
template<typename T>
details::ErrWrapper<T> Err(T&& val) {
static_assert(sizeof(details::ErrWrapper<T>) == sizeof(void*), "");
return {std::forward<T>(val)};
}
constexpr details::EmptyWrapper Err() {
return {};
}
template<typename T, typename E>
struct Result : private details::BaseResult<T, E> {
static_assert(!std::is_rvalue_reference<T>::value,
"Result<T,E> can't hold rvalue references.");
static_assert(!std::is_rvalue_reference<E>::value,
"Result<T,E> can't hold rvalue references.");
static_assert(!std::is_convertible<T, E>::value,
"T and E cannot be convertible between each other.");
static_assert(!std::is_convertible<E, T>::value,
"T and E cannot be convertible between each other.");
private:
using Base = details::BaseResult<T, E>;
using ValidityState = details::ValidityState;
using Error_T = std::remove_reference_t<decltype(
std::declval<typename Base::contents_t>().err.get())>;
using Ok_T = std::remove_reference_t<decltype(
std::declval<typename Base::contents_t>().val.get())>;
using Ok_Wrap_T = decltype(Base::contents_t::val);
using Error_Wrap_T = decltype(Base::contents_t::err);
template<typename U, typename F, typename = std::true_type>
struct construct_contract_t {};
template<typename U, typename F>
struct construct_contract_t<
U,
F,
std::integral_constant<bool,
!std::is_lvalue_reference<F>::value and
std::is_lvalue_reference<U>::value>> {
static_assert(std::is_copy_constructible<std::decay_t<U>>::value,
"Attempted to create a Result<T,E> with a move-only type"
" by reference. "
"Did you intend to std::move() it?");
};
template<typename U>
auto reconstruct(U&& val, details::ok_tag)
-> decltype(construct_contract_t<U, T>{}, void()) {
this->validityState_ = ValidityState::ok;
::new (&(this->contents.val)) Ok_Wrap_T(std::forward<U>(val));
}
template<typename U>
auto reconstruct(U&& val, details::err_tag)
-> decltype(construct_contract_t<U, E>{}, void()) {
this->validityState_ = ValidityState::err;
::new (&(this->contents.err)) Error_Wrap_T(std::forward<U>(val));
}
void copy_assign(const Result& other) {
switch (other.validityState_) {
case ValidityState::ok:
reconstruct(other.contents.ok(), details::ok_tag{});
break;
case ValidityState::err:
reconstruct(other.contents.err(), details::err_tag{});
break;
case ValidityState::invalid:
// TODO: this is always a bug, right?
std::fprintf(stderr, "BUG\n");
std::abort();
break;
}
}
void move_assign(Result&& other) {
switch (other.validityState_) {
case ValidityState::ok:
// Forward because we may have reference params.
reconstruct(std::forward<T>(other.ok()), details::ok_tag{});
break;
case ValidityState::err:
reconstruct(std::forward<E>(other.err()), details::err_tag{});
break;
case ValidityState::invalid:
// TODO: this is always a bug, right?
std::fprintf(stderr, "BUG\n");
std::abort();
break;
}
// Reset the other.
other.destruct();
other.validityState_ = ValidityState::invalid;
}
public:
Result& operator=(const Result& other) {
if (this == &other) {
return *this;
}
this->destruct();
copy_assign(other);
return *this;
}
Result& operator=(Result&& other) {
if (this == &other) {
return *this;
}
this->destruct();
move_assign(std::move(other));
return *this;
}
template<typename U>
Result& operator=(const details::OkWrapper<U>& val) {
this->destruct();
reconstruct(std::forward<U>(val.contents), details::ok_tag{});
return *this;
}
template<typename U>
Result& operator=(const details::ErrWrapper<U>& val) {
this->destruct();
reconstruct(std::forward<U>(val.contents), details::err_tag{});
return *this;
}
Result(const Result& other) {
copy_assign(other);
}
Result(Result&& other) {
move_assign(std::move(other));
}
~Result() = default;
template<typename U>
Result(details::OkWrapper<U>&& val) {
reconstruct(std::forward<U>(val.contents), details::ok_tag{});
}
template<typename U>
Result(details::ErrWrapper<U>&& val) {
reconstruct(std::forward<U>(val.contents), details::err_tag{});
}
constexpr Result(details::EmptyWrapper e)
: Base(e) {
}
template<typename U, REQUIRES(std::is_constructible<Ok_T, U&&>{})>
Result(U&& val) {
reconstruct(std::forward<U>(val), details::ok_tag{});
}
template<typename U, REQUIRES(std::is_constructible<Error_T, U&&>{})>
Result(U&& val) {
reconstruct(std::forward<U>(val), details::err_tag{});
}
constexpr bool is_err() const noexcept {
return this->validityState_ == details::ValidityState::err;
}
constexpr bool is_ok() const noexcept {
return this->validityState_ == details::ValidityState::ok;
}
constexpr bool is_invalid() const noexcept {
return this->validityState_ == details::ValidityState::invalid;
}
constexpr explicit operator bool() const noexcept {
return is_ok();
}
// TODO
const T& ok_unchecked(const char* msg = nullptr) const & {
return get_(msg);
}
T& ok_unchecked(const char* msg = nullptr) & {
return get_(msg);
}
T&& ok_unchecked(const char* msg = nullptr) && {
return std::move(get_(msg));
}
const T& ok(const char* msg = nullptr) const & {
return get_(msg);
}
T& ok(const char* msg = nullptr) & {
return get_(msg);
}
T&& ok(const char* msg = nullptr) && {
return std::move(get_(msg));
}
const E& err(const char* msg = nullptr) const & {
return getErr_(msg);
}
E& err(const char* msg = nullptr) & {
return getErr_(msg);
}
E&& err(const char* msg = nullptr) && {
return std::move(getErr_(msg));
}
// TODO
const E& err_unchecked(const char* msg = nullptr) const & {
return getErr_(msg);
}
E& err_unchecked(const char* msg = nullptr) & {
return getErr_(msg);
}
E&& err_unchecked(const char* msg = nullptr) && {
return std::move(getErr_(msg));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// APPLY() IMPLEMENTATION
////////////////////////////////////////////////////////////////////////////////////////////////////
private:
template<typename F>
using apply_traits = details::apply_traits<F>;
template<typename F>
using apply_ret_t =
std::enable_if_t<details::isCallable<F> and
not std::is_same<std::result_of_t<F>, void>::value,
Result<typename apply_traits<F>::flatten_t, E>>;
public:
// attempt to call it by move first if we're an rvalue ref, otherwise SFINAE
// back to by ref.
/** Apply a function @p F(T) -> U mapping Result<T,E> -> Result<U,E>
* If U is a Result such that U = Result<T2,E> then apply will flatten the
* Result.
*
*/
template<typename F, REQUIRES(details::isCallable<F(T&&)>)>
apply_ret_t<F(T&&)> apply(F&& fn) && {
if (is_ok()) {
return fn(std::move(ok()));
} else {
return std::move(err());
}
}
template<typename F, REQUIRES(not details::isCallable<F(T&&)>)>
apply_ret_t<F(T&)> apply(F&& fn) && {
if (is_ok()) {
return fn(ok());
} else {
return std::move(err());
}
}
// TODO: wrapper for apply that returns the same Result<T,E> which only
// conditionally moves if it is actually assigned.
template<typename F>
apply_ret_t<F(T&)> apply(F&& fn) & {
// static_assert(not std::is_same<res_t, void>::value,
// "Cannot apply a function that returns void.");
if (is_ok()) {
return fn(ok());
} else {
return err();
}
}
template<typename F,
REQUIRES(details::isCallable<F(T&)>and
std::is_same<std::result_of_t<F(T&)>, void>())>
Result& apply(F&& fn) {
if (is_ok()) {
fn(ok());
}
return *this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// OK_OR() IMPL
////////////////////////////////////////////////////////////////////////////////////////////////////
private:
public:
template<typename T2, REQUIRES(not details::isCallable<T2()>)>
T ok_or(T2&& orVal) && {
static_assert(std::is_move_constructible<T>::value,
"T must be move constructible to use ok_or() with rvalue.");
static_assert(std::is_convertible<T2&&, T>::value,
"Provided value cannot be converted to T.");
if (is_ok()) {
return std::move(ok());
} else {
return std::forward<T2>(orVal);
}
}
/**
* Overload for callable F allowing the alternative to be computed lazily.
*/
template<typename F, REQUIRES(details::isCallable<F()>)>
T ok_or(F&& orFn) && {
static_assert(std::is_convertible<std::result_of_t<F()>, T>::value,
"Alternative does not return a type convertible to T.");
static_assert(std::is_move_constructible<T>::value,
"T must be move constructible to use ok_or() with rvalue.");
if (is_ok()) {
return std::move(ok());
} else {
return static_cast<T>(orFn());
}
}
template<typename T2, REQUIRES(not details::isCallable<T2()>)>
T ok_or(T2&& orVal) const & {
static_assert(std::is_copy_constructible<T>::value,
"lvalue okOr requires a copy constructible T.");
static_assert(std::is_convertible<T2&&, T>::value,
"Provided value cannot be converted to T.");
if (is_ok()) {
return ok();
} else {
return static_cast<T>(std::forward<T2>(orVal));
}
}
/**
* Overload for callable F allowing the alternative to be computed lazily.
*/
template<typename F, REQUIRES(details::isCallable<F()>)>
T ok_or(F&& orFn) const & {
static_assert(std::is_convertible<std::result_of_t<F()>, T>::value,
"Alternative does not return a type convertible to T.");
static_assert(std::is_copy_constructible<T>::value,
"lvalue okOr requires a copy constructible T.");
if (is_ok()) {
return ok();
} else {
return static_cast<T>(orFn());
}
}
template<typename... Args>
Result& context(Args&&... args) & {
if (is_err()) {
err().context(std::forward<Args...>(args...));
}
return {*this};
}
template<typename... Args>
Result&& context(Args&&... args) && {
if (is_err()) {
err().context(std::forward<Args...>(args...));
}
return std::move(*this);
}
// T& operator->() {
// return ok();
// }
//
// T& operator->() const {
// return ok();
// }
private:
void err_if_(bool b, const char* msg) const {
if (UNLIKELY(b)) {
abort_(msg);
}
}
T& get_(const char* msg = nullptr) noexcept {
err_if_(!is_ok(), msg);
return this->contents.val.get();
}
const T& get_(const char* msg = nullptr) const noexcept {
err_if_(!is_ok(), msg);
return this->contents.val.get();
}
E& getErr_(const char* msg = nullptr) {
err_if_(!is_err(), msg);
return this->contents.err.get();
}
const E& getErr_(const char* msg = nullptr) const noexcept {
err_if_(!is_err(), msg);
return this->contents.err.get();
}
template<typename U>
struct has_get_ctx {
using Yes = char;
using No = Yes[2];
template<typename C>
static auto test(void*)
-> decltype(get_context(std::declval<C>()), Yes{});
template<typename>
static No& test(...);
static constexpr bool value = sizeof(test<E>(0)) == sizeof(Yes);
};
template<REQUIRES(not has_get_ctx<E>::value)>
void print_ctx() const {
}
template<REQUIRES(has_get_ctx<E>::value)>
void print_ctx() const {
static_assert(
std::is_same<decltype(get_context(err())), const char*>::value,
"get_context must return a c string");
std::fprintf(stderr, "Context: %s\n", get_context(err()));
}
void abort_(const char* msg) const {
std::fprintf(stderr,
"Invalid Result access, message given: %s\n",
msg ? msg : "No message given.");
if (is_err()) {
print_ctx();
}
std::abort();
details::unreachable();
}
};
//Provide generic interop with optional<T>
template<typename E, typename T, template<typename> class O>
Result<T,E> ok_or(O<T>&& opt){
if(opt.has_value()){
return *opt;
}
return Err();
}
// rvalue ref keeps a temporary alive the same as a const ref [dcl.init.ref]
//
// TODO: unlikely TODO: currently attempts to return a default error if it's
// invalid.
//
#define Try_(expr) \
({ \
auto result_var_ = (expr); \
if (result_var_.is_err()) { \
return util::Err(std::move(result_var_.err())); \
} else if (result_var_.is_invalid()) { \
std::abort(); \
return util::Err(); \
} \
std::move(result_var_.ok()); \
})
} // namespace util
#pragma pop_macro("LIKELY")
#pragma pop_macro("UNLIKELY")
#pragma pop_macro("REQUIRES")
#endif /* end of include guard: RESULT_HPP_7LRAEJZ5 */