[ox] Make Error its own type, not dependent on Integer and make nodiscard

This commit is contained in:
2020-10-11 19:00:58 -05:00
parent 8204188008
commit a725369311
8 changed files with 33 additions and 20 deletions

View File

@@ -17,40 +17,45 @@
namespace ox {
struct BaseError {
struct [[nodiscard]] Error {
const char *msg = nullptr;
const char *file = "";
uint16_t line = 0;
uint64_t m_i = 0;
BaseError() = default;
constexpr Error(uint64_t i = 0) {
m_i = i;
}
constexpr BaseError(const BaseError &o) noexcept {
constexpr Error(const Error &o) noexcept {
msg = o.msg;
file = o.file;
line = o.line;
}
constexpr BaseError operator=(const BaseError &o) noexcept {
constexpr Error &operator=(const Error &o) noexcept {
msg = o.msg;
file = o.file;
line = o.line;
return *this;
}
constexpr operator uint64_t() const noexcept {
return m_i;
}
};
using Error = Integer<uint64_t, BaseError>;
static constexpr Error _error(const char *file, uint32_t line, uint64_t errCode, const char *msg = nullptr) {
Error err = static_cast<ox::Error>(errCode);
err.file = file;
err.line = line;
err.msg = msg;
auto err = static_cast<ox::Error>(errCode);
err.file = file;
err.line = line;
err.msg = msg;
return err;
}
template<typename T>
struct ValErr {
struct [[nodiscard]] ValErr {
T value;
Error error;

View File

@@ -126,7 +126,9 @@ constexpr Integer<T, Base>::Integer(const Integer<T, Base> &i) noexcept: Base(i)
template<typename T, class Base>
constexpr Integer<T, Base> Integer<T, Base>::operator=(Integer<T, Base> i) noexcept {
Base::operator=(i);
// needed in case T has nodiscard
constexpr auto ignore = [](Base) {};
ignore(Base::operator=(i));
m_i = i.m_i;
return *this;
}

View File

@@ -51,6 +51,12 @@ class OutStream {
return *this;
}
inline OutStream &operator<<(Error err) {
m_msg.msg += m_delimiter;
m_msg.msg += static_cast<int64_t>(err);
return *this;
}
/**
* del sets the delimiter between log segments.
*/