[ox] Remove OxException
All checks were successful
Build / build (push) Successful in 2m52s

This commit is contained in:
Gary Talent 2024-12-14 00:40:05 -06:00
parent ed910c0beb
commit ac7e5be187
2 changed files with 61 additions and 68 deletions

View File

@ -19,7 +19,7 @@ OrganicClawReader::OrganicClawReader(const uint8_t *buff, std::size_t buffSize)
Json::CharReaderBuilder parserBuilder; Json::CharReaderBuilder parserBuilder;
auto parser = std::unique_ptr<Json::CharReader>(parserBuilder.newCharReader()); auto parser = std::unique_ptr<Json::CharReader>(parserBuilder.newCharReader());
if (!parser->parse(json, json + jsonLen, &m_json, nullptr)) { if (!parser->parse(json, json + jsonLen, &m_json, nullptr)) {
throw OxException(1, "Could not parse JSON"); throw ox::Exception(1, "Could not parse JSON");
} }
} }
@ -27,7 +27,7 @@ OrganicClawReader::OrganicClawReader(const char *json, std::size_t jsonLen) {
Json::CharReaderBuilder parserBuilder; Json::CharReaderBuilder parserBuilder;
auto parser = std::unique_ptr<Json::CharReader>(parserBuilder.newCharReader()); auto parser = std::unique_ptr<Json::CharReader>(parserBuilder.newCharReader());
if (!parser->parse(json, json + jsonLen, &m_json, nullptr)) { if (!parser->parse(json, json + jsonLen, &m_json, nullptr)) {
throw OxException(1, "Could not parse JSON"); throw ox::Exception(1, "Could not parse JSON");
} }
} }

View File

