forked from jeremy-rifkin/cpptrace
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresult.hpp
More file actions
185 lines (163 loc) · 5.71 KB
/
Copy pathresult.hpp
File metadata and controls
185 lines (163 loc) · 5.71 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
175
176
177
178
179
180
181
182
183
184
185
#ifndef RESULT_HPP
#define RESULT_HPP
#include <new>
#include <type_traits>
#include <utility>
#include "utils/common.hpp"
#include "utils/error.hpp"
#include "utils/optional.hpp"
#include "options.hpp"
#include "logging.hpp"
CPPTRACE_BEGIN_NAMESPACE
namespace detail {
template<typename T, typename E, typename std::enable_if<!std::is_same<T, E>::value, int>::type = 0>
class Result {
using value_type = well_behaved<T>;
union {
value_type value_;
E error_;
};
enum class member { value, error };
member active;
void destroy() {
if(active == member::value) {
value_.~value_type();
} else {
error_.~E();
}
}
public:
Result(value_type&& value) : value_(std::move(value)), active(member::value) {}
Result(E&& error) : error_(std::move(error)), active(member::error) {
log::debug("Error result constructed: {}", unwrap_error().what());
}
Result(const value_type& value) : value_(value_type(value)), active(member::value) {}
Result(const E& error) : error_(E(error)), active(member::error) {
log::debug("Error result constructed: {}", unwrap_error().what());
}
template<
typename U = T,
typename std::enable_if<
!std::is_same<typename std::decay<U>::type, Result<T, E>>::value &&
std::is_constructible<value_type, U>::value &&
!std::is_constructible<E, U>::value,
int
>::type = 0
>
Result(U&& u) : Result(value_type(std::forward<U>(u))) {}
Result(Result&& other) : active(other.active) {
if(other.active == member::value) {
new (&value_) value_type(std::move(other.value_));
} else {
new (&error_) E(std::move(other.error_));
}
}
~Result() {
destroy();
}
Result& operator=(const Result& other) {
if (this != &other) {
destroy();
if(other.active == member::value) {
new (&value_) value_type(other.value_);
} else {
new (&error_) E(other.error_);
}
active = other.active;
}
return *this;
}
Result& operator=(Result&& other)
noexcept(
std::is_nothrow_move_constructible<value_type>::value && std::is_nothrow_move_constructible<E>::value
)
{
if (this != &other) {
destroy();
if(other.active == member::value) {
new (&value_) value_type(std::move(other.value_));
} else {
new (&error_) E(std::move(other.error_));
}
active = other.active;
}
return *this;
}
bool has_value() const {
return active == member::value;
}
bool is_error() const {
return active == member::error;
}
explicit operator bool() const {
return has_value();
}
NODISCARD optional<T> value() const & {
return has_value() ? optional<T>(value_) : nullopt;
}
NODISCARD optional<E> error() const & {
return is_error() ? optional<E>(error_) : nullopt;
}
NODISCARD optional<T> value() && {
return has_value() ? optional<T>(std::move(value_)) : nullopt;
}
NODISCARD optional<E> error() && {
return is_error() ? optional<E>(std::move(error_)) : nullopt;
}
NODISCARD T& unwrap_value() & {
ASSERT(has_value(), "Result does not contain a value");
return value_;
}
NODISCARD const T& unwrap_value() const & {
ASSERT(has_value(), "Result does not contain a value");
return value_;
}
NODISCARD T unwrap_value() && {
ASSERT(has_value(), "Result does not contain a value");
return std::move(value_);
}
NODISCARD E& unwrap_error() & {
ASSERT(is_error(), "Result does not contain an error");
return error_;
}
NODISCARD const E& unwrap_error() const & {
ASSERT(is_error(), "Result does not contain an error");
return error_;
}
NODISCARD E unwrap_error() && {
ASSERT(is_error(), "Result does not contain an error");
return std::move(error_);
}
template<typename U>
NODISCARD T value_or(U&& default_value) const & {
return has_value() ? static_cast<T>(value_) : static_cast<T>(std::forward<U>(default_value));
}
template<typename U>
NODISCARD T value_or(U&& default_value) && {
return has_value() ? static_cast<T>(std::move(value_)) : static_cast<T>(std::forward<U>(default_value));
}
template<typename F>
NODISCARD auto transform(F&& f) & -> Result<decltype(f(std::declval<T&>())), E> {
if(has_value()) {
return f(unwrap_value());
} else {
return unwrap_error();
}
}
template<typename F>
NODISCARD auto transform(F&& f) && -> Result<decltype(f(std::declval<T&&>())), E> {
if(has_value()) {
return f(std::move(unwrap_value()));
} else {
return unwrap_error();
}
}
void drop_error() const {
if(is_error()) {
log::error(unwrap_error().what());
}
}
};
}
CPPTRACE_END_NAMESPACE
#endif