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