2019-02-25 20:47:56 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <LibC/errno_numbers.h>
|
|
|
|
|
2019-06-07 17:13:23 +02:00
|
|
|
enum KSuccessTag {
|
2019-05-28 11:53:16 +02:00
|
|
|
KSuccess
|
|
|
|
};
|
2019-02-25 20:47:56 +01:00
|
|
|
|
|
|
|
class KResult {
|
|
|
|
public:
|
2019-05-28 11:53:16 +02:00
|
|
|
explicit KResult(int negative_e)
|
|
|
|
: m_error(negative_e)
|
|
|
|
{
|
|
|
|
ASSERT(negative_e <= 0);
|
|
|
|
}
|
|
|
|
KResult(KSuccessTag)
|
|
|
|
: m_error(0)
|
|
|
|
{
|
|
|
|
}
|
2019-02-25 20:47:56 +01:00
|
|
|
operator int() const { return m_error; }
|
|
|
|
|
2019-02-27 14:11:25 +01:00
|
|
|
bool is_success() const { return m_error == ESUCCESS; }
|
|
|
|
bool is_error() const { return !is_success(); }
|
|
|
|
|
2019-02-25 20:47:56 +01:00
|
|
|
private:
|
2019-05-28 11:53:16 +02:00
|
|
|
template<typename T>
|
|
|
|
friend class KResultOr;
|
|
|
|
KResult() {}
|
2019-02-25 20:47:56 +01:00
|
|
|
|
|
|
|
int m_error { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class alignas(T) KResultOr {
|
|
|
|
public:
|
|
|
|
KResultOr(KResult error)
|
|
|
|
: m_error(error)
|
|
|
|
, m_is_error(true)
|
2019-05-28 11:53:16 +02:00
|
|
|
{
|
|
|
|
}
|
2019-02-25 20:47:56 +01:00
|
|
|
|
|
|
|
KResultOr(T&& value)
|
|
|
|
{
|
|
|
|
new (&m_storage) T(move(value));
|
|
|
|
}
|
|
|
|
|
2019-03-06 22:14:31 +01:00
|
|
|
template<typename U>
|
|
|
|
KResultOr(U&& value)
|
|
|
|
{
|
|
|
|
new (&m_storage) T(move(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
KResultOr(KResultOr&& other)
|
|
|
|
{
|
2019-06-13 16:37:01 +03:00
|
|
|
m_is_error = other.m_is_error;
|
|
|
|
if (m_is_error)
|
|
|
|
m_error = other.m_error;
|
2019-12-29 00:50:25 -05:00
|
|
|
else {
|
2019-06-13 16:37:01 +03:00
|
|
|
new (&m_storage) T(move(other.value()));
|
2019-12-29 00:50:25 -05:00
|
|
|
other.value().~T();
|
|
|
|
}
|
2019-03-06 22:14:31 +01:00
|
|
|
other.m_is_error = true;
|
|
|
|
other.m_error = KSuccess;
|
|
|
|
}
|
|
|
|
|
2019-12-29 00:50:25 -05:00
|
|
|
KResultOr& operator=(KResultOr&& other)
|
|
|
|
{
|
|
|
|
if (!m_is_error)
|
|
|
|
value().~T();
|
|
|
|
m_is_error = other.m_is_error;
|
|
|
|
if (m_is_error)
|
|
|
|
m_error = other.m_error;
|
|
|
|
else {
|
|
|
|
new (&m_storage) T(move(other.value()));
|
|
|
|
other.value().~T();
|
|
|
|
}
|
|
|
|
other.m_is_error = true;
|
|
|
|
other.m_error = KSuccess;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-02-25 20:47:56 +01:00
|
|
|
~KResultOr()
|
|
|
|
{
|
|
|
|
if (!m_is_error)
|
|
|
|
value().~T();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_error() const { return m_is_error; }
|
2019-05-28 11:53:16 +02:00
|
|
|
KResult error() const
|
|
|
|
{
|
|
|
|
ASSERT(m_is_error);
|
|
|
|
return m_error;
|
|
|
|
}
|
|
|
|
T& value()
|
|
|
|
{
|
|
|
|
ASSERT(!m_is_error);
|
|
|
|
return *reinterpret_cast<T*>(&m_storage);
|
|
|
|
}
|
|
|
|
const T& value() const
|
|
|
|
{
|
|
|
|
ASSERT(!m_is_error);
|
|
|
|
return *reinterpret_cast<T*>(&m_storage);
|
|
|
|
}
|
2019-02-25 20:47:56 +01:00
|
|
|
|
2019-04-09 02:37:05 +02:00
|
|
|
T release_value()
|
|
|
|
{
|
|
|
|
ASSERT(!m_is_error);
|
|
|
|
T released_value = *reinterpret_cast<T*>(&m_storage);
|
|
|
|
value().~T();
|
|
|
|
return released_value;
|
|
|
|
}
|
|
|
|
|
2019-02-25 20:47:56 +01:00
|
|
|
private:
|
2019-08-02 19:22:48 +02:00
|
|
|
alignas (T) char m_storage[sizeof(T)];
|
2019-02-25 20:47:56 +01:00
|
|
|
KResult m_error;
|
|
|
|
bool m_is_error { false };
|
|
|
|
};
|