[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, inode->fileType,
}); });
} }
return ValErr<StatInfo>({}, 1); return ValErr<StatInfo>({}, OxError(0));
} }
template<typename size_t> template<typename size_t>
@ -547,7 +547,7 @@ Error FileStoreTemplate<size_t>::placeItem(ItemPtr item) {
item->left = root->left; item->left = root->left;
item->right = root->right; item->right = root->right;
oxTrace("ox::fs::FileStoreTemplate::placeItem") << "Overwrote Root Item:" << item->id; oxTrace("ox::fs::FileStoreTemplate::placeItem") << "Overwrote Root Item:" << item->id;
return 0; return OxError(0);
} else { } else {
return placeItem(root, item); 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.error);
oxReturnError(rootDir.value.write(path, inode)); oxReturnError(rootDir.value.write(path, inode));
oxReturnError(write(inode, buffer, size, fileType)); oxReturnError(write(inode, buffer, size, fileType));
return 0; return OxError(0);
} }
template<typename FileStore, typename Directory> 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 // Gets the get item in the path
Error PathIterator::get(char *pathOut, std::size_t pathOutSize) { Error PathIterator::get(char *pathOut, std::size_t pathOutSize) {
std::size_t size = 0; std::size_t size = 0;
Error retval = 1; auto retval = OxError(1);
if (m_iterator < m_maxSize && ox_strlen(&m_path[m_iterator])) { if (m_iterator < m_maxSize && ox_strlen(&m_path[m_iterator])) {
retval = 0; retval = OxError(0);
auto start = m_iterator; auto start = m_iterator;
if (m_path[start] == '/') { if (m_path[start] == '/') {
start++; start++;
@ -85,15 +85,15 @@ Error PathIterator::get(char *pathOut, std::size_t pathOutSize) {
size--; size--;
} }
pathOut[size] = 0; // end with null terminator pathOut[size] = 0; // end with null terminator
return OxError(retval); return retval;
} }
// Gets the get item in the path // Gets the get item in the path
Error PathIterator::next(char *pathOut, std::size_t pathOutSize) { Error PathIterator::next(char *pathOut, std::size_t pathOutSize) {
std::size_t size = 0; std::size_t size = 0;
Error retval = 1; auto retval = OxError(1);
if (m_iterator < m_maxSize && ox_strlen(&m_path[m_iterator])) { if (m_iterator < m_maxSize && ox_strlen(&m_path[m_iterator])) {
retval = 0; retval = OxError(0);
if (m_path[m_iterator] == '/') { if (m_path[m_iterator] == '/') {
m_iterator++; m_iterator++;
} }
@ -118,7 +118,7 @@ Error PathIterator::next(char *pathOut, std::size_t pathOutSize) {
} }
pathOut[size] = 0; // end with null terminator pathOut[size] = 0; // end with null terminator
m_iterator += size; 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 { ValErr<std::size_t> PathIterator::nextSize() const {
std::size_t size = 0; std::size_t size = 0;
Error retval = 1; auto retval = OxError(1);
auto it = m_iterator; auto it = m_iterator;
if (it < m_maxSize && ox_strlen(&m_path[it])) { if (it < m_maxSize && ox_strlen(&m_path[it])) {
retval = 0; retval = OxError(0);
if (m_path[it] == '/') { if (m_path[it] == '/') {
it++; it++;
} }
@ -155,7 +155,7 @@ ValErr<std::size_t> PathIterator::nextSize() const {
size = end - start; size = end - start;
} }
it += size; it += size;
return {size, OxError(retval)}; return {size, retval};
} }
bool PathIterator::hasNext() const { bool PathIterator::hasNext() const {

View File

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

View File

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

View File

@ -32,9 +32,9 @@ Error FieldPresenceIndicator::set(int i, bool on) {
} else { } else {
m_mask[i / 8] &= ~(1 << (i % 8)); m_mask[i / 8] &= ~(1 << (i % 8));
} }
return 0; return OxError(0);
} else { } 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) { Error MetalClawReader::field(const char*, bool *val) {
*val = m_fieldPresence.get(m_field++); *val = m_fieldPresence.get(m_field++);
return 0; return OxError(0);
} }
Error MetalClawReader::field(const char*, SerStr val) { Error MetalClawReader::field(const char*, SerStr val) {

View File

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

View File

@ -43,7 +43,7 @@ struct TestStruct {
template<typename T> template<typename T>
ox::Error model(T *io, TestStructNest *obj) { ox::Error model(T *io, TestStructNest *obj) {
ox::Error err = 0; auto err = OxError(0);
io->setTypeInfo("TestStructNest", 3); io->setTypeInfo("TestStructNest", 3);
err |= io->field("Bool", &obj->Bool); err |= io->field("Bool", &obj->Bool);
err |= io->field("Int", &obj->Int); err |= io->field("Int", &obj->Int);
@ -53,7 +53,7 @@ ox::Error model(T *io, TestStructNest *obj) {
template<typename T> template<typename T>
ox::Error model(T *io, TestStruct *obj) { ox::Error model(T *io, TestStruct *obj) {
ox::Error err = 0; auto err = OxError(0);
io->setTypeInfo("TestStruct", 14); io->setTypeInfo("TestStruct", 14);
err |= io->field("Bool", &obj->Bool); err |= io->field("Bool", &obj->Bool);
err |= io->field("Int", &obj->Int); err |= io->field("Int", &obj->Int);
@ -81,7 +81,7 @@ std::map<std::string, ox::Error(*)()> tests = {
// doesn't segfault // doesn't segfault
constexpr size_t buffLen = 1024; constexpr size_t buffLen = 1024;
uint8_t buff[buffLen]; uint8_t buff[buffLen];
ox::Error err = 0; auto err = ox::Error(0);
TestStruct ts; TestStruct ts;
err |= ox::writeMC(buff, buffLen, &ts); err |= ox::writeMC(buff, buffLen, &ts);
@ -241,7 +241,7 @@ std::map<std::string, ox::Error(*)()> tests = {
{ {
"MetalClawDef", "MetalClawDef",
[] { [] {
ox::Error err = 0; auto err = OxError(0);
//constexpr size_t descBuffLen = 1024; //constexpr size_t descBuffLen = 1024;
//uint8_t descBuff[descBuffLen]; //uint8_t descBuff[descBuffLen];
constexpr size_t dataBuffLen = 1024; 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) { Error MetalClawWriter::field(const char*, SerStr val) {
int err = 0; auto err = OxError(0);
bool fieldSet = false; bool fieldSet = false;
if (val.cap()) { if (val.cap()) {
// write the length // write the length
@ -75,7 +75,7 @@ Error MetalClawWriter::field(const char*, SerStr val) {
m_buffIt += val.bytes(); m_buffIt += val.bytes();
fieldSet = true; fieldSet = true;
} else { } else {
err = MC_BUFFENDED; err = OxError(MC_BUFFENDED);
} }
} }
err |= m_fieldPresence.set(m_field, fieldSet); 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> template<typename T>
Error MetalClawWriter::field(const char*, T *val) { Error MetalClawWriter::field(const char*, T *val) {
int err = 0; auto err = OxError(0);
bool fieldSet = false; bool fieldSet = false;
MetalClawWriter writer(m_buff + m_buffIt, m_buffLen - m_buffIt); MetalClawWriter writer(m_buff + m_buffIt, m_buffLen - m_buffIt);
if (val) { if (val) {
@ -106,7 +106,7 @@ Error MetalClawWriter::field(const char*, ox::Vector<T> *val) {
template<typename T> template<typename T>
Error MetalClawWriter::field(const char*, T *val, std::size_t len) { Error MetalClawWriter::field(const char*, T *val, std::size_t len) {
Error err = 0; auto err = OxError(0);
bool fieldSet = false; bool fieldSet = false;
if (len) { 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); ox_memcpy(&m_buff[m_buffIt], arrLen.data, arrLen.length);
m_buffIt += arrLen.length; m_buffIt += arrLen.length;
} else { } else {
err = MC_BUFFENDED; err = OxError(MC_BUFFENDED);
} }
MetalClawWriter writer(m_buff + m_buffIt, m_buffLen - m_buffIt); 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> template<typename I>
Error MetalClawWriter::appendInteger(I val) { Error MetalClawWriter::appendInteger(I val) {
Error err = 0; auto err = OxError(0);
bool fieldSet = false; bool fieldSet = false;
if (val) { if (val) {
auto mi = mc::encodeInteger(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); ox_memcpy(&m_buff[m_buffIt], mi.data, mi.length);
m_buffIt += mi.length; m_buffIt += mi.length;
} else { } else {
err |= MC_BUFFENDED; err |= OxError(MC_BUFFENDED);
} }
} }
err |= m_fieldPresence.set(m_field, fieldSet); err |= m_fieldPresence.set(m_field, fieldSet);

View File

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

View File

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

View File

@ -15,6 +15,11 @@
#include "trace.hpp" #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 { namespace ox::trace {
#if defined(OX_USE_STDLIB) #if defined(OX_USE_STDLIB)
@ -23,18 +28,6 @@ static const auto OxPrintTrace = std::getenv("OXTRACE");
constexpr auto OxPrintTrace = false; constexpr auto OxPrintTrace = false;
#endif #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) { OutStream::OutStream(const char *file, int line, const char *ch, const char *msg) {
m_msg.file = file; m_msg.file = file;
m_msg.line = line; m_msg.line = line;
@ -43,7 +36,7 @@ OutStream::OutStream(const char *file, int line, const char *ch, const char *msg
} }
OutStream::~OutStream() { 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() { 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 defined(OX_USE_STDLIB)
if (OxPrintTrace) { if (OxPrintTrace) {
std::cout << std::setw(53) << std::left << m_msg.ch.c_str() << '|'; std::cout << std::setw(53) << std::left << m_msg.ch.c_str() << '|';