-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.hxx
71 lines (59 loc) · 1.52 KB
/
index.hxx
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
#include <variant>
#include <string>
#include <iostream>
namespace Hyper {
namespace Util {
class Error {
public:
std::string message;
size_t code;
Error (const std::string& s) : message(s) {};
};
template<typename T, class E = Error>
class Result {
bool hasError;
bool initialized = false;
bool checked = false;
T value;
public:
std::string type;
E error;
operator bool() {
this->checked = true;
return !this->hasError;
}
Result& operator= (const T& v) {
this->initialized = true;
this->value = v;
this->hasError = false;
return *this;
}
Result& operator= (const E& e) {
this->initialized = true;
this->error = e;
this->hasError = true;
return *this;
}
bool empty() {
return this->initialized;
}
T unwrap() {
if (!this->checked) {
throw std::runtime_error(".unwrap() called before check");
}
return this->value;
}
Result () : error("") {};
Result (const E& e) : error(e) {
this->initialized = true;
this->hasError = true;
this->type = typeid(E).name();
};
Result (const T& value) : error("") {
this->initialized = true;
this->hasError = false;
this->value = value;
};
};
} // namespace Util
} // namespace Hyper