From b6241c46d3cb0e65f780dfe976125b97a203e0c3 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Fri, 12 Aug 2016 22:17:17 -0400 Subject: [PATCH] Remove a few more naked new in CommandManager.cpp --- src/commands/CommandManager.cpp | 30 +++++++++++------------------- src/commands/CommandManager.h | 17 +++++++++-------- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/commands/CommandManager.cpp b/src/commands/CommandManager.cpp index 7663fc443..2e5127a25 100644 --- a/src/commands/CommandManager.cpp +++ b/src/commands/CommandManager.cpp @@ -413,7 +413,6 @@ private: CommandManager::CommandManager(): mCurrentID(17000), mCurrentMenuName(COMMAND), - mCurrentMenu(NULL), mDefaultFlags(AlwaysEnabledFlag), mDefaultMask(AlwaysEnabledFlag) { @@ -442,7 +441,6 @@ void CommandManager::PurgeData() mCommandKeyHash.clear(); mCommandIDHash.clear(); - mCurrentMenu = NULL; mCurrentMenuName = COMMAND; mCurrentID = 0; } @@ -504,9 +502,7 @@ wxMenuBar * CommandManager::CurrentMenuBar() const /// void CommandManager::BeginMenu(const wxString & tName) { - wxMenu *tmpMenu = new wxMenu(); - - mCurrentMenu = tmpMenu; + mCurrentMenu = std::make_unique(); mCurrentMenuName = tName; } @@ -519,8 +515,7 @@ void CommandManager::EndMenu() // Add the menu to the menubar after all menu items have been // added to the menu to allow OSX to rearrange special menu // items like Preferences, About, and Quit. - CurrentMenuBar()->Append(mCurrentMenu, mCurrentMenuName); - mCurrentMenu = NULL; + CurrentMenuBar()->Append(mCurrentMenu.release(), mCurrentMenuName); mCurrentMenuName = COMMAND; } @@ -530,14 +525,10 @@ void CommandManager::EndMenu() /// the function's argument. wxMenu* CommandManager::BeginSubMenu(const wxString & tName) { - const auto result = new wxMenu{}; -#ifdef __AUDACITY_OLD_STD__ - mSubMenuList.push_back(SubMenuListEntry{ tName, result }); -#else - mSubMenuList.emplace_back(tName, result); -#endif + mSubMenuList.push_back + (make_movable< SubMenuListEntry > ( tName, std::make_unique() )); mbSeparatorAllowed = false; - return result; + return mSubMenuList.back()->menu.get(); } @@ -548,13 +539,14 @@ wxMenu* CommandManager::BeginSubMenu(const wxString & tName) void CommandManager::EndSubMenu() { //Save the submenu's information - SubMenuListEntry tmpSubMenu = mSubMenuList.back(); + SubMenuListEntry tmpSubMenu { std::move( *mSubMenuList.back() ) }; //Pop off the NEW submenu so CurrentMenu returns the parent of the submenu mSubMenuList.pop_back(); //Add the submenu to the current menu - CurrentMenu()->Append(0, tmpSubMenu.name, tmpSubMenu.menu, tmpSubMenu.name); + CurrentMenu()->Append + (0, tmpSubMenu.name, tmpSubMenu.menu.release(), tmpSubMenu.name); mbSeparatorAllowed = true; } @@ -567,7 +559,7 @@ wxMenu * CommandManager::CurrentSubMenu() const if(mSubMenuList.empty()) return NULL; - return mSubMenuList.back().menu; + return mSubMenuList.back()->menu.get(); } /// @@ -583,7 +575,7 @@ wxMenu * CommandManager::CurrentMenu() const if(!tmpCurrentSubMenu) { - return mCurrentMenu; + return mCurrentMenu.get(); } return tmpCurrentSubMenu; @@ -834,7 +826,7 @@ CommandListEntry *CommandManager::NewIdentifier(const wxString & name, wxString labelPrefix; if (!mSubMenuList.empty()) { - labelPrefix = mSubMenuList.back().name; + labelPrefix = mSubMenuList.back()->name; } // wxMac 2.5 and higher will do special things with the diff --git a/src/commands/CommandManager.h b/src/commands/CommandManager.h index cbecef758..ae0460052 100644 --- a/src/commands/CommandManager.h +++ b/src/commands/CommandManager.h @@ -40,12 +40,14 @@ struct MenuBarListEntry struct SubMenuListEntry { - SubMenuListEntry(const wxString &name_, wxMenu *menu_) - : name(name_), menu(menu_) + SubMenuListEntry(const wxString &name_, std::unique_ptr &&menu_) + : name(name_), menu( std::move(menu_) ) {} + // SubMenuListEntry( SubMenuListEntry&& ) = default; + wxString name; - wxMenu *menu; + std::unique_ptr menu; }; struct CommandListEntry @@ -71,7 +73,9 @@ struct CommandListEntry }; using MenuBarList = std::vector < MenuBarListEntry >; -using SubMenuList = std::vector < SubMenuListEntry >; + +// to do: remove the extra indirection when Mac compiler moves to newer version +using SubMenuList = std::vector < movable_ptr >; // This is an array of pointers, not structures, because the hash maps also point to them, // so we don't want the structures to relocate with vector operations. @@ -109,9 +113,6 @@ class AUDACITY_DLL_API CommandManager final : public XMLTagHandler wxMenu* BeginSubMenu(const wxString & tName); void EndSubMenu(); - void SetToMenu( wxMenu * menu ){ - mCurrentMenu = menu; - }; void InsertItem(const wxString & name, const wxString & label, @@ -311,7 +312,7 @@ private: bool mbSeparatorAllowed; // false at the start of a menu and immediately after a separator. wxString mCurrentMenuName; - wxMenu * mCurrentMenu; + std::unique_ptr mCurrentMenu; CommandFlag mDefaultFlags; CommandMask mDefaultMask;