[ox/std] Add ox::Exception
This commit is contained in:
parent
2f9accf5ba
commit
eb39c6aaf0
26
deps/ox/src/ox/std/error.hpp
vendored
26
deps/ox/src/ox/std/error.hpp
vendored
@ -8,6 +8,14 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#if __has_include(<exception>)
|
||||||
|
#include <exception>
|
||||||
|
#else
|
||||||
|
namespace std {
|
||||||
|
class exception {};
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "defines.hpp"
|
#include "defines.hpp"
|
||||||
#include "strongint.hpp"
|
#include "strongint.hpp"
|
||||||
#include "typetraits.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>
|
template<typename T>
|
||||||
struct [[nodiscard]] Result {
|
struct [[nodiscard]] Result {
|
||||||
|
|
||||||
@ -125,10 +147,10 @@ constexpr Error toError(const Result<T> &ve) noexcept {
|
|||||||
constexpr void oxIgnoreError(ox::Error) noexcept {}
|
constexpr void oxIgnoreError(ox::Error) noexcept {}
|
||||||
#if __cplusplus >= 202002L
|
#if __cplusplus >= 202002L
|
||||||
#define oxReturnError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] return _ox_error
|
#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
|
#else
|
||||||
#define oxReturnError(err) if (const auto _ox_error = ox::detail::toError(err)) return _ox_error
|
#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
|
#endif
|
||||||
#define oxConcatImpl(a, b) a##b
|
#define oxConcatImpl(a, b) a##b
|
||||||
#define oxConcat(a, b) oxConcatImpl(a, b)
|
#define oxConcat(a, b) oxConcatImpl(a, b)
|
||||||
|
Loading…
Reference in New Issue
Block a user