2022-07-30 12:24:03 -04:00
|
|
|
#pragma once
|
|
|
|
|
2022-11-28 10:42:19 -05:00
|
|
|
#include "../DefaultInclude.hpp"
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-11-28 10:42:19 -05:00
|
|
|
#include <string>
|
2022-07-30 12:24:03 -04:00
|
|
|
#include <string_view>
|
2022-11-28 10:42:19 -05:00
|
|
|
#include <variant>
|
|
|
|
#include <fmt/format.h>
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
// clang-format off
|
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
namespace geode {
|
2022-11-28 10:42:19 -05:00
|
|
|
namespace {
|
|
|
|
struct AnyType {
|
|
|
|
explicit AnyType() = delete;
|
2022-07-30 12:24:03 -04:00
|
|
|
};
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-11-28 10:42:19 -05:00
|
|
|
template <class V, class RV>
|
|
|
|
concept ConvertibleToResult =
|
|
|
|
std::is_convertible_v<std::remove_reference_t<V>, std::remove_reference_t<RV>> ||
|
|
|
|
std::is_same_v<std::remove_reference_t<V>, std::remove_reference_t<RV>>;
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-11-28 10:42:19 -05:00
|
|
|
using DefaultValue = std::monostate;
|
|
|
|
using DefaultError = std::string;
|
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
template<class T, class E>
|
|
|
|
struct Storage {
|
|
|
|
std::variant<T, E> m_value;
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
bool holdsOk() const {
|
|
|
|
return std::holds_alternative<T>(m_value);
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
T getOk() const requires std::is_copy_constructible_v<T> {
|
|
|
|
return std::get<T>(m_value);
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
T&& getOk() requires(!std::is_copy_constructible_v<T>) {
|
|
|
|
return std::move(std::get<T>(m_value));
|
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
E getErr() const requires std::is_copy_constructible_v<E> {
|
|
|
|
return std::get<E>(m_value);
|
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
E&& getErr() requires(!std::is_copy_constructible_v<E>) {
|
|
|
|
return std::move(std::get<E>(m_value));
|
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
template<class T2, class E2>
|
|
|
|
requires
|
|
|
|
std::is_convertible_v<T2, T> &&
|
|
|
|
std::is_convertible_v<E2, E>
|
|
|
|
Storage(Storage<T2, E2> const& other)
|
|
|
|
requires
|
|
|
|
std::is_copy_constructible_v<T> &&
|
|
|
|
std::is_copy_constructible_v<E>
|
|
|
|
{
|
|
|
|
if (other.holdsOk()) {
|
|
|
|
m_value = other.getOk();
|
|
|
|
} else {
|
|
|
|
m_value = other.getErr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Storage(T const& value)
|
|
|
|
requires std::is_copy_constructible_v<T>
|
|
|
|
: m_value(value) {}
|
|
|
|
|
|
|
|
Storage(T&& value)
|
|
|
|
requires std::is_move_constructible_v<T>
|
|
|
|
: m_value(std::forward<T>(value)) {}
|
|
|
|
|
|
|
|
Storage(E const& value, std::monostate)
|
|
|
|
requires std::is_copy_constructible_v<E>
|
|
|
|
: m_value(value) {}
|
|
|
|
|
|
|
|
Storage(E&& value, std::monostate)
|
|
|
|
requires std::is_move_constructible_v<E>
|
|
|
|
: m_value(std::forward<E>(value)) {}
|
|
|
|
};
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
template<class T>
|
|
|
|
struct Storage<T, T> {
|
|
|
|
bool m_holdsOk;
|
|
|
|
T m_value;
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
bool holdsOk() const {
|
|
|
|
return m_holdsOk;
|
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
T getOk() const requires std::is_copy_constructible_v<T> {
|
|
|
|
return m_value;
|
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
T&& getOk() requires(!std::is_copy_constructible_v<T>) {
|
|
|
|
return std::move(m_value);
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
T getErr() const requires std::is_copy_constructible_v<T> {
|
|
|
|
return m_value;
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
T&& getErr() requires(!std::is_copy_constructible_v<T>) {
|
|
|
|
return std::move(m_value);
|
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
template<class T2, class E2>
|
|
|
|
requires
|
|
|
|
std::is_convertible_v<T2, T> &&
|
|
|
|
std::is_convertible_v<E2, T>
|
|
|
|
Storage(Storage<T2, E2> const& other)
|
|
|
|
requires
|
|
|
|
std::is_copy_constructible_v<T> &&
|
|
|
|
std::is_copy_constructible_v<T>
|
|
|
|
: m_value(other.holdsOk() ? other.getOk() : other.getErr()),
|
|
|
|
m_holdsOk(other.holdsOk()) {}
|
|
|
|
|
|
|
|
Storage(T const& value)
|
|
|
|
requires std::is_copy_constructible_v<T>
|
|
|
|
: m_value(value),
|
|
|
|
m_holdsOk(true) {}
|
|
|
|
|
|
|
|
Storage(T&& value)
|
|
|
|
requires std::is_move_constructible_v<T>
|
|
|
|
: m_value(std::forward<T>(value)),
|
|
|
|
m_holdsOk(true) {}
|
|
|
|
|
|
|
|
Storage(T const& value, std::monostate)
|
|
|
|
requires std::is_copy_constructible_v<T>
|
|
|
|
: m_value(value),
|
|
|
|
m_holdsOk(false) {}
|
|
|
|
|
|
|
|
Storage(T&& value, std::monostate)
|
|
|
|
requires std::is_move_constructible_v<T>
|
|
|
|
: m_value(std::forward<T>(value)),
|
|
|
|
m_holdsOk(false) {}
|
|
|
|
};
|
|
|
|
}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
template <class T = DefaultValue, class E = DefaultError>
|
|
|
|
class [[nodiscard]] Result final {
|
2022-11-28 10:42:19 -05:00
|
|
|
public:
|
|
|
|
using value_type = std::remove_reference_t<T>;
|
2022-11-30 10:07:05 -05:00
|
|
|
using error_type = std::remove_reference_t<E>;
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-11-28 10:42:19 -05:00
|
|
|
// for some reason doing requires causes errors with pch...
|
|
|
|
static_assert(
|
2022-11-30 10:07:05 -05:00
|
|
|
std::is_copy_constructible_v<value_type> || std::is_move_constructible_v<value_type>,
|
2022-11-28 10:42:19 -05:00
|
|
|
"T must be copiable or movable!"
|
|
|
|
);
|
2022-11-30 10:07:05 -05:00
|
|
|
static_assert(
|
|
|
|
std::is_copy_constructible_v<error_type> || std::is_move_constructible_v<error_type>,
|
|
|
|
"E must be copiable or movable!"
|
|
|
|
);
|
2022-11-28 10:42:19 -05:00
|
|
|
|
|
|
|
protected:
|
2022-11-30 10:07:05 -05:00
|
|
|
Storage<value_type, error_type> m_value;
|
2022-11-28 10:42:19 -05:00
|
|
|
|
|
|
|
public:
|
2022-11-30 10:07:05 -05:00
|
|
|
Storage<value_type, error_type> const& _Raw_Storage() const {
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
2022-11-28 10:42:19 -05:00
|
|
|
bool isOk() const {
|
2022-11-30 10:07:05 -05:00
|
|
|
return m_value.holdsOk();
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
|
2022-11-28 10:42:19 -05:00
|
|
|
bool isErr() const {
|
2022-11-30 10:07:05 -05:00
|
|
|
return !m_value.holdsOk();
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-11-28 10:42:19 -05:00
|
|
|
template<class ... Args>
|
|
|
|
Result<T, std::string> expect(const char* str, Args&&... args) {
|
|
|
|
if (isErr()) {
|
2022-11-30 10:07:05 -05:00
|
|
|
return Result<T, std::string>(fmt::format(
|
|
|
|
str,
|
|
|
|
std::forward<Args>(args)...,
|
|
|
|
fmt::arg("error", unwrapErr())
|
|
|
|
), std::monostate());
|
2022-11-28 10:42:19 -05:00
|
|
|
} else {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
explicit Result(value_type const& value)
|
|
|
|
requires std::is_copy_constructible_v<value_type>
|
|
|
|
: m_value(value) {}
|
|
|
|
|
|
|
|
explicit Result(value_type&& value)
|
|
|
|
requires std::is_move_constructible_v<value_type>
|
|
|
|
: m_value(std::forward<value_type>(value)) {}
|
|
|
|
|
|
|
|
explicit Result(error_type const& value, std::monostate)
|
|
|
|
requires std::is_copy_constructible_v<error_type>
|
|
|
|
: m_value(value, std::monostate()) {}
|
|
|
|
|
|
|
|
explicit Result(error_type&& value, std::monostate)
|
|
|
|
requires std::is_move_constructible_v<error_type>
|
|
|
|
: m_value(std::forward<error_type>(value), std::monostate()) {}
|
|
|
|
|
|
|
|
Result(Result<T, E> const& other)
|
|
|
|
requires
|
|
|
|
std::is_copy_constructible_v<value_type> &&
|
|
|
|
std::is_copy_constructible_v<error_type>
|
|
|
|
= default;
|
|
|
|
|
|
|
|
Result(Result<T, E>&& other)
|
|
|
|
requires(
|
|
|
|
!std::is_copy_constructible_v<value_type> ||
|
|
|
|
!std::is_copy_constructible_v<error_type>
|
|
|
|
) = default;
|
|
|
|
|
|
|
|
template<class T2, class E2>
|
|
|
|
requires
|
|
|
|
std::is_convertible_v<T2, T> &&
|
|
|
|
std::is_convertible_v<E2, E>
|
|
|
|
Result(Result<T2, E2> const& other)
|
|
|
|
requires
|
|
|
|
std::is_copy_constructible_v<value_type> &&
|
|
|
|
std::is_copy_constructible_v<error_type>
|
|
|
|
: m_value(other._Raw_Storage()) {}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
|
|
|
template <class T2>
|
2022-11-30 10:07:05 -05:00
|
|
|
requires ConvertibleToResult<T2, T>
|
|
|
|
Result(Result<T2, AnyType> const& other)
|
|
|
|
requires
|
|
|
|
std::is_copy_constructible_v<value_type> &&
|
|
|
|
std::is_copy_constructible_v<error_type>
|
|
|
|
: Result(value_type(other.unwrap())) {}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
|
|
|
template <class E2>
|
2022-11-30 10:07:05 -05:00
|
|
|
requires ConvertibleToResult<E2, E>
|
|
|
|
Result(Result<AnyType, E2> const& other)
|
|
|
|
requires
|
|
|
|
std::is_copy_constructible_v<value_type> &&
|
|
|
|
std::is_copy_constructible_v<error_type>
|
|
|
|
: m_value(std::forward<E2>(other.unwrapErr()), std::monostate()) {}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
template <class T2>
|
|
|
|
requires ConvertibleToResult<T2, T>
|
|
|
|
Result(Result<T2, AnyType>&& other)
|
|
|
|
requires(
|
|
|
|
!std::is_copy_constructible_v<value_type> ||
|
|
|
|
!std::is_copy_constructible_v<error_type>
|
|
|
|
)
|
|
|
|
: m_value(other.unwrap()) {}
|
2022-11-28 10:42:19 -05:00
|
|
|
|
|
|
|
template <class E2>
|
2022-11-30 10:07:05 -05:00
|
|
|
requires ConvertibleToResult<E2, E>
|
|
|
|
Result(Result<AnyType, E2>&& other)
|
|
|
|
requires(
|
|
|
|
!std::is_copy_constructible_v<value_type> ||
|
|
|
|
!std::is_copy_constructible_v<error_type>
|
|
|
|
)
|
|
|
|
: Result(std::forward<error_type>(other.unwrapErr()), std::monostate()) {}
|
2022-07-30 12:24:03 -04:00
|
|
|
|
2022-11-28 10:42:19 -05:00
|
|
|
value_type unwrap() const requires std::is_copy_constructible_v<value_type> {
|
2022-11-30 10:07:05 -05:00
|
|
|
return m_value.getOk();
|
2022-11-28 10:42:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
value_type&& unwrap() requires(!std::is_copy_constructible_v<value_type>) {
|
2022-11-30 10:07:05 -05:00
|
|
|
return std::forward<value_type>(m_value.getOk());
|
2022-11-28 10:42:19 -05:00
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-11-28 10:42:19 -05:00
|
|
|
error_type unwrapErr() const requires std::is_copy_constructible_v<error_type> {
|
2022-11-30 10:07:05 -05:00
|
|
|
return m_value.getErr();
|
2022-11-28 10:42:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
error_type&& unwrapErr() requires(!std::is_copy_constructible_v<error_type>) {
|
2022-11-30 10:07:05 -05:00
|
|
|
return std::forward<error_type>(m_value.getErr());
|
2022-11-28 10:42:19 -05:00
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
explicit operator bool() const requires(
|
|
|
|
!std::is_same_v<T, bool> &&
|
|
|
|
!std::is_same_v<E, bool>
|
|
|
|
) {
|
2022-11-28 10:42:19 -05:00
|
|
|
return this->isOk();
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
|
|
|
};
|
2022-11-28 10:42:19 -05:00
|
|
|
|
|
|
|
template <class T = DefaultValue, class E = AnyType>
|
2022-11-30 10:07:05 -05:00
|
|
|
requires std::is_copy_constructible_v<T>
|
|
|
|
Result<T, E> Ok(T value = T()) {
|
2022-11-28 10:42:19 -05:00
|
|
|
return Result<T, E>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T = DefaultValue, class E = AnyType>
|
2022-11-30 10:07:05 -05:00
|
|
|
requires(!std::is_copy_constructible_v<T>)
|
|
|
|
Result<T, E> Ok(T&& value) {
|
2022-11-28 10:42:19 -05:00
|
|
|
return Result<T, E>(std::forward<T>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class E = DefaultError, class T = AnyType>
|
2022-11-30 10:07:05 -05:00
|
|
|
requires std::is_copy_constructible_v<E>
|
|
|
|
Result<T, E> Err(E error) {
|
|
|
|
return Result<T, E>(error, std::monostate());
|
2022-11-28 10:42:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class E = DefaultError, class T = AnyType>
|
2022-11-30 10:07:05 -05:00
|
|
|
requires(!std::is_copy_constructible_v<E>)
|
|
|
|
Result<T, E> Err(E&& error) {
|
|
|
|
return Result<T, E>(std::forward<E>(error), std::monostate());
|
2022-11-28 10:42:19 -05:00
|
|
|
}
|
|
|
|
|
2022-11-30 10:07:05 -05:00
|
|
|
#define GEODE_UNWRAP_INTO(into, ...) \
|
2022-11-28 10:42:19 -05:00
|
|
|
auto GEODE_CONCAT(unwrap_res_, __LINE__) = (__VA_ARGS__); \
|
|
|
|
if (GEODE_CONCAT(unwrap_res_, __LINE__).isErr()) { \
|
|
|
|
return Err(std::move(GEODE_CONCAT(unwrap_res_, __LINE__).unwrapErr())); \
|
|
|
|
} \
|
2022-11-30 10:07:05 -05:00
|
|
|
into = std::move(GEODE_CONCAT(unwrap_res_, __LINE__).unwrap())
|
|
|
|
|
|
|
|
#define GEODE_UNWRAP(...) \
|
|
|
|
{ \
|
|
|
|
auto GEODE_CONCAT(unwrap_res_, __LINE__) = (__VA_ARGS__); \
|
|
|
|
if (GEODE_CONCAT(unwrap_res_, __LINE__).isErr()) { \
|
|
|
|
return Err(std::move(GEODE_CONCAT(unwrap_res_, __LINE__).unwrapErr())); \
|
|
|
|
} \
|
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
}
|
2022-11-30 10:07:05 -05:00
|
|
|
|
|
|
|
// clang-format on
|