Debian Codegraph @

src/utils/result.hpp

raw
1 #ifndef RESULT_HPP
2 #define RESULT_HPP
3
4 #include <new>
5 #include <type_traits>
6 #include <utility>
7
8 #include "utils/common.hpp"
9 #include "utils/error.hpp"
10 #include "utils/optional.hpp"
11 #include "options.hpp"
12 #include "logging.hpp"
13
14 CPPTRACE_BEGIN_NAMESPACE
15 namespace detail {
16 template<typename T, typename E, typename std::enable_if<!std::is_same<T, E>::value, int>::type = 0>
17 class Result {
18 using value_type = well_behaved<T>;
19 union {
20 value_type value_;
21 E error_;
22 };
23 enum class member { value, error };
24 member active;
25 void destroy() {
26 if(active == member::value) {
27 value_.~value_type();
28 } else {
29 error_.~E();
30 }
31 }
32 public:
33 Result(value_type&& value) : value_(std::move(value)), active(member::value) {}
34 Result(E&& error) : error_(std::move(error)), active(member::error) {
35 log::debug("Error result constructed: {}", unwrap_error().what());
36 }
37 Result(const value_type& value) : value_(value_type(value)), active(member::value) {}
38 Result(const E& error) : error_(E(error)), active(member::error) {
39 log::debug("Error result constructed: {}", unwrap_error().what());
40 }
41 template<
42 typename U = T,
43 typename std::enable_if<
44 !std::is_same<typename std::decay<U>::type, Result<T, E>>::value &&
45 std::is_constructible<value_type, U>::value &&
46 !std::is_constructible<E, U>::value,
47 int
48 >::type = 0
49 >
50 Result(U&& u) : Result(value_type(std::forward<U>(u))) {}
51 Result(Result&& other) : active(other.active) {
52 if(other.active == member::value) {
53 new (&value_) value_type(std::move(other.value_));
54 } else {
55 new (&error_) E(std::move(other.error_));
56 }
57 }
58 ~Result() {
59 destroy();
60 }
61 Result& operator=(const Result& other) {
62 if (this != &other) {
63 destroy();
64 if(other.active == member::value) {
65 new (&value_) value_type(other.value_);
66 } else {
67 new (&error_) E(other.error_);
68 }
69 active = other.active;
70 }
71 return *this;
72 }
73 Result& operator=(Result&& other)
74 noexcept(
75 std::is_nothrow_move_constructible<value_type>::value && std::is_nothrow_move_constructible<E>::value
76 )
77 {
78 if (this != &other) {
79 destroy();
80 if(other.active == member::value) {
81 new (&value_) value_type(std::move(other.value_));
82 } else {
83 new (&error_) E(std::move(other.error_));
84 }
85 active = other.active;
86 }
87 return *this;
88 }
89
90 bool has_value() const {
91 return active == member::value;
92 }
93
94 bool is_error() const {
95 return active == member::error;
96 }
97
98 explicit operator bool() const {
99 return has_value();
100 }
101
102 NODISCARD optional<T> value() const & {
103 return has_value() ? optional<T>(value_) : nullopt;
104 }
105
106 NODISCARD optional<E> error() const & {
107 return is_error() ? optional<E>(error_) : nullopt;
108 }
109
110 NODISCARD optional<T> value() && {
111 return has_value() ? optional<T>(std::move(value_)) : nullopt;
112 }
113
114 NODISCARD optional<E> error() && {
115 return is_error() ? optional<E>(std::move(error_)) : nullopt;
116 }
117
118 NODISCARD T& unwrap_value() & {
119 ASSERT(has_value(), "Result does not contain a value");
120 return value_;
121 }
122
123 NODISCARD const T& unwrap_value() const & {
124 ASSERT(has_value(), "Result does not contain a value");
125 return value_;
126 }
127
128 NODISCARD T unwrap_value() && {
129 ASSERT(has_value(), "Result does not contain a value");
130 return std::move(value_);
131 }
132
133 NODISCARD E& unwrap_error() & {
134 ASSERT(is_error(), "Result does not contain an error");
135 return error_;
136 }
137
138 NODISCARD const E& unwrap_error() const & {
139 ASSERT(is_error(), "Result does not contain an error");
140 return error_;
141 }
142
143 NODISCARD E unwrap_error() && {
144 ASSERT(is_error(), "Result does not contain an error");
145 return std::move(error_);
146 }
147
148 template<typename U>
149 NODISCARD T value_or(U&& default_value) const & {
150 return has_value() ? static_cast<T>(value_) : static_cast<T>(std::forward<U>(default_value));
151 }
152
153 template<typename U>
154 NODISCARD T value_or(U&& default_value) && {
155 return has_value() ? static_cast<T>(std::move(value_)) : static_cast<T>(std::forward<U>(default_value));
156 }
157
158 template<typename F>
159 NODISCARD auto transform(F&& f) & -> Result<decltype(f(std::declval<T&>())), E> {
160 if(has_value()) {
161 return f(unwrap_value());
162 } else {
163 return unwrap_error();
164 }
165 }
166
167 template<typename F>
168 NODISCARD auto transform(F&& f) && -> Result<decltype(f(std::declval<T&&>())), E> {
169 if(has_value()) {
170 return f(std::move(unwrap_value()));
171 } else {
172 return unwrap_error();
173 }
174 }
175
176 void drop_error() const {
177 if(is_error()) {
178 log::error(unwrap_error().what());
179 }
180 }
181 };
182 }
183 CPPTRACE_END_NAMESPACE
184
185 #endif