From e420eca2b244c303af51534ab09632045a186b21 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Thu, 3 Oct 2024 08:07:05 -0500 Subject: Cleanup from clang-tidy. --- include/fud_result.hpp | 52 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 9 deletions(-) (limited to 'include/fud_result.hpp') diff --git a/include/fud_result.hpp b/include/fud_result.hpp index 9b96399..076af21 100644 --- a/include/fud_result.hpp +++ b/include/fud_result.hpp @@ -23,6 +23,30 @@ namespace fud { +namespace detail { +template +class CopyMove { + public: + explicit constexpr CopyMove(T value) : m_value{std::move(value)} + { + } + + constexpr T&& take() + { + return std::move(m_value); + } + + constexpr T copy() const + { + return m_value; + } + + private: + T m_value; +}; + +} // namespace detail + /** \brief A result type which contains either a T on success or an E on error. */ template class [[nodiscard]] Result { @@ -58,35 +82,45 @@ class [[nodiscard]] Result { return (m_value.index() == 1); } - T getOkay() + [[nodiscard]] T getOkay() const + { + return std::get>(m_value).copy(); + } + + [[nodiscard]] E getError() const + { + return std::get>(m_value).copy(); + } + + [[nodiscard]] T&& getOkay() { - return std::get(m_value); + return std::get>(m_value).take(); } - E getError() + [[nodiscard]] E&& getError() { - return std::get(m_value); + return std::get>(m_value).take(); } private: explicit Result() : m_value() { } - explicit Result(const T& value) : m_value(value) + explicit Result(const T& value) : m_value{detail::CopyMove{value}} { } - explicit Result(const E& value) : m_value(value) + explicit Result(const E& value) : m_value{detail::CopyMove{value}} { } - explicit Result(T&& value) : m_value(std::move(value)) + explicit Result(T&& value) : m_value{detail::CopyMove{std::move(value)}} { } - explicit Result(E&& value) : m_value(std::move(value)) + explicit Result(E&& value) : m_value{detail::CopyMove{std::move(value)}} { } - std::variant m_value; + std::variant, detail::CopyMove> m_value; }; } // namespace fud -- cgit v1.2.3