-
Notifications
You must be signed in to change notification settings - Fork 0
/
either.hpp
173 lines (132 loc) · 3.98 KB
/
either.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
#ifndef EITHER_HPP
#define EITHER_HPP
#include "functor.hpp"
#include "monad.hpp"
#include "overload.hpp"
#include <type_traits>
#include <cassert>
#include <new>
// either monad
template<class Left, class Right>
class either: public functor,
public monad {
typename std::aligned_union<0, Left, Right>::type storage;
const bool ok;
template<class T>
T& cast() {
return *reinterpret_cast<T*>(&storage);
}
template<class T>
const T& cast() const {
return *reinterpret_cast<const T*>(&storage);
}
public:
using value_type = Right;
// monadic return (none/just)
either(Right right): ok(true) {
new (&storage) Right{std::move(right)};
}
either(Left left): ok(false) {
new (&storage) Left{std::move(left)};
}
explicit operator bool() const { return ok; }
template<class Self, class Cont>
static auto visit(Self&& self, Cont cont) {
if(self.ok) {
return cont(std::forward<Self>(self).right());
} else {
return cont(std::forward<Self>(self).left());
}
}
template<class Cont>
friend auto visit(either&& self, Cont cont) {
return either::visit(std::move(self), std::move(cont));
}
template<class Cont>
friend auto visit(const either& self, Cont cont) {
return either::visit(self, std::move(cont));
}
template<class Self, class ... Cases>
static auto match(Self&& self, Cases... cases) {
return visit(std::forward<Self>(self), overload<Cases...>{cases...});
}
template<class ... Cases>
friend auto match(either&& self, Cases&&... cases) {
return match(std::move(self), std::forward<Cases>(cases)...);
}
template<class ... Cases>
friend auto match(const either& self, Cases&&... cases) {
return match(self, std::forward<Cases>(cases)...);
}
const Right* get() const {
if(!ok) return nullptr;
return reinterpret_cast<const Right*>(&storage);
}
Right* get() {
if(!ok) return nullptr;
return reinterpret_cast<Right*>(&storage);
}
const Right& right() const {
assert(ok);
return cast<Right>();
};
Right& right() {
assert(ok);
return cast<Right>();
};
const Left& left() const {
assert(!ok);
return cast<Left>();
};
Left& left() {
assert(!ok);
return cast<Left>();
};
template<class Func>
static auto map(either&& self, Func func) {
using value_type = typename std::result_of<Func(Right)>::type;
using result_type = either<Left, value_type>;
return match(self,
[](Left& left) -> result_type {
return std::move(left);
},
[&](Right& right) -> result_type {
return func(std::move(right));
});
}
template<class Func>
static auto map(const either& self, Func func) {
using value_type = typename std::result_of<Func(Right)>::type;
using result_type = either<Left, value_type>;
return match(self,
[](const Left& left) -> result_type {
return left;
},
[&](const Right& right) -> result_type {
return func(right);
});
}
template<class Func>
static auto bind(either&& self, const Func& func) {
using result_type = typename std::result_of<Func(Right)>::type;
return match(self,
[](Left& left) -> result_type {
return std::move(left);
},
[&](Right& right) -> result_type {
return func(std::move(right));
});
}
template<class Func>
static auto bind(const either& self, const Func& func) {
using result_type = typename std::result_of<Func(Right)>::type;
return match(self,
[](const Left& left) -> result_type {
return std::move(left);
},
[&](const Right& right) -> result_type {
return func(std::move(right));
});
}
};
#endif