Compare commits

..

2 Commits

Author SHA1 Message Date
6cf042e65e
Result: Remove m_has_value, rely on Option::has_value()
All checks were successful
continuous-integration/drone/push Build is passing
2023-01-11 17:09:32 +01:00
67ebb00bd3
Option, Result: Introduce try_move_value(), which is the release_value() equivalent of try_set_value() 2023-01-11 17:06:17 +01:00
2 changed files with 21 additions and 12 deletions

View File

@ -101,6 +101,14 @@ template <typename T> class Option
return true; return true;
} }
bool try_move_value(T& ref) const
{
if (!has_value()) return false;
m_has_value = false;
ref = move(m_storage.fetch_reference());
return true;
}
~Option() ~Option()
{ {
if (has_value()) m_storage.destroy(); if (has_value()) m_storage.destroy();

View File

@ -20,24 +20,23 @@ struct Error
template <typename T> class Result template <typename T> class Result
{ {
public: public:
Result(const T& value) : m_value(value), m_has_value(true) Result(const T& value) : m_value(value)
{ {
} }
Result(T&& value) : m_value(move(value)), m_has_value(true) Result(T&& value) : m_value(move(value))
{ {
} }
Result(const Result<T>& other) : m_value(other.m_value), m_has_value(other.m_has_value), m_error(other.m_error) Result(const Result<T>& other) : m_value(other.m_value), m_error(other.m_error)
{ {
} }
Result(Result<T>&& other) : m_value(move(other.m_value)), m_has_value(other.m_has_value), m_error(other.m_error) Result(Result<T>&& other) : m_value(move(other.m_value)), m_error(other.m_error)
{ {
other.m_has_value = false;
} }
Result(const Error& err) : m_value(), m_has_value(false), m_error(err.error) Result(const Error& err) : m_value(), m_error(err.error)
{ {
} }
@ -45,7 +44,6 @@ template <typename T> class Result
{ {
if (this == &other) return *this; if (this == &other) return *this;
m_has_value = other.m_has_value;
m_error = other.m_error; m_error = other.m_error;
m_value = other.m_value; m_value = other.m_value;
@ -56,8 +54,6 @@ template <typename T> class Result
{ {
if (this == &other) return *this; if (this == &other) return *this;
m_has_value = other.m_has_value;
other.m_has_value = false;
m_error = other.m_error; m_error = other.m_error;
m_value = move(other.m_value); m_value = move(other.m_value);
@ -66,12 +62,12 @@ template <typename T> class Result
bool has_error() const bool has_error() const
{ {
return !m_has_value; return !m_value.has_value();
} }
bool has_value() const bool has_value() const
{ {
return m_has_value; return m_value.has_value();
} }
int error() const int error() const
@ -114,6 +110,12 @@ template <typename T> class Result
return m_value.try_set_value(ref); return m_value.try_set_value(ref);
} }
bool try_move_value(T& ref) const
{
return m_value.try_move_value(ref);
}
// FIXME: Please remove this.
Result<bool> try_set_value_with_specific_error(T& ref, int error) Result<bool> try_set_value_with_specific_error(T& ref, int error)
{ {
if (has_error() && m_error != error) return release_error(); if (has_error() && m_error != error) return release_error();
@ -135,7 +137,6 @@ template <typename T> class Result
private: private:
Option<T> m_value; Option<T> m_value;
bool m_has_value;
int m_error; int m_error;
}; };