1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-16 16:10:06 +02:00

Remove needless indirection for more little structures, in CommandManager...

... Also use std::shared_ptr for functors to simplify resource management
This commit is contained in:
Paul Licameli 2016-02-18 19:09:10 -05:00
parent 74acd266e2
commit e0c88b1e53
3 changed files with 60 additions and 80 deletions

View File

@ -198,9 +198,9 @@ void AudacityProjectCommandFunctor::operator()(int index, const wxEvent * evt)
(mProject->*(mCommandFunction)) (); (mProject->*(mCommandFunction)) ();
} }
#define FN(X) new AudacityProjectCommandFunctor(this, &AudacityProject:: X ) #define FN(X) CommandFunctorPointer{safenew AudacityProjectCommandFunctor(this, &AudacityProject:: X )}
#define FNI(X, I) new AudacityProjectCommandFunctor(this, &AudacityProject:: X, I) #define FNI(X, I) CommandFunctorPointer{safenew AudacityProjectCommandFunctor(this, &AudacityProject:: X, I)}
#define FNS(X, S) new AudacityProjectCommandFunctor(this, &AudacityProject:: X, S) #define FNS(X, S) CommandFunctorPointer{safenew AudacityProjectCommandFunctor(this, &AudacityProject:: X, S)}
// //
// Effects menu arrays // Effects menu arrays

View File

