From 492c1dd7c790472fa9d24d2456db6c0146aa7955 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 17 Apr 2023 20:07:40 +0100 Subject: [PATCH] Use std::unordered_map<> in wxArchiveFSHandler code In addition to removing wx hash map macros, also change the private wxArchiveFSEntryHash to store std::unique_ptr rather than the raw pointers to ensure they're destroyed automatically and we don't need to use WX_CLEAR_HASH_MAP() for them. --- include/wx/fs_arc.h | 5 +++-- src/common/fs_arc.cpp | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/include/wx/fs_arc.h b/include/wx/fs_arc.h index 0412cd26ef..c252f8ad8c 100644 --- a/include/wx/fs_arc.h +++ b/include/wx/fs_arc.h @@ -14,9 +14,10 @@ #if wxUSE_FS_ARCHIVE #include "wx/filesys.h" -#include "wx/hashmap.h" -WX_DECLARE_STRING_HASH_MAP(int, wxArchiveFilenameHashMap); +#include + +using wxArchiveFilenameHashMap = std::unordered_map; //--------------------------------------------------------------------------- // wxArchiveFSHandler diff --git a/src/common/fs_arc.cpp b/src/common/fs_arc.cpp index b1e2a954be..e5b2cb523b 100644 --- a/src/common/fs_arc.cpp +++ b/src/common/fs_arc.cpp @@ -32,7 +32,8 @@ // version. //--------------------------------------------------------------------------- -WX_DECLARE_STRING_HASH_MAP(wxArchiveEntry*, wxArchiveFSEntryHash); +using wxArchiveFSEntryHash = + std::unordered_map>; struct wxArchiveFSEntry { @@ -59,6 +60,7 @@ public: wxArchiveFSEntry *GetNext(wxArchiveFSEntry *fse); private: + // Takes ownership of "entry". wxArchiveFSEntry *AddToCache(wxArchiveEntry *entry); void CloseStreams(); @@ -98,8 +100,6 @@ wxArchiveFSCacheDataImpl::wxArchiveFSCacheDataImpl( wxArchiveFSCacheDataImpl::~wxArchiveFSCacheDataImpl() { - WX_CLEAR_HASH_MAP(wxArchiveFSEntryHash, m_hash); - wxArchiveFSEntry *entry = m_begin; while (entry) @@ -114,7 +114,7 @@ wxArchiveFSCacheDataImpl::~wxArchiveFSCacheDataImpl() wxArchiveFSEntry *wxArchiveFSCacheDataImpl::AddToCache(wxArchiveEntry *entry) { - m_hash[entry->GetName(wxPATH_UNIX)] = entry; + m_hash[entry->GetName(wxPATH_UNIX)] = std::unique_ptr(entry); wxArchiveFSEntry *fse = new wxArchiveFSEntry; *m_endptr = fse; (*m_endptr)->entry = entry; @@ -131,10 +131,10 @@ void wxArchiveFSCacheDataImpl::CloseStreams() wxArchiveEntry *wxArchiveFSCacheDataImpl::Get(const wxString& name) { - wxArchiveFSEntryHash::iterator it = m_hash.find(name); + const auto it = m_hash.find(name); if (it != m_hash.end()) - return it->second; + return it->second.get(); if (!m_archive) return nullptr; @@ -254,7 +254,8 @@ wxArchiveFSCacheData& wxArchiveFSCacheData::operator=( // of wxFileSystem. //--------------------------------------------------------------------------- -WX_DECLARE_STRING_HASH_MAP(wxArchiveFSCacheData, wxArchiveFSCacheDataHash); +using wxArchiveFSCacheDataHash = + std::unordered_map; class wxArchiveFSCache { @@ -289,9 +290,9 @@ wxArchiveFSCacheData* wxArchiveFSCache::Add( wxArchiveFSCacheData *wxArchiveFSCache::Get(const wxString& name) { - wxArchiveFSCacheDataHash::iterator it; + const auto it = m_hash.find(name); - if ((it = m_hash.find(name)) != m_hash.end()) + if (it != m_hash.end()) return &it->second; return nullptr;