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
|
#pragma once
#include <type_traits>
// A somewhat implementation of the expected monad, proposed here:
// https://isocpp.org/files/papers/n4015.pdf
// A class used to differentiate errors and values when they are of the same type.
template <typename Error>
class Unexpected
{
static_assert(!std::is_same<Error, void>::value, "Cannot have an Unexpected void");
public:
Unexpected() = delete;
constexpr explicit Unexpected(const Error &error) : mValue(error) {}
constexpr explicit Unexpected(Error &&error) : mValue(std::move(error)) {}
// For implicit conversions when doing makeUnexpected(other)
template <typename Other>
constexpr explicit Unexpected(const Unexpected<Other> &error) : mValue(error.value())
{
}
template <typename Other>
constexpr explicit Unexpected(Unexpected<Other> &&error) : mValue(std::move(error.value()))
{
}
constexpr const Error &value() const &
{
return mValue;
}
Error &value() &
{
return mValue;
}
constexpr const Error &&value() const &&
{
return std::move(mValue);
}
Error &&value() &&
{
return std::move(mValue);
}
private:
Error mValue;
};
template <class Error>
Unexpected<typename std::decay<Error>::type> makeUnexpected(Error &&e)
{
return Unexpected<typename std::decay<Error>::type>(std::forward<Error>(e));
}
template <typename Error>
bool operator==(const Unexpected<Error> &lhs, const Unexpected<Error> &rhs)
{
return lhs.value() == rhs.value();
}
template <typename Error>
bool operator!=(const Unexpected<Error> &lhs, const Unexpected<Error> &rhs)
{
return lhs.value() != rhs.value();
}
namespace detail {
namespace tags {
struct Expected
{};
struct Unexpected
{};
} // namespace tags
// Write functions here when storage related and when Type != void
template <typename Error, typename Type>
struct StorageBase
{
protected:
// Rule of 5 {{{
StorageBase(const StorageBase &other) : mIsValue(other.mIsValue)
{
// This is a constructor, you have to construct object, not assign them
// (hence the placement new)
if (mIsValue) {
new (std::addressof(mValue)) Type(other.mValue);
} else {
new (std::addressof(mError)) Unexpected<Error>(other.mError);
}
}
StorageBase(StorageBase &&other) : mIsValue(other.mIsValue)
{
// This is a constructor, you have to construct object, not assign them
// (hence the placement new)
if (mIsValue) {
new (std::addressof(mValue)) Type(std::move(other.mValue));
} else {
new (std::addressof(mError)) Unexpected<Error>(std::move(other.mError));
}
}
constexpr StorageBase &operator=(const StorageBase &other)
{
mIsValue = other.mIsValue;
if (mIsValue) {
mValue = other.mValue;
} else {
mError = other.mError;
}
return *this;
}
constexpr StorageBase &operator=(StorageBase &&other)
{
this->~StorageBase();
mIsValue = other.mIsValue;
if (mIsValue) {
mValue = std::move(other.mValue);
} else {
mError = std::move(other.mError);
}
return *this;
}
~StorageBase()
{
if (mIsValue) {
mValue.~Type();
} else {
mError.~Unexpected<Error>();
}
}
// }}}
template <typename... Args>
constexpr StorageBase(tags::Expected, Args &&... args)
: mValue(std::forward<Args>(args)...), mIsValue(true)
{
}
template <typename... Args>
constexpr StorageBase(tags::Unexpected, Args &&... args)
: mError(std::forward<Args>(args)...), mIsValue(false)
{
}
union
{
Unexpected<Error> mError;
Type mValue;
};
bool mIsValue;
};
// Write functions here when storage related and when Type == void
template <typename Error>
struct StorageBase<Error, void>
{
protected:
constexpr StorageBase(tags::Expected) : mIsValue(true) {}
template <typename... Args>
constexpr StorageBase(tags::Unexpected, Args &&... args)
: mError(std::forward<Args>(args)...), mIsValue(false)
{
}
Unexpected<Error> mError;
bool mIsValue;
};
// Write functions here when storage related, whether Type is void or not
template <typename Error, typename Type>
struct Storage : StorageBase<Error, Type>
{
protected:
// Forward the construction to StorageBase
using StorageBase<Error, Type>::StorageBase;
};
// Write functions here when dev API related and when Type != void
template <typename Error, typename Type>
struct ExpectedBase : detail::Storage<Error, Type>
{
constexpr ExpectedBase() : detail::Storage<Error, Type>(detail::tags::Expected{}) {}
template <typename OtherError>
constexpr ExpectedBase(const Unexpected<OtherError> &error)
: detail::Storage<Error, Type>(detail::tags::Unexpected{}, error)
{
}
template <typename OtherError>
constexpr ExpectedBase(Unexpected<OtherError> &&error)
: detail::Storage<Error, Type>(detail::tags::Unexpected{}, std::move(error))
{
}
constexpr ExpectedBase(const Type &value)
: detail::Storage<Error, Type>(detail::tags::Expected{}, value)
{
}
constexpr ExpectedBase(Type &&value)
: detail::Storage<Error, Type>(detail::tags::Expected{}, std::move(value))
{
}
// Warning: will crash if this is an error. You should always check this is
// an expected value before calling `.value()`
constexpr const Type &value() const &
{
Q_ASSERT(this->mIsValue);
return this->mValue;
}
Type &&value() &&
{
Q_ASSERT(this->mIsValue);
return std::move(this->mValue);
}
};
// Write functions here when dev API related and when Type == void
template <typename Error>
struct ExpectedBase<Error, void> : detail::Storage<Error, void>
{
// Rewrite constructors for unexpected because Expected doesn't have direct access to it.
template <typename OtherError>
constexpr ExpectedBase(const Unexpected<OtherError> &error)
: detail::Storage<Error, void>(detail::tags::Unexpected{}, error)
{
}
template <typename OtherError>
constexpr ExpectedBase(Unexpected<OtherError> &&error)
: detail::Storage<Error, void>(detail::tags::Unexpected{}, std::move(error))
{
}
};
} // namespace detail
// Write functions here when dev API related, whether Type is void or not
template <typename Error, typename Type = void>
class Expected : public detail::ExpectedBase<Error, Type>
{
static_assert(!std::is_same<Error, void>::value, "Expected with void Error is not implemented");
public:
using detail::ExpectedBase<Error, Type>::ExpectedBase;
constexpr const Error &error() const &
{
return this->mError.value();
}
constexpr bool isValue() const
{
return this->mIsValue;
}
constexpr explicit operator bool() const
{
return this->mIsValue;
}
};
|