From eb39c6aaf0642f570a4408181d0533d7578e6ebf Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 17 Jul 2021 12:23:36 -0500 Subject: [PATCH] [ox/std] Add ox::Exception --- deps/ox/src/ox/std/error.hpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/deps/ox/src/ox/std/error.hpp b/deps/ox/src/ox/std/error.hpp index bcb6a14b..bbc12f5b 100644 --- a/deps/ox/src/ox/std/error.hpp +++ b/deps/ox/src/ox/std/error.hpp @@ -8,6 +8,14 @@ #pragma once +#if __has_include() +#include +#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 struct [[nodiscard]] Result { @@ -125,10 +147,10 @@ constexpr Error toError(const Result &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)