[ox/fs] Add modelWrite to FileAddress and add copy constructor and assignment

This commit is contained in:
Gary Talent 2019-10-27 16:22:37 -05:00
parent 656039e011
commit 79a1a6f896
2 changed files with 60 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -22,6 +22,9 @@ enum class FileAddressType: int8_t {
class FileAddress {
template<typename T>
friend ox::Error modelRead(T*, FileAddress*);
template<typename T>
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<typename T>
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<typename T>
ox::Error modelWrite(T *io, FileAddress *fa) {
io->setTypeInfo("ox::FileAddress", FileAddress::Fields);