Result: Avoid double-checking when calling through to the underlying Option
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
8e59a0a79f
commit
400d0395a2
12
luna/include/luna/Badge.h
Normal file
12
luna/include/luna/Badge.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
template <class T> struct Badge
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
constexpr Badge() = default;
|
||||||
|
|
||||||
|
Badge(const Badge<T>&) = delete;
|
||||||
|
Badge(Badge<T>&&) = delete;
|
||||||
|
|
||||||
|
friend T;
|
||||||
|
};
|
@ -1,8 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <luna/Badge.h>
|
||||||
#include <luna/Check.h>
|
#include <luna/Check.h>
|
||||||
#include <luna/Move.h>
|
#include <luna/Move.h>
|
||||||
#include <luna/PlacementNew.h>
|
#include <luna/PlacementNew.h>
|
||||||
|
|
||||||
|
template <typename T> class Result;
|
||||||
|
|
||||||
template <typename T> class Option
|
template <typename T> class Option
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -42,6 +45,11 @@ template <typename T> class Option
|
|||||||
return m_storage.fetch_reference();
|
return m_storage.fetch_reference();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
T unchecked_value(Badge<Result<T>>) const
|
||||||
|
{
|
||||||
|
return m_storage.fetch_reference();
|
||||||
|
}
|
||||||
|
|
||||||
T release_value()
|
T release_value()
|
||||||
{
|
{
|
||||||
expect(has_value(), "Option::release_value called on an empty Option");
|
expect(has_value(), "Option::release_value called on an empty Option");
|
||||||
@ -49,6 +57,12 @@ template <typename T> class Option
|
|||||||
return move(m_storage.fetch_reference());
|
return move(m_storage.fetch_reference());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
T unchecked_release_value(Badge<Result<T>>)
|
||||||
|
{
|
||||||
|
m_has_value = false;
|
||||||
|
return move(m_storage.fetch_reference());
|
||||||
|
}
|
||||||
|
|
||||||
T value_or(const T& other) const
|
T value_or(const T& other) const
|
||||||
{
|
{
|
||||||
if (has_value()) return m_storage.fetch_reference();
|
if (has_value()) return m_storage.fetch_reference();
|
||||||
|
@ -72,13 +72,13 @@ template <typename T> class Result
|
|||||||
T value() const
|
T value() const
|
||||||
{
|
{
|
||||||
expect(has_value(), "Result::value() called on a Result that holds an error");
|
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
|
T expect_value(const char* reason) const
|
||||||
{
|
{
|
||||||
expect(has_value(), reason);
|
expect(has_value(), reason);
|
||||||
return m_value.value();
|
return m_value.unchecked_value({});
|
||||||
}
|
}
|
||||||
|
|
||||||
T value_or(const T& other) const
|
T value_or(const T& other) const
|
||||||
@ -101,13 +101,13 @@ template <typename T> class Result
|
|||||||
T release_value()
|
T release_value()
|
||||||
{
|
{
|
||||||
expect(has_value(), "Result::release_value() called on a Result that holds an error");
|
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)
|
T expect_release_value(const char* reason)
|
||||||
{
|
{
|
||||||
expect(has_value(), reason);
|
expect(has_value(), reason);
|
||||||
return m_value.release_value();
|
return m_value.unchecked_release_value({});
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Loading…
Reference in New Issue
Block a user