Use std::unordered_map<std::unique_ptr<>> in wxMemoryFSHandler
Replace a map of raw pointers defined using wx synonym with the direct use of the standard class using smart pointers to simplify code and make it more robust. Don't define wxMemoryFSHash in the global scope any longer, it shouldn't be used by any existing code, but if it is, it's better to prevent it from compiling immediately, as it would be broken anyhow by the change to the map value type.
This commit is contained in:
parent
1e1c970e50
commit
8bd30d2a8a
2 changed files with 22 additions and 15 deletions
|
|
@ -15,10 +15,10 @@
|
|||
|
||||
#include "wx/filesys.h"
|
||||
|
||||
#include "wx/hashmap.h"
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
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<wxString, std::unique_ptr<wxMemoryFSFile>>;
|
||||
static wxMemoryFSHash m_Hash;
|
||||
|
||||
// the file name currently being searched for, i.e. the argument of the
|
||||
|
|
|
|||
|
|
@ -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<wxMemoryFSFile>(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
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue