From 79a1a6f8963b02feb675ae91d6ec156dfc907d7c Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 27 Oct 2019 16:22:37 -0500 Subject: [PATCH] [ox/fs] Add modelWrite to FileAddress and add copy constructor and assignment --- deps/ox/src/ox/fs/filesystem/filelocation.cpp | 29 ++++++++++++++++- deps/ox/src/ox/fs/filesystem/filelocation.hpp | 32 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.cpp b/deps/ox/src/ox/fs/filesystem/filelocation.cpp index 44cd279c..e3d82d60 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.cpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.cpp @@ -15,6 +15,13 @@ FileAddress::FileAddress() { m_type = FileAddressType::Inode; } +FileAddress::FileAddress(const FileAddress &other) { + operator=(other); +} + +FileAddress::FileAddress(std::nullptr_t) { +} + FileAddress::FileAddress(uint64_t inode) { m_data.inode = inode; m_type = FileAddressType::Inode; @@ -32,9 +39,29 @@ FileAddress::FileAddress(const char *path) { FileAddress::~FileAddress() { if (m_type == FileAddressType::Path) { - delete m_data.path; + delete[] m_data.path; m_data.path = nullptr; } } +const FileAddress &FileAddress::operator=(const FileAddress &other) { + m_type = other.m_type; + switch (m_type) { + case FileAddressType::Path: + { + auto strSize = ox_strlen(other.m_data.path) + 1; + m_data.path = new char[strSize]; + ox_memcpy(m_data.path, other.m_data.path, strSize); + break; + } + case FileAddressType::ConstPath: + case FileAddressType::Inode: + m_data = other.m_data; + break; + case FileAddressType::None: + break; + } + return *this; +} + } diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.hpp b/deps/ox/src/ox/fs/filesystem/filelocation.hpp index 488a13dd..048d6eda 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.hpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.hpp @@ -22,6 +22,9 @@ enum class FileAddressType: int8_t { class FileAddress { + template + friend ox::Error modelRead(T*, FileAddress*); + template friend ox::Error modelWrite(T*, FileAddress*); @@ -39,6 +42,10 @@ class FileAddress { public: FileAddress(); + FileAddress(const FileAddress &other); + + FileAddress(std::nullptr_t); + FileAddress(uint64_t inode); FileAddress(char *path); @@ -47,6 +54,8 @@ class FileAddress { ~FileAddress(); + const FileAddress &operator=(const FileAddress &other); + [[nodiscard]] constexpr FileAddressType type() const noexcept { switch (m_type) { case FileAddressType::Path: @@ -77,8 +86,31 @@ class FileAddress { } } + operator bool() const { + return m_type != FileAddressType::None; + } + }; +template +ox::Error modelRead(T *io, FileAddress *fa) { + io->setTypeInfo("ox::FileAddress", FileAddress::Fields); + decltype(fa->m_data.inode) inode = 0; + const auto strSize = io->stringLength() + 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->setTypeInfo("ox::FileAddress", FileAddress::Fields);