[ox/fs] Add move methods to FileAddress and fix copy operator to cleanup

This commit is contained in:
Gary Talent 2021-05-03 11:39:29 -04:00
parent a84eb013c9
commit 8f53dda1e2
2 changed files with 45 additions and 4 deletions

View File

@ -6,6 +6,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/ */
#include <ox/model/modelops.hpp>
#include "filelocation.hpp" #include "filelocation.hpp"
namespace ox { namespace ox {
@ -19,6 +21,10 @@ FileAddress::FileAddress(const FileAddress &other) noexcept {
operator=(other); operator=(other);
} }
FileAddress::FileAddress(FileAddress &&other) noexcept {
operator=(move(other));
}
FileAddress::FileAddress(std::nullptr_t) noexcept { FileAddress::FileAddress(std::nullptr_t) noexcept {
} }
@ -40,13 +46,11 @@ FileAddress::FileAddress(const char *path) noexcept {
} }
FileAddress::~FileAddress() noexcept { FileAddress::~FileAddress() noexcept {
if (m_type == FileAddressType::Path) { cleanup();
delete[] m_data.path;
m_data.path = nullptr;
}
} }
const FileAddress &FileAddress::operator=(const FileAddress &other) noexcept { const FileAddress &FileAddress::operator=(const FileAddress &other) noexcept {
cleanup();
m_type = other.m_type; m_type = other.m_type;
switch (m_type) { switch (m_type) {
case FileAddressType::Path: case FileAddressType::Path:
@ -66,4 +70,26 @@ const FileAddress &FileAddress::operator=(const FileAddress &other) noexcept {
return *this; return *this;
} }
const FileAddress &FileAddress::operator=(FileAddress &&other) noexcept {
cleanup();
m_type = other.m_type;
memcpy(this, &other, sizeof(*this));
other.clear();
return *this;
}
void FileAddress::cleanup() noexcept {
if (m_type == FileAddressType::Path) {
delete[] m_data.path;
clear();
}
}
void FileAddress::clear() noexcept {
if (m_type == FileAddressType::Path) {
m_data.path = nullptr;
m_type = FileAddressType::None;
}
}
} }

View File

@ -46,6 +46,8 @@ class FileAddress {
FileAddress(const FileAddress &other) noexcept; FileAddress(const FileAddress &other) noexcept;
FileAddress(FileAddress &&other) noexcept;
FileAddress(std::nullptr_t) noexcept; FileAddress(std::nullptr_t) noexcept;
FileAddress(uint64_t inode) noexcept; FileAddress(uint64_t inode) noexcept;
@ -58,6 +60,8 @@ class FileAddress {
const FileAddress &operator=(const FileAddress &other) noexcept; const FileAddress &operator=(const FileAddress &other) noexcept;
const FileAddress &operator=(FileAddress &&other) noexcept;
[[nodiscard]] [[nodiscard]]
constexpr FileAddressType type() const noexcept { constexpr FileAddressType type() const noexcept {
switch (m_type) { switch (m_type) {
@ -93,6 +97,17 @@ class FileAddress {
return m_type != FileAddressType::None; return m_type != FileAddressType::None;
} }
private:
/**
* Cleanup memory allocations.
*/
void cleanup() noexcept;
/**
* Clears fields, but does not delete allocations.
*/
void clear() noexcept;
}; };
template<typename T> template<typename T>