1
0
mirror of https://github.com/cookiengineer/audacity synced 2026-04-24 06:54:14 +02:00

Use weak pointers to simplify MenuCreator

This commit is contained in:
Paul Licameli
2018-10-18 14:50:06 -04:00
parent c22c7b6fce
commit 9ad88e091c
4 changed files with 43 additions and 41 deletions

View File

@@ -101,6 +101,8 @@ size_t FileHistory::GetCount()
void FileHistory::UseMenu(wxMenu *menu)
{
Compress();
auto end = mMenus.end();
auto iter = std::find(mMenus.begin(), end, menu);
auto found = (iter != end);
@@ -112,19 +114,6 @@ void FileHistory::UseMenu(wxMenu *menu)
}
}
void FileHistory::RemoveMenu(wxMenu *menu)
{
auto end = mMenus.end();
auto iter = std::find(mMenus.begin(), end, menu);
auto found = (iter != end);
if (found)
mMenus.erase(iter);
else {
wxASSERT(false);
}
}
void FileHistory::Load(wxConfigBase & config, const wxString & group)
{
mHistory.Clear();
@@ -161,8 +150,10 @@ void FileHistory::Save(wxConfigBase & config, const wxString & group)
void FileHistory::AddFilesToMenu()
{
Compress();
for (auto pMenu : mMenus)
AddFilesToMenu(pMenu);
if (pMenu)
AddFilesToMenu(pMenu);
}
void FileHistory::AddFilesToMenu(wxMenu *menu)
@@ -183,3 +174,15 @@ void FileHistory::AddFilesToMenu(wxMenu *menu)
menu->Append(mIDBase, _("&Clear"));
menu->Enable(mIDBase, mHistory.GetCount() > 0);
}
void FileHistory::Compress()
{
// Clear up expired weak pointers
auto end = mMenus.end();
mMenus.erase(
std::remove_if( mMenus.begin(), end,
[](wxWeakRef<wxMenu> &pMenu){ return !pMenu; } ),
end
);
}