From 6766f8d2a71ffd82a5b256ac984ec20efee1964a Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 28 Jul 2019 00:15:10 -0500 Subject: [PATCH] [ox] Make Error use a strong int (synced from f4b336dd77302e0945bf9143959ae0e165a4d836) --- src/ox/fs/filestore/filestoretemplate.hpp | 4 ++-- src/ox/fs/filesystem/filesystem.hpp | 2 +- src/ox/fs/filesystem/pathiterator.cpp | 18 +++++++++--------- src/ox/fs/test/CMakeLists.txt | 2 -- src/ox/mc/intops.hpp | 4 ++-- src/ox/mc/presenceindicator.cpp | 4 ++-- src/ox/mc/read.cpp | 2 +- src/ox/mc/read.hpp | 4 ++-- src/ox/mc/test/tests.cpp | 8 ++++---- src/ox/mc/write.cpp | 4 ++-- src/ox/mc/write.hpp | 10 +++++----- src/ox/std/bitops.hpp | 2 +- src/ox/std/error.hpp | 11 ++++++----- src/ox/std/trace.cpp | 21 +++++++-------------- 14 files changed, 44 insertions(+), 52 deletions(-) diff --git a/src/ox/fs/filestore/filestoretemplate.hpp b/src/ox/fs/filestore/filestoretemplate.hpp index 8ac31bfc8..0d406aa4b 100644 --- a/src/ox/fs/filestore/filestoretemplate.hpp +++ b/src/ox/fs/filestore/filestoretemplate.hpp @@ -444,7 +444,7 @@ ValErr FileStoreTemplate::stat(InodeId_t id) { inode->fileType, }); } - return ValErr({}, 1); + return ValErr({}, OxError(0)); } template @@ -547,7 +547,7 @@ Error FileStoreTemplate::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); } diff --git a/src/ox/fs/filesystem/filesystem.hpp b/src/ox/fs/filesystem/filesystem.hpp index 91afd5346..d1a3e6515 100644 --- a/src/ox/fs/filesystem/filesystem.hpp +++ b/src/ox/fs/filesystem/filesystem.hpp @@ -263,7 +263,7 @@ ox::Error FileSystemTemplate::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 diff --git a/src/ox/fs/filesystem/pathiterator.cpp b/src/ox/fs/filesystem/pathiterator.cpp index 88e48af0b..0527c1d1b 100644 --- a/src/ox/fs/filesystem/pathiterator.cpp +++ b/src/ox/fs/filesystem/pathiterator.cpp @@ -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 *fileName) { ValErr 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 PathIterator::nextSize() const { size = end - start; } it += size; - return {size, OxError(retval)}; + return {size, retval}; } bool PathIterator::hasNext() const { diff --git a/src/ox/fs/test/CMakeLists.txt b/src/ox/fs/test/CMakeLists.txt index 1587076d0..599260984 100644 --- a/src/ox/fs/test/CMakeLists.txt +++ b/src/ox/fs/test/CMakeLists.txt @@ -1,5 +1,3 @@ -cmake_minimum_required(VERSION 2.8) - add_executable( FSTests tests.cpp diff --git a/src/ox/mc/intops.hpp b/src/ox/mc/intops.hpp index b409f592d..a4e03b5b5 100644 --- a/src/ox/mc/intops.hpp +++ b/src/ox/mc/intops.hpp @@ -126,7 +126,7 @@ template *bytesRead = bytes; I out = 0; ox_memcpy(&out, &buff[1], sizeof(I)); - return {LittleEndian(out), 0}; + return {LittleEndian(out), OxError(0)}; } else if (buffLen >= bytes) { *bytesRead = bytes; uint64_t decoded = 0; @@ -144,7 +144,7 @@ template decoded |= sign << (Bits - 1); ox_memcpy(&out, &decoded, sizeof(out)); } - return {out, 0}; + return {out, OxError(0)}; } return {0, OxError(1)}; } diff --git a/src/ox/mc/presenceindicator.cpp b/src/ox/mc/presenceindicator.cpp index d0590cfae..b587214f3 100644 --- a/src/ox/mc/presenceindicator.cpp +++ b/src/ox/mc/presenceindicator.cpp @@ -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); } } diff --git a/src/ox/mc/read.cpp b/src/ox/mc/read.cpp index e4aade5a2..cbef562de 100644 --- a/src/ox/mc/read.cpp +++ b/src/ox/mc/read.cpp @@ -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) { diff --git a/src/ox/mc/read.hpp b/src/ox/mc/read.hpp index 80e918442..344dc52f3 100644 --- a/src/ox/mc/read.hpp +++ b/src/ox/mc/read.hpp @@ -106,7 +106,7 @@ class MetalClawReader { template 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 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) { diff --git a/src/ox/mc/test/tests.cpp b/src/ox/mc/test/tests.cpp index 1bea80fa3..cbdea9c50 100644 --- a/src/ox/mc/test/tests.cpp +++ b/src/ox/mc/test/tests.cpp @@ -43,7 +43,7 @@ struct TestStruct { template 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 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 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 tests = { { "MetalClawDef", [] { - ox::Error err = 0; + auto err = OxError(0); //constexpr size_t descBuffLen = 1024; //uint8_t descBuff[descBuffLen]; constexpr size_t dataBuffLen = 1024; diff --git a/src/ox/mc/write.cpp b/src/ox/mc/write.cpp index aa1481467..89397c57b 100644 --- a/src/ox/mc/write.cpp +++ b/src/ox/mc/write.cpp @@ -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); diff --git a/src/ox/mc/write.hpp b/src/ox/mc/write.hpp index aadc929a8..783eea6dc 100644 --- a/src/ox/mc/write.hpp +++ b/src/ox/mc/write.hpp @@ -84,7 +84,7 @@ Error MetalClawWriter::field(const char *name, ox::BString *val) { template 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 *val) { template 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 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); diff --git a/src/ox/std/bitops.hpp b/src/ox/std/bitops.hpp index a76185546..138d48b9e 100644 --- a/src/ox/std/bitops.hpp +++ b/src/ox/std/bitops.hpp @@ -20,7 +20,7 @@ template template [[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(1) << i; } diff --git a/src/ox/std/error.hpp b/src/ox/std/error.hpp index 31af8820e..48d85bd8f 100644 --- a/src/ox/std/error.hpp +++ b/src/ox/std/error.hpp @@ -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(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(err & onMask(48)); + this->file = reinterpret_cast(static_cast(err & onMask(48))); this->line = static_cast((err >> 48) & onMask(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(file) | _errorTags(line, errCode) : 0; + return Error(errCode ? reinterpret_cast(file) | _errorTags(Error(line), errCode) : 0); } template @@ -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 { diff --git a/src/ox/std/trace.cpp b/src/ox/std/trace.cpp index 546f95f75..8934cc563 100644 --- a/src/ox/std/trace.cpp +++ b/src/ox/std/trace.cpp @@ -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() << '|';