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

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

View File

@ -60,7 +60,7 @@ struct type_version<T, decltype((void) T::TypeVersion, -1)> {
template<typename T>
constexpr const char *getTypeName(T *t) noexcept {
TypeInfoCatcher tnc;
model(&tnc, t);
oxIgnoreError(model(&tnc, t));
return tnc.name;
}

View File

@ -235,7 +235,7 @@ Error FileStoreTemplate<size_t>::decLinks(InodeId_t id) {
if (item.valid()) {
item->links--;
if (item->links == 0) {
remove(item);
oxReturnError(remove(item));
}
return OxError(0);
}

View File

@ -155,7 +155,7 @@ ox::Error Directory<FileStore, InodeId_t>::mkdir(PathIterator path, bool parents
// determine if already exists
auto name = nameBuff;
path.get(name);
oxReturnError(path.get(name));
auto childInode = find(name->c_str());
if (!childInode.ok()) {
// if this is not the last item in the path and parents is disabled,
@ -223,7 +223,7 @@ ox::Error Directory<FileStore, InodeId_t>::write(PathIterator path, InodeId_t in
// insert the new entry on this directory
// get the name
path.next(name);
oxReturnError(path.next(name));
// find existing version of directory
oxTrace("ox::fs::Directory::write") << "Searching for directory inode" << m_inodeId;

View File

@ -302,7 +302,7 @@ std::map<std::string, ox::Error(*)()> tests = {
oxAssert(ox::writeMC(dataBuff, dataBuffLen, &testIn), "Data generation failed");
auto type = ox::buildTypeDef(&testIn);
oxAssert(type.error, "Descriptor write failed");
ox::walkModel<ox::MetalClawReader>(type.value, dataBuff, dataBuffLen,
oxReturnError(ox::walkModel<ox::MetalClawReader>(type.value, dataBuff, dataBuffLen,
[](const ox::Vector<ox::FieldName>&, const ox::Vector<ox::TypeName>&, const ox::DescriptorField &f, ox::MetalClawReader *rdr) -> ox::Error {
//std::cout << f.fieldName.c_str() << '\n';
auto fieldName = f.fieldName.c_str();
@ -387,7 +387,7 @@ std::map<std::string, ox::Error(*)()> tests = {
}
return OxError(0);
}
);
));
delete type.value;
return OxError(0);
}

View File

@ -182,7 +182,7 @@ std::map<std::string, ox::Error(*)()> tests = {
oxAssert(ocErr, "Data generation failed");
auto type = ox::buildTypeDef(&testIn);
oxAssert(type.error, "Descriptor write failed");
ox::walkModel<ox::OrganicClawReader>(type.value, ox::bit_cast<uint8_t*>(oc.data()), oc.size(),
oxReturnError(ox::walkModel<ox::OrganicClawReader>(type.value, ox::bit_cast<uint8_t*>(oc.data()), oc.size(),
[](const ox::Vector<ox::FieldName>&, const ox::Vector<ox::TypeName>&, const ox::DescriptorField &f, ox::OrganicClawReader *rdr) -> ox::Error {
//std::cout << f.fieldName.c_str() << '\n';
auto fieldName = f.fieldName.c_str();
@ -267,7 +267,7 @@ std::map<std::string, ox::Error(*)()> tests = {
}
return OxError(0);
}
);
));
delete type.value;
return OxError(0);

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.
*/