From c27cc56e31dbd667ab6a3b87b4960a5384138332 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 30 Jul 2019 19:00:01 -0500 Subject: [PATCH] [ox] Make ValErr::operator T() explicit --- deps/ox/src/ox/fs/filesystem/directory.hpp | 14 +++++++------- deps/ox/src/ox/fs/filesystem/filesystem.hpp | 20 +++++++++++--------- deps/ox/src/ox/fs/test/tests.cpp | 2 +- deps/ox/src/ox/mc/read.cpp | 18 ++++++++++-------- deps/ox/src/ox/mc/read.hpp | 6 +++--- deps/ox/src/ox/std/error.hpp | 18 ++++++++++++------ 6 files changed, 44 insertions(+), 34 deletions(-) diff --git a/deps/ox/src/ox/fs/filesystem/directory.hpp b/deps/ox/src/ox/fs/filesystem/directory.hpp index 9ab21891..43677b8f 100644 --- a/deps/ox/src/ox/fs/filesystem/directory.hpp +++ b/deps/ox/src/ox/fs/filesystem/directory.hpp @@ -167,19 +167,19 @@ ox::Error Directory::mkdir(PathIterator path, bool parents oxReturnError(childInode.error); // initialize the directory - Directory child(m_fs, childInode); + Directory child(m_fs, childInode.value); oxReturnError(child.init()); - auto err = write(name->c_str(), childInode); + auto err = write(name->c_str(), childInode.value); if (err) { oxLogError(err); // could not index the directory, delete it - oxLogError(m_fs.remove(childInode)); + oxLogError(m_fs.remove(childInode.value)); return err; } } - Directory child(m_fs, childInode); + Directory child(m_fs, childInode.value); if (path.hasNext()) { oxReturnError(child.mkdir(path.next(), parents, nameBuff)); } @@ -189,8 +189,6 @@ ox::Error Directory::mkdir(PathIterator path, bool parents template ox::Error Directory::write(PathIterator path, InodeId_t inode, FileName *nameBuff) noexcept { - InodeId_t nextChild = 0; - // reuse nameBuff if it has already been allocated, as it is a rather large variable if (nameBuff == nullptr) { nameBuff = reinterpret_cast(ox_alloca(sizeof(FileName))); @@ -203,7 +201,9 @@ ox::Error Directory::write(PathIterator path, InodeId_t in oxTrace("ox::fs::Directory::write") << "Attempting to write to next sub-Directory: " << name->c_str() << " of " << path.fullPath(); - nextChild = findEntry(*name); + auto [nextChild, err] = findEntry(*name); + oxReturnError(err); + oxTrace("ox::fs::Directory::write") << name->c_str() << ": " << nextChild; if (nextChild) { diff --git a/deps/ox/src/ox/fs/filesystem/filesystem.hpp b/deps/ox/src/ox/fs/filesystem/filesystem.hpp index d1a3e651..1734cf16 100644 --- a/deps/ox/src/ox/fs/filesystem/filesystem.hpp +++ b/deps/ox/src/ox/fs/filesystem/filesystem.hpp @@ -184,8 +184,8 @@ ox::Error FileSystemTemplate::move(const char *src, const auto fd = fileSystemData(); oxReturnError(fd.error); Directory rootDir(m_fs, fd.value.rootDirInode); - auto inode = rootDir.find(src); - oxReturnError(inode.error); + auto [inode, err] = rootDir.find(src); + oxReturnError(err); oxReturnError(rootDir.write(dest, inode)); oxReturnError(rootDir.remove(src)); return OxError(0); @@ -196,8 +196,8 @@ ox::Error FileSystemTemplate::read(const char *path, void auto fd = fileSystemData(); oxReturnError(fd.error); Directory rootDir(m_fs, fd.value.rootDirInode); - auto inode = rootDir.find(path); - oxReturnError(inode.error); + auto [inode, err] = rootDir.find(path); + oxReturnError(err); return read(inode, buffer, buffSize); } @@ -227,12 +227,12 @@ ox::Error FileSystemTemplate::remove(const char *path, boo Directory rootDir(m_fs, fd.value.rootDirInode); auto inode = rootDir.find(path); oxReturnError(inode.error); - auto st = stat(inode); + auto st = stat(inode.value); oxReturnError(st.error); if (st.value.fileType == FileType_NormalFile || recursive) { if (auto err = rootDir.remove(path)) { // removal failed, try putting the index back - oxLogError(rootDir.write(path, inode)); + oxLogError(rootDir.write(path, inode.value)); return err; } } else { @@ -255,9 +255,11 @@ ox::Error FileSystemTemplate::resize(uint64_t size, void * template ox::Error FileSystemTemplate::write(const char *path, void *buffer, uint64_t size, uint8_t fileType) { - auto inode = find(path); - if (inode.error) { - inode.value = m_fs.generateInodeId(); + auto [inode, err] = find(path); + if (err) { + auto generated = m_fs.generateInodeId(); + oxReturnError(generated.error); + inode = generated.value; } auto rootDir = this->rootDir(); oxReturnError(rootDir.error); diff --git a/deps/ox/src/ox/fs/test/tests.cpp b/deps/ox/src/ox/fs/test/tests.cpp index 29d16d5d..1efa5157 100644 --- a/deps/ox/src/ox/fs/test/tests.cpp +++ b/deps/ox/src/ox/fs/test/tests.cpp @@ -182,7 +182,7 @@ map tests = { oxTrace("ox::fs::test::Directory") << "find"; oxAssert(dir.find("file1").error, "Could not find file1"); - oxAssert(dir.find("file1") == 1, "Could not find file1"); + oxAssert(dir.find("file1").value == 1, "Could not find file1"); oxTrace("ox::fs::test::Directory") << "write 2"; oxAssert(dir.write("/file3", 3) == 0, "Directory write of file3 failed"); diff --git a/deps/ox/src/ox/mc/read.cpp b/deps/ox/src/ox/mc/read.cpp index cbef562d..0ed0828a 100644 --- a/deps/ox/src/ox/mc/read.cpp +++ b/deps/ox/src/ox/mc/read.cpp @@ -61,8 +61,9 @@ Error MetalClawReader::field(const char*, uint64_t *val) { } Error MetalClawReader::field(const char*, bool *val) { - *val = m_fieldPresence.get(m_field++); - return OxError(0); + auto valErr = m_fieldPresence.get(m_field++); + *val = valErr.value; + return OxError(valErr.error); } Error MetalClawReader::field(const char*, SerStr val) { @@ -72,9 +73,9 @@ Error MetalClawReader::field(const char*, SerStr val) { return OxError(MC_BUFFENDED); } std::size_t bytesRead = 0; - auto size = mc::decodeInteger(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead); + auto [size, err] = mc::decodeInteger(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead); m_buffIt += bytesRead; - oxReturnError(size.error); + oxReturnError(err); // read the string if (val.cap() > -1 && static_cast(val.cap()) >= size) { @@ -101,7 +102,7 @@ Error MetalClawReader::field(const char*, SerStr val) { return OxError(MC_BUFFENDED); } std::size_t bytesRead = 0; - len = mc::decodeInteger(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead); + auto [len, _] = mc::decodeInteger(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead); if (pass) { m_buffIt += sizeof(ArrayLength); } @@ -113,7 +114,8 @@ Error MetalClawReader::field(const char*, SerStr val) { if (m_fieldPresence.get(m_field)) { // read the length std::size_t bytesRead = 0; - return mc::decodeInteger(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead); + auto [len, _] = mc::decodeInteger(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead); + return len; } return 0; } @@ -130,11 +132,11 @@ MetalClawReader MetalClawReader::child() { } bool MetalClawReader::fieldPresent() const { - return m_fieldPresence.get(m_field); + return m_fieldPresence.get(m_field).value; } bool MetalClawReader::fieldPresent(int fieldNo) const { - return m_fieldPresence.get(fieldNo); + return m_fieldPresence.get(fieldNo).value; } void MetalClawReader::nextField() noexcept { diff --git a/deps/ox/src/ox/mc/read.hpp b/deps/ox/src/ox/mc/read.hpp index 344dc52f..96585a37 100644 --- a/deps/ox/src/ox/mc/read.hpp +++ b/deps/ox/src/ox/mc/read.hpp @@ -151,10 +151,10 @@ Error MetalClawReader::field(const char*, T *val, std::size_t valLen) { oxReturnError(len.error); // read the list - if (valLen >= len) { + if (valLen >= len.value) { auto reader = child(); - reader.setTypeInfo("List", len); - for (std::size_t i = 0; i < len; i++) { + reader.setTypeInfo("List", len.value); + for (std::size_t i = 0; i < len.value; i++) { err |= reader.field("", &val[i]); } } else { diff --git a/deps/ox/src/ox/std/error.hpp b/deps/ox/src/ox/std/error.hpp index aed7b914..300e3f6d 100644 --- a/deps/ox/src/ox/std/error.hpp +++ b/deps/ox/src/ox/std/error.hpp @@ -61,23 +61,29 @@ struct ValErr { T value; Error error; - inline constexpr ValErr() = default; - - inline constexpr ValErr(T value, Error error = OxError(0)): value(ox::move(value)), error(error) { + constexpr ValErr() noexcept: error(0) { } - inline constexpr operator const T&() const { + constexpr ValErr(T value, Error error = OxError(0)) noexcept: value(ox::move(value)), error(error) { + } + + explicit constexpr operator const T&() const noexcept { return value; } - inline constexpr operator T&() { + explicit constexpr operator T&() noexcept { return value; } - inline constexpr bool ok() const { + constexpr bool ok() const noexcept { return error == 0; } + constexpr ox::Error get(T *val) noexcept { + *val = value; + return error; + } + }; namespace error {