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
|
#pragma once
#include "StarVariant.hpp"
namespace Star {
STAR_EXCEPTION(EitherException, StarException);
template <typename Value>
struct EitherLeftValue {
Value value;
};
template <typename Value>
struct EitherRightValue {
Value value;
};
template <typename Value>
EitherLeftValue<Value> makeLeft(Value value);
template <typename Value>
EitherRightValue<Value> makeRight(Value value);
// Container that contains exactly one of either Left or Right.
template <typename Left, typename Right>
class Either {
public:
// Constructs Either that contains a default constructed Left value
Either();
Either(EitherLeftValue<Left> left);
Either(EitherRightValue<Right> right);
template <typename T>
Either(EitherLeftValue<T> left);
template <typename T>
Either(EitherRightValue<T> right);
Either(Either const& rhs);
Either(Either&& rhs);
Either& operator=(Either const& rhs);
Either& operator=(Either&& rhs);
template <typename T>
Either& operator=(EitherLeftValue<T> left);
template <typename T>
Either& operator=(EitherRightValue<T> right);
bool isLeft() const;
bool isRight() const;
void setLeft(Left left);
void setRight(Right left);
// left() and right() throw EitherException on invalid access
Left const& left() const;
Right const& right() const;
Left& left();
Right& right();
Maybe<Left> maybeLeft() const;
Maybe<Right> maybeRight() const;
// leftPtr() and rightPtr() do not throw on invalid access
Left const* leftPtr() const;
Right const* rightPtr() const;
Left* leftPtr();
Right* rightPtr();
private:
typedef EitherLeftValue<Left> LeftType;
typedef EitherRightValue<Right> RightType;
Variant<LeftType, RightType> m_value;
};
template <typename Value>
EitherLeftValue<Value> makeLeft(Value value) {
return {std::move(value)};
}
template <typename Value>
EitherRightValue<Value> makeRight(Value value) {
return {std::move(value)};
}
template <typename Left, typename Right>
Either<Left, Right>::Either() {}
template <typename Left, typename Right>
Either<Left, Right>::Either(EitherLeftValue<Left> left)
: m_value(std::move(left)) {}
template <typename Left, typename Right>
Either<Left, Right>::Either(EitherRightValue<Right> right)
: m_value(std::move(right)) {}
template <typename Left, typename Right>
template <typename T>
Either<Left, Right>::Either(EitherLeftValue<T> left)
: Either(LeftType{std::move(left.value)}) {}
template <typename Left, typename Right>
template <typename T>
Either<Left, Right>::Either(EitherRightValue<T> right)
: Either(RightType{std::move(right.value)}) {}
template <typename Left, typename Right>
Either<Left, Right>::Either(Either const& rhs)
: m_value(rhs.m_value) {}
template <typename Left, typename Right>
Either<Left, Right>::Either(Either&& rhs)
: m_value(std::move(rhs.m_value)) {}
template <typename Left, typename Right>
Either<Left, Right>& Either<Left, Right>::operator=(Either const& rhs) {
m_value = rhs.m_value;
return *this;
}
template <typename Left, typename Right>
Either<Left, Right>& Either<Left, Right>::operator=(Either&& rhs) {
m_value = std::move(rhs.m_value);
return *this;
}
template <typename Left, typename Right>
template <typename T>
Either<Left, Right>& Either<Left, Right>::operator=(EitherLeftValue<T> left) {
m_value = LeftType{std::move(left.value)};
return *this;
}
template <typename Left, typename Right>
template <typename T>
Either<Left, Right>& Either<Left, Right>::operator=(EitherRightValue<T> right) {
m_value = RightType{std::move(right.value)};
return *this;
}
template <typename Left, typename Right>
bool Either<Left, Right>::isLeft() const {
return m_value.template is<LeftType>();
}
template <typename Left, typename Right>
bool Either<Left, Right>::isRight() const {
return m_value.template is<RightType>();
}
template <typename Left, typename Right>
void Either<Left, Right>::setLeft(Left left) {
m_value = LeftType{std::move(left)};
}
template <typename Left, typename Right>
void Either<Left, Right>::setRight(Right right) {
m_value = RightType{std::move(right)};
}
template <typename Left, typename Right>
Left const& Either<Left, Right>::left() const {
if (auto l = leftPtr())
return *l;
throw EitherException("Improper access of left side of Either");
}
template <typename Left, typename Right>
Right const& Either<Left, Right>::right() const {
if (auto r = rightPtr())
return *r;
throw EitherException("Improper access of right side of Either");
}
template <typename Left, typename Right>
Left& Either<Left, Right>::left() {
if (auto l = leftPtr())
return *l;
throw EitherException("Improper access of left side of Either");
}
template <typename Left, typename Right>
Right& Either<Left, Right>::right() {
if (auto r = rightPtr())
return *r;
throw EitherException("Improper access of right side of Either");
}
template <typename Left, typename Right>
Maybe<Left> Either<Left, Right>::maybeLeft() const {
if (auto l = leftPtr())
return *l;
return {};
}
template <typename Left, typename Right>
Maybe<Right> Either<Left, Right>::maybeRight() const {
if (auto r = rightPtr())
return *r;
return {};
}
template <typename Left, typename Right>
Left const* Either<Left, Right>::leftPtr() const {
if (auto l = m_value.template ptr<LeftType>())
return &l->value;
return nullptr;
}
template <typename Left, typename Right>
Right const* Either<Left, Right>::rightPtr() const {
if (auto r = m_value.template ptr<RightType>())
return &r->value;
return nullptr;
}
template <typename Left, typename Right>
Left* Either<Left, Right>::leftPtr() {
if (auto l = m_value.template ptr<LeftType>())
return &l->value;
return nullptr;
}
template <typename Left, typename Right>
Right* Either<Left, Right>::rightPtr() {
if (auto r = m_value.template ptr<RightType>())
return &r->value;
return nullptr;
}
}
|