diff --git a/luna/include/luna/Option.h b/luna/include/luna/Option.h index 9ef4a58d..c4be03be 100644 --- a/luna/include/luna/Option.h +++ b/luna/include/luna/Option.h @@ -6,34 +6,29 @@ template class Option { public: - Option(const T& value) + Option(const T& value) : m_has_value(true) { 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_has_value = true; } - Option(const Option& other) + Option(const Option& 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()); } } - Option(Option&& other) + Option(Option&& other) : m_has_value(other.m_has_value) { - m_has_value = other.has_value(); other.m_has_value = false; 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 @@ -72,6 +67,7 @@ template class Option if (has_value()) m_storage.destroy(); } + // For compatibility with TRY() struct ErrorHandle { private: @@ -80,12 +76,10 @@ template class Option } }; - Option(ErrorHandle) + Option(const ErrorHandle&) : m_has_value(false) { - m_has_value = false; } - // For compatibility with TRY() ErrorHandle release_error() { expect(!has_value(), "Option::release_error called on a non-empty Option"); diff --git a/luna/include/luna/Result.h b/luna/include/luna/Result.h index b8ac716e..4179728a 100644 --- a/luna/include/luna/Result.h +++ b/luna/include/luna/Result.h @@ -19,58 +19,30 @@ struct Error template class Result { 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& other) : m_value(other.m_value) + Result(const Result& 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&& other) : m_value(move(other.m_value)) + Result(Result&& other) : m_value(move(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; - } + other.m_has_value = false; } - 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() { - return m_has_error; + return !m_has_value; } bool has_value() @@ -132,35 +104,27 @@ template class Result private: Option m_value; - int m_error; - bool m_has_error; bool m_has_value; + int m_error; }; template <> class Result { public: - Result() + Result() : m_has_error(false) { - m_has_error = false; } - Result(const Result& other) + Result(const Result& 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&& other) + Result(Result&& 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() @@ -216,8 +180,8 @@ template <> class Result } private: - int m_error; bool m_has_error; + int m_error; }; // clang-format off