[ox/fs] Add modelWrite to FileAddress and add copy constructor and assignment
This commit is contained in:
parent
656039e011
commit
79a1a6f896
29
deps/ox/src/ox/fs/filesystem/filelocation.cpp
vendored
29
deps/ox/src/ox/fs/filesystem/filelocation.cpp
vendored
@ -15,6 +15,13 @@ FileAddress::FileAddress() {
|
|||||||
m_type = FileAddressType::Inode;
|
m_type = FileAddressType::Inode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FileAddress::FileAddress(const FileAddress &other) {
|
||||||
|
operator=(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
FileAddress::FileAddress(std::nullptr_t) {
|
||||||
|
}
|
||||||
|
|
||||||
FileAddress::FileAddress(uint64_t inode) {
|
FileAddress::FileAddress(uint64_t inode) {
|
||||||
m_data.inode = inode;
|
m_data.inode = inode;
|
||||||
m_type = FileAddressType::Inode;
|
m_type = FileAddressType::Inode;
|
||||||
@ -32,9 +39,29 @@ FileAddress::FileAddress(const char *path) {
|
|||||||
|
|
||||||
FileAddress::~FileAddress() {
|
FileAddress::~FileAddress() {
|
||||||
if (m_type == FileAddressType::Path) {
|
if (m_type == FileAddressType::Path) {
|
||||||
delete m_data.path;
|
delete[] m_data.path;
|
||||||
m_data.path = nullptr;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
32
deps/ox/src/ox/fs/filesystem/filelocation.hpp
vendored
32
deps/ox/src/ox/fs/filesystem/filelocation.hpp
vendored
@ -22,6 +22,9 @@ enum class FileAddressType: int8_t {
|
|||||||
|
|
||||||
class FileAddress {
|
class FileAddress {
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
friend ox::Error modelRead(T*, FileAddress*);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
friend ox::Error modelWrite(T*, FileAddress*);
|
friend ox::Error modelWrite(T*, FileAddress*);
|
||||||
|
|
||||||
@ -39,6 +42,10 @@ class FileAddress {
|
|||||||
public:
|
public:
|
||||||
FileAddress();
|
FileAddress();
|
||||||
|
|
||||||
|
FileAddress(const FileAddress &other);
|
||||||
|
|
||||||
|
FileAddress(std::nullptr_t);
|
||||||
|
|
||||||
FileAddress(uint64_t inode);
|
FileAddress(uint64_t inode);
|
||||||
|
|
||||||
FileAddress(char *path);
|
FileAddress(char *path);
|
||||||
@ -47,6 +54,8 @@ class FileAddress {
|
|||||||
|
|
||||||
~FileAddress();
|
~FileAddress();
|
||||||
|
|
||||||
|
const FileAddress &operator=(const FileAddress &other);
|
||||||
|
|
||||||
[[nodiscard]] constexpr FileAddressType type() const noexcept {
|
[[nodiscard]] constexpr FileAddressType type() const noexcept {
|
||||||
switch (m_type) {
|
switch (m_type) {
|
||||||
case FileAddressType::Path:
|
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>
|
template<typename T>
|
||||||
ox::Error modelWrite(T *io, FileAddress *fa) {
|
ox::Error modelWrite(T *io, FileAddress *fa) {
|
||||||
io->setTypeInfo("ox::FileAddress", FileAddress::Fields);
|
io->setTypeInfo("ox::FileAddress", FileAddress::Fields);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user