2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-11-29 15:57:48 +00:00

chore: pass ZoneLoading error as result

This commit is contained in:
Jan Laupetin
2025-10-11 15:28:19 +01:00
parent 098be53559
commit b27b7e77bd
9 changed files with 181 additions and 121 deletions

View File

@@ -6,112 +6,171 @@
using NoResult = std::monostate;
// Can be replaced by std::expected with c++23
template<typename TResult, typename TError> class Result
namespace result
{
public:
Result(TResult result)
: m_data(std::variant<TResult, TError>(std::in_place_index<0>, std::move(result)))
template<typename TError> class Unexpected
{
}
public:
Unexpected(TError result)
: m_data(std::move(result))
{
}
static Result Ok(TResult result)
constexpr std::add_lvalue_reference_t<TError> value() &
{
return m_data;
}
constexpr std::add_const_t<std::add_lvalue_reference_t<TError>> value() const&
{
return m_data;
}
constexpr std::add_rvalue_reference_t<TError> value() &&
{
return std::move(m_data);
}
constexpr std::add_const_t<std::add_rvalue_reference_t<TError>> value() const&&
{
return std::move(m_data);
}
constexpr std::add_lvalue_reference_t<TError> operator*() &
{
return m_data;
}
constexpr std::add_const_t<std::add_lvalue_reference_t<TError>> operator*() const&
{
return m_data;
}
constexpr std::add_rvalue_reference_t<TError> operator*() &&
{
return std::move(m_data);
}
constexpr std::add_const_t<std::add_rvalue_reference_t<TError>> operator*() const&&
{
return std::move(m_data);
}
constexpr std::add_pointer_t<TError> operator->()
{
return m_data;
}
constexpr std::add_const_t<std::add_pointer_t<TError>> operator->() const
{
return m_data;
}
private:
TError m_data;
};
template<typename TResult, typename TError> class Expected
{
return Result(std::in_place_index<0>, std::move(result));
}
public:
Expected(TResult result)
: m_data(std::variant<TResult, TError>(std::in_place_index<0>, std::move(result)))
{
}
static Result Bad(TError error)
{
return Result(std::in_place_index<1>, std::move(error));
}
Expected(Unexpected<TError> unexpected)
: m_data(std::variant<TResult, TError>(std::in_place_index<1>, std::move(*unexpected)))
{
}
constexpr operator bool() const noexcept
{
return m_data.index() == 0;
}
constexpr operator bool() const noexcept
{
return m_data.index() == 0;
}
constexpr bool has_value() const noexcept
{
return m_data.index() == 0;
}
constexpr bool has_value() const noexcept
{
return m_data.index() == 0;
}
constexpr std::add_lvalue_reference_t<TResult> value() &
{
return std::get<0, TResult>(m_data);
}
constexpr std::add_lvalue_reference_t<TResult> value() &
{
return std::get<0>(m_data);
}
constexpr std::add_const_t<std::add_lvalue_reference_t<TResult>> value() const&
{
return std::get<0, TResult>(m_data);
}
constexpr std::add_const_t<std::add_lvalue_reference_t<TResult>> value() const&
{
return std::get<0>(m_data);
}
constexpr std::add_rvalue_reference_t<TResult> value() &&
{
return std::move(std::get<0, TResult>(m_data));
}
constexpr std::add_rvalue_reference_t<TResult> value() &&
{
return std::move(std::get<0>(m_data));
}
constexpr std::add_const_t<std::add_rvalue_reference_t<TResult>> value() const&&
{
return std::move(std::get<0, TResult>(m_data));
}
constexpr std::add_const_t<std::add_rvalue_reference_t<TResult>> value() const&&
{
return std::move(std::get<0>(m_data));
}
constexpr std::add_lvalue_reference_t<TResult> operator*() &
{
return std::get<0, TResult>(m_data);
}
constexpr std::add_lvalue_reference_t<TResult> operator*() &
{
return std::get<0>(m_data);
}
constexpr std::add_const_t<std::add_lvalue_reference_t<TResult>> operator*() const&
{
return std::get<0, TResult>(m_data);
}
constexpr std::add_const_t<std::add_lvalue_reference_t<TResult>> operator*() const&
{
return std::get<0>(m_data);
}
constexpr std::add_rvalue_reference_t<TResult> operator*() &&
{
return std::move(std::get<0, TResult>(m_data));
}
constexpr std::add_rvalue_reference_t<TResult> operator*() &&
{
return std::move(std::get<0>(m_data));
}
constexpr std::add_const_t<std::add_rvalue_reference_t<TResult>> operator*() const&&
{
return std::move(std::get<0, TResult>(m_data));
}
constexpr std::add_const_t<std::add_rvalue_reference_t<TResult>> operator*() const&&
{
return std::move(std::get<0>(m_data));
}
constexpr std::add_pointer_t<TResult> operator->()
{
return std::get<0, TResult>(m_data);
}
constexpr std::add_pointer_t<TResult> operator->()
{
return std::get<0>(m_data);
}
constexpr std::add_const_t<std::add_pointer_t<TResult>> operator->() const
{
return std::get<0, TResult>(m_data);
}
constexpr std::add_const_t<std::add_pointer_t<TResult>> operator->() const
{
return std::get<0>(m_data);
}
constexpr std::add_lvalue_reference_t<TError> error() &
{
return std::get<1, TError>(m_data);
}
constexpr std::add_lvalue_reference_t<TError> error() &
{
return std::get<1>(m_data);
}
constexpr std::add_const_t<std::add_lvalue_reference_t<TError>> error() const&
{
return std::get<1, TError>(m_data);
}
constexpr std::add_const_t<std::add_lvalue_reference_t<TError>> error() const&
{
return std::get<1>(m_data);
}
constexpr std::add_rvalue_reference_t<TError> error() &&
{
return std::move(std::get<1, TError>(m_data));
}
constexpr std::add_rvalue_reference_t<TError> error() &&
{
return std::move(std::get<1>(m_data));
}
constexpr std::add_const_t<std::add_rvalue_reference_t<TError>> error() const&&
{
return std::move(std::get<1, TError>(m_data));
}
constexpr std::add_const_t<std::add_rvalue_reference_t<TError>> error() const&&
{
return std::move(std::get<1>(m_data));
}
private:
explicit Result(std::variant<TResult, TError> data)
: m_data(std::move(data))
{
}
private:
explicit Expected(std::variant<TResult, TError> data)
: m_data(std::move(data))
{
}
std::variant<TResult, TError> m_data;
};
std::variant<TResult, TError> m_data;
};
#define ENSURE_RESULT_VAR(var) \
if (!(var)) \
@@ -122,3 +181,5 @@ private:
if (!result) \
return result; \
}
} // namespace result