RPNX::DataStructures
Header-only C++ data structures and supporting utilities.
Loading...
Searching...
No Matches
result.hpp
1// Copyright (c) 2026 Ryan P. Nicholl <rnicholl@protonmail.com>
2// Copyright 2024 Ryan P. Nicholl, rnicholl@protonmail.com
3
4#ifndef RPNXDATASTRUCTURES_RESULT_HPP
5#define RPNXDATASTRUCTURES_RESULT_HPP
6
7#include <exception>
8#include <optional>
9#include <cassert>
10#include <stdexcept>
11#include <utility>
12
13namespace rpnx
14{
19 template < typename T >
20 class result
21 {
22 std::optional< T > t;
23 std::exception_ptr er;
24
25 public:
30 result(T t) : t(std::move(t))
31 {
32 }
33
38 {
39 }
40
45 result(std::exception_ptr er) : er(er)
46 {
47 }
48
53 void test() const
54 {
55 if (er)
56 {
57 std::rethrow_exception(er);
58 }
59 }
60
67 T & get() &
68 {
69 if (er)
70 {
71 std::rethrow_exception(er);
72 }
73
74 assert(t.has_value());
75
76 return t.value();
77 }
78
80 T const & get() const &
81 {
82 if (er)
83 {
84 std::rethrow_exception(er);
85 }
86
87 assert(t.has_value());
88
89 return t.value();
90 }
91
98 T && get() &&
99 {
100 if (er)
101 {
102 std::rethrow_exception(er);
103 }
104
105 assert(t.has_value());
106
107 return std::move(t.value());
108 }
109
111 T const && get() const &&
112 {
113 if (er)
114 {
115 std::rethrow_exception(er);
116 }
117
118 assert(t.has_value());
119
120 return std::move(t.value());
121 }
122
124 T & value() &
125 {
126 return get();
127 }
128
130 T const & value() const &
131 {
132 return get();
133 }
134
136 T && value() &&
137 {
138 return std::move(*this).get();
139 }
140
142 T const && value() const &&
143 {
144 return std::move(*this).get();
145 }
146
151 void set_value(T t)
152 {
153 this->er = nullptr;
154 this->t = std::move(t);
155 }
156
161 void set_error(std::exception_ptr er)
162 {
163 this->t.reset();
164 this->er = er;
165 }
166
168 void set_exception(std::exception_ptr er)
169 {
170 set_error(er);
171 }
172
177 bool has_result() const
178 {
179 return t.has_value() || er != nullptr;
180 }
181
186 bool has_value() const
187 {
188 return t.has_value();
189 }
190
195 bool has_error() const
196 {
197 return er != nullptr;
198 }
199
201 bool has_exception() const
202 {
203 return has_error();
204 }
205
210 std::exception_ptr get_error() const
211 {
212 return er;
213 }
214
219 inline operator bool() const
220 {
221 return has_result();
222 }
223 };
224
228 template <>
229 class result< void >
230 {
231 bool t = false;
232 std::exception_ptr er;
233
234 public:
239 {
240 }
241
246 result(std::exception_ptr er) : er(er)
247 {
248 }
249
254 void test() const
255 {
256 if (er)
257 {
258 std::rethrow_exception(er);
259 }
260 }
261
267 void get() const
268 {
269 if (er)
270 std::rethrow_exception(er);
271 if (!t)
272 throw std::logic_error("No value");
273 return;
274 }
275
277 void value() const
278 {
279 get();
280 }
281
286 {
287 this->er = nullptr;
288 this->t = true;
289 }
290
295 void set_error(std::exception_ptr er)
296 {
297 this->t = false;
298 this->er = er;
299 }
300
302 void set_exception(std::exception_ptr er)
303 {
304 set_error(er);
305 }
306
311 bool has_value() const
312 {
313 return t;
314 }
315
320 bool has_error() const
321 {
322 return er != nullptr;
323 }
324
326 bool has_exception() const
327 {
328 return has_error();
329 }
330
335 std::exception_ptr get_error() const
336 {
337 return er;
338 }
339
344 inline bool has_result() const
345 {
346 return has_value() || has_error();
347 }
348
353 inline operator bool() const
354 {
355 return has_result();
356 }
357 };
358} // namespace rpnx
359
360#endif // RPNXDATASTRUCTURES_RESULT_HPP
result()
Constructs an empty result object.
Definition result.hpp:238
bool has_value() const
Checks if the result has a value (void).
Definition result.hpp:311
void set_error(std::exception_ptr er)
Sets the exception of the result.
Definition result.hpp:295
void test() const
Tests if the result contains an exception and rethrows it if present.
Definition result.hpp:254
std::exception_ptr get_error() const
Gets the stored exception.
Definition result.hpp:335
result(std::exception_ptr er)
Constructs a result object with an exception.
Definition result.hpp:246
bool has_error() const
Checks if the result has an exception.
Definition result.hpp:320
void set_value()
Sets the value of the result (void).
Definition result.hpp:285
bool has_exception() const
Alias for has_error().
Definition result.hpp:326
void set_exception(std::exception_ptr er)
Alias for set_error().
Definition result.hpp:302
bool has_result() const
Checks if the result has a value (void) or an exception.
Definition result.hpp:344
void value() const
Alias for get().
Definition result.hpp:277
void get() const
Gets the stored value (void).
Definition result.hpp:267
T const & value() const &
Alias for get().
Definition result.hpp:130
T & get() &
Returns the stored value from an lvalue result.
Definition result.hpp:67
bool has_exception() const
Alias for has_error().
Definition result.hpp:201
bool has_error() const
Checks if the result has an exception.
Definition result.hpp:195
T const & get() const &
Returns the stored value from an lvalue result.
Definition result.hpp:80
T && value() &&
Alias for rvalue-qualified get().
Definition result.hpp:136
void set_value(T t)
Sets the value of the result.
Definition result.hpp:151
void set_error(std::exception_ptr er)
Sets the exception of the result.
Definition result.hpp:161
result(std::exception_ptr er)
Constructs a result object with an exception.
Definition result.hpp:45
void set_exception(std::exception_ptr er)
Alias for set_error().
Definition result.hpp:168
T && get() &&
Returns the stored value from an rvalue result.
Definition result.hpp:98
T & value() &
Alias for get().
Definition result.hpp:124
result(T t)
Constructs a result object with a value.
Definition result.hpp:30
bool has_value() const
Checks if the result has a value.
Definition result.hpp:186
T const && get() const &&
Returns the stored value from an rvalue result.
Definition result.hpp:111
T const && value() const &&
Alias for const rvalue-qualified get().
Definition result.hpp:142
void test() const
Tests if the result contains an exception and rethrows it if present.
Definition result.hpp:53
bool has_result() const
Checks if the result has a value or an exception.
Definition result.hpp:177
std::exception_ptr get_error() const
Gets the stored exception.
Definition result.hpp:210
result()
Constructs an empty result object.
Definition result.hpp:37
Containers, iterator adapters, callable wrappers, and value utilities.
Definition annex.hpp:14