2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-02-25 19:47:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
2021-01-20 22:11:17 +00:00
|
|
|
#include <AK/Platform.h>
|
|
|
|
#include <AK/StdLibExtras.h>
|
2019-02-25 19:47:56 +00:00
|
|
|
#include <LibC/errno_numbers.h>
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-06-07 15:13:23 +00:00
|
|
|
enum KSuccessTag {
|
2019-05-28 09:53:16 +00:00
|
|
|
KSuccess
|
|
|
|
};
|
2019-02-25 19:47:56 +00:00
|
|
|
|
2020-12-30 21:44:54 +00:00
|
|
|
class [[nodiscard]] KResult {
|
2019-02-25 19:47:56 +00:00
|
|
|
public:
|
2021-01-20 22:11:17 +00:00
|
|
|
KResult(ErrnoCode error)
|
|
|
|
: m_error(-error)
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
KResult(KSuccessTag)
|
|
|
|
: m_error(0)
|
|
|
|
{
|
|
|
|
}
|
2019-02-25 19:47:56 +00:00
|
|
|
operator int() const { return m_error; }
|
2020-08-08 19:29:09 +00:00
|
|
|
[[nodiscard]] int error() const { return m_error; }
|
2019-02-25 19:47:56 +00:00
|
|
|
|
2020-08-08 19:29:09 +00:00
|
|
|
[[nodiscard]] bool is_success() const { return m_error == 0; }
|
|
|
|
[[nodiscard]] bool is_error() const { return !is_success(); }
|
2019-02-27 13:11:25 +00:00
|
|
|
|
2019-02-25 19:47:56 +00:00
|
|
|
private:
|
2019-05-28 09:53:16 +00:00
|
|
|
template<typename T>
|
|
|
|
friend class KResultOr;
|
2021-02-28 13:42:08 +00:00
|
|
|
KResult() = default;
|
2019-02-25 19:47:56 +00:00
|
|
|
|
|
|
|
int m_error { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2020-12-30 21:44:54 +00:00
|
|
|
class alignas(T) [[nodiscard]] KResultOr {
|
2019-02-25 19:47:56 +00:00
|
|
|
public:
|
|
|
|
KResultOr(KResult error)
|
|
|
|
: m_error(error)
|
|
|
|
, m_is_error(true)
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
}
|
2019-02-25 19:47:56 +00:00
|
|
|
|
2021-01-20 22:11:17 +00:00
|
|
|
KResultOr(ErrnoCode error)
|
|
|
|
: m_error(error)
|
|
|
|
, m_is_error(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE KResultOr(T&& value)
|
2019-02-25 19:47:56 +00:00
|
|
|
{
|
|
|
|
new (&m_storage) T(move(value));
|
2020-09-18 22:13:41 +00:00
|
|
|
m_have_storage = true;
|
2019-02-25 19:47:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 21:14:31 +00:00
|
|
|
template<typename U>
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE KResultOr(U&& value)
|
2019-03-06 21:14:31 +00:00
|
|
|
{
|
|
|
|
new (&m_storage) T(move(value));
|
2020-09-18 22:13:41 +00:00
|
|
|
m_have_storage = true;
|
2019-03-06 21:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KResultOr(KResultOr&& other)
|
|
|
|
{
|
2019-06-13 13:37:01 +00:00
|
|
|
m_is_error = other.m_is_error;
|
|
|
|
if (m_is_error)
|
|
|
|
m_error = other.m_error;
|
2019-12-29 05:50:25 +00:00
|
|
|
else {
|
2020-09-18 22:13:41 +00:00
|
|
|
if (other.m_have_storage) {
|
|
|
|
new (&m_storage) T(move(other.value()));
|
|
|
|
m_have_storage = true;
|
|
|
|
other.value().~T();
|
|
|
|
other.m_have_storage = false;
|
|
|
|
}
|
2019-12-29 05:50:25 +00:00
|
|
|
}
|
2019-03-06 21:14:31 +00:00
|
|
|
other.m_is_error = true;
|
|
|
|
other.m_error = KSuccess;
|
|
|
|
}
|
|
|
|
|
2019-12-29 05:50:25 +00:00
|
|
|
KResultOr& operator=(KResultOr&& other)
|
|
|
|
{
|
2021-02-07 21:55:56 +00:00
|
|
|
if (&other == this)
|
|
|
|
return *this;
|
2020-09-18 22:13:41 +00:00
|
|
|
if (!m_is_error && m_have_storage) {
|
2020-02-08 10:57:13 +00:00
|
|
|
value().~T();
|
2020-09-18 22:13:41 +00:00
|
|
|
m_have_storage = false;
|
|
|
|
}
|
2019-12-29 05:50:25 +00:00
|
|
|
m_is_error = other.m_is_error;
|
|
|
|
if (m_is_error)
|
|
|
|
m_error = other.m_error;
|
|
|
|
else {
|
2020-09-18 22:13:41 +00:00
|
|
|
if (other.m_have_storage) {
|
|
|
|
new (&m_storage) T(move(other.value()));
|
|
|
|
m_have_storage = true;
|
|
|
|
other.value().~T();
|
|
|
|
other.m_have_storage = false;
|
|
|
|
}
|
2019-12-29 05:50:25 +00:00
|
|
|
}
|
|
|
|
other.m_is_error = true;
|
|
|
|
other.m_error = KSuccess;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-02-25 19:47:56 +00:00
|
|
|
~KResultOr()
|
|
|
|
{
|
2020-09-18 22:13:41 +00:00
|
|
|
if (!m_is_error && m_have_storage)
|
2019-02-25 19:47:56 +00:00
|
|
|
value().~T();
|
|
|
|
}
|
|
|
|
|
2020-08-08 19:29:09 +00:00
|
|
|
[[nodiscard]] bool is_error() const { return m_is_error; }
|
|
|
|
|
2021-02-14 23:21:38 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE KResult error() const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_is_error);
|
2019-05-28 09:53:16 +00:00
|
|
|
return m_error;
|
|
|
|
}
|
2020-08-08 19:29:09 +00:00
|
|
|
|
2021-02-14 23:21:38 +00:00
|
|
|
[[nodiscard]] KResult result() const { return m_is_error ? m_error : KSuccess; }
|
2020-08-08 19:29:09 +00:00
|
|
|
|
2021-02-14 23:21:38 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T& value()
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!m_is_error);
|
2019-05-28 09:53:16 +00:00
|
|
|
return *reinterpret_cast<T*>(&m_storage);
|
|
|
|
}
|
2020-08-08 19:29:09 +00:00
|
|
|
|
2021-02-14 23:21:38 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE const T& value() const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!m_is_error);
|
2019-05-28 09:53:16 +00:00
|
|
|
return *reinterpret_cast<T*>(&m_storage);
|
|
|
|
}
|
2019-02-25 19:47:56 +00:00
|
|
|
|
2021-02-14 23:21:38 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T release_value()
|
2019-04-09 00:37:05 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!m_is_error);
|
|
|
|
VERIFY(m_have_storage);
|
2021-02-07 21:12:13 +00:00
|
|
|
T released_value(move(*reinterpret_cast<T*>(&m_storage)));
|
2019-04-09 00:37:05 +00:00
|
|
|
value().~T();
|
2020-09-18 22:13:41 +00:00
|
|
|
m_have_storage = false;
|
2019-04-09 00:37:05 +00:00
|
|
|
return released_value;
|
|
|
|
}
|
|
|
|
|
2019-02-25 19:47:56 +00:00
|
|
|
private:
|
2021-02-07 22:30:23 +00:00
|
|
|
union {
|
|
|
|
alignas(T) char m_storage[sizeof(T)];
|
|
|
|
KResult m_error;
|
|
|
|
};
|
2019-02-25 19:47:56 +00:00
|
|
|
bool m_is_error { false };
|
2020-09-18 22:13:41 +00:00
|
|
|
bool m_have_storage { false };
|
2019-02-25 19:47:56 +00:00
|
|
|
};
|
2020-02-16 00:27:42 +00:00
|
|
|
|
|
|
|
}
|
2021-01-13 23:10:32 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
struct AK::Formatter<Kernel::KResult> : Formatter<int> {
|
|
|
|
void format(FormatBuilder& builder, Kernel::KResult value)
|
|
|
|
{
|
|
|
|
return Formatter<int>::format(builder, value);
|
|
|
|
}
|
|
|
|
};
|