2020-01-18 11:38:21 +03: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 22:47:56 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#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
|
|
|
|
|
|
|
class KResult {
|
|
|
|
public:
|
2020-06-02 19:30:14 +03:00
|
|
|
ALWAYS_INLINE explicit KResult(int negative_e)
|
2019-05-28 12:53:16 +03:00
|
|
|
: m_error(negative_e)
|
|
|
|
{
|
|
|
|
ASSERT(negative_e <= 0);
|
|
|
|
}
|
|
|
|
KResult(KSuccessTag)
|
|
|
|
: m_error(0)
|
|
|
|
{
|
|
|
|
}
|
2019-02-25 22:47:56 +03:00
|
|
|
operator int() const { return m_error; }
|
2020-01-06 14:08:27 +03:00
|
|
|
int error() const { return m_error; }
|
2019-02-25 22:47:56 +03:00
|
|
|
|
2020-04-10 13:58:27 +03:00
|
|
|
bool is_success() const { return m_error == 0; }
|
2019-02-27 16:11:25 +03:00
|
|
|
bool is_error() const { return !is_success(); }
|
|
|
|
|
2019-02-25 22:47:56 +03:00
|
|
|
private:
|
2019-05-28 12:53:16 +03:00
|
|
|
template<typename T>
|
|
|
|
friend class KResultOr;
|
2020-06-02 19:30:14 +03:00
|
|
|
KResult() { }
|
2019-02-25 22:47:56 +03: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 12:53:16 +03:00
|
|
|
{
|
|
|
|
}
|
2019-02-25 22:47:56 +03:00
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-06-13 16:37:01 +03:00
|
|
|
new (&m_storage) T(move(other.value()));
|
2019-12-29 08:50:25 +03:00
|
|
|
other.value().~T();
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
if (!m_is_error)
|
2020-02-08 13:57:13 +03:00
|
|
|
value().~T();
|
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 {
|
|
|
|
new (&m_storage) T(move(other.value()));
|
|
|
|
other.value().~T();
|
|
|
|
}
|
|
|
|
other.m_is_error = true;
|
|
|
|
other.m_error = KSuccess;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:47:56 +03:00
|
|
|
~KResultOr()
|
|
|
|
{
|
|
|
|
if (!m_is_error)
|
|
|
|
value().~T();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_error() const { return m_is_error; }
|
2020-06-02 19:30:14 +03:00
|
|
|
ALWAYS_INLINE KResult error() const
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
|
|
|
ASSERT(m_is_error);
|
|
|
|
return m_error;
|
|
|
|
}
|
2020-02-08 13:57:13 +03:00
|
|
|
KResult result() const { return m_is_error ? KSuccess : m_error; }
|
2020-06-02 19:30:14 +03:00
|
|
|
ALWAYS_INLINE T& value()
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
|
|
|
ASSERT(!m_is_error);
|
|
|
|
return *reinterpret_cast<T*>(&m_storage);
|
|
|
|
}
|
2020-06-02 19:30:14 +03:00
|
|
|
ALWAYS_INLINE const T& value() const
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
|
|
|
ASSERT(!m_is_error);
|
|
|
|
return *reinterpret_cast<T*>(&m_storage);
|
|
|
|
}
|
2019-02-25 22:47:56 +03:00
|
|
|
|
2020-06-02 19:30:14 +03:00
|
|
|
ALWAYS_INLINE T release_value()
|
2019-04-09 03:37:05 +03:00
|
|
|
{
|
|
|
|
ASSERT(!m_is_error);
|
|
|
|
T released_value = *reinterpret_cast<T*>(&m_storage);
|
|
|
|
value().~T();
|
|
|
|
return released_value;
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:47:56 +03:00
|
|
|
private:
|
2020-02-08 13:57:13 +03:00
|
|
|
alignas(T) char m_storage[sizeof(T)];
|
2019-02-25 22:47:56 +03:00
|
|
|
KResult m_error;
|
|
|
|
bool m_is_error { false };
|
|
|
|
};
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|