-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult.hpp
More file actions
1365 lines (1137 loc) · 48.7 KB
/
result.hpp
File metadata and controls
1365 lines (1137 loc) · 48.7 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
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef CPP_RESULT_RESULT_HPP
#define CPP_RESULT_RESULT_HPP
#include <functional>
#include <iostream>
#include <type_traits>
#if defined(__cplusplus) && __cplusplus >= 201703L
# define RESULT_MAYBE_UNUSED [[maybe_unused]]
# define RESULT_NODISCARD [[nodiscard]]
#elif defined(__GNUC__) || defined(__clang__)
# define RESULT_MAYBE_UNUSED __attribute__((unused))
# define RESULT_NODISCARD __attribute__((warn_unused_result))
#elif defined(_MSC_VER)
# define RESULT_MAYBE_UNUSED __pragma(warning(suppress : 4505))
# define RESULT_NODISCARD __pragma(warning(error : 6031))
#else
# define RESULT_MAYBE_UNUSED
# define RESULT_NODISCARD
#endif
#define RESULT_ERROR(_m) \
do { \
std::cerr << _m << std::endl; \
std::terminate(); \
} while (0)
#ifdef RESULT_NAMESPACE
namespace Result {
#endif
// forward declaration
template <typename T, typename E>
struct Result;
namespace Utils {
// implementation of a replacement of C++17's std::void_t
template <typename...>
struct make_void {
typedef void type;
};
template <typename... Args>
using void_t = typename make_void<Args...>::type;
// comparable trait
template <typename L, typename R, typename _ = void>
struct is_eq_comparable : std::false_type {};
template <typename L, typename R>
struct is_eq_comparable<L, R, void_t<decltype(std::declval<L>() == std::declval<R>())>>
: std::true_type {};
template <typename L, typename R, typename _ = void>
struct is_ne_comparable : std::false_type {};
template <typename L, typename R>
struct is_ne_comparable<L, R, void_t<decltype(std::declval<L>() != std::declval<R>())>>
: std::true_type {};
template <typename L, typename R, typename _ = void>
struct is_lt_comparable : std::false_type {};
template <typename L, typename R>
struct is_lt_comparable<L, R, void_t<decltype(std::declval<L>() < std::declval<R>())>>
: std::true_type {};
template <typename L, typename R, typename _ = void>
struct is_gt_comparable : std::false_type {};
template <typename L, typename R>
struct is_gt_comparable<L, R, void_t<decltype(std::declval<L>() > std::declval<R>())>>
: std::true_type {};
template <typename L, typename R, typename _ = void>
struct is_le_comparable : std::false_type {};
template <typename L, typename R>
struct is_le_comparable<L, R, void_t<decltype(std::declval<L>() <= std::declval<R>())>>
: std::true_type {};
template <typename L, typename R, typename _ = void>
struct is_ge_comparable : std::false_type {};
template <typename L, typename R>
struct is_ge_comparable<L, R, void_t<decltype(std::declval<L>() >= std::declval<R>())>>
: std::true_type {};
// implementation of a replacement of C++14's Utils::enable_if_t
template <bool B, typename T = void>
using enable_if_t = typename std::enable_if<B, T>::type;
// implementation of a replacement of C++14's std::is_convertible
namespace Details {
template <typename, typename _ = void>
struct is_returnable : std::false_type {};
template <typename T>
struct is_returnable<T, void_t<decltype(static_cast<T (*)()>(nullptr))>> : std::true_type {};
template <typename, typename, typename = void>
struct is_implicit_convertible : std::false_type {};
template <typename From, typename To>
struct is_implicit_convertible<From, To,
void_t<decltype(std::declval<void (&)(To)>()(std::declval<From>()))>>
: std::true_type {};
template <typename F, typename T>
struct is_convertible : std::integral_constant<bool, is_returnable<T>::value &&
is_implicit_convertible<F, T>::value> {};
template <>
struct is_convertible<void, void> : std::true_type {};
} // namespace Details
template <typename F, typename T>
struct is_convertible : Details::is_convertible<F, T> {};
// implementation of a replacement of C++14's constexpr Utils::max
template <typename T, Utils::enable_if_t<Utils::is_gt_comparable<T, T>::value, bool> = true>
constexpr T max(T a, T b) {
return a > b ? a : b;
}
// decomposition of function-like calls to extract the result
template <typename F, typename C = void>
struct result_of;
template <typename R, typename... Args>
struct result_of<R(Args...)> {
typedef R type;
};
template <typename C, typename... Args, typename R>
struct result_of<R (C::*)(Args...)> : result_of<R(Args...)> {};
template <typename C, typename... Args, typename R>
struct result_of<R (C::*)(Args...) const> : result_of<R(Args...)> {};
template <typename R, typename... Args>
struct result_of<R (*)(Args...)> : result_of<R(Args...)> {};
template <typename F>
struct result_of<F, Utils::void_t<decltype(&F::operator())>> : result_of<decltype(&F::operator())> {
};
} // namespace Utils
namespace Wrapper {
template <typename T>
struct Ok {
explicit Ok(const T &t) : _t{ t } {}
explicit Ok(T &&t) : _t{ std::move(t) } {}
template <typename U, Utils::enable_if_t<Utils::is_eq_comparable<T, U>::value, bool> = true>
bool operator==(const Ok<U> &other) {
return _t == other._t;
}
template <typename U, Utils::enable_if_t<Utils::is_ne_comparable<T, U>::value, bool> = true>
bool operator!=(const Ok<U> &other) {
return _t != other._t;
}
template <typename U, Utils::enable_if_t<Utils::is_lt_comparable<T, U>::value, bool> = true>
bool operator<(const Ok<U> &other) {
return _t < other._t;
}
template <typename U, Utils::enable_if_t<Utils::is_gt_comparable<T, U>::value, bool> = true>
bool operator>(const Ok<U> &other) {
return _t > other._t;
}
template <typename U, Utils::enable_if_t<Utils::is_le_comparable<T, U>::value, bool> = true>
bool operator<=(const Ok<U> &other) {
return _t <= other._t;
}
template <typename U, Utils::enable_if_t<Utils::is_ge_comparable<T, U>::value, bool> = true>
bool operator>=(const Ok<U> &other) {
return _t >= other._t;
}
T _t;
};
template <>
struct Ok<void> {};
template <typename E>
struct Err {
explicit Err(const E &e) : _e{ e } {}
explicit Err(E &&e) : _e{ std::move(e) } {}
template <typename U, Utils::enable_if_t<Utils::is_eq_comparable<E, U>::value, bool> = true>
bool operator==(const Err<U> &other) {
return _e == other._e;
}
template <typename U, Utils::enable_if_t<Utils::is_ne_comparable<E, U>::value, bool> = true>
bool operator!=(const Err<U> &other) {
return _e != other._e;
}
template <typename U, Utils::enable_if_t<Utils::is_lt_comparable<E, U>::value, bool> = true>
bool operator<(const Err<U> &other) {
return _e < other._e;
}
template <typename U, Utils::enable_if_t<Utils::is_gt_comparable<E, U>::value, bool> = true>
bool operator>(const Err<U> &other) {
return _e > other._e;
}
template <typename U, Utils::enable_if_t<Utils::is_le_comparable<E, U>::value, bool> = true>
bool operator<=(const Err<U> &other) {
return _e <= other._e;
}
template <typename U, Utils::enable_if_t<Utils::is_ge_comparable<E, U>::value, bool> = true>
bool operator>=(const Err<U> &other) {
return _e >= other._e;
}
E _e;
};
template <>
struct Err<void> {};
} // namespace Wrapper
template <typename T>
RESULT_MAYBE_UNUSED static Wrapper::Ok<typename std::decay<T>::type> Ok(T &&t) {
return Wrapper::Ok<typename std::decay<T>::type>(std::forward<T>(t));
}
template <typename E>
RESULT_MAYBE_UNUSED static Wrapper::Err<typename std::decay<E>::type> Err(E &&e) {
return Wrapper::Err<typename std::decay<E>::type>(std::forward<E>(e));
}
RESULT_MAYBE_UNUSED static Wrapper::Ok<void> Ok() { return Wrapper::Ok<void>{}; }
RESULT_MAYBE_UNUSED static Wrapper::Err<void> Err() { return Wrapper::Err<void>{}; }
namespace Utils {
// QoL utility to check if some type is Result<T, E> and to decompose Result<T,
// E>
template <typename T>
struct is_result : std::false_type {};
template <typename T, typename E>
struct is_result<Result<T, E>> : std::true_type {};
template <typename T>
struct destruct_result {
typedef void type;
};
template <typename T, typename E>
struct destruct_result<Result<T, E>> {
typedef T Ok;
typedef E Err;
};
// storage container for an arbitrary result
template <typename T, typename E>
struct Storage {
alignas(Utils::max(alignof(T), alignof(E)))
std::array<std::uint8_t, Utils::max(sizeof(T), sizeof(E))> _storage = { 0 };
std::size_t _type;
RESULT_MAYBE_UNUSED explicit Storage(const Wrapper::Ok<T> &s)
: _type{ typeid(Wrapper::Ok<T>).hash_code() } {
construct(s);
}
RESULT_MAYBE_UNUSED explicit Storage(const Wrapper::Err<E> &s)
: _type{ typeid(Wrapper::Err<E>).hash_code() } {
construct(s);
}
RESULT_MAYBE_UNUSED explicit Storage(Wrapper::Ok<T> &&s)
: _type{ typeid(Wrapper::Ok<T>).hash_code() } {
construct(std::move(s));
}
RESULT_MAYBE_UNUSED explicit Storage(Wrapper::Err<E> &&s)
: _type{ typeid(Wrapper::Err<E>).hash_code() } {
construct(std::move(s));
}
void construct(Wrapper::Ok<T> ok) {
(void) (*reinterpret_cast<T *>(this->_storage.data()) = std::move(ok._t));
}
void construct(Wrapper::Err<E> err) {
(void) (*reinterpret_cast<E *>(this->_storage.data()) = std::move(err._e));
}
template <typename _, typename U = typename std::decay<_>::type,
Utils::enable_if_t<!std::is_same<_, void>::value, bool> = true>
auto get() const -> const U & {
return *reinterpret_cast<const U *>(this->_storage.data());
}
template <typename _, typename U = typename std::decay<_>::type,
Utils::enable_if_t<!std::is_same<_, void>::value, bool> = true>
auto get() -> U & {
return *reinterpret_cast<U *>(this->_storage.data());
}
template <typename _, typename U = typename std::decay<_>::type,
Utils::enable_if_t<std::is_same<_, void>::value, bool> = true>
auto get() const -> void {}
template <typename S>
RESULT_MAYBE_UNUSED RESULT_NODISCARD bool holds_alternative() const {
return typeid(S).hash_code() == _type;
}
};
template <typename E>
struct Storage<void, E> {
alignas(alignof(E)) std::array<std::uint8_t, sizeof(E)> _storage = { 0 };
std::size_t _type;
RESULT_MAYBE_UNUSED explicit Storage(const Wrapper::Ok<void> &t)
: _type{ typeid(Wrapper::Ok<void>).hash_code() } {
construct(t);
}
RESULT_MAYBE_UNUSED explicit Storage(const Wrapper::Err<E> &s)
: _type{ typeid(Wrapper::Err<E>).hash_code() } {
construct(s);
}
RESULT_MAYBE_UNUSED explicit Storage(Wrapper::Ok<void> &&t)
: _type{ typeid(Wrapper::Ok<void>).hash_code() } {
construct(std::move(t));
}
RESULT_MAYBE_UNUSED explicit Storage(Wrapper::Err<E> &&s)
: _type{ typeid(Wrapper::Err<E>).hash_code() } {
construct(std::move(s));
}
void construct(Wrapper::Ok<void> ok) { (void) ok; }
void construct(Wrapper::Err<E> err) {
(void) (*reinterpret_cast<E *>(this->_storage.data()) = std::move(err._e));
}
template <typename _, typename U = typename std::decay<_>::type,
Utils::enable_if_t<!std::is_same<_, void>::value, bool> = true>
auto get() const -> const U & {
return *reinterpret_cast<const U *>(this->_storage.data());
}
template <typename _, typename U = typename std::decay<_>::type,
Utils::enable_if_t<!std::is_same<_, void>::value, bool> = true>
auto get() -> U & {
return *reinterpret_cast<U *>(this->_storage.data());
}
template <typename _, typename U = typename std::decay<_>::type,
Utils::enable_if_t<std::is_same<_, void>::value, bool> = true>
auto get() const -> void {}
template <typename S>
RESULT_MAYBE_UNUSED RESULT_NODISCARD bool holds_alternative() const {
return typeid(S).hash_code() == _type;
}
};
template <typename T>
struct Storage<T, void> {
alignas(alignof(T)) std::array<std::uint8_t, sizeof(T)> _storage = { 0 };
std::size_t _type;
RESULT_MAYBE_UNUSED explicit Storage(const Wrapper::Ok<T> &s)
: _type{ typeid(Wrapper::Ok<T>).hash_code() } {
construct(s);
}
RESULT_MAYBE_UNUSED explicit Storage(const Wrapper::Err<void> &e)
: _type{ typeid(Wrapper::Err<void>).hash_code() } {
construct(e);
}
RESULT_MAYBE_UNUSED explicit Storage(Wrapper::Ok<T> &&s)
: _type{ typeid(Wrapper::Ok<T>).hash_code() } {
construct(std::move(s));
}
RESULT_MAYBE_UNUSED explicit Storage(Wrapper::Err<void> &&e)
: _type{ typeid(Wrapper::Err<void>).hash_code() } {
construct(std::move(e));
}
void construct(Wrapper::Ok<T> ok) {
(void) (*reinterpret_cast<T *>(this->_storage.data()) = std::move(ok._t));
}
void construct(Wrapper::Err<void> err) { (void) err; }
template <typename _, typename U = typename std::decay<_>::type,
Utils::enable_if_t<!std::is_same<_, void>::value, bool> = true>
auto get() const -> const U & {
return *reinterpret_cast<const U *>(this->_storage.data());
}
template <typename _, typename U = typename std::decay<_>::type,
Utils::enable_if_t<!std::is_same<_, void>::value, bool> = true>
auto get() -> U & {
return *reinterpret_cast<U *>(this->_storage.data());
}
template <typename _, typename U = typename std::decay<_>::type,
Utils::enable_if_t<std::is_same<_, void>::value, bool> = true>
auto get() const -> void {}
template <typename S>
RESULT_MAYBE_UNUSED RESULT_NODISCARD bool holds_alternative() const {
return typeid(S).hash_code() == _type;
}
};
template <>
struct Storage<void, void> {
std::size_t _type;
RESULT_MAYBE_UNUSED explicit Storage(const Wrapper::Ok<void> &t)
: _type{ typeid(Wrapper::Ok<void>).hash_code() } {
std::ignore = t;
}
RESULT_MAYBE_UNUSED explicit Storage(const Wrapper::Err<void> &e)
: _type{ typeid(Wrapper::Err<void>).hash_code() } {
std::ignore = e;
}
RESULT_MAYBE_UNUSED explicit Storage(Wrapper::Ok<void> &&t)
: _type{ typeid(Wrapper::Ok<void>).hash_code() } {
std::ignore = t;
}
RESULT_MAYBE_UNUSED explicit Storage(Wrapper::Err<void> &&e)
: _type{ typeid(Wrapper::Err<void>).hash_code() } {
std::ignore = e;
}
RESULT_MAYBE_UNUSED void construct(Wrapper::Ok<void> ok) { (void) ok; }
RESULT_MAYBE_UNUSED void construct(Wrapper::Err<void> err) { (void) err; }
template <typename _, typename U = std::decay<_>>
void get() {}
template <typename S>
RESULT_MAYBE_UNUSED RESULT_NODISCARD bool holds_alternative() const {
return typeid(S).hash_code() == _type;
}
};
} // namespace Utils
namespace Wrapper {
template <typename S>
struct Get;
template <>
struct Get<Ok<void>> {
template <typename _>
static Ok<void> get(const Result<void, _> &r) {
std::ignore = r;
return Ok<void>{};
}
};
template <typename T>
struct Get<Ok<T>> {
template <typename _>
static Ok<T> get(const Result<T, _> &r) {
auto ok = r.storage().template get<T>();
return Ok<T>(std::move(ok));
}
};
template <>
struct Get<Err<void>> {
template <typename _>
static Err<void> get(const Result<_, void> &r) {
std::ignore = r;
return Err<void>{};
}
};
template <typename E>
struct Get<Err<E>> {
template <typename _>
static Err<E> get(const Result<_, E> &r) {
auto err = r.storage().template get<E>();
return Err<E>(std::move(err));
}
};
} // namespace Wrapper
/**
* Implementation namespace of transformer functions
*/
namespace Impl {
// map
template <typename T>
struct Map;
template <typename R, typename C, typename... Args>
struct Map<R (C::*)(Args...) const> : public Map<R(Args...)> {};
template <typename R, typename C, typename Args>
struct Map<R (C::*)(Args...)> : public Map<R(Args...)> {};
template <typename R, typename Arg>
struct Map<R(Arg)> {
template <typename T, typename E, typename F,
Utils::enable_if_t<Utils::is_convertible<T, Arg>::value, bool> = true>
RESULT_MAYBE_UNUSED static Result<R, E> map(const Result<T, E> &r RESULT_MAYBE_UNUSED, F fun) {
auto ok = r.storage().template get<T>();
auto ret = fun(std::move(ok));
return Ok(std::move(ret));
}
};
template <typename Arg>
struct Map<void(Arg)> {
template <typename T, typename E, typename F,
Utils::enable_if_t<Utils::is_convertible<T, Arg>::value, bool> = true>
RESULT_MAYBE_UNUSED static Result<void, E> map(const Result<T, E> &r RESULT_MAYBE_UNUSED,
F fun) {
auto ok = r.storage().template get<T>();
fun(std::move(ok));
return Wrapper::Ok<void>();
}
};
template <typename R>
struct Map<R(void)> {
template <typename T, typename E, typename F>
RESULT_MAYBE_UNUSED static Result<R, E> map(const Result<T, E> &r RESULT_MAYBE_UNUSED, F fun) {
auto ret = fun();
return Ok(std::move(ret));
}
};
template <>
struct Map<void(void)> {
template <typename T, typename E, typename F>
RESULT_MAYBE_UNUSED static Result<void, E> map(const Result<T, E> &r RESULT_MAYBE_UNUSED,
F fun) {
fun();
return Wrapper::Ok<void>();
}
};
// map err
template <typename T>
struct MapErr;
template <typename R, typename C, typename... Args>
struct MapErr<R (C::*)(Args...) const> : public MapErr<R(Args...)> {};
template <typename R, typename C, typename... Args>
struct MapErr<R (C::*)(Args...)> : public MapErr<R(Args...)> {};
template <typename R, typename Arg>
struct MapErr<R(Arg)> {
template <typename T, typename E, typename F,
Utils::enable_if_t<Utils::is_convertible<E, Arg>::value, bool> = true>
RESULT_MAYBE_UNUSED static Result<T, R> map_err(const Result<T, E> &r RESULT_MAYBE_UNUSED,
F fun) {
auto err = r.storage().template get<E>();
auto ret = fun(std::move(err));
return Err(std::move(ret));
}
};
template <typename Arg>
struct MapErr<void(Arg)> {
template <typename T, typename E, typename F,
Utils::enable_if_t<Utils::is_convertible<E, Arg>::value, bool> = true>
RESULT_MAYBE_UNUSED static Result<T, void> map_err(const Result<T, E> &r RESULT_MAYBE_UNUSED,
F fun) {
auto err = r.storage().template get<E>();
fun(std::move(err));
return Wrapper::Err<void>();
}
};
template <typename R>
struct MapErr<R(void)> {
template <typename T, typename E, typename F>
RESULT_MAYBE_UNUSED static Result<T, R> map_err(const Result<T, E> &r RESULT_MAYBE_UNUSED,
F fun) {
auto ret = fun();
return Err(std::move(ret));
}
};
template <>
struct MapErr<void(void)> {
template <typename T, typename E, typename F>
RESULT_MAYBE_UNUSED static Result<T, void> map_err(const Result<T, E> &r RESULT_MAYBE_UNUSED,
F fun) {
fun();
return Wrapper::Err<void>();
}
};
} // namespace Impl
/**
* SFINAE resolver namespace
*/
namespace Resolve {
// map
template <typename F>
struct Map : public Impl::Map<decltype(&F::operator())> {};
template <typename R, typename... Args>
struct Map<R (*)(Args...)> : public Impl::Map<R(Args...)> {};
template <typename R, typename C, typename... Args>
struct Map<R (C::*)(Args...)> : public Impl::Map<R(Args...)> {};
template <typename R, typename C, typename... Args>
struct Map<R (C::*)(Args...) const> : public Impl::Map<R(Args...)> {};
template <typename R, typename... Args>
struct Map<std::function<R(Args...)>> : public Impl::Map<R(Args...)> {};
// map error
template <typename F>
struct MapErr : public Impl::MapErr<decltype(&F::operator())> {};
template <typename R, typename... Args>
struct MapErr<R (*)(Args...)> : public Impl::MapErr<R(Args...)> {};
template <typename R, typename C, typename... Args>
struct MapErr<R (C::*)(Args...)> : public Impl::MapErr<R(Args...)> {};
template <typename R, typename C, typename... Args>
struct MapErr<R (C::*)(Args...) const> : public Impl::MapErr<R(Args...)> {};
template <typename R, typename... Args>
struct MapErr<std::function<R(Args...)>> : public Impl::MapErr<R(Args...)> {};
} // namespace Resolve
namespace Proxy {
template <typename T, typename E, typename F, typename R = typename Utils::result_of<F>::type>
static Result<R, E> map(const Result<T, E> &res, F fun) {
return Resolve::Map<F>::map(res, fun);
}
template <typename T, typename E, typename F, typename R = typename Utils::result_of<F>::type>
static Result<T, R> map_err(const Result<T, E> &res, F fun) {
return Resolve::MapErr<F>::map_err(res, fun);
}
} // namespace Proxy
template <typename T, typename E>
struct Result {
private:
Utils::Storage<T, E> _storage;
public:
/**
* @brief Allows the construction of a Result<T, E> instance through
* rvalue's of type Ok<T> and Err<E>.
*/
RESULT_NODISCARD Result(Wrapper::Ok<T> &&s) noexcept : _storage{ std::move(s) } {}
RESULT_NODISCARD Result(Wrapper::Err<E> &&s) noexcept : _storage{ std::move(s) } {}
/**
* @brief Allows the construction of a Result<T, E> instance through an
* rvalue of type Result<T, E>.
*/
RESULT_NODISCARD Result(Result<T, E> &&o) noexcept : _storage{ std::move(o._storage) } {}
RESULT_NODISCARD Result<T, E> &operator=(Result<T, E> &&o) noexcept {
this->_storage = std::move(o._storage);
return *this;
}
/**
* @brief Results should not be copyable.
*/
RESULT_NODISCARD Result(const Result &o) = delete;
RESULT_NODISCARD Result<T, E> &operator=(const Result &o) noexcept = delete;
/**
* @brief Reference to the underlying container storing either an instance
* of T or E.
*/
RESULT_MAYBE_UNUSED decltype(Result<T, E>::_storage) &storage() { return this->_storage; }
RESULT_MAYBE_UNUSED const decltype(Result<T, E>::_storage) &storage() const {
return this->_storage;
}
/**
* @brief Queries information about whether the contained result is Ok<T> or
* Err<E>
*/
RESULT_MAYBE_UNUSED RESULT_NODISCARD bool is_ok() const {
return storage().template holds_alternative<Wrapper::Ok<T>>();
}
RESULT_MAYBE_UNUSED RESULT_NODISCARD bool is_err() const {
return storage().template holds_alternative<Wrapper::Err<E>>();
}
/**
* @brief Only available if the type T of Ok<T> is not void.
* Moves the contained instance of type T when the Result<T, E>
* contains Ok<T>. However in case of containing an Err<E> it will fail.
*/
template <typename _ = typename std::decay<T>::type,
typename R = typename std::enable_if<!std::is_same<_, void>::value, _>::type>
RESULT_MAYBE_UNUSED R unwrap() {
if (is_ok()) {
return std::move(storage().template get<R>());
}
RESULT_ERROR("Tried to unwrap a result containing an error");
}
/**
* @brief Only available if the type E of Err<E> is not void.
* Moves the contained instance of type E when the Result<T, E>
* contains Err<E>. However in case of containing an Ok<T> it will fail.
*/
template <typename _ = typename std::decay<E>::type,
typename R = typename std::enable_if<!std::is_same<_, void>::value, _>::type>
RESULT_MAYBE_UNUSED R unwrap_err() {
if (is_err()) {
return std::move(storage().template get<R>());
}
RESULT_ERROR("Tried to unwrap an error containing a result");
}
/**
* @brief Unchecked variants of unwrap and unwrap_err.
* These should only be used if we have absolute certainty abut the
* desired result. Moves the contained T (or E) from the result.
*/
template <typename _ = typename std::decay<E>::type,
typename R = typename std::enable_if<!std::is_same<_, void>::value, _>::type>
RESULT_MAYBE_UNUSED R unwrap_unchecked() {
return std::move(storage().template get<R>());
}
template <typename _ = typename std::decay<E>::type,
typename R = typename std::enable_if<!std::is_same<_, void>::value, _>::type>
RESULT_MAYBE_UNUSED R unwrap_err_unchecked() {
return std::move(storage().template get<R>());
}
/**
* @brief Only available if the type T of Ok<T> is not void.
* Returns a reference to the contained instance of type T when the
* Result<T, E> is Ok<T>. However in case of Err<E> it will return a reference
* to the explicitly stated reference of type T.
*/
template <typename _ = typename std::decay<T>::type,
typename R = typename std::enable_if<!std::is_same<_, void>::value, _>::type>
RESULT_MAYBE_UNUSED R unwrap_or(const _ &t) {
if (is_ok()) {
return std::move(storage().template get<T>());
}
return t;
}
/**
* @brief Only available if type T of Ok<T> can be default constructed and
* if T is not void. In case the Result<T, E> contains Err<E> this function
* returns a default constructed instance of T, else it moves the contained
* instance of T.
*/
template <typename _ = typename std::decay<T>::type,
typename R = typename std::enable_if<!std::is_same<_, void>::value, _>::type,
Utils::enable_if_t<std::is_default_constructible<R>::value, bool> = true>
RESULT_MAYBE_UNUSED R unwrap_or_default() {
if (is_err()) {
return R{};
}
return std::move(storage().template get<R>());
}
/**
* @brief Only available if the type T of Ok<T> is not void.
* Moves the contained instance of T when the Result<T, E> contains
* Ok<T>. However in case of Err<E> it will fail like unwrap() but display the
* specific given string.
*/
template <typename _ = typename std::decay<T>::type,
typename R = typename std::enable_if<!std::is_same<_, void>::value, _>::type>
RESULT_MAYBE_UNUSED R expect(const std::string s) {
if (is_ok()) {
return std::move(storage().template get<R>());
}
RESULT_ERROR(s);
}
/**
* @brief Transforms a Result<T, E> to Result<R, E>.
* If the result contains an Err<E> nothing happens.
* The function used for the mapping must either discard T (no arg)
* or take exactly one argument which T needs to be implicit convertible to.
* Consumes the underlying Result<T, E>.
*/
template <typename F, typename R = typename Utils::result_of<F>::type,
Utils::enable_if_t<!Utils::is_result<R>::value, bool> = true>
RESULT_MAYBE_UNUSED Result<R, E> map(F fun) {
if (is_ok()) {
return Proxy::map(*this, fun);
}
// constructs a Result<R, E> from Err<E> contained in Result<T, E>
return Wrapper::Get<Wrapper::Err<E>>::get(*this);
}
/**
* @brief Transforms a Result<T, E> to Result<T, R>.
* If the result contains an Ok<T> nothing happens.
* The function used for the mapping must either discard E (no arg)
* or take exactly one argument which E needs to be implicit convertible to.
* Consumes the underlying Result<T, E>.
*/
template <typename F, typename R = typename Utils::result_of<F>::type,
Utils::enable_if_t<!Utils::is_result<R>::value, bool> = true>
RESULT_MAYBE_UNUSED Result<T, R> map_err(F fun) {
if (is_err()) {
return Proxy::map_err(*this, fun);
}
// constructs a Result<R, E> from Err<E> contained in Result<T, E>
return Wrapper::Get<Wrapper::Ok<T>>::get(*this);
}
/**
* @brief Transforms a Result<T, E> to Result<R, T>.
* If the result contains an Err<E> the specified instance of type R
* will be returned via reference, else the transformation is applied and the
* result Consumes the underlying base Result<T, E>.
*/
template <typename F, typename R = typename Utils::result_of<F>::type,
Utils::enable_if_t<!std::is_same<R, void>::value, bool> = true>
RESULT_MAYBE_UNUSED R map_or(F fun, R r) {
if (is_ok()) {
return std::move(Proxy::map(*this, fun).storage().template get<R>());
}
return r;
}
/**
* @brief Transforms a Result<T, E> to R if the result contains an Ok<T>.
* Transforms a Result<T, E> to R if the result contains an Err<E>.
* These functions do not need to be the same.
* Consumes the underlying base Result<T, E>.
*/
template <
typename F, typename O, typename R = typename Utils::result_of<F>::type,
Utils::enable_if_t<std::is_same<R, typename Utils::result_of<O>::type>::value, bool> = true>
RESULT_MAYBE_UNUSED R map_or_else(F fun, O other) {
if (is_ok()) {
return std::move(Proxy::map(*this, fun).storage().template get<R>());
} else {
return std::move(Proxy::map_err(*this, other).storage().template get<R>());
}
}
/**
* @brief Transforms a Result<T, E> to Result<U, E> is the result contains
* OK<T> else it returns Err<E> contained in the result. Consumes the
* underlying Result<T, E>.
*/
template <typename F, typename R = typename Utils::result_of<F>::type,
typename U = typename Utils::destruct_result<R>::Ok>
RESULT_MAYBE_UNUSED Result<U, E> and_then(F fun) {
if (is_ok()) {
return std::move(Proxy::map(*this, fun).storage().template get<R>());
}
// constructs Result<U, E> from Err<E>
return Wrapper::Get<Wrapper::Err<E>>::get(*this);
}
/**
* @brief Transforms a Result<T, E> to Result<U, E> is the result contains
* OK<T> else it returns Err<E> contained in the result. Consumes the
* underlying Result<T, E>.
*/
template <typename F, typename R = typename Utils::result_of<F>::type,
typename U = typename Utils::destruct_result<R>::Err>
RESULT_MAYBE_UNUSED Result<T, U> or_else(F fun) {
if (is_err()) {
return std::move(Proxy::map_err(*this, fun).storage().template get<R>());
}
// constructs Result<U, E> from Err<E>
return Wrapper::Get<Wrapper::Ok<T>>::get(*this);
}
/**
* @brief Only available if T is equality comparable.
* Returns true if both Result<T, E> and Ok<T> contain Ok<T>
* and the instances of T are equal, else false.
*/
template <typename U, Utils::enable_if_t<Utils::is_eq_comparable<U, T>::value, bool> = true>
bool operator==(const Wrapper::Ok<U> &other) {
return is_ok() && (storage().template get<T>() == other._t);
}
/**
* @brief Only available if E is equality comparable.
* Returns true if both Result<T, E> and Err<E> contain Err<E>
* and the instances of E are equal, else false.
*/
template <typename U, Utils::enable_if_t<Utils::is_eq_comparable<U, E>::value, bool> = true>
bool operator==(const Wrapper::Err<U> &other) {
return is_err() && (storage().template get<E>() == other._e);
}
/**
* @brief Only available if T but no E is equality comparable.
* Returns true if both results contain Ok<T> and the instances of T
* are equal, else false.
*/
template <typename F, typename G,
Utils::enable_if_t<Utils::is_eq_comparable<F, T>::value, bool> = true,
Utils::enable_if_t<!Utils::is_eq_comparable<G, E>::value, bool> = true>
bool operator==(const Result<F, G> &other) {
return (is_ok() && other.is_ok()) &&
(storage().template get<T>() == other.storage().template get<F>());
}
/**
* @brief Only available if E but no T is equality comparable.
* Returns true if both results contain Err<E> and the instances of E
* are equal, else false.
*/
template <typename F, typename G,
Utils::enable_if_t<!Utils::is_eq_comparable<F, T>::value, bool> = true,
Utils::enable_if_t<Utils::is_eq_comparable<G, E>::value, bool> = true>
bool operator==(const Result<F, G> &other) {
return (is_err() && other.is_err()) &&
(storage().template get<E>() == other.storage().template get<G>());
}
/**
* @brief Only available if T and E are equality comparable.
* Returns true if both results contain Ok<T> and the instances of T
* are equal. Returns true if both results contain Err<E> and the instances of
* E are equal. Else returns false.
*/
template <typename F, typename G,
Utils::enable_if_t<Utils::is_eq_comparable<F, T>::value, bool> = true,
Utils::enable_if_t<Utils::is_eq_comparable<G, E>::value, bool> = true>
bool operator==(const Result<F, G> &other) {
if (is_ok() && other.is_ok()) {
return storage().template get<T>() == other.storage().template get<F>();
}
if (is_err() && other.is_err()) {
return storage().template get<E>() == other.storage().template get<G>();
}
return false;