diff --git a/include/wx/fs_mem.h b/include/wx/fs_mem.h index 9e6e126bb2..6a40ae1a7b 100644 --- a/include/wx/fs_mem.h +++ b/include/wx/fs_mem.h @@ -15,10 +15,10 @@ #include "wx/filesys.h" -#include "wx/hashmap.h" +#include +#include class wxMemoryFSFile; -WX_DECLARE_STRING_HASH_MAP(wxMemoryFSFile *, wxMemoryFSHash); #if wxUSE_GUI #include "wx/bitmap.h" @@ -59,7 +59,13 @@ protected: // error and returns false if it does exist static bool CheckDoesntExist(const wxString& filename); + // add the given object to m_Hash, taking ownership of the pointer + static void DoAddFile(const wxString& filename, wxMemoryFSFile* file); + +private: // the hash map indexed by the names of the files stored in the memory FS + using wxMemoryFSHash = + std::unordered_map>; static wxMemoryFSHash m_Hash; // the file name currently being searched for, i.e. the argument of the diff --git a/src/common/fs_mem.cpp b/src/common/fs_mem.cpp index f4e943b941..08fbed97c1 100644 --- a/src/common/fs_mem.cpp +++ b/src/common/fs_mem.cpp @@ -77,7 +77,7 @@ private: //-------------------------------------------------------------------------------- -wxMemoryFSHash wxMemoryFSHandlerBase::m_Hash; +wxMemoryFSHandlerBase::wxMemoryFSHash wxMemoryFSHandlerBase::m_Hash; wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler() @@ -89,7 +89,6 @@ wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase() // as only one copy of FS handler is supposed to exist, we may silently // delete static data here. (There is no way how to remove FS handler from // wxFileSystem other than releasing _all_ handlers.) - WX_CLEAR_HASH_MAP(wxMemoryFSHash, m_Hash); } bool wxMemoryFSHandlerBase::CanOpen(const wxString& location) @@ -104,7 +103,7 @@ wxFSFile * wxMemoryFSHandlerBase::OpenFile(wxFileSystem& WXUNUSED(fs), if ( i == m_Hash.end() ) return nullptr; - const wxMemoryFSFile * const obj = i->second; + const auto& obj = i->second; return new wxFSFile ( @@ -175,6 +174,13 @@ bool wxMemoryFSHandlerBase::CheckDoesntExist(const wxString& filename) } +/*static*/ +void wxMemoryFSHandlerBase::DoAddFile(const wxString& filename, + wxMemoryFSFile* file) +{ + m_Hash[filename] = std::unique_ptr(file); +} + /*static*/ void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString& filename, const wxString& textdata, @@ -199,7 +205,7 @@ void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString& filename, if ( !CheckDoesntExist(filename) ) return; - m_Hash[filename] = new wxMemoryFSFile(binarydata, size, mimetype); + DoAddFile(filename, new wxMemoryFSFile(binarydata, size, mimetype)); } /*static*/ @@ -221,17 +227,12 @@ void wxMemoryFSHandlerBase::AddFile(const wxString& filename, /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString& filename) { - wxMemoryFSHash::iterator i = m_Hash.find(filename); - if ( i == m_Hash.end() ) + if ( !m_Hash.erase(filename) ) { wxLogError(_("Trying to remove file '%s' from memory VFS, " "but it is not loaded!"), filename); - return; } - - delete i->second; - m_Hash.erase(i); } #endif // wxUSE_BASE @@ -250,11 +251,11 @@ wxMemoryFSHandler::AddFile(const wxString& filename, wxMemoryOutputStream mems; if ( image.IsOk() && image.SaveFile(mems, type) ) { - m_Hash[filename] = new wxMemoryFSFile - ( + DoAddFile(filename, new wxMemoryFSFile + ( mems, wxImage::FindHandler(type)->GetMimeType() - ); + )); } else {