Result, Option: Move member initialization to constructors

This commit is contained in:
apio 2022-12-17 15:14:27 +01:00
parent ee6387e7b5
commit 90bd4a83c0
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 20 additions and 62 deletions

View File

@ -6,34 +6,29 @@
template <typename T> class Option template <typename T> class Option
{ {
public: public:
Option(const T& value) Option(const T& value) : m_has_value(true)
{ {
m_storage.store_reference(value); m_storage.store_reference(value);
m_has_value = true;
} }
Option(T&& value) Option(T&& value) : m_has_value(true)
{ {
m_storage.store_moved_reference(move(value)); m_storage.store_moved_reference(move(value));
m_has_value = true;
} }
Option(const Option<T>& other) Option(const Option<T>& other) : m_has_value(other.m_has_value)
{ {
m_has_value = other.has_value();
if (m_has_value) { m_storage.store_reference(other.m_storage.fetch_reference()); } if (m_has_value) { m_storage.store_reference(other.m_storage.fetch_reference()); }
} }
Option(Option<T>&& other) Option(Option<T>&& other) : m_has_value(other.m_has_value)
{ {
m_has_value = other.has_value();
other.m_has_value = false; other.m_has_value = false;
if (m_has_value) { m_storage.store_moved_reference(move(other.m_storage.fetch_reference())); } if (m_has_value) { m_storage.store_moved_reference(move(other.m_storage.fetch_reference())); }
} }
Option() Option() : m_has_value(false)
{ {
m_has_value = false;
} }
bool has_value() const bool has_value() const
@ -72,6 +67,7 @@ template <typename T> class Option
if (has_value()) m_storage.destroy(); if (has_value()) m_storage.destroy();
} }
// For compatibility with TRY()
struct ErrorHandle struct ErrorHandle
{ {
private: private:
@ -80,12 +76,10 @@ template <typename T> class Option
} }
}; };
Option(ErrorHandle) Option(const ErrorHandle&) : m_has_value(false)
{ {
m_has_value = false;
} }
// For compatibility with TRY()
ErrorHandle release_error() ErrorHandle release_error()
{ {
expect(!has_value(), "Option::release_error called on a non-empty Option"); expect(!has_value(), "Option::release_error called on a non-empty Option");

View File

@ -19,58 +19,30 @@ struct Error
template <typename T> class Result template <typename T> class Result
{ {
public: public:
Result(const T& value) : m_value(value) Result(const T& value) : m_value(value), m_has_value(true)
{ {
m_has_value = true;
m_has_error = false;
} }
Result(T&& value) : m_value(move(value)) Result(T&& value) : m_value(move(value)), m_has_value(true)
{ {
m_has_value = true;
m_has_error = false;
} }
Result(const Result<T>& other) : m_value(other.m_value) Result(const Result<T>& other) : m_value(other.m_value), m_has_value(other.m_has_value), m_error(other.m_error)
{ {
if (!other.m_has_error)
{
m_has_value = true;
m_has_error = false;
}
else
{
m_has_error = true;
m_has_value = false;
m_error = other.m_error;
}
} }
Result(Result<T>&& other) : m_value(move(other.m_value)) Result(Result<T>&& other) : m_value(move(other.m_value)), m_has_value(other.m_has_value), m_error(other.m_error)
{ {
if (!other.m_has_error) other.m_has_value = false;
{
m_has_value = true;
m_has_error = false;
}
else
{
m_has_error = true;
m_has_value = false;
m_error = other.m_error;
}
} }
Result(const Error& err) : m_value() Result(const Error& err) : m_value(), m_has_value(false), m_error(err.error)
{ {
m_error = err.error;
m_has_error = true;
m_has_value = false;
} }
bool has_error() bool has_error()
{ {
return m_has_error; return !m_has_value;
} }
bool has_value() bool has_value()
@ -132,35 +104,27 @@ template <typename T> class Result
private: private:
Option<T> m_value; Option<T> m_value;
int m_error;
bool m_has_error;
bool m_has_value; bool m_has_value;
int m_error;
}; };
template <> class Result<void> template <> class Result<void>
{ {
public: public:
Result() Result() : m_has_error(false)
{ {
m_has_error = false;
} }
Result(const Result<void>& other) Result(const Result<void>& other) : m_has_error(other.m_has_error), m_error(other.m_error)
{ {
m_has_error = other.m_has_error;
m_error = other.m_error;
} }
Result(Result<void>&& other) Result(Result<void>&& other) : m_has_error(other.m_has_error), m_error(other.m_error)
{ {
m_has_error = other.m_has_error;
m_error = other.m_error;
} }
Result(const Error& err) Result(const Error& err) : m_has_error(true), m_error(err.error)
{ {
m_error = err.error;
m_has_error = true;
} }
bool has_error() bool has_error()
@ -216,8 +180,8 @@ template <> class Result<void>
} }
private: private:
int m_error;
bool m_has_error; bool m_has_error;
int m_error;
}; };
// clang-format off // clang-format off