@ -413,29 +413,12 @@ CommandManager::~CommandManager()
void CommandManager::PurgeData() void CommandManager::PurgeData()
{ {
// Delete callback functors BEFORE clearing mCommandList!
// These are the items created within 'FN()'
size_t i;
CommandFunctor * pCallback = NULL;
for(i=0; i<mCommandList.GetCount(); i++)
{
CommandListEntry *tmpEntry = mCommandList[i];
// JKC: We only want to DELETE each callbacks once.
// AddItemList() may have inserted the same callback
// several times over.
if( tmpEntry->callback != pCallback )
{
pCallback = tmpEntry->callback;
delete pCallback;
}
}
// mCommandList contains pointers to CommandListEntrys // mCommandList contains pointers to CommandListEntrys
// mMenuBarList contains pointers to MenuBarListEntrys. // mMenuBarList contains MenuBarListEntrys.
// mSubMenuList contains pointers to SubMenuListEntrys // mSubMenuList contains SubMenuListEntrys
WX_CLEAR_ARRAY( mCommandList ); WX_CLEAR_ARRAY( mCommandList );
WX_CLEAR_ARRAY( mMenuBarList ); mMenuBarList.clear();
WX_CLEAR_ARRAY( mSubMenuList ); mSubMenuList.clear();
mCommandNameHash.clear(); mCommandNameHash.clear();
mCommandKeyHash.clear(); mCommandKeyHash.clear();
@ -458,14 +441,10 @@ wxMenuBar *CommandManager::AddMenuBar(const wxString & sMenu)
if (menuBar) if (menuBar)
return menuBar; return menuBar;
MenuBarListEntry *tmpEntry = new MenuBarListEntry; const auto result = new wxMenuBar{};
mMenuBarList.emplace_back(sMenu, result);
tmpEntry->menubar = new wxMenuBar(); return result;
tmpEntry->name = sMenu;
mMenuBarList.Add(tmpEntry);
return tmpEntry->menubar;
} }
@ -474,10 +453,10 @@ wxMenuBar *CommandManager::AddMenuBar(const wxString & sMenu)
/// ///
wxMenuBar * CommandManager::GetMenuBar(const wxString & sMenu) const wxMenuBar * CommandManager::GetMenuBar(const wxString & sMenu) const
{ {
for(unsigned int i = 0; i < mMenuBarList.GetCount(); i++) for (const auto &entry : mMenuBarList)
{ {
if(!mMenuBarList[i]->name.Cmp(sMenu)) if(!entry.name.Cmp(sMenu))
return mMenuBarList[i]->menubar; return entry.menubar;
} }
return NULL; return NULL;
@ -489,10 +468,10 @@ wxMenuBar * CommandManager::GetMenuBar(const wxString & sMenu) const
/// last on in the mMenuBarList. /// last on in the mMenuBarList.
wxMenuBar * CommandManager::CurrentMenuBar() const wxMenuBar * CommandManager::CurrentMenuBar() const
{ {
if(mMenuBarList.IsEmpty()) if(mMenuBarList.empty())
return NULL; return NULL;
return mMenuBarList[mMenuBarList.GetCount()-1]->menubar; return mMenuBarList.back().menubar;
} }
@ -527,15 +506,10 @@ void CommandManager::EndMenu()
/// the function's argument. /// the function's argument.
wxMenu* CommandManager::BeginSubMenu(const wxString & tName) wxMenu* CommandManager::BeginSubMenu(const wxString & tName)
{ {
SubMenuListEntry *tmpEntry = new SubMenuListEntry; const auto result = new wxMenu{};
mSubMenuList.emplace_back(tName, result);
tmpEntry->menu = new wxMenu();
tmpEntry->name = tName;
mSubMenuList.Add(tmpEntry);
mbSeparatorAllowed = false; mbSeparatorAllowed = false;
return result;
return(tmpEntry->menu);
} }
@ -545,19 +519,15 @@ wxMenu* CommandManager::BeginSubMenu(const wxString & tName)
/// after BeginSubMenu() is called but before EndSubMenu() is called. /// after BeginSubMenu() is called but before EndSubMenu() is called.
void CommandManager::EndSubMenu() void CommandManager::EndSubMenu()
{ {
size_t submenu_count = mSubMenuList.GetCount()-1;
//Save the submenu's information //Save the submenu's information
SubMenuListEntry *tmpSubMenu = mSubMenuList[submenu_count]; SubMenuListEntry tmpSubMenu = mSubMenuList.back();
//Pop off the NEW submenu so CurrentMenu returns the parent of the submenu //Pop off the NEW submenu so CurrentMenu returns the parent of the submenu
mSubMenuList.RemoveAt(submenu_count); mSubMenuList.pop_back();
//Add the submenu to the current menu //Add the submenu to the current menu
CurrentMenu()->Append(0, tmpSubMenu->name, tmpSubMenu->menu, tmpSubMenu->name); CurrentMenu()->Append(0, tmpSubMenu.name, tmpSubMenu.menu, tmpSubMenu.name);
mbSeparatorAllowed = true; mbSeparatorAllowed = true;
delete tmpSubMenu;
} }
@ -566,10 +536,10 @@ void CommandManager::EndSubMenu()
/// end of the mSubMenuList (or NULL, if it doesn't exist). /// end of the mSubMenuList (or NULL, if it doesn't exist).
wxMenu * CommandManager::CurrentSubMenu() const wxMenu * CommandManager::CurrentSubMenu() const
{ {
if(mSubMenuList.IsEmpty()) if(mSubMenuList.empty())
return NULL; return NULL;
return mSubMenuList[mSubMenuList.GetCount()-1]->menu; return mSubMenuList.back().menu;
} }
/// ///
@ -596,7 +566,7 @@ wxMenu * CommandManager::CurrentMenu() const
/// given functor will be called /// given functor will be called
void CommandManager::InsertItem(const wxString & name, void CommandManager::InsertItem(const wxString & name,
const wxString & label_in, const wxString & label_in,
CommandFunctor *callback, const CommandFunctorPointer &callback,
const wxString & after, const wxString & after,
int checkmark) int checkmark)
{ {
@ -665,7 +635,7 @@ void CommandManager::InsertItem(const wxString & name,
void CommandManager::AddCheck(const wxChar *name, void CommandManager::AddCheck(const wxChar *name,
const wxChar *label, const wxChar *label,
CommandFunctor *callback, const CommandFunctorPointer &callback,
int checkmark) int checkmark)
{ {
AddItem(name, label, callback, wxT(""), (unsigned int)NoFlagsSpecifed, (unsigned int)NoFlagsSpecifed, checkmark); AddItem(name, label, callback, wxT(""), (unsigned int)NoFlagsSpecifed, (unsigned int)NoFlagsSpecifed, checkmark);
@ -673,7 +643,7 @@ void CommandManager::AddCheck(const wxChar *name,
void CommandManager::AddCheck(const wxChar *name, void CommandManager::AddCheck(const wxChar *name,
const wxChar *label, const wxChar *label,
CommandFunctor *callback, const CommandFunctorPointer &callback,
int checkmark, int checkmark,
unsigned int flags, unsigned int flags,
unsigned int mask) unsigned int mask)
@ -683,7 +653,7 @@ void CommandManager::AddCheck(const wxChar *name,
void CommandManager::AddItem(const wxChar *name, void CommandManager::AddItem(const wxChar *name,
const wxChar *label, const wxChar *label,
CommandFunctor *callback, const CommandFunctorPointer &callback,
unsigned int flags, unsigned int flags,
unsigned int mask) unsigned int mask)
{ {
@ -692,7 +662,7 @@ void CommandManager::AddItem(const wxChar *name,
void CommandManager::AddItem(const wxChar *name, void CommandManager::AddItem(const wxChar *name,
const wxChar *label_in, const wxChar *label_in,
CommandFunctor *callback, const CommandFunctorPointer &callback,
const wxChar *accel, const wxChar *accel,
unsigned int flags, unsigned int flags,
unsigned int mask, unsigned int mask,
@ -726,7 +696,7 @@ void CommandManager::AddItem(const wxChar *name,
/// all of the items at once. /// all of the items at once.
void CommandManager::AddItemList(const wxString & name, void CommandManager::AddItemList(const wxString & name,
const wxArrayString & labels, const wxArrayString & labels,
CommandFunctor *callback) const CommandFunctorPointer &callback)
{ {
for (size_t i = 0, cnt = labels.GetCount(); i < cnt; i++) { for (size_t i = 0, cnt = labels.GetCount(); i < cnt; i++) {
CommandListEntry *entry = NewIdentifier(name, CommandListEntry *entry = NewIdentifier(name,
@ -746,7 +716,7 @@ void CommandManager::AddItemList(const wxString & name,
/// given function pointer will be called (via the CommandManagerListener) /// given function pointer will be called (via the CommandManagerListener)
void CommandManager::AddCommand(const wxChar *name, void CommandManager::AddCommand(const wxChar *name,
const wxChar *label, const wxChar *label,
CommandFunctor *callback, const CommandFunctorPointer &callback,
unsigned int flags, unsigned int flags,
unsigned int mask) unsigned int mask)
{ {
@ -755,7 +725,7 @@ void CommandManager::AddCommand(const wxChar *name,
void CommandManager::AddCommand(const wxChar *name, void CommandManager::AddCommand(const wxChar *name,
const wxChar *label_in, const wxChar *label_in,
CommandFunctor *callback, const CommandFunctorPointer &callback,
const wxChar *accel, const wxChar *accel,
unsigned int flags, unsigned int flags,
unsigned int mask) unsigned int mask)
@ -769,7 +739,7 @@ void CommandManager::AddCommand(const wxChar *name,
void CommandManager::AddGlobalCommand(const wxChar *name, void CommandManager::AddGlobalCommand(const wxChar *name,
const wxChar *label_in, const wxChar *label_in,
CommandFunctor *callback, const CommandFunctorPointer &callback,
const wxChar *accel) const wxChar *accel)
{ {
CommandListEntry *entry = NewIdentifier(name, label_in, accel, NULL, callback, false, 0, 0); CommandListEntry *entry = NewIdentifier(name, label_in, accel, NULL, callback, false, 0, 0);
@ -806,7 +776,7 @@ int CommandManager::NextIdentifier(int ID)
CommandListEntry *CommandManager::NewIdentifier(const wxString & name, CommandListEntry *CommandManager::NewIdentifier(const wxString & name,
const wxString & label, const wxString & label,
wxMenu *menu, wxMenu *menu,
CommandFunctor *callback, const CommandFunctorPointer &callback,
bool multi, bool multi,
int index, int index,
int count) int count)
@ -825,7 +795,7 @@ CommandListEntry *CommandManager::NewIdentifier(const wxString & name,
const wxString & label, const wxString & label,
const wxString & accel, const wxString & accel,
wxMenu *menu, wxMenu *menu,
CommandFunctor *callback, const CommandFunctorPointer &callback,
bool multi, bool multi,
int index, int index,
int count) int count)
@ -833,8 +803,8 @@ CommandListEntry *CommandManager::NewIdentifier(const wxString & name,
CommandListEntry *entry = new CommandListEntry; CommandListEntry *entry = new CommandListEntry;
wxString labelPrefix; wxString labelPrefix;
if (!mSubMenuList.IsEmpty()) { if (!mSubMenuList.empty()) {
labelPrefix = mSubMenuList[mSubMenuList.GetCount() - 1]->name; labelPrefix = mSubMenuList.back().name;
} }
// wxMac 2.5 and higher will do special things with the // wxMac 2.5 and higher will do special things with the

View File

@ -34,16 +34,26 @@ public:
struct MenuBarListEntry struct MenuBarListEntry
{ {
MenuBarListEntry(const wxString &name_, wxMenuBar *menubar_)
: name(name_), menubar(menubar_)
{}
wxString name; wxString name;
wxMenuBar *menubar; wxMenuBar *menubar;
}; };
struct SubMenuListEntry struct SubMenuListEntry
{ {
SubMenuListEntry(const wxString &name_, wxMenu *menu_)
: name(name_), menu(menu_)
{}
wxString name; wxString name;
wxMenu *menu; wxMenu *menu;
}; };
using CommandFunctorPointer = std::shared_ptr <CommandFunctor>;
struct CommandListEntry struct CommandListEntry
{ {
int id; int id;
@ -54,7 +64,7 @@ struct CommandListEntry
wxString labelPrefix; wxString labelPrefix;
wxString labelTop; wxString labelTop;
wxMenu *menu; wxMenu *menu;
CommandFunctor *callback; CommandFunctorPointer callback;
bool multi; bool multi;
int index; int index;
int count; int count;
@ -66,8 +76,8 @@ struct CommandListEntry
wxUint32 mask; wxUint32 mask;
}; };
WX_DEFINE_USER_EXPORTED_ARRAY(MenuBarListEntry *, MenuBarList, class AUDACITY_DLL_API); using MenuBarList = std::vector < MenuBarListEntry >;
WX_DEFINE_USER_EXPORTED_ARRAY(SubMenuListEntry *, SubMenuList, class AUDACITY_DLL_API); using SubMenuList = std::vector < SubMenuListEntry >;
WX_DEFINE_USER_EXPORTED_ARRAY(CommandListEntry *, CommandList, class AUDACITY_DLL_API); WX_DEFINE_USER_EXPORTED_ARRAY(CommandListEntry *, CommandList, class AUDACITY_DLL_API);
WX_DECLARE_STRING_HASH_MAP_WITH_DECL(CommandListEntry *, CommandNameHash, class AUDACITY_DLL_API); WX_DECLARE_STRING_HASH_MAP_WITH_DECL(CommandListEntry *, CommandNameHash, class AUDACITY_DLL_API);
@ -105,35 +115,35 @@ class AUDACITY_DLL_API CommandManager: public XMLTagHandler
void InsertItem(const wxString & name, void InsertItem(const wxString & name,
const wxString & label, const wxString & label,
CommandFunctor *callback, const CommandFunctorPointer &callback,
const wxString & after, const wxString & after,
int checkmark = -1); int checkmark = -1);
void AddItemList(const wxString & name, void AddItemList(const wxString & name,
const wxArrayString & labels, const wxArrayString & labels,
CommandFunctor *callback); const CommandFunctorPointer &callback);
void AddCheck(const wxChar *name, void AddCheck(const wxChar *name,
const wxChar *label, const wxChar *label,
CommandFunctor *callback, const CommandFunctorPointer &callback,
int checkmark = 0); int checkmark = 0);
void AddCheck(const wxChar *name, void AddCheck(const wxChar *name,
const wxChar *label, const wxChar *label,
CommandFunctor *callback, const CommandFunctorPointer &callback,
int checkmark, int checkmark,
unsigned int flags, unsigned int flags,
unsigned int mask); unsigned int mask);
void AddItem(const wxChar *name, void AddItem(const wxChar *name,
const wxChar *label, const wxChar *label,
CommandFunctor *callback, const CommandFunctorPointer &callback,
unsigned int flags = NoFlagsSpecifed, unsigned int flags = NoFlagsSpecifed,
unsigned int mask = NoFlagsSpecifed); unsigned int mask = NoFlagsSpecifed);
void AddItem(const wxChar *name, void AddItem(const wxChar *name,
const wxChar *label_in, const wxChar *label_in,
CommandFunctor *callback, const CommandFunctorPointer &callback,
const wxChar *accel, const wxChar *accel,
unsigned int flags = NoFlagsSpecifed, unsigned int flags = NoFlagsSpecifed,
unsigned int mask = NoFlagsSpecifed, unsigned int mask = NoFlagsSpecifed,
@ -145,20 +155,20 @@ class AUDACITY_DLL_API CommandManager: public XMLTagHandler
// keyboard shortcut. // keyboard shortcut.
void AddCommand(const wxChar *name, void AddCommand(const wxChar *name,
const wxChar *label, const wxChar *label,
CommandFunctor *callback, const CommandFunctorPointer &callback,
unsigned int flags = NoFlagsSpecifed, unsigned int flags = NoFlagsSpecifed,
unsigned int mask = NoFlagsSpecifed); unsigned int mask = NoFlagsSpecifed);
void AddCommand(const wxChar *name, void AddCommand(const wxChar *name,
const wxChar *label, const wxChar *label,
CommandFunctor *callback, const CommandFunctorPointer &callback,
const wxChar *accel, const wxChar *accel,
unsigned int flags = NoFlagsSpecifed, unsigned int flags = NoFlagsSpecifed,
unsigned int mask = NoFlagsSpecifed); unsigned int mask = NoFlagsSpecifed);
void AddGlobalCommand(const wxChar *name, void AddGlobalCommand(const wxChar *name,
const wxChar *label, const wxChar *label,
CommandFunctor *callback, const CommandFunctorPointer &callback,
const wxChar *accel); const wxChar *accel);
// //
// Command masks // Command masks
@ -242,7 +252,7 @@ protected:
CommandListEntry *NewIdentifier(const wxString & name, CommandListEntry *NewIdentifier(const wxString & name,
const wxString & label, const wxString & label,
wxMenu *menu, wxMenu *menu,
CommandFunctor *callback, const CommandFunctorPointer &callback,
bool multi, bool multi,
int index, int index,
int count); int count);
@ -250,7 +260,7 @@ protected:
const wxString & label, const wxString & label,
const wxString & accel, const wxString & accel,
wxMenu *menu, wxMenu *menu,
CommandFunctor *callback, const CommandFunctorPointer &callback,
bool multi, bool multi,
int index, int index,
int count); int count);