From 070f778dcd471dc30af791d2cda492aff93b968c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 17 Apr 2023 20:36:21 +0100 Subject: [PATCH] Use std::unordered_map<> in wxFileSystemWatcher code Use the standard containers directly instead of defining a typedef for it using wx macros. --- include/wx/fswatcher.h | 5 +++-- include/wx/private/fswatcher.h | 8 +++++--- src/osx/fswatcher_fsevents.cpp | 31 ++++++++++++++++--------------- src/unix/fswatcher_inotify.cpp | 8 ++++---- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/include/wx/fswatcher.h b/include/wx/fswatcher.h index b28d2f2d74..b696d74f44 100644 --- a/include/wx/fswatcher.h +++ b/include/wx/fswatcher.h @@ -19,7 +19,8 @@ #include "wx/evtloop.h" #include "wx/filename.h" #include "wx/dir.h" -#include "wx/hashmap.h" + +#include #define wxTRACE_FSWATCHER "fswatcher" @@ -279,7 +280,7 @@ protected: int m_refcount; }; -WX_DECLARE_STRING_HASH_MAP(wxFSWatchInfo, wxFSWatchInfoMap); +using wxFSWatchInfoMap = std::unordered_map; /** * Encapsulation of platform-specific file system event mechanism diff --git a/include/wx/private/fswatcher.h b/include/wx/private/fswatcher.h index d3602f7861..d6de03b9d7 100644 --- a/include/wx/private/fswatcher.h +++ b/include/wx/private/fswatcher.h @@ -12,20 +12,22 @@ #include "wx/sharedptr.h" +#include + #ifdef wxHAS_INOTIFY class wxFSWatchEntryUnix; #define wxFSWatchEntry wxFSWatchEntryUnix - WX_DECLARE_STRING_HASH_MAP(wxSharedPtr,wxFSWatchEntries); + using wxFSWatchEntries = std::unordered_map>; #include "wx/unix/private/fswatcher_inotify.h" #elif defined(wxHAS_KQUEUE) class wxFSWatchEntryKq; #define wxFSWatchEntry wxFSWatchEntryKq - WX_DECLARE_STRING_HASH_MAP(wxSharedPtr,wxFSWatchEntries); + using wxFSWatchEntries = std::unordered_map>; #include "wx/unix/private/fswatcher_kqueue.h" #elif defined(__WINDOWS__) class wxFSWatchEntryMSW; #define wxFSWatchEntry wxFSWatchEntryMSW - WX_DECLARE_STRING_HASH_MAP(wxSharedPtr,wxFSWatchEntries); + using wxFSWatchEntries = std::unordered_map>; #include "wx/msw/private/fswatcher.h" #else #define wxFSWatchEntry wxFSWatchEntryPolling diff --git a/src/osx/fswatcher_fsevents.cpp b/src/osx/fswatcher_fsevents.cpp index cea11ade62..8cde8c6b5b 100644 --- a/src/osx/fswatcher_fsevents.cpp +++ b/src/osx/fswatcher_fsevents.cpp @@ -21,6 +21,8 @@ #include #include +#include + // A small class that we will give the FSEvents // framework, which will be forwarded to the function // that gets called when files change. @@ -275,12 +277,10 @@ static void wxDeleteContext(const void* context) delete watcherContext; } -WX_DECLARE_STRING_HASH_MAP(FSEventStreamRef, FSEventStreamRefMap); - struct wxFsEventsFileSystemWatcher::PrivateData { // map of path => FSEventStreamRef - FSEventStreamRefMap m_streams; + std::unordered_map m_streams; }; wxFsEventsFileSystemWatcher::wxFsEventsFileSystemWatcher() @@ -408,7 +408,7 @@ bool wxFsEventsFileSystemWatcher::RemoveTree(const wxFileName& path) } } - FSEventStreamRefMap::iterator it = m_pImpl->m_streams.find(canonical); + const auto it = m_pImpl->m_streams.find(canonical); bool removed = false; if ( it != m_pImpl->m_streams.end() ) { @@ -425,17 +425,18 @@ bool wxFsEventsFileSystemWatcher::RemoveAll() { // remove all watches created with Add() bool ret = wxKqueueFileSystemWatcher::RemoveAll(); - FSEventStreamRefMap::iterator it = m_pImpl->m_streams.begin(); - while ( it != m_pImpl->m_streams.end() ) + if ( m_pImpl->m_streams.empty() ) + return ret; + + for ( const auto& kv : m_pImpl->m_streams ) { - FSEventStreamStop(it->second); - FSEventStreamInvalidate(it->second); - FSEventStreamRelease(it->second); - ++it; - ret = true; + FSEventStreamRef stream = kv.second; + FSEventStreamStop(stream); + FSEventStreamInvalidate(stream); + FSEventStreamRelease(stream); } m_pImpl->m_streams.clear(); - return ret; + return true; } void wxFsEventsFileSystemWatcher::PostChange(const wxFileName& oldFileName, @@ -510,10 +511,10 @@ int wxFsEventsFileSystemWatcher::GetWatchedPaths(wxArrayString* paths) const return 0; } wxFileSystemWatcherBase::GetWatchedPaths(paths); - FSEventStreamRefMap::const_iterator it = m_pImpl->m_streams.begin(); - for ( ; it != m_pImpl->m_streams.end(); ++it ) + + for ( const auto& kv : m_pImpl->m_streams ) { - paths->push_back(it->first); + paths->push_back(kv.first); } return paths->size(); } diff --git a/src/unix/fswatcher_inotify.cpp b/src/unix/fswatcher_inotify.cpp index a0a381dfea..1e27aec4a0 100644 --- a/src/unix/fswatcher_inotify.cpp +++ b/src/unix/fswatcher_inotify.cpp @@ -21,17 +21,17 @@ #include #include "wx/private/fswatcher.h" +#include + // ============================================================================ // wxFSWatcherImpl implementation & helper wxFSWSourceHandler implementation // ============================================================================ // inotify watch descriptor => wxFSWatchEntry* map -WX_DECLARE_HASH_MAP(int, wxFSWatchEntry*, wxIntegerHash, wxIntegerEqual, - wxFSWatchEntryDescriptors); +using wxFSWatchEntryDescriptors = std::unordered_map; // inotify event cookie => inotify_event* map -WX_DECLARE_HASH_MAP(int, inotify_event*, wxIntegerHash, wxIntegerEqual, - wxInotifyCookies); +using wxInotifyCookies = std::unordered_map; /** * Helper class encapsulating inotify mechanism