diff --git a/luna/include/luna/Badge.h b/luna/include/luna/Badge.h new file mode 100644 index 00000000..c319b035 --- /dev/null +++ b/luna/include/luna/Badge.h @@ -0,0 +1,12 @@ +#pragma once + +template struct Badge +{ + private: + constexpr Badge() = default; + + Badge(const Badge&) = delete; + Badge(Badge&&) = delete; + + friend T; +}; \ No newline at end of file diff --git a/luna/include/luna/Option.h b/luna/include/luna/Option.h index cbe7ad7d..2173b704 100644 --- a/luna/include/luna/Option.h +++ b/luna/include/luna/Option.h @@ -1,8 +1,11 @@ #pragma once +#include #include #include #include +template class Result; + template class Option { public: @@ -42,6 +45,11 @@ template class Option return m_storage.fetch_reference(); } + T unchecked_value(Badge>) const + { + return m_storage.fetch_reference(); + } + T release_value() { expect(has_value(), "Option::release_value called on an empty Option"); @@ -49,6 +57,12 @@ template class Option return move(m_storage.fetch_reference()); } + T unchecked_release_value(Badge>) + { + m_has_value = false; + return move(m_storage.fetch_reference()); + } + T value_or(const T& other) const { if (has_value()) return m_storage.fetch_reference(); diff --git a/luna/include/luna/Result.h b/luna/include/luna/Result.h index 180ea158..36124bac 100644 --- a/luna/include/luna/Result.h +++ b/luna/include/luna/Result.h @@ -72,13 +72,13 @@ template class Result T value() const { expect(has_value(), "Result::value() called on a Result that holds an error"); - return m_value.value(); + return m_value.unchecked_value({}); } T expect_value(const char* reason) const { expect(has_value(), reason); - return m_value.value(); + return m_value.unchecked_value({}); } T value_or(const T& other) const @@ -101,13 +101,13 @@ template class Result T release_value() { expect(has_value(), "Result::release_value() called on a Result that holds an error"); - return m_value.release_value(); + return m_value.unchecked_release_value({}); } T expect_release_value(const char* reason) { expect(has_value(), reason); - return m_value.release_value(); + return m_value.unchecked_release_value({}); } private: