[ox/std] Add ox::Exception

This commit is contained in:
Gary Talent 2021-07-17 12:23:36 -05:00
parent 2f9accf5ba
commit eb39c6aaf0

View File

@ -8,6 +8,14 @@
#pragma once
#if __has_include(<exception>)
#include <exception>
#else
namespace std {
class exception {};
}
#endif
#include "defines.hpp"
#include "strongint.hpp"
#include "typetraits.hpp"
@ -57,6 +65,20 @@ struct [[nodiscard]] Error {
};
struct Exception: public std::exception {
const char *msg = nullptr;
const char *file = nullptr;
uint16_t line = 0;
ErrorCode errCode = 0;
inline explicit Exception(const Error &err) {
this->msg = err.msg;
this->file = err.file;
this->line = err.line;
this->errCode = err.errCode;
}
};
template<typename T>
struct [[nodiscard]] Result {
@ -125,10 +147,10 @@ constexpr Error toError(const Result<T> &ve) noexcept {
constexpr void oxIgnoreError(ox::Error) noexcept {}
#if __cplusplus >= 202002L
#define oxReturnError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] return _ox_error
#define oxThrowError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] throw _ox_error
#define oxThrowError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] ox::Exception(_ox_error)
#else
#define oxReturnError(err) if (const auto _ox_error = ox::detail::toError(err)) return _ox_error
#define oxThrowError(err) if (const auto _ox_error = ox::detail::toError(err)) throw ox::move(_ox_error)
#define oxThrowError(err) if (const auto _ox_error = ox::detail::toError(err)) throw ox::Exception(_ox_error)
#endif
#define oxConcatImpl(a, b) a##b
#define oxConcat(a, b) oxConcatImpl(a, b)