From 0d8dc5aabc8a62bf6a6733dd87afac708c63eb24 Mon Sep 17 00:00:00 2001 From: elsid Date: Sun, 17 Dec 2023 15:20:48 +0100 Subject: [PATCH] Use string_view for VFS lookups --- apps/openmw_test_suite/testing_util.hpp | 4 ++-- components/vfs/archive.hpp | 7 +++++-- components/vfs/bsaarchive.hpp | 4 ++-- components/vfs/filesystemarchive.cpp | 10 ++++------ components/vfs/filesystemarchive.hpp | 8 +++----- components/vfs/manager.cpp | 6 +++--- components/vfs/manager.hpp | 8 ++++---- 7 files changed, 23 insertions(+), 24 deletions(-) diff --git a/apps/openmw_test_suite/testing_util.hpp b/apps/openmw_test_suite/testing_util.hpp index 89fb7f9e73..0c941053a7 100644 --- a/apps/openmw_test_suite/testing_util.hpp +++ b/apps/openmw_test_suite/testing_util.hpp @@ -57,13 +57,13 @@ namespace TestingOpenMW { } - void listResources(std::map& out) override + void listResources(VFS::FileMap& out) override { for (const auto& [key, value] : mFiles) out.emplace(VFS::Path::normalizeFilename(key), value); } - bool contains(const std::string& file) const override { return mFiles.count(file) != 0; } + bool contains(std::string_view file) const override { return mFiles.contains(file); } std::string getDescription() const override { return "TestData"; } }; diff --git a/components/vfs/archive.hpp b/components/vfs/archive.hpp index e377e8c5b6..79c876b391 100644 --- a/components/vfs/archive.hpp +++ b/components/vfs/archive.hpp @@ -3,6 +3,7 @@ #include #include +#include #include @@ -19,16 +20,18 @@ namespace VFS virtual std::filesystem::path getPath() = 0; }; + using FileMap = std::map>; + class Archive { public: virtual ~Archive() = default; /// List all resources contained in this archive. - virtual void listResources(std::map& out) = 0; + virtual void listResources(FileMap& out) = 0; /// True if this archive contains the provided normalized file. - virtual bool contains(const std::string& file) const = 0; + virtual bool contains(std::string_view file) const = 0; virtual std::string getDescription() const = 0; }; diff --git a/components/vfs/bsaarchive.hpp b/components/vfs/bsaarchive.hpp index 517c95e273..29098db45d 100644 --- a/components/vfs/bsaarchive.hpp +++ b/components/vfs/bsaarchive.hpp @@ -48,7 +48,7 @@ namespace VFS virtual ~BsaArchive() {} - void listResources(std::map& out) override + void listResources(FileMap& out) override { for (auto& resource : mResources) { @@ -59,7 +59,7 @@ namespace VFS } } - bool contains(const std::string& file) const override + bool contains(std::string_view file) const override { for (const auto& it : mResources) { diff --git a/components/vfs/filesystemarchive.cpp b/components/vfs/filesystemarchive.cpp index 96f5e87f5e..7d88dd9cc0 100644 --- a/components/vfs/filesystemarchive.cpp +++ b/components/vfs/filesystemarchive.cpp @@ -17,7 +17,7 @@ namespace VFS { } - void FileSystemArchive::listResources(std::map& out) + void FileSystemArchive::listResources(FileMap& out) { if (!mBuiltIndex) { @@ -51,14 +51,12 @@ namespace VFS } else { - for (index::iterator it = mIndex.begin(); it != mIndex.end(); ++it) - { - out[it->first] = &it->second; - } + for (auto& [k, v] : mIndex) + out[k] = &v; } } - bool FileSystemArchive::contains(const std::string& file) const + bool FileSystemArchive::contains(std::string_view file) const { return mIndex.find(file) != mIndex.end(); } diff --git a/components/vfs/filesystemarchive.hpp b/components/vfs/filesystemarchive.hpp index 39d711b327..e31ef9bd30 100644 --- a/components/vfs/filesystemarchive.hpp +++ b/components/vfs/filesystemarchive.hpp @@ -27,16 +27,14 @@ namespace VFS public: FileSystemArchive(const std::filesystem::path& path); - void listResources(std::map& out) override; + void listResources(FileMap& out) override; - bool contains(const std::string& file) const override; + bool contains(std::string_view file) const override; std::string getDescription() const override; private: - typedef std::map index; - index mIndex; - + std::map> mIndex; bool mBuiltIndex; std::filesystem::path mPath; }; diff --git a/components/vfs/manager.cpp b/components/vfs/manager.cpp index ec8939f5dc..cc231847f5 100644 --- a/components/vfs/manager.cpp +++ b/components/vfs/manager.cpp @@ -35,11 +35,11 @@ namespace VFS return getNormalized(Path::normalizeFilename(name)); } - Files::IStreamPtr Manager::getNormalized(const std::string& normalizedName) const + Files::IStreamPtr Manager::getNormalized(std::string_view normalizedName) const { - std::map::const_iterator found = mIndex.find(normalizedName); + const auto found = mIndex.find(normalizedName); if (found == mIndex.end()) - throw std::runtime_error("Resource '" + normalizedName + "' not found"); + throw std::runtime_error("Resource '" + std::string(normalizedName) + "' not found"); return found->second->open(); } diff --git a/components/vfs/manager.hpp b/components/vfs/manager.hpp index db38e4b240..76405aae2c 100644 --- a/components/vfs/manager.hpp +++ b/components/vfs/manager.hpp @@ -41,7 +41,7 @@ namespace VFS class RecursiveDirectoryIterator { public: - RecursiveDirectoryIterator(std::map::const_iterator it) + RecursiveDirectoryIterator(FileMap::const_iterator it) : mIt(it) { } @@ -55,7 +55,7 @@ namespace VFS } private: - std::map::const_iterator mIt; + FileMap::const_iterator mIt; }; using RecursiveDirectoryRange = IteratorPair; @@ -83,7 +83,7 @@ namespace VFS /// Retrieve a file by name (name is already normalized). /// @note Throws an exception if the file can not be found. /// @note May be called from any thread once the index has been built. - Files::IStreamPtr getNormalized(const std::string& normalizedName) const; + Files::IStreamPtr getNormalized(std::string_view normalizedName) const; std::string getArchive(std::string_view name) const; @@ -101,7 +101,7 @@ namespace VFS private: std::vector> mArchives; - std::map mIndex; + FileMap mIndex; }; }