From 57afa1399e39632588580fe9a0f20310b881d682 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sun, 7 Aug 2016 14:56:54 -0400 Subject: [PATCH] Remove some naked new amd delete in: commands --- src/commands/CommandDirectory.cpp | 48 ++++++++++++++----------------- src/commands/CommandDirectory.h | 2 +- src/commands/CommandHandler.cpp | 7 +++-- src/commands/CommandHandler.h | 3 +- src/commands/CommandManager.h | 4 +-- src/commands/CommandMisc.h | 4 ++- src/commands/CommandTargets.h | 29 +++++++++---------- src/commands/CommandType.cpp | 24 +++++----------- src/commands/CommandType.h | 7 +++-- 9 files changed, 58 insertions(+), 70 deletions(-) diff --git a/src/commands/CommandDirectory.cpp b/src/commands/CommandDirectory.cpp index 5e67cabf7..f678e0a75 100644 --- a/src/commands/CommandDirectory.cpp +++ b/src/commands/CommandDirectory.cpp @@ -39,36 +39,30 @@ CommandDirectory::CommandDirectory() { // Create the command map. // Adding an entry here is the easiest way to register a Command class. - AddCommand(new ScreenshotCommandType()); - AddCommand(new BatchEvalCommandType()); - AddCommand(new ExecMenuCommandType()); - AddCommand(new GetAllMenuCommandsType()); - AddCommand(new MessageCommandType()); - AddCommand(new GetTrackInfoCommandType()); - AddCommand(new GetProjectInfoCommandType()); + AddCommand(make_movable()); + AddCommand(make_movable()); + AddCommand(make_movable()); + AddCommand(make_movable()); + AddCommand(make_movable()); + AddCommand(make_movable()); + AddCommand(make_movable()); - AddCommand(new HelpCommandType()); - AddCommand(new SelectCommandType()); - AddCommand(new CompareAudioCommandType()); - AddCommand(new SetTrackInfoCommandType()); - AddCommand(new SetProjectInfoCommandType()); + AddCommand(make_movable()); + AddCommand(make_movable()); + AddCommand(make_movable()); + AddCommand(make_movable()); + AddCommand(make_movable()); - AddCommand(new SetPreferenceCommandType()); - AddCommand(new GetPreferenceCommandType()); - AddCommand(new ImportCommandType()); - AddCommand(new ExportCommandType()); - AddCommand(new OpenProjectCommandType()); - AddCommand(new SaveProjectCommandType()); + AddCommand(make_movable()); + AddCommand(make_movable()); + AddCommand(make_movable()); + AddCommand(make_movable()); + AddCommand(make_movable()); + AddCommand(make_movable()); } CommandDirectory::~CommandDirectory() { - // Delete the factories - CommandMap::iterator iter; - for (iter = mCmdMap.begin(); iter != mCmdMap.end(); ++iter) - { - delete iter->second; - } } CommandType *CommandDirectory::LookUp(const wxString &cmdName) const @@ -78,10 +72,10 @@ CommandType *CommandDirectory::LookUp(const wxString &cmdName) const { return NULL; } - return iter->second; + return iter->second.get(); } -void CommandDirectory::AddCommand(CommandType *type) +void CommandDirectory::AddCommand(movable_ptr &&type) { wxASSERT(type != NULL); wxString cmdName = type->GetName(); @@ -89,7 +83,7 @@ void CommandDirectory::AddCommand(CommandType *type) , wxT("A command named ") + cmdName + wxT(" already exists.")); - mCmdMap[cmdName] = type; + mCmdMap[cmdName] = std::move(type); } CommandDirectory *CommandDirectory::Get() diff --git a/src/commands/CommandDirectory.h b/src/commands/CommandDirectory.h index c60e952e2..5615fa400 100644 --- a/src/commands/CommandDirectory.h +++ b/src/commands/CommandDirectory.h @@ -44,7 +44,7 @@ public: CommandType *LookUp(const wxString &cmdName) const; /// Register a type of command with the directory. - void AddCommand(CommandType *type); + void AddCommand(movable_ptr &&type); /// Get a pointer to the singleton instance static CommandDirectory *Get(); diff --git a/src/commands/CommandHandler.cpp b/src/commands/CommandHandler.cpp index 7d2c1e6ee..381b97bcf 100644 --- a/src/commands/CommandHandler.cpp +++ b/src/commands/CommandHandler.cpp @@ -16,20 +16,21 @@ *//*******************************************************************/ -#include +#include "../Audacity.h" #include "CommandHandler.h" +#include #include "../Project.h" #include "Command.h" #include "AppCommandEvent.h" #include "ScriptCommandRelay.h" CommandHandler::CommandHandler(AudacityApp &app) - : mCurrentContext(new CommandExecutionContext(&app, GetActiveProject())) + : mCurrentContext(std::make_unique + (&app, GetActiveProject())) { } CommandHandler::~CommandHandler() { - delete mCurrentContext; } void CommandHandler::SetProject(AudacityProject *) diff --git a/src/commands/CommandHandler.h b/src/commands/CommandHandler.h index 4212a898e..3b7877740 100644 --- a/src/commands/CommandHandler.h +++ b/src/commands/CommandHandler.h @@ -16,6 +16,7 @@ #ifndef __COMMANDHANDLER__ #define __COMMANDHANDLER__ +#include "../MemoryX.h" #include "../AudacityApp.h" class AudacityProject; class AppCommandEvent; @@ -24,7 +25,7 @@ class CommandExecutionContext; class CommandHandler { private: - CommandExecutionContext *mCurrentContext; + std::unique_ptr mCurrentContext; public: CommandHandler(AudacityApp &app); diff --git a/src/commands/CommandManager.h b/src/commands/CommandManager.h index a2b76120d..cbecef758 100644 --- a/src/commands/CommandManager.h +++ b/src/commands/CommandManager.h @@ -93,8 +93,8 @@ class AUDACITY_DLL_API CommandManager final : public XMLTagHandler CommandManager(); virtual ~CommandManager(); - CommandManager(const CommandManager&) = delete; - CommandManager &operator= (const CommandManager&) = delete; + CommandManager(const CommandManager&) PROHIBITED; + CommandManager &operator= (const CommandManager&) PROHIBITED; void PurgeData(); diff --git a/src/commands/CommandMisc.h b/src/commands/CommandMisc.h index 61531a8b6..b48c16502 100644 --- a/src/commands/CommandMisc.h +++ b/src/commands/CommandMisc.h @@ -23,6 +23,7 @@ class CommandType; // Map from parameter name to the value of the parameter +// to do: use hash typedef std::map ParamValueMap; // Map from parameter name to a suitable Validator @@ -30,6 +31,7 @@ typedef std::map ParamValueMap; typedef std::map> ValidatorMap; // Map from command name to type -typedef std::map CommandMap; +// to do: use hash +typedef std::map> CommandMap; #endif /* End of include guard: __COMMANDMISC__ */ diff --git a/src/commands/CommandTargets.h b/src/commands/CommandTargets.h index 2bfcb7c5f..281bb3bb5 100644 --- a/src/commands/CommandTargets.h +++ b/src/commands/CommandTargets.h @@ -47,7 +47,7 @@ public: }; /// Sends command progress information to a ProgressDialog -class GUIProgressTarget : public CommandProgressTarget +class GUIProgressTarget final : public CommandProgressTarget { private: ProgressDialog &mProgress; @@ -74,18 +74,17 @@ public: class ProgressToMessageTarget final : public CommandProgressTarget { private: - CommandMessageTarget &mTarget; + std::unique_ptr mTarget; public: - ProgressToMessageTarget(CommandMessageTarget *target) - : mTarget(*target) + ProgressToMessageTarget(std::unique_ptr &&target) + : mTarget(std::move(target)) { } virtual ~ProgressToMessageTarget() { - // delete &mTarget; } void Update(double completed) override { - mTarget.Update(wxString::Format(wxT("%.2f%%"), completed*100)); + mTarget->Update(wxString::Format(wxT("%.2f%%"), completed*100)); } }; @@ -94,7 +93,7 @@ class NullMessageTarget final : public CommandMessageTarget { public: virtual ~NullMessageTarget() {} - void Update(const wxString &message) override {} + void Update(const wxString &) override {} }; /// Displays messages from a command in a wxMessageBox @@ -146,18 +145,17 @@ public: class CombinedMessageTarget final : public CommandMessageTarget { private: - CommandMessageTarget *m1, *m2; + std::unique_ptr m1, m2; public: - CombinedMessageTarget(CommandMessageTarget *t1, CommandMessageTarget *t2) - : m1(t1), m2(t2) + CombinedMessageTarget(std::unique_ptr &&t1, + std::unique_ptr &&t2) + : m1(std::move(t1)), m2(std::move(t2)) { - wxASSERT(t1 != NULL); - wxASSERT(t2 != NULL); + wxASSERT(m1); + wxASSERT(m2); } ~CombinedMessageTarget() { - delete m1; - delete m2; } void Update(const wxString &message) override { @@ -167,7 +165,8 @@ public: }; -// By default, we ignore progress updates but display all other messages directly +// By default, we ignore progress updates but display all other messages +// directly class TargetFactory { public: diff --git a/src/commands/CommandType.cpp b/src/commands/CommandType.cpp index 359fae879..e302471b7 100644 --- a/src/commands/CommandType.cpp +++ b/src/commands/CommandType.cpp @@ -24,35 +24,25 @@ Also acts as a factory. #include CommandType::CommandType() - : mName(NULL), mSignature(NULL) + : mName{}, mSignature{} { } CommandType::~CommandType() { - if (mName != NULL) - { - delete mName; - } - if (mSignature != NULL) - { - delete mSignature; - } } -wxString CommandType::GetName() +const wxString &CommandType::GetName() { - if (mName == NULL) - { - mName = new wxString(BuildName()); - } - return *mName; + if (mName.empty()) + mName = BuildName(); + return mName; } CommandSignature &CommandType::GetSignature() { - if (mSignature == NULL) + if (!mSignature) { - mSignature = new CommandSignature(); + mSignature.create(); BuildSignature(*mSignature); } return *mSignature; diff --git a/src/commands/CommandType.h b/src/commands/CommandType.h index 825a02e21..222f0f72a 100644 --- a/src/commands/CommandType.h +++ b/src/commands/CommandType.h @@ -17,6 +17,7 @@ #define __COMMANDTYPE__ #include "CommandMisc.h" +#include "CommandSignature.h" #include "../MemoryX.h" class Command; @@ -28,13 +29,13 @@ class wxString; class CommandType /* not final */ { private: - wxString *mName; - CommandSignature *mSignature; + wxString mName; + Maybe mSignature; public: CommandType(); virtual ~CommandType(); - wxString GetName(); + const wxString &GetName(); CommandSignature &GetSignature(); wxString Describe();