Print Result(E) if not absorbing trace exceptions

This commit is contained in:
Jeremy 2024-03-31 14:15:19 -05:00
parent 4db73b1856
commit 8007413ff6
No known key found for this signature in database
GPG Key ID: 19AA8270105E8EB4

View File

@ -2,6 +2,7 @@
#define UTILS_HPP #define UTILS_HPP
#include <algorithm> #include <algorithm>
#include <atomic>
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
@ -325,11 +326,10 @@ namespace detail {
} }
}; };
// TODO: Better dump error extern std::atomic_bool absorb_trace_exceptions;
// TODO: Explicit constructors for value, then add Ok()/Error() helpers
template<typename T, typename E, typename std::enable_if<!std::is_same<T, E>::value, int>::type = 0> template<typename T, typename E, typename std::enable_if<!std::is_same<T, E>::value, int>::type = 0>
class Result { class Result {
// Not using a union because I don't want to have to deal with that
union { union {
T value_; T value_;
E error_; E error_;
@ -338,7 +338,11 @@ namespace detail {
member active; member active;
public: public:
Result(T value) : value_(std::move(value)), active(member::value) {} Result(T value) : value_(std::move(value)), active(member::value) {}
Result(E error) : error_(std::move(error)), active(member::error) {} Result(E error) : error_(std::move(error)), active(member::error) {
if(!absorb_trace_exceptions.load()) {
std::fprintf(stderr, "%s\n", unwrap_error().what());
}
}
Result(Result&& other) : active(other.active) { Result(Result&& other) : active(other.active) {
if(other.active == member::value) { if(other.active == member::value) {
new (&value_) T(std::move(other.value_)); new (&value_) T(std::move(other.value_));