summaryrefslogtreecommitdiff
path: root/include/fud_result.hpp
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-10-03 08:07:05 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-10-03 08:07:05 -0500
commite420eca2b244c303af51534ab09632045a186b21 (patch)
treeba4ec2d6863b716672f35898266516b58c727aa9 /include/fud_result.hpp
parent97bbafb762defc01abc38834b70a7e8f20d654f5 (diff)
Cleanup from clang-tidy.
Diffstat (limited to 'include/fud_result.hpp')
-rw-r--r--include/fud_result.hpp52
1 files changed, 43 insertions, 9 deletions
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 <typename T>
+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 <typename T, typename E>
class [[nodiscard]] Result {
@@ -58,35 +82,45 @@ class [[nodiscard]] Result {
return (m_value.index() == 1);
}
- T getOkay()
+ [[nodiscard]] T getOkay() const
+ {
+ return std::get<detail::CopyMove<T>>(m_value).copy();
+ }
+
+ [[nodiscard]] E getError() const
+ {
+ return std::get<detail::CopyMove<E>>(m_value).copy();
+ }
+
+ [[nodiscard]] T&& getOkay()
{
- return std::get<T>(m_value);
+ return std::get<detail::CopyMove<T>>(m_value).take();
}
- E getError()
+ [[nodiscard]] E&& getError()
{
- return std::get<E>(m_value);
+ return std::get<detail::CopyMove<E>>(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<T>{value}}
{
}
- explicit Result(const E& value) : m_value(value)
+ explicit Result(const E& value) : m_value{detail::CopyMove<E>{value}}
{
}
- explicit Result(T&& value) : m_value(std::move(value))
+ explicit Result(T&& value) : m_value{detail::CopyMove<T>{std::move(value)}}
{
}
- explicit Result(E&& value) : m_value(std::move(value))
+ explicit Result(E&& value) : m_value{detail::CopyMove<E>{std::move(value)}}
{
}
- std::variant<T, E> m_value;
+ std::variant<detail::CopyMove<T>, detail::CopyMove<E>> m_value;
};
} // namespace fud