2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-02-25 19:47:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#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:
|
2020-06-02 16:30:14 +00:00
|
|
|
ALWAYS_INLINE explicit KResult(int negative_e)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_error(negative_e)
|
|
|
|
{
|
|
|
|
ASSERT(negative_e <= 0);
|
|
|
|
}
|
|
|
|
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;
|
2020-06-02 16:30:14 +00:00
|
|
|
KResult() { }
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-18 07:49:51 +00:00
|
|
|
// FIXME: clang-format gets confused about KResultOr. Why?
|
|
|
|
// clang-format off
|
2019-03-06 21:14:31 +00:00
|
|
|
KResultOr(KResultOr&& other)
|
2020-09-18 07:49:51 +00:00
|
|
|
// clang-format on
|
2019-03-06 21:14:31 +00:00
|
|
|
{
|
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)
|
|
|
|
{
|
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; }
|
|
|
|
|
2020-06-02 16:30:14 +00:00
|
|
|
ALWAYS_INLINE KResult error() const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_is_error);
|
|
|
|
return m_error;
|
|
|
|
}
|
2020-08-08 19:29:09 +00:00
|
|
|
|
2020-02-08 10:57:13 +00:00
|
|
|
KResult result() const { return m_is_error ? KSuccess : m_error; }
|
2020-08-08 19:29:09 +00:00
|
|
|
|
2020-06-02 16:30:14 +00:00
|
|
|
ALWAYS_INLINE T& value()
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
ASSERT(!m_is_error);
|
|
|
|
return *reinterpret_cast<T*>(&m_storage);
|
|
|
|
}
|
2020-08-08 19:29:09 +00:00
|
|
|
|
2020-06-02 16:30:14 +00:00
|
|
|
ALWAYS_INLINE const T& value() const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
ASSERT(!m_is_error);
|
|
|
|
return *reinterpret_cast<T*>(&m_storage);
|
|
|
|
}
|
2019-02-25 19:47:56 +00:00
|
|
|
|
2020-06-02 16:30:14 +00:00
|
|
|
ALWAYS_INLINE T release_value()
|
2019-04-09 00:37:05 +00:00
|
|
|
{
|
|
|
|
ASSERT(!m_is_error);
|
2020-09-18 22:13:41 +00:00
|
|
|
ASSERT(m_have_storage);
|
2019-04-09 00:37:05 +00:00
|
|
|
T released_value = *reinterpret_cast<T*>(&m_storage);
|
|
|
|
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:
|
2020-02-08 10:57:13 +00:00
|
|
|
alignas(T) char m_storage[sizeof(T)];
|
2019-02-25 19:47:56 +00:00
|
|
|
KResult m_error;
|
|
|
|
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
|
|
|
|
|
|
|
}
|