[ox] Make Error use a strong int

This commit is contained in:
Gary Talent 2019-07-28 00:15:10 -05:00
parent 3c86aae616
commit f4b336dd77
14 changed files with 44 additions and 52 deletions

View File

@ -444,7 +444,7 @@ ValErr<StatInfo> FileStoreTemplate<size_t>::stat(InodeId_t id) {
inode->fileType,
});
}
return ValErr<StatInfo>({}, 1);
return ValErr<StatInfo>({}, OxError(0));
}
template<typename size_t>
@ -547,7 +547,7 @@ Error FileStoreTemplate<size_t>::placeItem(ItemPtr item) {
item->left = root->left;
item->right = root->right;
oxTrace("ox::fs::FileStoreTemplate::placeItem") << "Overwrote Root Item:" << item->id;
return 0;
return OxError(0);
} else {
return placeItem(root, item);
}

View File

@ -263,7 +263,7 @@ ox::Error FileSystemTemplate<FileStore, Directory>::write(const char *path, void
oxReturnError(rootDir.error);
oxReturnError(rootDir.value.write(path, inode));
oxReturnError(write(inode, buffer, size, fileType));
return 0;
return OxError(0);
}
template<typename FileStore, typename Directory>

View File

@ -59,9 +59,9 @@ Error PathIterator::fileName(char *out, std::size_t outSize) {
// Gets the get item in the path
Error PathIterator::get(char *pathOut, std::size_t pathOutSize) {
std::size_t size = 0;
Error retval = 1;
auto retval = OxError(1);
if (m_iterator < m_maxSize && ox_strlen(&m_path[m_iterator])) {
retval = 0;
retval = OxError(0);
auto start = m_iterator;
if (m_path[start] == '/') {
start++;
@ -85,15 +85,15 @@ Error PathIterator::get(char *pathOut, std::size_t pathOutSize) {
size--;
}
pathOut[size] = 0; // end with null terminator
return OxError(retval);
return retval;
}
// Gets the get item in the path
Error PathIterator::next(char *pathOut, std::size_t pathOutSize) {
std::size_t size = 0;
Error retval = 1;
auto retval = OxError(1);
if (m_iterator < m_maxSize && ox_strlen(&m_path[m_iterator])) {
retval = 0;
retval = OxError(0);
if (m_path[m_iterator] == '/') {
m_iterator++;
}
@ -118,7 +118,7 @@ Error PathIterator::next(char *pathOut, std::size_t pathOutSize) {
}
pathOut[size] = 0; // end with null terminator
m_iterator += size;
return OxError(retval);
return retval;
}
/**
@ -137,10 +137,10 @@ Error PathIterator::next(BString<MaxFileNameLength> *fileName) {
ValErr<std::size_t> PathIterator::nextSize() const {
std::size_t size = 0;
Error retval = 1;
auto retval = OxError(1);
auto it = m_iterator;
if (it < m_maxSize && ox_strlen(&m_path[it])) {
retval = 0;
retval = OxError(0);
if (m_path[it] == '/') {
it++;
}
@ -155,7 +155,7 @@ ValErr<std::size_t> PathIterator::nextSize() const {
size = end - start;
}
it += size;
return {size, OxError(retval)};
return {size, retval};
}
bool PathIterator::hasNext() const {

View File

@ -1,5 +1,3 @@
cmake_minimum_required(VERSION 2.8)
add_executable(
FSTests
tests.cpp

View File

@ -126,7 +126,7 @@ template<typename I>
*bytesRead = bytes;
I out = 0;
ox_memcpy(&out, &buff[1], sizeof(I));
return {LittleEndian<I>(out), 0};
return {LittleEndian<I>(out), OxError(0)};
} else if (buffLen >= bytes) {
*bytesRead = bytes;
uint64_t decoded = 0;
@ -144,7 +144,7 @@ template<typename I>
decoded |= sign << (Bits<I> - 1);
ox_memcpy(&out, &decoded, sizeof(out));
}
return {out, 0};
return {out, OxError(0)};
}
return {0, OxError(1)};
}

View File

@ -32,9 +32,9 @@ Error FieldPresenceIndicator::set(int i, bool on) {
} else {
m_mask[i / 8] &= ~(1 << (i % 8));
}
return 0;
return OxError(0);
} else {
return MC_PRESENCEMASKOUTBOUNDS;
return OxError(MC_PRESENCEMASKOUTBOUNDS);
}
}

View File

@ -62,7 +62,7 @@ Error MetalClawReader::field(const char*, uint64_t *val) {
Error MetalClawReader::field(const char*, bool *val) {
*val = m_fieldPresence.get(m_field++);
return 0;
return OxError(0);
}
Error MetalClawReader::field(const char*, SerStr val) {

View File

@ -106,7 +106,7 @@ class MetalClawReader {
template<typename T>
Error MetalClawReader::field(const char*, T *val) {
Error err = 0;
auto err = OxError(0);
if (val && m_fieldPresence.get(m_field++)) {
auto reader = child();
err |= model(&reader, val);
@ -139,7 +139,7 @@ Error MetalClawReader::readInteger(I *val) {
// array handler
template<typename T>
Error MetalClawReader::field(const char*, T *val, std::size_t valLen) {
Error err = 0;
auto err = OxError(0);
if (m_fieldPresence.get(m_field++)) {
// read the length
if (m_buffIt >= m_buffLen) {

View File

@ -43,7 +43,7 @@ struct TestStruct {
template<typename T>
ox::Error model(T *io, TestStructNest *obj) {
ox::Error err = 0;
auto err = OxError(0);
io->setTypeInfo("TestStructNest", 3);
err |= io->field("Bool", &obj->Bool);
err |= io->field("Int", &obj->Int);
@ -53,7 +53,7 @@ ox::Error model(T *io, TestStructNest *obj) {
template<typename T>
ox::Error model(T *io, TestStruct *obj) {
ox::Error err = 0;
auto err = OxError(0);
io->setTypeInfo("TestStruct", 14);
err |= io->field("Bool", &obj->Bool);
err |= io->field("Int", &obj->Int);
@ -81,7 +81,7 @@ std::map<std::string, ox::Error(*)()> tests = {
// doesn't segfault
constexpr size_t buffLen = 1024;
uint8_t buff[buffLen];
ox::Error err = 0;
auto err = ox::Error(0);
TestStruct ts;
err |= ox::writeMC(buff, buffLen, &ts);
@ -241,7 +241,7 @@ std::map<std::string, ox::Error(*)()> tests = {
{
"MetalClawDef",
[] {
ox::Error err = 0;
auto err = OxError(0);
//constexpr size_t descBuffLen = 1024;
//uint8_t descBuff[descBuffLen];
constexpr size_t dataBuffLen = 1024;

View File

@ -61,7 +61,7 @@ Error MetalClawWriter::field(const char*, bool *val) {
}
Error MetalClawWriter::field(const char*, SerStr val) {
int err = 0;
auto err = OxError(0);
bool fieldSet = false;
if (val.cap()) {
// write the length
@ -75,7 +75,7 @@ Error MetalClawWriter::field(const char*, SerStr val) {
m_buffIt += val.bytes();
fieldSet = true;
} else {
err = MC_BUFFENDED;
err = OxError(MC_BUFFENDED);
}
}
err |= m_fieldPresence.set(m_field, fieldSet);

View File

@ -84,7 +84,7 @@ Error MetalClawWriter::field(const char *name, ox::BString<L> *val) {
template<typename T>
Error MetalClawWriter::field(const char*, T *val) {
int err = 0;
auto err = OxError(0);
bool fieldSet = false;
MetalClawWriter writer(m_buff + m_buffIt, m_buffLen - m_buffIt);
if (val) {
@ -106,7 +106,7 @@ Error MetalClawWriter::field(const char*, ox::Vector<T> *val) {
template<typename T>
Error MetalClawWriter::field(const char*, T *val, std::size_t len) {
Error err = 0;
auto err = OxError(0);
bool fieldSet = false;
if (len) {
@ -116,7 +116,7 @@ Error MetalClawWriter::field(const char*, T *val, std::size_t len) {
ox_memcpy(&m_buff[m_buffIt], arrLen.data, arrLen.length);
m_buffIt += arrLen.length;
} else {
err = MC_BUFFENDED;
err = OxError(MC_BUFFENDED);
}
MetalClawWriter writer(m_buff + m_buffIt, m_buffLen - m_buffIt);
@ -138,7 +138,7 @@ Error MetalClawWriter::field(const char*, T *val, std::size_t len) {
template<typename I>
Error MetalClawWriter::appendInteger(I val) {
Error err = 0;
auto err = OxError(0);
bool fieldSet = false;
if (val) {
auto mi = mc::encodeInteger(val);
@ -147,7 +147,7 @@ Error MetalClawWriter::appendInteger(I val) {
ox_memcpy(&m_buff[m_buffIt], mi.data, mi.length);
m_buffIt += mi.length;
} else {
err |= MC_BUFFENDED;
err |= OxError(MC_BUFFENDED);
}
}
err |= m_fieldPresence.set(m_field, fieldSet);

View File

@ -20,7 +20,7 @@ template<typename T>
template<typename T>
[[nodiscard]] constexpr T onMask(int bits = sizeof(T) << 3 /* *8 */) noexcept {
T out = 0;
T out = T(0);
for (auto i = 0; i < bits; i++) {
out |= static_cast<T>(1) << i;
}

View File

@ -8,6 +8,7 @@
#pragma once
#include "strongint.hpp"
#include "typetraits.hpp"
#include "utility.hpp"
@ -21,7 +22,7 @@
namespace ox {
using Error = uint64_t;
using Error = Uint64;
constexpr Error errCode(Error err) {
return (err >> 59) & onMask<Error>(5);
@ -30,12 +31,12 @@ constexpr Error errCode(Error err) {
struct ErrorInfo {
const char *file = nullptr;
int line = -1;
Error errCode = 0;
Error errCode = Error(0);
ErrorInfo() = default;
ErrorInfo(Error err) {
this->file = reinterpret_cast<const char*>(err & onMask<Error>(48));
this->file = reinterpret_cast<const char*>(static_cast<uint64_t>(err & onMask<Error>(48)));
this->line = static_cast<int>((err >> 48) & onMask<Error>(11));
this->errCode = ox::errCode(err);
}
@ -50,7 +51,7 @@ static constexpr Error _errorTags(Error line, Error errCode) {
}
static constexpr Error _error(const char *file, uint32_t line, Error errCode) {
return errCode ? reinterpret_cast<uint64_t>(file) | _errorTags(line, errCode) : 0;
return Error(errCode ? reinterpret_cast<uint64_t>(file) | _errorTags(Error(line), errCode) : 0);
}
template<typename T>
@ -60,7 +61,7 @@ struct ValErr {
inline constexpr ValErr() = default;
inline constexpr ValErr(T value, Error error = 0): value(ox::move(value)), error(error) {
inline constexpr ValErr(T value, Error error = OxError(0)): value(ox::move(value)), error(error) {
}
inline constexpr operator const T&() const {

View File

@ -15,6 +15,11 @@
#include "trace.hpp"
extern "C"
void oxTraceHook([[maybe_unused]] const char *file, [[maybe_unused]] int line,
[[maybe_unused]] const char *ch, [[maybe_unused]] const char *msg) {
}
namespace ox::trace {
#if defined(OX_USE_STDLIB)
@ -23,18 +28,6 @@ static const auto OxPrintTrace = std::getenv("OXTRACE");
constexpr auto OxPrintTrace = false;
#endif
namespace gdblogger {
void captureLogFunc([[maybe_unused]] const char *file, [[maybe_unused]] int line,
[[maybe_unused]] const char *ch, [[maybe_unused]] const char *msg) {
}
void logFunc(const char *file, int line, const char *ch, const char *msg) {
captureLogFunc(file, line, ch, msg);
}
}
OutStream::OutStream(const char *file, int line, const char *ch, const char *msg) {
m_msg.file = file;
m_msg.line = line;
@ -43,7 +36,7 @@ OutStream::OutStream(const char *file, int line, const char *ch, const char *msg
}
OutStream::~OutStream() {
gdblogger::logFunc(m_msg.file.c_str(), m_msg.line, m_msg.ch.c_str(), m_msg.msg.c_str());
oxTraceHook(m_msg.file.c_str(), m_msg.line, m_msg.ch.c_str(), m_msg.msg.c_str());
}
@ -55,7 +48,7 @@ StdOutStream::StdOutStream(const char *file, int line, const char *ch, const cha
}
StdOutStream::~StdOutStream() {
gdblogger::logFunc(m_msg.file.c_str(), m_msg.line, m_msg.ch.c_str(), m_msg.msg.c_str());
oxTraceHook(m_msg.file.c_str(), m_msg.line, m_msg.ch.c_str(), m_msg.msg.c_str());
#if defined(OX_USE_STDLIB)
if (OxPrintTrace) {
std::cout << std::setw(53) << std::left << m_msg.ch.c_str() << '|';