From 3ff4a593731ba5df067aa8a2547835731c3e2edb Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 16 Apr 2020 23:34:08 -0500 Subject: [PATCH] [ox/fs] Make FileAddress model use builtin union support --- deps/ox/src/ox/fs/filesystem/filelocation.hpp | 72 +++---------------- 1 file changed, 9 insertions(+), 63 deletions(-) diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.hpp b/deps/ox/src/ox/fs/filesystem/filelocation.hpp index cac98c0f..cef93d64 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.hpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.hpp @@ -23,10 +23,7 @@ enum class FileAddressType: int8_t { class FileAddress { template - friend ox::Error modelRead(T*, FileAddress*); - - template - friend ox::Error modelWrite(T*, FileAddress*); + friend ox::Error model(T*, FileAddress*); public: static constexpr auto TypeName = "ox::FileAddress"; @@ -34,6 +31,8 @@ class FileAddress { protected: union Data { + static constexpr auto TypeName = "ox::FileAddress::Data"; + static constexpr auto Fields = 3; char *path; const char *constPath; uint64_t inode; @@ -95,72 +94,19 @@ class FileAddress { }; template -ox::Error modelRead(T *io, FileAddress *fa) { - io->template setTypeInfo(); - decltype(fa->m_data.inode) inode = 0; - const auto strSize = io->stringLength("path") + 1; - auto path = new char[strSize]; - oxReturnError(io->field("path", SerStr(path, strSize - 1))); - oxReturnError(io->field("inode", &inode)); - if (strSize) { - fa->m_data.path = path; - fa->m_type = FileAddressType::Path; - } else { - fa->m_data.inode = inode; - fa->m_type = FileAddressType::Inode; - delete[] path; - } - return OxError(0); -} - -template -ox::Error modelWrite(T *io, FileAddress *fa) { - io->template setTypeInfo(); - switch (fa->m_type) { - case FileAddressType::Path: - case FileAddressType::ConstPath: - { - decltype(fa->m_data.inode) blank = 0; - const auto strSize = ox_strlen(fa->m_data.constPath) + 1; - auto path = ox_malloca(strSize, char, 0); - memcpy(path.get(), fa->m_data.constPath, strSize); - oxReturnError(io->field("path", SerStr(path.get(), strSize - 1))); - oxReturnError(io->field("inode", &blank)); - break; - } - case FileAddressType::Inode: - { - char blankPath[1] = ""; - oxReturnError(io->field("path", SerStr(blankPath, 0))); - oxReturnError(io->field("inode", &fa->m_data.inode)); - break; - } - case FileAddressType::None: - { - char blankPath[1] = ""; - decltype(fa->m_data.inode) blankInode = 0; - oxReturnError(io->field("path", SerStr(blankPath, 0))); - oxReturnError(io->field("inode", &blankInode)); - break; - } - } - return OxError(0); -} - -template -ox::Error modelWriteDefinition(T *io, FileAddress::Data *obj) { +ox::Error model(T *io, FileAddress::Data *obj) { io->template setTypeInfo(); - oxReturnError(io->field("path", &obj->path)); - oxReturnError(io->field("constPath", &obj->constPath)); + oxReturnError(io->field("path", SerStr(&obj->path))); + oxReturnError(io->field("constPath", SerStr(&obj->path))); oxReturnError(io->field("inode", &obj->inode)); return OxError(0); } template -ox::Error modelWriteDefinition(T *io, FileAddress *fa) { +ox::Error model(T *io, FileAddress *fa) { io->template setTypeInfo(); - oxReturnError(io->field("type", &fa->m_type)); - oxReturnError(io->field("data", UnionView(&fa->m_data, fa->m_type))); + oxReturnError(io->field("type", bit_cast(&fa->m_type))); + oxReturnError(io->field("data", UnionView(&fa->m_data, static_cast(fa->m_type)))); return OxError(0); }