-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult.hpp
More file actions
174 lines (148 loc) · 4.18 KB
/
result.hpp
File metadata and controls
174 lines (148 loc) · 4.18 KB
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
#ifndef result_hpp
#define result_hpp
#include <stddef.h>
#include <stdint.h>
#include "macros.hpp"
#include <utility>
#include <new>
#include <type_traits>
#define PANIC(msg)
namespace hls
{
using namespace std;
enum class Error : uint64_t
{
UNDEFINED_ERROR,
NOT_FOUND,
INVALID_ARGUMENT,
INVALID_INDEX,
INVALID_PAGE_TABLE,
INVALID_PAGE_ENTRY,
INVALID_VIRTUAL_ADDRESS,
MISALIGNED_MEMORY_ADDRESS,
ADDRESS_ALREADY_MAPPED,
NOT_ENOUGH_CONTIGUOUS_MEMORY,
OUT_OF_MEMORY,
OUT_OF_BOUNDS,
VALUE_LIMIT_REACHED,
READ_NOT_ALLOWED,
WRITE_NOT_ALLOWED,
EXECUTE_NOT_ALLOWED,
OPERATION_NOT_ALLOWED,
_OVERFLOW,
CORRUPTED_DATA_STRUCTURE
};
template <typename T>
requires(!std::is_reference_v<T>)
class Result
{
SET_USING_CLASS(T, value_type);
SET_USING_CLASS(Error, error_type);
union {
Error e;
alignas(T) char value[sizeof(T)];
} m_stored;
bool m_is_error = false;
Result(value_type_const_reference v)
{
new (reinterpret_cast<value_type *>(m_stored.value)) T(v);
}
Result(value_type_rvalue_reference v)
{
using type = std::remove_cvref_t<T>;
new (reinterpret_cast<value_type *>(m_stored.value)) type(std::move(v));
}
Result(error_type e)
{
m_stored.e = e;
m_is_error = true;
}
public:
Result(Result &&other)
{
if (other.is_error())
{
m_stored.e = other.m_stored.e;
}
else
{
auto *p = reinterpret_cast<value_type *>(m_stored.value);
new (p) T(std::move(reinterpret_cast<value_type &>(other.m_stored.value)));
}
}
Result(const Result &other)
{
if (other.is_error())
{
m_stored.e = other.m_stored.e;
}
else
{
auto *p = reinterpret_cast<value_type_ptr>(m_stored.value);
new (p) T(reinterpret_cast<value_type_const_reference>(other.m_stored.value));
}
}
~Result()
{
if (is_value())
{
reinterpret_cast<value_type_ptr>(m_stored.value)->~T();
}
}
bool is_error() const
{
return m_is_error;
}
bool is_value() const
{
return !is_error();
}
value_type_reference get_value()
{
const auto &a = *this;
return const_cast<value_type_reference>(a.get_value());
}
// Given that a value is expected, in Kernel code if it doesn't hold, we
// panic, after all, the system might end in a unusable state if we consume
// the value as if it was proper.
value_type_const_reference get_value() const
{
if (is_error())
{
PANIC("Result contains error instead of expected value.");
}
return *reinterpret_cast<const value_type *>(m_stored.value);
}
error_type get_error() const
{
if (!is_error())
{
PANIC("Result contains value instead of error.");
}
return m_stored.e;
}
static auto error(error_type e)
{
using type = std::remove_cvref_t<value_type>;
return Result<type>(e);
}
static auto value(value_type_rvalue_reference v)
{
using type = std::remove_cvref_t<value_type>;
return Result<type>(std::move(v));
}
static Result value(value_type_const_reference v)
{
using type = std::remove_cvref_t<value_type>;
return Result<type>(v);
}
};
template <typename T>
auto error = &Result<T>::error;
auto value(auto v)
{
using type = std::remove_cvref_t<decltype(v)>;
return Result<type>::value(hls::forward<decltype(v)>(v));
}
} // namespace hls
#endif