@ -17,7 +17,7 @@ class exception {
virtual ~exception() = default; virtual ~exception() = default;
[[nodiscard]] [[nodiscard]]
virtual const char *what() const noexcept { virtual char const*what() const noexcept {
return ""; return "";
} }
}; };
@ -30,64 +30,46 @@ class exception {
#include "typetraits.hpp" #include "typetraits.hpp"
#include "utility.hpp" #include "utility.hpp"
#define OxException(...) ox::Exception(__FILE__, __LINE__, __VA_ARGS__)
namespace ox { namespace ox {
using ErrorCode = uint16_t; using ErrorCode = uint16_t;
struct [[nodiscard]] Error { struct [[nodiscard]] Error {
const char *msg = nullptr; ox::CString msg = nullptr;
const char *file = nullptr; ox::CString file = nullptr;
uint16_t line = 0; uint16_t line = 0;
ErrorCode errCode = 0; ErrorCode errCode = 0;
constexpr Error() noexcept = default; constexpr Error() noexcept = default;
explicit constexpr Error( explicit constexpr Error(
const char *file, ox::CString file,
uint32_t const line, uint32_t const line,
ErrorCode const errCode, ErrorCode const errCode,
const char *msg = nullptr) noexcept { ox::CString msg = nullptr) noexcept:
this->file = file; msg{msg},
this->line = static_cast<uint16_t>(line); file{file},
this->msg = msg; line{static_cast<uint16_t>(line)},
this->errCode = errCode; errCode{errCode} {}
}
explicit constexpr Error( explicit constexpr Error(
ErrorCode const errCode, ErrorCode const errCode,
std::source_location const&src = std::source_location::current()) noexcept { std::source_location const&src = std::source_location::current()) noexcept:
this->file = src.file_name(); file{src.file_name()},
this->line = static_cast<uint16_t>(src.line()); line{static_cast<uint16_t>(src.line())},
this->errCode = errCode; errCode{errCode}
} {}
explicit constexpr Error( explicit constexpr Error(
ErrorCode const errCode, ErrorCode const errCode,
const char *msg, ox::CString msg,
std::source_location const&src = std::source_location::current()) noexcept { std::source_location const&src = std::source_location::current()) noexcept:
this->file = src.file_name(); msg{msg},
this->line = static_cast<uint16_t>(src.line()); file{src.file_name()},
this->msg = msg; line{static_cast<uint16_t>(src.line())},
this->errCode = errCode; errCode{errCode}
} {}
constexpr Error(const Error &o) noexcept {
this->msg = o.msg;
this->file = o.file;
this->line = o.line;
this->errCode = o.errCode;
}
constexpr Error &operator=(const Error &o) noexcept {
this->msg = o.msg;
this->file = o.file;
this->line = o.line;
this->errCode = o.errCode;
return *this;
}
constexpr operator uint64_t() const noexcept { constexpr operator uint64_t() const noexcept {
return errCode; return errCode;
@ -96,51 +78,62 @@ struct [[nodiscard]] Error {
}; };
[[nodiscard]] [[nodiscard]]
constexpr auto errCode(const Error &err) noexcept { constexpr auto errCode(Error const&err) noexcept {
return err.errCode; return err.errCode;
} }
template<typename T=const char*> template<typename T = char const*>
[[nodiscard]] [[nodiscard]]
constexpr auto toStr(const Error &err) noexcept { constexpr auto toStr(Error const&err) noexcept {
return err.msg ? T(err.msg) : ""; return err.msg ? T{err.msg} : "";
} }
struct Exception: public std::exception { struct Exception: public std::exception {
const char *msg = nullptr; ox::CString msg = nullptr;
const char *file = nullptr; ox::CString file = nullptr;
uint16_t line = 0; uint16_t line = 0;
ErrorCode errCode = 0; ErrorCode errCode = 0;
explicit inline Exception(const char *file, uint32_t line, ErrorCode errCode, const char *msg = "") noexcept { explicit inline Exception(ox::CString file, uint32_t line, ErrorCode errCode, char const*msg = "") noexcept {
this->file = file; this->file = file;
this->line = static_cast<uint16_t>(line); this->line = static_cast<uint16_t>(line);
this->msg = msg; this->msg = msg;
this->errCode = errCode; this->errCode = errCode;
} }
explicit inline Exception(const Error &err) { explicit inline Exception(
if (err.msg) { ErrorCode const errCode,
this->msg = err.msg; std::source_location const&src = std::source_location::current()) noexcept:
} else { file{src.file_name()},
this->msg = ""; line{static_cast<uint16_t>(src.line())},
} errCode{errCode} {}
this->file = err.file;
this->line = err.line; explicit inline Exception(
this->errCode = err.errCode; ErrorCode const errCode,
} ox::CString msg,
std::source_location const&src = std::source_location::current()) noexcept:
msg{msg},
file{src.file_name()},
line{static_cast<uint16_t>(src.line())},
errCode{errCode} {}
explicit inline Exception(Error const&err) noexcept:
msg{err.msg ? err.msg : ""},
file{err.file},
line{err.line},
errCode{err.errCode} {}
constexpr Error toError() const noexcept { constexpr Error toError() const noexcept {
return Error(file, line, errCode, msg); return Error(file, line, errCode, msg);
} }
[[nodiscard]] [[nodiscard]]
const char *what() const noexcept override { char const*what() const noexcept override {
return msg; return msg;
} }
}; };
void panic(const char *file, int line, const char *panicMsg, const Error &err) noexcept; void panic(char const*file, int line, char const*panicMsg, Error const&err) noexcept;
template<typename T> template<typename T>
struct [[nodiscard]] Result { struct [[nodiscard]] Result {
@ -154,25 +147,25 @@ struct [[nodiscard]] Result {
} }
template<typename U> template<typename U>
constexpr Result(const Result<U> &other) noexcept: value(other.value), error(other.error) { constexpr Result(Result<U> const&other) noexcept: value(other.value), error(other.error) {
} }
template<typename U> template<typename U>
constexpr Result(Result<U> &&other) noexcept: value(std::move(other.value)), error(std::move(other.error)) { constexpr Result(Result<U> &&other) noexcept: value(std::move(other.value)), error(std::move(other.error)) {
} }
constexpr Result(const Error &error) noexcept: value(), error(error) { constexpr Result(Error const&error) noexcept: value(), error(error) {
} }
constexpr Result(const type &value, const Error &error = ox::Error(0)) noexcept: value(value), error(error) { constexpr Result(type const&value, Error const&error = {}) noexcept: value(value), error(error) {
} }
constexpr Result(type &&value, const Error &error = ox::Error(0)) noexcept: value(std::move(value)), error(error) { constexpr Result(type &&value, Error const&error = {}) noexcept: value(std::move(value)), error(error) {
} }
constexpr ~Result() noexcept = default; constexpr ~Result() noexcept = default;
explicit constexpr operator const type&() const noexcept { explicit constexpr operator type const&() const noexcept {
return value; return value;
} }
@ -341,18 +334,18 @@ struct [[nodiscard]] Result {
namespace detail { namespace detail {
constexpr Error toError(const Error &e) noexcept { constexpr Error toError(Error const&e) noexcept {
return e; return e;
} }
template<typename T> template<typename T>
constexpr Error toError(const Result<T> &r) noexcept { constexpr Error toError(Result<T> const&r) noexcept {
return r.error; return r.error;
} }
} }
constexpr void primitiveAssert(const char *file, int line, bool pass, const char *msg) noexcept { constexpr void primitiveAssert(char const*file, int line, bool pass, char const*msg) noexcept {
if constexpr(ox::defines::Debug) { if constexpr(ox::defines::Debug) {
if (!pass) [[unlikely]] { if (!pass) [[unlikely]] {
panic(file, line, msg, ox::Error(1)); panic(file, line, msg, ox::Error(1));