BAN: Add specialization for ErrorOr<LValueReference>

ErrorOr can now return a reference :)
This commit is contained in:
Bananymous 2023-04-18 22:02:47 +03:00
parent 5aaf2128a8
commit 1dc81abca4
2 changed files with 28 additions and 0 deletions

View File

@ -85,6 +85,33 @@ namespace BAN
Variant<Error, T> m_data;
};
template<lvalue_reference T>
class [[nodiscard]] ErrorOr<T>
{
public:
ErrorOr(T value)
{
m_data.template set<T>(value);
}
ErrorOr(Error&& error)
: m_data(move(error))
{ }
ErrorOr(const Error& error)
: m_data(error)
{ }
bool is_error() const { return m_data.template has<Error>(); }
Error& error() { return m_data.template get<Error>(); }
const Error& error() const { return m_data.template get<Error>(); }
T value() { return m_data.template get<T>(); }
Error release_error() { return move(error()); m_data.clear(); }
T release_value() { return value(); m_data.clear(); }
private:
Variant<Error, T> m_data;
};
template<>
class [[nodiscard]] ErrorOr<void>
{

View File

@ -40,6 +40,7 @@ namespace BAN
template<typename T> struct is_lvalue_reference : false_type {};
template<typename T> struct is_lvalue_reference<T&> : true_type {};
template<typename T> inline constexpr bool is_lvalue_reference_v = is_lvalue_reference<T>::value;
template<typename T> concept lvalue_reference = is_lvalue_reference_v<T>;
template<typename T> struct is_integral { static constexpr bool value = requires (T t, T* p, void (*f)(T)) { reinterpret_cast<T>(t); f(0); p + t; }; };
template<typename T> inline constexpr bool is_integral_v = is_integral<T>::value;