Add a variant of check() that accepts an error message

This commit is contained in:
apio 2022-12-04 12:19:17 +01:00
parent a0c4bbe6f3
commit bb92480aa3
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 12 additions and 10 deletions

View File

@ -9,3 +9,5 @@ extern _noreturn bool __check_failed(const char* file, const char* line, const c
#endif #endif
#define check(expr) (expr) || __check_failed(__FILE__, STRINGIZE_VALUE_OF(__LINE__), __PRETTY_FUNCTION__, #expr) #define check(expr) (expr) || __check_failed(__FILE__, STRINGIZE_VALUE_OF(__LINE__), __PRETTY_FUNCTION__, #expr)
#define expect(expr, message) \
(expr) || __check_failed(__FILE__, STRINGIZE_VALUE_OF(__LINE__), __PRETTY_FUNCTION__, message)

View File

@ -83,25 +83,25 @@ template <typename T> class Result
int error() int error()
{ {
check(has_error()); expect(has_error(), "Result::error() called on a Result that holds a value");
return m_error; return m_error;
} }
Error release_error() Error release_error()
{ {
check(has_error()); expect(has_error(), "Result::release_error() called on a Result that holds a value");
return {m_error}; return {m_error};
} }
const char* error_string() const char* error_string()
{ {
check(has_error()); expect(has_error(), "Result::error_string() called on a Result that holds a value");
return ::error_string(m_error); return ::error_string(m_error);
} }
T value() T value()
{ {
check(has_value()); expect(has_value(), "Result::value() called on a Result that holds an error");
return m_storage.fetch_reference(); return m_storage.fetch_reference();
} }
@ -120,7 +120,7 @@ template <typename T> class Result
T release_value() T release_value()
{ {
check(has_value()); expect(has_value(), "Result::release_value() called on a Result that holds an error");
T item = m_storage.fetch_reference(); T item = m_storage.fetch_reference();
m_has_value = false; m_has_value = false;
m_storage.destroy(); m_storage.destroy();
@ -221,31 +221,31 @@ template <> class Result<void>
int error() int error()
{ {
check(has_error()); expect(has_error(), "Result::error() called on a Result that holds a value");
return m_error; return m_error;
} }
Error release_error() Error release_error()
{ {
check(has_error()); expect(has_error(), "Result::release_error() called on a Result that holds a value");
return {m_error}; return {m_error};
} }
const char* error_string() const char* error_string()
{ {
check(has_error()); expect(has_error(), "Result::error_string() called on a Result that holds a value");
return ::error_string(m_error); return ::error_string(m_error);
} }
void value() void value()
{ {
check(has_value()); expect(has_value(), "Result::value() called on a Result that holds an error");
return; return;
} }
void release_value() void release_value()
{ {
check(has_value()); expect(has_value(), "Result::release_value() called on a Result that holds an error");
return; return;
} }