This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monadic_error.hpp
305 lines (250 loc) · 10.8 KB
/
monadic_error.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
#ifndef MUSH_MONADIC_ERROR_HPP
#define MUSH_MONADIC_ERROR_HPP
#include <utility>
#include <functional>
#include <new>
#include <string>
namespace mush
{
struct Result_OK {};
template <typename FlagType>
struct Result_Flags
{
constexpr static FlagType CLEAR_FLAGS = 0x00;
constexpr static FlagType HAS_VALUE = 0x01;
constexpr static FlagType NEED_CLEANUP = 0x02;
FlagType flags = 0;
void set_value() { flags |= HAS_VALUE; }
void unset_value() { flags &= ~HAS_VALUE; }
void set_dirty() { flags |= NEED_CLEANUP; }
void set_clean() { flags &= ~NEED_CLEANUP; }
void clear_flags() { flags = 0; }
bool has_value() const { return flags & HAS_VALUE; }
bool is_dirty() const { return flags & NEED_CLEANUP; }
Result_Flags(bool value = false) { if (value) set_value(); }
};
template <typename ValueType, typename ErrorType, typename FlagType, bool ValueIsRef>
struct Result_Storage
{
union {
ValueType value;
ErrorType error;
};
Result_Flags<FlagType> flags;
constexpr Result_Storage() {}
template <typename V = ValueType, typename std::enable_if_t<!std::is_same<V,ErrorType>::value, int> = 0>
constexpr Result_Storage(ValueType value) : value(std::move(value)), flags(true) {}
constexpr Result_Storage(ErrorType error) : error(std::move(error)), flags() {}
void clean()
{
if (flags.is_dirty())
{
if (flags.has_value()) value.~ValueType();
else error.~ErrorType();
}
}
~Result_Storage() { clean(); }
};
template <typename ValueType, typename ErrorType, typename FlagType>
struct Result_Storage<ValueType, ErrorType, FlagType, true>
{
union {
typename std::remove_reference<ValueType>::type* value;
ErrorType error;
};
Result_Flags<FlagType> flags;
constexpr Result_Storage(ValueType& ref) : flags(true) { value = &ref; }
constexpr Result_Storage(ErrorType error) : error(std::move(error)), flags() {}
void clean()
{
if (flags.has_value()) return;
if (flags.is_dirty())
error.~ErrorType();
}
~Result_Storage() { clean(); }
};
template <typename ErrorType, typename FlagType, bool ValueIsRef>
struct Result_Storage<void, ErrorType, FlagType, ValueIsRef>
{
ErrorType error;
Result_Flags<FlagType> flags;
constexpr Result_Storage(bool good = false) : flags(good) {}
constexpr Result_Storage(ErrorType error) : error(std::move(error)), flags() {}
void clean()
{
if (flags.is_dirty())
error.~ErrorType();
}
~Result_Storage() { clean(); }
};
template <typename ValueType, typename ErrorType, typename FlagType = unsigned char,
bool ValueIsRef = std::is_reference<ValueType>::value>
class Basic_Result
{
private:
Result_Storage<ValueType, ErrorType, FlagType, ValueIsRef> stored;
public:
constexpr Basic_Result() {}
template <typename V = ValueType,
typename std::enable_if_t<std::is_void<V>::value, int> = 0>
constexpr Basic_Result(Result_OK) : stored(true) {}
template <typename V = ValueType,
typename std::enable_if_t<!std::is_same<V,ErrorType>::value, int> = 0,
typename std::enable_if_t<!std::is_void<V>::value, int> = 0>
constexpr Basic_Result(V value) : stored(std::forward<V>(value)) {}
constexpr Basic_Result(ErrorType error) : stored(std::forward<ErrorType>(error)) {}
template <typename T>
constexpr Basic_Result& operator=(T in_value)
{
if constexpr(std::is_convertible<T, ValueType>::value)
{
stored.clean();
new (&stored.value) ValueType(std::move(in_value));
stored.flags.set_value();
if constexpr(std::is_trivial<ValueType>::value) stored.flags.set_dirty();
}
else if constexpr(std::is_convertible<T, ErrorType>::value)
{
stored.clean();
new (&stored.error) ErrorType(std::move(in_value));
stored.flags.unset_value();
if constexpr(!std::is_trivial<ErrorType>::value) stored.flags.set_dirty();
}
else
static_assert(std::is_convertible<T, ValueType>::value || std::is_convertible<T, ErrorType>::value);
return *this;
}
constexpr operator bool() const { return stored.flags.has_value(); }
constexpr ValueType copy_value() const { if (*this) return stored.value; return ValueType(); }
constexpr ErrorType copy_error() const { if (*this) return stored.value; return ValueType(); }
// Stuff to get to the actual result value
template <typename V = ValueType, typename E = ErrorType,
typename std::enable_if_t<!std::is_void<V>::value, int> = 0,
typename std::enable_if_t<!std::is_void<E>::value, int> = 0>
ValueType catch_error(std::function<ValueType(E&)> handler)
{
if (*this) return unwrap();
return std::forward<ValueType>(handler(stored.error));
}
// Stuff to get to the actual result value
template <typename V = ValueType, typename E = ErrorType,
typename std::enable_if_t<std::is_void<V>::value, int> = 0,
typename std::enable_if_t<!std::is_void<E>::value, int> = 0>
void catch_error(std::function<void(ErrorType&)> handler)
{
if (*this) return;
return handler(stored.error);
}
template <typename V = ValueType,
typename std::enable_if_t<!std::is_void<V>::value, int> = 0>
ValueType unwrap()
{
if (*this) {
stored.flags.clear_flags();
return std::move(stored.value);
}
stored.clean();
std::abort();
}
template <typename V = ValueType, typename std::enable_if_t<!std::is_void<V>::value, int> = 0>
ValueType value_or(V rval)
{
if (*this) return unwrap();
else return std::move(rval);
}
};
template <typename ValueType, typename ErrorType, typename FlagType>
class Basic_Result<ValueType, ErrorType, FlagType, true>
{
private:
Result_Storage<ValueType, ErrorType, FlagType, true> stored;
public:
constexpr Basic_Result() {}
constexpr Basic_Result(ValueType& value) : stored(value) {}
constexpr Basic_Result(ErrorType error) : stored(std::forward<ErrorType>(error)) {}
template <typename T>
constexpr Basic_Result& operator=(T&& in_value)
{
if constexpr(std::is_convertible<T, ValueType>::value)
{
stored.clean();
stored.flags.set_value();
}
else if constexpr(std::is_convertible<T, ErrorType>::value)
{
stored.clean();
new (&stored.error) ErrorType(std::move(in_value));
stored.flags.unset_value();
if constexpr(!std::is_trivial<ErrorType>::value) stored.flags.set_dirty();
}
else
static_assert(std::is_convertible<T, ValueType>::value || std::is_convertible<T, ErrorType>::value);
return *this;
}
constexpr operator bool() const { return stored.flags.has_value(); }
// Stuff to get to the actual result value
template <typename V = ValueType, typename E = ErrorType,
typename std::enable_if_t<!std::is_void<E>::value, int> = 0>
ValueType& catch_error(std::function<ValueType(E&)> handler)
{
if (*this) return unwrap();
return std::forward<ValueType>(handler(stored.error));
}
ValueType& unwrap()
{
if (*this) {
stored.flags.clear_flags();
return *stored.value;
}
stored.clean();
std::abort();
}
ValueType& value_or(ValueType& rval)
{
if (*this) return unwrap();
else return rval;
}
};
// Provide very simple default error
struct Error
{
const std::string message;
Error() : message("") {}
Error(const char* e) : message(e) {}
const char* msg() { return message.c_str(); }
};
template <typename ValueType, bool IsReference = std::is_reference<ValueType>::value>
using Result = mush::Basic_Result<ValueType, Error, uint8_t, IsReference>;
using Flags = uint16_t;
}
/*
Discarded snippets
From class Basic_Result
// Copy from other result
constexpr Basic_Result(const Basic_Result& x)
{
stored.flags = x.stored.flags;
if (*this) stored.value = x.value;
else stored.error = x.error;
}
// Move from other result
constexpr Basic_Result(Basic_Result&&) = default;
constexpr Basic_Result& operator=(Basic_Result&&) = default;
*/
#endif
/*
Copyright (c) 2017 Jari Ronkainen
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose, including
commercial applications, and to alter it and redistribute it freely, subject to
the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim
that you wrote the original software. If you use this software in a product,
an acknowledgment in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/