From 0626c2a815d0c79190d87af5e82355ecb3419d83 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 1 Feb 2024 21:02:46 -0600 Subject: [PATCH] [ox/fs] Add comparison operator for FileAddress --- deps/ox/src/ox/fs/filesystem/filelocation.cpp | 26 +++++++++++++++++++ deps/ox/src/ox/fs/filesystem/filelocation.hpp | 11 +++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.cpp b/deps/ox/src/ox/fs/filesystem/filelocation.cpp index 417b44d4..88f9ee21 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.cpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.cpp @@ -88,6 +88,32 @@ FileAddress &FileAddress::operator=(FileAddress &&other) noexcept { return *this; } +bool FileAddress::operator==(FileAddress const&other) const noexcept { + if (m_type != other.m_type) { + auto const aIsPath = + m_type == FileAddressType::Path || m_type == FileAddressType::ConstPath; + auto const bIsPath = + other.m_type == FileAddressType::Path || other.m_type == FileAddressType::ConstPath; + if (!(aIsPath && bIsPath)) { + return false; + } + } + switch (m_type) { + case FileAddressType::ConstPath: + case FileAddressType::Path: { + auto const a = getPath(); + auto const b = other.getPath(); + return (other.m_type == FileAddressType::ConstPath || other.m_type == FileAddressType::Path) + && (a.value == b.value); + } + case FileAddressType::Inode: + return m_data.inode == other.m_data.inode; + case FileAddressType::None: + return true; + } + return true; +} + bool FileAddress::operator==(CRStringView path) const noexcept { auto [p, err] = getPath(); if (err) { diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.hpp b/deps/ox/src/ox/fs/filesystem/filelocation.hpp index 587fb9bd..ded301e9 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 { Inode, }; +template +constexpr Error model(T *h, CommonPtrWith auto *fa) noexcept; + class FileAddress { template @@ -67,6 +70,8 @@ class FileAddress { FileAddress &operator=(FileAddress &&other) noexcept; + bool operator==(const FileAddress &other) const noexcept; + bool operator==(CRStringView path) const noexcept; [[nodiscard]] @@ -89,12 +94,12 @@ class FileAddress { } } - constexpr Result getPath() const noexcept { + constexpr Result getPath() const noexcept { switch (m_type) { case FileAddressType::Path: - return ox::StringView(m_data.path); + return ox::CStringView(m_data.path); case FileAddressType::ConstPath: - return ox::StringView(m_data.constPath); + return ox::CStringView(m_data.constPath); default: return OxError(1); }