-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresult.hpp
350 lines (268 loc) · 7.26 KB
/
result.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
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
/*!
\file result.hpp
\date 03.10.2020
\author Ildar Kasimov
This file is a single-header library which was written in C++14 standard.
The library is an implementation of a type to provide simple yet explicit
error handling like that's implemented in Rust programming language.
The usage of the library is pretty simple, just copy this file into your enviornment and
predefine RESULT_IMPLEMENTATION macro before its inclusion like the following
\code
#define RESULT_IMPLEMENTATION
#include "result.hpp"
\endcode
The library has almost zero dependencies except a few standard library's containers.
*/
#pragma once
#include <type_traits>
#include <iostream>
#include <string>
///< Library's configs
#define RESULT_DISABLE_EXCEPTIONS 1
#define RESULT_ENABLE_EXPORT 1
#if RESULT_DISABLE_EXCEPTIONS
#define RESULT_NOEXCEPT noexcept
#else
#define RESULT_NOEXCEPT
#endif
namespace Wrench
{
/*!
\brief The function stops execution of a program
The method is used to terminate a program when some error
occurs instead of trying to solve it
\param[in] message A message that will be printed as an error output
*/
inline void Panic(const std::string& message) RESULT_NOEXCEPT
{
std::cerr << message << std::endl;
std::terminate();
}
template <typename T>
struct TOkValue
{
TOkValue() RESULT_NOEXCEPT = delete;
TOkValue(const T& value) RESULT_NOEXCEPT : mValue(value) { }
TOkValue(T&& value) RESULT_NOEXCEPT : mValue(std::move(value)) { }
T mValue;
};
template <> struct TOkValue<void> {};
template <typename E>
struct TErrValue
{
TErrValue() RESULT_NOEXCEPT = delete;
TErrValue(const E& value) RESULT_NOEXCEPT : mError(value) { }
TErrValue(E&& value) RESULT_NOEXCEPT : mError(std::move(value)) { }
E mError;
};
template <typename T, typename E>
class Storage final
{
static_assert(!std::is_same<E, void>::value, "[Storage<T, E>] \"void\" is not allowed as an error type E");
public:
static constexpr size_t mSize = (sizeof(T) > sizeof(E)) ? sizeof(T) : sizeof(E);
static constexpr size_t mAlignment = (alignof(T) > alignof(E)) ? alignof(E) : alignof(E);
using StorageImpl = typename std::aligned_storage<mSize, mAlignment>::type;
public:
Storage() RESULT_NOEXCEPT : mIsInitialized(false), mIsValid(false) { }
Storage(const TOkValue<T>& value) RESULT_NOEXCEPT :
mIsInitialized(true), mIsValid(true)
{
new (&mData) T(value.mValue);
}
Storage(const TErrValue<E>& value) RESULT_NOEXCEPT :
mIsInitialized(true), mIsValid(false)
{
new (&mData) E(value.mError);
}
~Storage() RESULT_NOEXCEPT
{
_release();
}
void Reset(const TOkValue<T>& value) RESULT_NOEXCEPT
{
if (mIsInitialized)
{
_release();
}
new (&mData) T(value.mValue);
mIsInitialized = true;
mIsValid = true;
}
void Reset(const TErrValue<E>& value) RESULT_NOEXCEPT
{
if (mIsInitialized)
{
_release();
}
new (&mData) E(value.mError);
mIsInitialized = true;
}
template <typename U>
const U& GetAs() const RESULT_NOEXCEPT
{
return *reinterpret_cast<const U*>(&mData);
}
template <typename U>
U& GetAs() RESULT_NOEXCEPT
{
return *reinterpret_cast<U*>(&mData);
}
bool IsValid() const RESULT_NOEXCEPT { return mIsValid; }
private:
void _release() RESULT_NOEXCEPT
{
if (mIsInitialized)
{
if (mIsValid)
{
GetAs<T>().~T();
}
else
{
GetAs<E>().~E();
}
}
mIsInitialized = false;
mIsValid = false;
}
private:
bool mIsInitialized : 2;
bool mIsValid : 2;
StorageImpl mData;
};
template <typename E>
class Storage<void, E> final
{
static_assert(!std::is_same<E, void>::value, "[Storage<T, E>] \"void\" is not allowed as an error type E");
public:
using StorageImpl = typename std::aligned_storage<sizeof(E), alignof(E)>::type;
public:
Storage() RESULT_NOEXCEPT : mIsInitialized(false), mIsValid(false) { }
Storage(const TOkValue<void>& value) RESULT_NOEXCEPT :
mIsInitialized(true), mIsValid(true)
{
}
Storage(const TErrValue<E>& value) RESULT_NOEXCEPT :
mIsInitialized(true), mIsValid(false)
{
new (&mData) E(value.mError);
}
~Storage() RESULT_NOEXCEPT
{
_release();
}
void Reset(const TOkValue<void>& value) RESULT_NOEXCEPT
{
if (mIsInitialized)
{
_release();
}
mIsInitialized = true;
mIsValid = true;
}
void Reset(const TErrValue<E>& value) RESULT_NOEXCEPT
{
if (mIsInitialized)
{
_release();
}
new (&mData) E(value.mError);
mIsInitialized = true;
}
template <typename U>
const U& GetAs() const RESULT_NOEXCEPT
{
return *reinterpret_cast<U*>(&mData);
}
bool IsValid() const RESULT_NOEXCEPT { return mIsValid; }
private:
void _release() RESULT_NOEXCEPT
{
if (mIsInitialized)
{
if (!mIsValid)
{
GetAs<E>().~E();
}
}
mIsInitialized = false;
mIsValid = false;
}
private:
bool mIsInitialized : 2;
bool mIsValid : 2;
StorageImpl mData;
};
template <typename T, typename E>
class Result final
{
static_assert(!std::is_same<E, void>::value, "[Result<T, E>] \"void\" is not allowed as an error type E");
public:
using ResultStorage = Storage<T, E>;
public:
Result() RESULT_NOEXCEPT = delete;
Result(const TOkValue<T>& value) RESULT_NOEXCEPT : mData(value) { }
Result(const TErrValue<E>& error) RESULT_NOEXCEPT : mData(error) { }
Result(const Result& result) RESULT_NOEXCEPT : mData(result.mData) { }
Result(Result&& result) RESULT_NOEXCEPT : mData(std::move(result.mData)) { }
~Result() RESULT_NOEXCEPT = default;
/// Methods
template <typename U = T>
typename std::enable_if<!std::is_same<U, void>::value, U>::type
Get() const RESULT_NOEXCEPT
{
if (!mData.IsValid())
{
Panic("[Result<T, E>] Get() was invoked for an invalid Result<T, E> object");
}
return mData.template GetAs<U>();
}
template <typename U = T>
typename std::enable_if<!std::is_same<U, void>::value, U>::type
GetOrDefault(U&& altValue) RESULT_NOEXCEPT
{
if (!mData.IsValid())
{
return std::forward<U>(altValue);
}
return mData.template GetAs<U>();
}
template <typename U = E>
typename std::enable_if<!std::is_same<U, void>::value, U>::type
GetError() const RESULT_NOEXCEPT
{
if (mData.IsValid())
{
Panic("[Result<T, E>] GetError() was invoked for a valid Result<T, E> object");
}
return mData.template GetAs<U>();
}
bool IsOk() const RESULT_NOEXCEPT { return mData.IsValid(); }
bool HasError() const RESULT_NOEXCEPT { return !mData.IsValid(); }
/// Operators section
Result<T, E>& operator= (const TOkValue<T>& value) RESULT_NOEXCEPT
{
mData.Reset(value);
return *this;
}
Result<T, E>& operator= (const TErrValue<E>& error) RESULT_NOEXCEPT
{
mData.Reset(error);
return *this;
}
Result<T, E>& operator= (const Result<T, E>& result) RESULT_NOEXCEPT
{
mData.Reset(result.mData);
return *this;
}
Result<T, E>& operator= (Result<T, E>&& result) RESULT_NOEXCEPT
{
std::swap(mData, result.mData);
return *this;
}
operator bool() const RESULT_NOEXCEPT { return mData.IsValid(); }
private:
ResultStorage mData;
};
}