2022-10-22 10:49:09 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../DefaultInclude.hpp"
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-10-22 10:49:09 -04:00
|
|
|
#include <string>
|
2022-10-30 14:59:20 -04:00
|
|
|
#include <string_view>
|
2022-10-22 10:49:09 -04:00
|
|
|
#include <variant>
|
|
|
|
|
|
|
|
namespace geode {
|
|
|
|
namespace {
|
|
|
|
struct AnyType {
|
|
|
|
explicit AnyType() = delete;
|
|
|
|
};
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class V, class RV>
|
2022-10-22 10:49:09 -04:00
|
|
|
concept ConvertibleToResult =
|
2022-10-30 14:59:20 -04:00
|
|
|
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-22 10:49:09 -04:00
|
|
|
using DefaultValue = std::monostate;
|
|
|
|
using DefaultError = std::string;
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T = DefaultValue, class E = DefaultError>
|
2022-10-22 10:49:09 -04:00
|
|
|
class [[nodiscard]] NewResult {
|
|
|
|
public:
|
|
|
|
using value_type = std::remove_reference_t<T>;
|
|
|
|
using error_type = std::remove_reference_t<E>;
|
|
|
|
|
|
|
|
// for some reason doing requires causes errors with pch...
|
|
|
|
static_assert(
|
2022-10-30 14:59:20 -04:00
|
|
|
std::is_copy_constructible_v<value_type> || std::is_move_constructible_v<value_type>,
|
2022-10-22 10:49:09 -04:00
|
|
|
"T must be copiable or movable!"
|
|
|
|
);
|
|
|
|
static_assert(
|
2022-10-30 14:59:20 -04:00
|
|
|
std::is_copy_constructible_v<error_type> || std::is_move_constructible_v<error_type>,
|
2022-10-22 10:49:09 -04:00
|
|
|
"E must be copiable or movable!"
|
|
|
|
);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::variant<value_type, error_type> m_value;
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-10-22 10:49:09 -04:00
|
|
|
public:
|
|
|
|
bool isOk() const {
|
|
|
|
return std::holds_alternative<value_type>(m_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isErr() const {
|
|
|
|
return std::holds_alternative<error_type>(m_value);
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
explicit NewResult(value_type const& value
|
|
|
|
) requires std::is_copy_constructible_v<value_type> : m_value(value) {}
|
|
|
|
|
|
|
|
explicit NewResult(value_type&& value) requires std::is_move_constructible_v<value_type> :
|
|
|
|
m_value(std::forward<value_type>(value)) {}
|
|
|
|
|
|
|
|
explicit NewResult(error_type const& value
|
|
|
|
) requires std::is_copy_constructible_v<error_type> : m_value(value) {}
|
|
|
|
|
|
|
|
explicit NewResult(error_type&& value) requires std::is_move_constructible_v<error_type> :
|
|
|
|
m_value(std::forward<error_type>(value)) {}
|
|
|
|
|
|
|
|
NewResult(NewResult<T, E> const& other) requires std::is_copy_constructible_v<value_type> &&
|
|
|
|
std::is_copy_constructible_v<error_type>
|
|
|
|
= default;
|
|
|
|
|
|
|
|
NewResult(NewResult<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 ConvertibleToResult<T2, T> && ConvertibleToResult<E2, E> NewResult(
|
|
|
|
NewResult<T2, E2> const& other
|
|
|
|
)
|
|
|
|
|
|
|
|
requires std::is_copy_constructible_v<value_type> &&
|
|
|
|
std::is_copy_constructible_v<error_type> :
|
|
|
|
m_value(other.isOk() ? other.unwrap() : other.unwrapErr()) {}
|
|
|
|
|
|
|
|
template <class T2, class E2>
|
|
|
|
requires ConvertibleToResult<T2, T> && ConvertibleToResult<E2, E> NewResult(
|
|
|
|
NewResult<T2, E2>&& other
|
|
|
|
)
|
|
|
|
|
|
|
|
requires(!std::is_copy_constructible_v<value_type> || !std::is_copy_constructible_v<error_type>) :
|
|
|
|
m_value(other.isOk() ? other.unwrap() : other.unwrapErr()) {}
|
|
|
|
|
|
|
|
template <class T2>
|
|
|
|
requires ConvertibleToResult<T2, T> NewResult(NewResult<T2, AnyType> const& other)
|
|
|
|
|
|
|
|
requires std::is_copy_constructible_v<value_type> &&
|
|
|
|
std::is_copy_constructible_v<error_type> : NewResult(value_type(other.unwrap())) {}
|
|
|
|
|
|
|
|
template <class E2>
|
|
|
|
requires ConvertibleToResult<E2, E> NewResult(NewResult<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())) {}
|
|
|
|
|
|
|
|
template <class T2>
|
|
|
|
requires ConvertibleToResult<T2, T> NewResult(NewResult<T2, AnyType>&& other)
|
|
|
|
|
|
|
|
requires(!std::is_copy_constructible_v<value_type> || !std::is_copy_constructible_v<error_type>) :
|
|
|
|
m_value(other.unwrap()) {}
|
|
|
|
|
|
|
|
template <class E2>
|
|
|
|
requires ConvertibleToResult<E2, E> NewResult(NewResult<AnyType, E2>&& other)
|
|
|
|
|
|
|
|
requires(!std::is_copy_constructible_v<value_type> || !std::is_copy_constructible_v<error_type>) :
|
|
|
|
NewResult(std::forward<error_type>(other.unwrapErr())) {}
|
|
|
|
|
|
|
|
value_type unwrap() const requires std::is_copy_constructible_v<value_type> {
|
2022-10-22 10:49:09 -04:00
|
|
|
return std::get<value_type>(m_value);
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
value_type&& unwrap() requires(!std::is_copy_constructible_v<value_type>) {
|
2022-10-22 10:49:09 -04:00
|
|
|
return std::move(std::get<value_type>(m_value));
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
error_type unwrapErr() const requires std::is_copy_constructible_v<error_type> {
|
2022-10-22 10:49:09 -04:00
|
|
|
return std::get<error_type>(m_value);
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
error_type&& unwrapErr() requires(!std::is_copy_constructible_v<error_type>) {
|
2022-10-22 10:49:09 -04:00
|
|
|
return std::move(std::get<error_type>(m_value));
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
explicit operator bool() const
|
|
|
|
requires(!std::is_same_v<T, bool> && !std::is_same_v<E, bool>) {
|
2022-10-22 10:49:09 -04:00
|
|
|
return this->isOk();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T>
|
2022-10-22 10:49:09 -04:00
|
|
|
class [[nodiscard]] NewResult<T, T> {
|
|
|
|
public:
|
|
|
|
using value_type = std::remove_reference_t<T>;
|
|
|
|
using error_type = std::remove_reference_t<T>;
|
|
|
|
|
|
|
|
// for some reason doing requires causes errors with pch...
|
|
|
|
static_assert(
|
2022-10-30 14:59:20 -04:00
|
|
|
std::is_copy_constructible_v<value_type> || std::is_move_constructible_v<value_type>,
|
2022-10-22 10:49:09 -04:00
|
|
|
"T must be copiable or movable!"
|
|
|
|
);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool m_success;
|
|
|
|
value_type m_value;
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-10-22 10:49:09 -04:00
|
|
|
public:
|
|
|
|
bool isOk() const {
|
|
|
|
return m_success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isErr() const {
|
|
|
|
return !m_success;
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
explicit NewResult(value_type const& value, bool success) requires
|
|
|
|
std::is_copy_constructible_v<value_type> :
|
|
|
|
m_value(value),
|
|
|
|
m_success(success) {}
|
|
|
|
|
|
|
|
explicit NewResult(value_type&& value, bool success) requires
|
|
|
|
std::is_move_constructible_v<value_type> :
|
|
|
|
m_value(std::forward<value_type>(value)),
|
|
|
|
m_success(success) {}
|
|
|
|
|
|
|
|
NewResult(NewResult<T, T> const& other) requires std::is_copy_constructible_v<value_type>
|
|
|
|
= default;
|
|
|
|
|
|
|
|
NewResult(NewResult<T, T>&& other
|
|
|
|
) requires(!std::is_copy_constructible_v<value_type>) = default;
|
|
|
|
|
|
|
|
template <class T2, class E2>
|
|
|
|
requires ConvertibleToResult<T2, T> && ConvertibleToResult<E2, T> NewResult(
|
|
|
|
NewResult<T2, E2> const& other
|
|
|
|
)
|
|
|
|
|
|
|
|
requires std::is_copy_constructible_v<value_type> :
|
|
|
|
m_value(other.isOk() ? other.unwrap() : other.unwrapErr()),
|
2022-10-22 10:49:09 -04:00
|
|
|
m_success(other.isOk()) {}
|
2022-10-30 14:59:20 -04:00
|
|
|
|
|
|
|
template <class T2, class E2>
|
|
|
|
requires ConvertibleToResult<T2, T> && ConvertibleToResult<E2, T> NewResult(
|
|
|
|
NewResult<T2, E2>&& other
|
|
|
|
)
|
|
|
|
|
|
|
|
requires(!std::is_copy_constructible_v<value_type>) :
|
|
|
|
m_value(other.isOk() ? other.unwrap() : other.unwrapErr()), m_success(other.isOk()) {}
|
|
|
|
|
|
|
|
template <class T2>
|
|
|
|
requires ConvertibleToResult<T2, T> NewResult(NewResult<T2, AnyType> const& other)
|
|
|
|
|
|
|
|
requires std::is_copy_constructible_v<value_type> :
|
|
|
|
NewResult(value_type(other.unwrap()), true) {}
|
|
|
|
|
|
|
|
template <class T2>
|
|
|
|
requires ConvertibleToResult<T2, T> NewResult(NewResult<T2, AnyType>&& other)
|
|
|
|
|
|
|
|
requires(!std::is_copy_constructible_v<value_type>) :
|
|
|
|
NewResult(std::forward<value_type>(other.unwrap()), true) {}
|
|
|
|
|
|
|
|
template <class E2>
|
|
|
|
requires ConvertibleToResult<E2, T> NewResult(NewResult<AnyType, E2> const& other)
|
|
|
|
|
|
|
|
requires std::is_copy_constructible_v<value_type> :
|
|
|
|
NewResult(error_type(other.unwrapErr()), false) {}
|
|
|
|
|
|
|
|
template <class E2>
|
|
|
|
requires ConvertibleToResult<E2, T> NewResult(NewResult<AnyType, E2>&& other)
|
|
|
|
|
|
|
|
requires(!std::is_copy_constructible_v<value_type>) :
|
|
|
|
NewResult(std::forward<error_type>(other.unwrapErr()), false) {}
|
|
|
|
|
|
|
|
value_type unwrap() const requires std::is_copy_constructible_v<value_type> {
|
2022-10-22 10:49:09 -04:00
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
value_type&& unwrap() requires(!std::is_copy_constructible_v<value_type>) {
|
2022-10-22 10:49:09 -04:00
|
|
|
return std::move(m_value);
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
error_type unwrapErr() const requires std::is_copy_constructible_v<error_type> {
|
2022-10-22 10:49:09 -04:00
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
error_type&& unwrapErr() requires(!std::is_copy_constructible_v<error_type>) {
|
2022-10-22 10:49:09 -04:00
|
|
|
return std::move(m_value);
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
explicit operator bool() const requires(!std::is_same_v<T, bool>) {
|
2022-10-22 10:49:09 -04:00
|
|
|
return this->isOk();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T = DefaultValue, class E = AnyType>
|
|
|
|
|
|
|
|
requires std::is_copy_constructible_v<T> NewResult<T, E> NewOk(T value = T()) {
|
2022-10-22 10:49:09 -04:00
|
|
|
return NewResult<T, E>(value);
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class T = DefaultValue, class E = AnyType>
|
|
|
|
|
|
|
|
requires(!std::is_copy_constructible_v<T>) NewResult<T, E> NewOk(T&& value) {
|
2022-10-22 10:49:09 -04:00
|
|
|
return NewResult<T, E>(std::forward<T>(value));
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class E = DefaultError, class T = AnyType>
|
|
|
|
|
|
|
|
requires std::is_copy_constructible_v<E> NewResult<T, E> NewErr(E error = E()) {
|
2022-10-22 10:49:09 -04:00
|
|
|
return NewResult<T, E>(error);
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
template <class E = DefaultError, class T = AnyType>
|
|
|
|
|
|
|
|
requires(!std::is_copy_constructible_v<E>) NewResult<T, E> NewErr(E&& error) {
|
2022-10-22 10:49:09 -04:00
|
|
|
return NewResult<T, E>(std::forward<E>(error));
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
#define GEODE_UNWRAP_INTO(into, ...) \
|
|
|
|
auto GEODE_CONCAT(res_, __LINE__) = (__VA_ARGS__); \
|
|
|
|
if (GEODE_CONCAT(res_, __LINE__).isErr()) { \
|
|
|
|
return Err(std::move(GEODE_CONCAT(res_, __LINE__).unwrapErr())); \
|
|
|
|
} \
|
|
|
|
into = std::move(GEODE_CONCAT(res_, __LINE__).unwrap())
|
|
|
|
|
|
|
|
#define GEODE_UNWRAP(...) \
|
|
|
|
{ \
|
|
|
|
auto GEODE_CONCAT(res_, __LINE__) = (__VA_ARGS__); \
|
|
|
|
if (GEODE_CONCAT(res_, __LINE__).isErr()) { \
|
|
|
|
return Err(std::move(GEODE_CONCAT(res_, __LINE__).unwrapErr())); \
|
|
|
|
} \
|
|
|
|
}
|
2022-10-22 10:49:09 -04:00
|
|
|
}
|