From 8f53dda1e2273a13b2657ace06e9809b389899d3 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 3 May 2021 11:39:29 -0400 Subject: [PATCH] [ox/fs] Add move methods to FileAddress and fix copy operator to cleanup --- deps/ox/src/ox/fs/filesystem/filelocation.cpp | 34 ++++++++++++++++--- deps/ox/src/ox/fs/filesystem/filelocation.hpp | 15 ++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.cpp b/deps/ox/src/ox/fs/filesystem/filelocation.cpp index f5793edf..2d7556d8 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.cpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.cpp @@ -6,6 +6,8 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include + #include "filelocation.hpp" namespace ox { @@ -19,6 +21,10 @@ FileAddress::FileAddress(const FileAddress &other) noexcept { operator=(other); } +FileAddress::FileAddress(FileAddress &&other) noexcept { + operator=(move(other)); +} + FileAddress::FileAddress(std::nullptr_t) noexcept { } @@ -40,13 +46,11 @@ FileAddress::FileAddress(const char *path) noexcept { } FileAddress::~FileAddress() noexcept { - if (m_type == FileAddressType::Path) { - delete[] m_data.path; - m_data.path = nullptr; - } + cleanup(); } const FileAddress &FileAddress::operator=(const FileAddress &other) noexcept { + cleanup(); m_type = other.m_type; switch (m_type) { case FileAddressType::Path: @@ -66,4 +70,26 @@ const FileAddress &FileAddress::operator=(const FileAddress &other) noexcept { 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; + } +} + } diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.hpp b/deps/ox/src/ox/fs/filesystem/filelocation.hpp index e6861c73..3d546e20 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.hpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.hpp @@ -46,6 +46,8 @@ class FileAddress { FileAddress(const FileAddress &other) noexcept; + FileAddress(FileAddress &&other) noexcept; + FileAddress(std::nullptr_t) noexcept; FileAddress(uint64_t inode) noexcept; @@ -58,6 +60,8 @@ class FileAddress { const FileAddress &operator=(const FileAddress &other) noexcept; + const FileAddress &operator=(FileAddress &&other) noexcept; + [[nodiscard]] constexpr FileAddressType type() const noexcept { switch (m_type) { @@ -93,6 +97,17 @@ class FileAddress { return m_type != FileAddressType::None; } + private: + /** + * Cleanup memory allocations. + */ + void cleanup() noexcept; + + /** + * Clears fields, but does not delete allocations. + */ + void clear() noexcept; + }; template