Result: Make some member functions const

This commit is contained in:
apio 2022-12-17 15:15:00 +01:00
parent 90bd4a83c0
commit 34c738116c
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -40,52 +40,52 @@ template <typename T> class Result
{ {
} }
bool has_error() bool has_error() const
{ {
return !m_has_value; return !m_has_value;
} }
bool has_value() bool has_value() const
{ {
return m_has_value; return m_has_value;
} }
int error() int error() const
{ {
expect(has_error(), "Result::error() called on a Result that holds a value"); 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() const
{ {
expect(has_error(), "Result::release_error() called on a Result that holds a value"); 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() const
{ {
expect(has_error(), "Result::error_string() called on a Result that holds a value"); 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() const
{ {
expect(has_value(), "Result::value() called on a Result that holds an error"); expect(has_value(), "Result::value() called on a Result that holds an error");
return m_value.value(); return m_value.value();
} }
T expect_value(const char* reason) T expect_value(const char* reason) const
{ {
expect(has_value(), reason); expect(has_value(), reason);
return m_value.value(); return m_value.value();
} }
T value_or(const T& other) T value_or(const T& other) const
{ {
return m_value.value_or(other); return m_value.value_or(other);
} }
bool try_set_value(T& ref) bool try_set_value(T& ref) const
{ {
return m_value.try_set_value(ref); return m_value.try_set_value(ref);
} }
@ -127,53 +127,53 @@ template <> class Result<void>
{ {
} }
bool has_error() bool has_error() const
{ {
return m_has_error; return m_has_error;
} }
bool has_value() bool has_value() const
{ {
return !m_has_error; return !m_has_error;
} }
int error() int error() const
{ {
expect(has_error(), "Result::error() called on a Result that holds a value"); 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() const
{ {
expect(has_error(), "Result::release_error() called on a Result that holds a value"); 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() const
{ {
expect(has_error(), "Result::error_string() called on a Result that holds a value"); 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() const
{ {
expect(has_value(), "Result::value() called on a Result that holds an error"); expect(has_value(), "Result::value() called on a Result that holds an error");
return; return;
} }
void expect_value(const char* reason) void expect_value(const char* reason) const
{ {
expect(has_value(), reason); expect(has_value(), reason);
return; return;
} }
void release_value() void release_value() const
{ {
expect(has_value(), "Result::release_value() called on a Result that holds an error"); expect(has_value(), "Result::release_value() called on a Result that holds an error");
return; return;
} }
void expect_release_value(const char* reason) void expect_release_value(const char* reason) const
{ {
expect(has_value(), reason); expect(has_value(), reason);
return; return;