From e8ad90b3c9a35c84ecc4c1bf8af0636f20aa62ef Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Thu, 31 Mar 2016 07:40:05 -0400 Subject: [PATCH] Manage Commands and CommandOutputTarget objects with smart pointers --- src/Project.cpp | 6 ++-- src/Screenshot.cpp | 13 ++++----- src/commands/AppCommandEvent.cpp | 19 ++++++------- src/commands/AppCommandEvent.h | 8 ++++-- src/commands/BatchEvalCommand.cpp | 4 +-- src/commands/BatchEvalCommand.h | 6 ++-- src/commands/Command.cpp | 11 ++++---- src/commands/Command.h | 12 ++++---- src/commands/CommandBuilder.cpp | 38 ++++++++++---------------- src/commands/CommandBuilder.h | 8 +++--- src/commands/CommandHandler.cpp | 5 +--- src/commands/CommandType.h | 4 ++- src/commands/CompareAudioCommand.cpp | 4 +-- src/commands/CompareAudioCommand.h | 6 ++-- src/commands/ExecMenuCommand.cpp | 4 +-- src/commands/ExecMenuCommand.h | 6 ++-- src/commands/GetAllMenuCommands.cpp | 4 +-- src/commands/GetAllMenuCommands.h | 6 ++-- src/commands/GetProjectInfoCommand.cpp | 4 +-- src/commands/GetProjectInfoCommand.h | 6 ++-- src/commands/GetTrackInfoCommand.cpp | 4 +-- src/commands/GetTrackInfoCommand.h | 6 ++-- src/commands/HelpCommand.cpp | 4 +-- src/commands/HelpCommand.h | 6 ++-- src/commands/ImportExportCommands.cpp | 8 +++--- src/commands/ImportExportCommands.h | 12 ++++---- src/commands/MessageCommand.cpp | 4 +-- src/commands/MessageCommand.h | 6 ++-- src/commands/OpenSaveCommands.cpp | 8 +++--- src/commands/OpenSaveCommands.h | 12 ++++---- src/commands/PreferenceCommands.cpp | 8 +++--- src/commands/PreferenceCommands.h | 12 ++++---- src/commands/ScreenshotCommand.cpp | 4 +-- src/commands/ScreenshotCommand.h | 6 ++-- src/commands/ScriptCommandRelay.cpp | 28 ++++++++++--------- src/commands/ScriptCommandRelay.h | 4 ++- src/commands/SelectCommand.cpp | 4 +-- src/commands/SelectCommand.h | 6 ++-- src/commands/SetProjectInfoCommand.cpp | 4 +-- src/commands/SetProjectInfoCommand.h | 6 ++-- src/commands/SetTrackInfoCommand.cpp | 4 +-- src/commands/SetTrackInfoCommand.h | 6 ++-- 42 files changed, 164 insertions(+), 172 deletions(-) diff --git a/src/Project.cpp b/src/Project.cpp index 4d4daf3fb..39e9d2160 100644 --- a/src/Project.cpp +++ b/src/Project.cpp @@ -4632,13 +4632,13 @@ void AudacityProject::TP_DisplayStatusMessage(const wxString &msg) // (more overhead, but can be used from a non-GUI thread) void AudacityProject::SafeDisplayStatusMessage(const wxChar *msg) { - CommandOutputTarget *target - = new CommandOutputTarget(TargetFactory::ProgressDefault(), + auto target + = std::make_unique(TargetFactory::ProgressDefault(), new StatusBarTarget(*mStatusBar), TargetFactory::MessageDefault()); CommandType *type = CommandDirectory::Get()->LookUp(wxT("Message")); wxASSERT_MSG(type != NULL, wxT("Message command not found!")); - Command *statusCmd = type->Create(target); + CommandHolder statusCmd = type->Create(std::move(target)); statusCmd->SetParameter(wxT("MessageString"), msg); ScriptCommandRelay::PostCommand(this, statusCmd); diff --git a/src/Screenshot.cpp b/src/Screenshot.cpp index a2d37ada8..65fd0d220 100644 --- a/src/Screenshot.cpp +++ b/src/Screenshot.cpp @@ -97,7 +97,7 @@ class ScreenFrame final : public wxFrame void OnCaptureFirstTrack(wxCommandEvent & event); void OnCaptureSecondTrack(wxCommandEvent & event); - ScreenshotCommand *CreateCommand(); + std::unique_ptr CreateCommand(); wxCheckBox *mDelayCheckBox; wxTextCtrl *mDirectoryTextBox; @@ -105,7 +105,7 @@ class ScreenFrame final : public wxFrame wxToggleButton *mWhite; wxStatusBar *mStatus; - ScreenshotCommand *mCommand; + std::unique_ptr mCommand; CommandExecutionContext mContext; DECLARE_EVENT_TABLE() @@ -253,16 +253,16 @@ BEGIN_EVENT_TABLE(ScreenFrame, wxFrame) END_EVENT_TABLE(); // Must not be called before CreateStatusBar! -ScreenshotCommand *ScreenFrame::CreateCommand() +std::unique_ptr ScreenFrame::CreateCommand() { wxASSERT(mStatus != NULL); - CommandOutputTarget *output = - new CommandOutputTarget(new NullProgressTarget(), + auto output = + std::make_unique(new NullProgressTarget(), new StatusBarTarget(*mStatus), new MessageBoxTarget()); CommandType *type = CommandDirectory::Get()->LookUp(wxT("Screenshot")); wxASSERT_MSG(type != NULL, wxT("Screenshot command doesn't exist!")); - return new ScreenshotCommand(*type, output, this); + return std::make_unique(*type, std::move(output), this); } ScreenFrame::ScreenFrame(wxWindow * parent, wxWindowID id) @@ -293,7 +293,6 @@ ScreenFrame::ScreenFrame(wxWindow * parent, wxWindowID id) ScreenFrame::~ScreenFrame() { - delete mCommand; } void ScreenFrame::Populate() diff --git a/src/commands/AppCommandEvent.cpp b/src/commands/AppCommandEvent.cpp index 3ac04ab31..b3eb7dbeb 100644 --- a/src/commands/AppCommandEvent.cpp +++ b/src/commands/AppCommandEvent.cpp @@ -28,13 +28,14 @@ DEFINE_EVENT_TYPE(wxEVT_APP_COMMAND_RECEIVED) IMPLEMENT_DYNAMIC_CLASS(AppCommandEvent, wxEvent) AppCommandEvent::AppCommandEvent(wxEventType commandType, int id) -: wxCommandEvent(commandType, id), mCommand(NULL) +: wxCommandEvent(commandType, id) { } // Copy constructor -AppCommandEvent::AppCommandEvent(const AppCommandEvent &event) : wxCommandEvent(event) +AppCommandEvent::AppCommandEvent(const AppCommandEvent &event) + : wxCommandEvent(event) + , mCommand(event.mCommand) { - this->mCommand = event.mCommand; } AppCommandEvent::~AppCommandEvent() @@ -48,17 +49,13 @@ wxEvent *AppCommandEvent::Clone() const } /// Store a pointer to a command object -void AppCommandEvent::SetCommand(Command *cmd) +void AppCommandEvent::SetCommand(const CommandHolder &cmd) { - wxASSERT(NULL == mCommand); + wxASSERT(!mCommand); mCommand = cmd; } -// When the command pointer is retrieved, the caller is responsible for -// deletion. -Command *AppCommandEvent::GetCommand() +CommandHolder AppCommandEvent::GetCommand() { - Command *tmp = mCommand; - mCommand = NULL; - return tmp; + return mCommand; } diff --git a/src/commands/AppCommandEvent.h b/src/commands/AppCommandEvent.h index 62bdca726..319f12e4d 100644 --- a/src/commands/AppCommandEvent.h +++ b/src/commands/AppCommandEvent.h @@ -18,15 +18,17 @@ #include #include "../Audacity.h" +#include "../MemoryX.h" DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, wxEVT_APP_COMMAND_RECEIVED, -1); class Command; +using CommandHolder = std::shared_ptr; class AppCommandEvent final : public wxCommandEvent { private: - Command *mCommand; + CommandHolder mCommand; public: AppCommandEvent(wxEventType commandType = wxEVT_APP_COMMAND_RECEIVED, int id = 0); @@ -35,8 +37,8 @@ public: ~AppCommandEvent(); wxEvent *Clone() const override; - void SetCommand(Command *cmd); - Command *GetCommand(); + void SetCommand(const CommandHolder &cmd); + CommandHolder GetCommand(); private: DECLARE_DYNAMIC_CLASS(AppCommandEvent) diff --git a/src/commands/BatchEvalCommand.cpp b/src/commands/BatchEvalCommand.cpp index b4ffef883..864a98f91 100644 --- a/src/commands/BatchEvalCommand.cpp +++ b/src/commands/BatchEvalCommand.cpp @@ -30,9 +30,9 @@ void BatchEvalCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("ChainName"), wxT(""), chainValidator); } -Command *BatchEvalCommandType::Create(CommandOutputTarget *target) +CommandHolder BatchEvalCommandType::Create(std::unique_ptr &&target) { - return new BatchEvalCommand(*this, target); + return std::make_shared(*this, std::move(target)); } bool BatchEvalCommand::Apply(CommandExecutionContext WXUNUSED(context)) diff --git a/src/commands/BatchEvalCommand.h b/src/commands/BatchEvalCommand.h index 39cf40e2d..a5a5b720a 100644 --- a/src/commands/BatchEvalCommand.h +++ b/src/commands/BatchEvalCommand.h @@ -31,15 +31,15 @@ class BatchEvalCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class BatchEvalCommand final : public CommandImplementation { public: BatchEvalCommand(CommandType &type, - CommandOutputTarget *target) - : CommandImplementation(type, target) + std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } virtual ~BatchEvalCommand(); diff --git a/src/commands/Command.cpp b/src/commands/Command.cpp index d2bbecbbd..36653f32d 100644 --- a/src/commands/Command.cpp +++ b/src/commands/Command.cpp @@ -51,7 +51,6 @@ void DecoratedCommand::Error(const wxString &message) DecoratedCommand::~DecoratedCommand() { - delete mCommand; } wxString DecoratedCommand::GetName() @@ -80,7 +79,8 @@ bool ApplyAndSendResponse::Apply(CommandExecutionContext context) if (result) { response += wxT("OK"); - } else + } + else { response += wxT("Failed!"); } @@ -89,17 +89,16 @@ bool ApplyAndSendResponse::Apply(CommandExecutionContext context) } CommandImplementation::CommandImplementation(CommandType &type, - CommandOutputTarget *output) + std::unique_ptr &&output) : mType(type), mParams(type.GetSignature().GetDefaults()), - mOutput(output) + mOutput(std::move(output)) { - wxASSERT(output != NULL); + wxASSERT(mOutput); } CommandImplementation::~CommandImplementation() { - delete mOutput; } void CommandImplementation::TypeCheck(const wxString &typeName, diff --git a/src/commands/Command.h b/src/commands/Command.h index d1e6e84bd..cc9c331ce 100644 --- a/src/commands/Command.h +++ b/src/commands/Command.h @@ -70,17 +70,19 @@ public: virtual bool Apply(CommandExecutionContext context) = 0; }; +using CommandHolder = std::shared_ptr; + // Command which wraps another command class DecoratedCommand /* not final */ : public Command { protected: - Command *mCommand; + CommandHolder mCommand; public: void Progress(double completed) override; void Status(const wxString &message) override; void Error(const wxString &message) override; - DecoratedCommand(Command *cmd) + DecoratedCommand(const CommandHolder &cmd) : mCommand(cmd) { wxASSERT(cmd != NULL); @@ -96,7 +98,7 @@ public: class ApplyAndSendResponse final : public DecoratedCommand { public: - ApplyAndSendResponse(Command *cmd) + ApplyAndSendResponse(const CommandHolder &cmd) : DecoratedCommand(cmd) { } @@ -114,7 +116,7 @@ private: bool Valid(const wxString ¶mName, const wxVariant ¶mValue); protected: - CommandOutputTarget *mOutput; + std::unique_ptr mOutput; // Convenience methods for allowing subclasses to access parameters void TypeCheck(const wxString &typeName, @@ -135,7 +137,7 @@ public: /// Constructor should not be called directly; only by a factory which /// ensures name and params are set appropriately for the command. CommandImplementation(CommandType &type, - CommandOutputTarget *output); + std::unique_ptr &&output); virtual ~CommandImplementation(); diff --git a/src/commands/CommandBuilder.cpp b/src/commands/CommandBuilder.cpp index 6849ec0f2..60a915152 100644 --- a/src/commands/CommandBuilder.cpp +++ b/src/commands/CommandBuilder.cpp @@ -30,20 +30,19 @@ system by constructing BatchCommandEval objects. #include "ScriptCommandRelay.h" CommandBuilder::CommandBuilder(const wxString &cmdString) - : mValid(false), mCommand(NULL) + : mValid(false) { BuildCommand(cmdString); } CommandBuilder::CommandBuilder(const wxString &cmdName, const wxString ¶ms) - : mValid(false), mCommand(NULL) + : mValid(false) { BuildCommand(cmdName, params); } CommandBuilder::~CommandBuilder() { - Cleanup(); } bool CommandBuilder::WasValid() @@ -56,22 +55,13 @@ const wxString &CommandBuilder::GetErrorMessage() return mError; } -Command *CommandBuilder::GetCommand() +CommandHolder CommandBuilder::GetCommand() { wxASSERT(mValid); - wxASSERT(NULL != mCommand); - Command *tmp = mCommand; - mCommand = NULL; - return tmp; -} - -void CommandBuilder::Cleanup() -{ - if (mCommand != NULL) - { - delete mCommand; - mCommand = NULL; - } + wxASSERT(mCommand); + auto result = mCommand; + mCommand.reset(); + return result; } void CommandBuilder::Failure(const wxString &msg) @@ -80,7 +70,7 @@ void CommandBuilder::Failure(const wxString &msg) mValid = false; } -void CommandBuilder::Success(Command *cmd) +void CommandBuilder::Success(const CommandHolder &cmd) { mCommand = cmd; mValid = true; @@ -92,8 +82,8 @@ void CommandBuilder::BuildCommand(const wxString &cmdName, // Stage 1: create a Command object of the right type CommandMessageTarget *scriptOutput = ScriptCommandRelay::GetResponseTarget(); - CommandOutputTarget *output - = new CommandOutputTarget(new NullProgressTarget(), + auto output + = std::make_unique(new NullProgressTarget(), scriptOutput, scriptOutput); @@ -104,15 +94,15 @@ void CommandBuilder::BuildCommand(const wxString &cmdName, // Fall back to hoping the Batch Command system can handle it CommandType *type = CommandDirectory::Get()->LookUp(wxT("BatchCommand")); wxASSERT(type != NULL); - mCommand = type->Create(output); + mCommand = type->Create(std::move(output)); mCommand->SetParameter(wxT("CommandName"), cmdName); mCommand->SetParameter(wxT("ParamString"), cmdParamsArg); - Success(new ApplyAndSendResponse(mCommand)); + Success(std::make_shared(mCommand)); return; } CommandSignature &signature = factory->GetSignature(); - mCommand = factory->Create(output); + mCommand = factory->Create(std::move(output)); // Stage 2: set the parameters @@ -165,7 +155,7 @@ void CommandBuilder::BuildCommand(const wxString &cmdName, cmdParams = cmdParams.Mid(splitAt); } - Success(new ApplyAndSendResponse(mCommand)); + Success(std::make_shared(mCommand)); } void CommandBuilder::BuildCommand(const wxString &cmdStringArg) diff --git a/src/commands/CommandBuilder.h b/src/commands/CommandBuilder.h index 8c01b9af1..6d0208580 100644 --- a/src/commands/CommandBuilder.h +++ b/src/commands/CommandBuilder.h @@ -17,6 +17,7 @@ #define __COMMANDBUILDER__ class Command; +using CommandHolder = std::shared_ptr; class wxString; // CommandBuilder has the task of validating and interpreting a command string. @@ -26,11 +27,11 @@ class CommandBuilder { private: bool mValid; - Command *mCommand; + CommandHolder mCommand; wxString mError; void Failure(const wxString &msg = wxEmptyString); - void Success(Command *cmd); + void Success(const CommandHolder &cmd); void BuildCommand(const wxString &cmdName, const wxString &cmdParams); void BuildCommand(const wxString &cmdString); public: @@ -39,8 +40,7 @@ class CommandBuilder const wxString &cmdParams); ~CommandBuilder(); bool WasValid(); - Command *GetCommand(); - void Cleanup(); + CommandHolder GetCommand(); const wxString &GetErrorMessage(); }; #endif /* End of include guard: __COMMANDBUILDER__ */ diff --git a/src/commands/CommandHandler.cpp b/src/commands/CommandHandler.cpp index 46094abeb..7d2c1e6ee 100644 --- a/src/commands/CommandHandler.cpp +++ b/src/commands/CommandHandler.cpp @@ -41,7 +41,7 @@ void CommandHandler::SetProject(AudacityProject *) void CommandHandler::OnReceiveCommand(AppCommandEvent &event) { // First retrieve the actual command from the event 'envelope'. - Command *cmd = event.GetCommand(); + CommandHolder cmd = event.GetCommand(); // JKC: In case the user changed the project, let us track that. // This saves us the embarrassment (crash) of a NEW project @@ -53,9 +53,6 @@ void CommandHandler::OnReceiveCommand(AppCommandEvent &event) // different project. cmd->Apply(*mCurrentContext); - // Done with the command so DELETE it. - delete cmd; - // Redraw the project mCurrentContext->GetProject()->RedrawProject(); } diff --git a/src/commands/CommandType.h b/src/commands/CommandType.h index 75d41b045..825a02e21 100644 --- a/src/commands/CommandType.h +++ b/src/commands/CommandType.h @@ -17,8 +17,10 @@ #define __COMMANDTYPE__ #include "CommandMisc.h" +#include "../MemoryX.h" class Command; +using CommandHolder = std::shared_ptr; class CommandOutputTarget; class CommandSignature; class wxString; @@ -47,7 +49,7 @@ public: virtual void BuildSignature(CommandSignature &signature) = 0; // Create a command instance with the specified output target - virtual Command *Create(CommandOutputTarget *target) = 0; + virtual CommandHolder Create(std::unique_ptr &&target) = 0; }; #endif /* End of include guard: __COMMANDTYPE__ */ diff --git a/src/commands/CompareAudioCommand.cpp b/src/commands/CompareAudioCommand.cpp index 35833b9d5..285efe88d 100644 --- a/src/commands/CompareAudioCommand.cpp +++ b/src/commands/CompareAudioCommand.cpp @@ -33,9 +33,9 @@ void CompareAudioCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("Threshold"), 0.0, thresholdValidator); } -Command *CompareAudioCommandType::Create(CommandOutputTarget *target) +CommandHolder CompareAudioCommandType::Create(std::unique_ptr &&target) { - return new CompareAudioCommand(*this, target); + return std::make_shared(*this, std::move(target)); } // Update member variables with project selection data (and validate) diff --git a/src/commands/CompareAudioCommand.h b/src/commands/CompareAudioCommand.h index acb1fe9e0..464dc43b0 100644 --- a/src/commands/CompareAudioCommand.h +++ b/src/commands/CompareAudioCommand.h @@ -27,7 +27,7 @@ class CompareAudioCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class CompareAudioCommand final : public CommandImplementation @@ -44,8 +44,8 @@ protected: double CompareSample(double value1, double value2) /* not override */; public: - CompareAudioCommand(CommandType &type, CommandOutputTarget *target) - : CommandImplementation(type, target) + CompareAudioCommand(CommandType &type, std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } bool Apply(CommandExecutionContext context) override; }; diff --git a/src/commands/ExecMenuCommand.cpp b/src/commands/ExecMenuCommand.cpp index a5c4b4d0c..a4c276545 100644 --- a/src/commands/ExecMenuCommand.cpp +++ b/src/commands/ExecMenuCommand.cpp @@ -28,9 +28,9 @@ void ExecMenuCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("CommandName"), wxT(""), menuCommandValidator); } -Command *ExecMenuCommandType::Create(CommandOutputTarget *target) +CommandHolder ExecMenuCommandType::Create(std::unique_ptr &&target) { - return new ExecMenuCommand(*this, target); + return std::make_shared(*this, std::move(target)); } bool ExecMenuCommand::Apply(CommandExecutionContext context) diff --git a/src/commands/ExecMenuCommand.h b/src/commands/ExecMenuCommand.h index 8788ad9ad..fd46f9056 100644 --- a/src/commands/ExecMenuCommand.h +++ b/src/commands/ExecMenuCommand.h @@ -28,15 +28,15 @@ class ExecMenuCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class ExecMenuCommand final : public CommandImplementation { public: ExecMenuCommand(CommandType &type, - CommandOutputTarget *target) - : CommandImplementation(type, target) + std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } virtual ~ExecMenuCommand() { } bool Apply(CommandExecutionContext context) override; diff --git a/src/commands/GetAllMenuCommands.cpp b/src/commands/GetAllMenuCommands.cpp index b8898104a..16e0c7e38 100644 --- a/src/commands/GetAllMenuCommands.cpp +++ b/src/commands/GetAllMenuCommands.cpp @@ -28,9 +28,9 @@ void GetAllMenuCommandsType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("ShowStatus"), 0, showStatusValidator); } -Command *GetAllMenuCommandsType::Create(CommandOutputTarget *target) +CommandHolder GetAllMenuCommandsType::Create(std::unique_ptr &&target) { - return new GetAllMenuCommands(*this, target); + return std::make_shared(*this, std::move(target)); } bool GetAllMenuCommands::Apply(CommandExecutionContext context) diff --git a/src/commands/GetAllMenuCommands.h b/src/commands/GetAllMenuCommands.h index ab749f7f1..fd33c6b85 100644 --- a/src/commands/GetAllMenuCommands.h +++ b/src/commands/GetAllMenuCommands.h @@ -28,15 +28,15 @@ class GetAllMenuCommandsType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class GetAllMenuCommands final : public CommandImplementation { public: GetAllMenuCommands(CommandType &type, - CommandOutputTarget *target) - : CommandImplementation(type, target) + std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } virtual ~GetAllMenuCommands() diff --git a/src/commands/GetProjectInfoCommand.cpp b/src/commands/GetProjectInfoCommand.cpp index cc43d26cc..1dbaa8033 100644 --- a/src/commands/GetProjectInfoCommand.cpp +++ b/src/commands/GetProjectInfoCommand.cpp @@ -39,9 +39,9 @@ void GetProjectInfoCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("Type"), wxT("Name"), infoTypeValidator); } -Command *GetProjectInfoCommandType::Create(CommandOutputTarget *target) +CommandHolder GetProjectInfoCommandType::Create(std::unique_ptr &&target) { - return new GetProjectInfoCommand(*this, target); + return std::make_shared(*this, std::move(target)); } diff --git a/src/commands/GetProjectInfoCommand.h b/src/commands/GetProjectInfoCommand.h index 987241b97..7882c7cab 100644 --- a/src/commands/GetProjectInfoCommand.h +++ b/src/commands/GetProjectInfoCommand.h @@ -24,15 +24,15 @@ class GetProjectInfoCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class GetProjectInfoCommand final : public CommandImplementation { public: - GetProjectInfoCommand(CommandType &type, CommandOutputTarget *target) - : CommandImplementation(type, target) + GetProjectInfoCommand(CommandType &type, std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } virtual ~GetProjectInfoCommand() { } diff --git a/src/commands/GetTrackInfoCommand.cpp b/src/commands/GetTrackInfoCommand.cpp index daf44c0c4..faf605ae6 100644 --- a/src/commands/GetTrackInfoCommand.cpp +++ b/src/commands/GetTrackInfoCommand.cpp @@ -46,9 +46,9 @@ void GetTrackInfoCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("Type"), wxT("Name"), infoTypeValidator); } -Command *GetTrackInfoCommandType::Create(CommandOutputTarget *target) +CommandHolder GetTrackInfoCommandType::Create(std::unique_ptr &&target) { - return new GetTrackInfoCommand(*this, target); + return std::make_shared(*this, std::move(target)); } diff --git a/src/commands/GetTrackInfoCommand.h b/src/commands/GetTrackInfoCommand.h index 3cfde864c..0732dd590 100644 --- a/src/commands/GetTrackInfoCommand.h +++ b/src/commands/GetTrackInfoCommand.h @@ -24,14 +24,14 @@ class GetTrackInfoCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class GetTrackInfoCommand final : public CommandImplementation { public: - GetTrackInfoCommand(CommandType &type, CommandOutputTarget *target) - : CommandImplementation(type, target) + GetTrackInfoCommand(CommandType &type, std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } virtual ~GetTrackInfoCommand() { } diff --git a/src/commands/HelpCommand.cpp b/src/commands/HelpCommand.cpp index 05df0ccc4..9ec1cd057 100644 --- a/src/commands/HelpCommand.cpp +++ b/src/commands/HelpCommand.cpp @@ -28,9 +28,9 @@ void HelpCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("CommandName"), wxT(""), commandNameValidator); } -Command *HelpCommandType::Create(CommandOutputTarget *target) +CommandHolder HelpCommandType::Create(std::unique_ptr &&target) { - return new HelpCommand(*this, target); + return std::make_shared(*this, std::move(target)); } bool HelpCommand::Apply(CommandExecutionContext WXUNUSED(context)) diff --git a/src/commands/HelpCommand.h b/src/commands/HelpCommand.h index 8d49c0522..19ce69e07 100644 --- a/src/commands/HelpCommand.h +++ b/src/commands/HelpCommand.h @@ -27,14 +27,14 @@ class HelpCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class HelpCommand final : public CommandImplementation { public: - HelpCommand(HelpCommandType &type, CommandOutputTarget *target) - : CommandImplementation(type, target) { } + HelpCommand(HelpCommandType &type, std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } bool Apply(CommandExecutionContext context) override; }; diff --git a/src/commands/ImportExportCommands.cpp b/src/commands/ImportExportCommands.cpp index 0d77cc084..37f44ce67 100644 --- a/src/commands/ImportExportCommands.cpp +++ b/src/commands/ImportExportCommands.cpp @@ -31,9 +31,9 @@ void ImportCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("Filename"), wxT(""), filenameValidator); } -Command *ImportCommandType::Create(CommandOutputTarget *target) +CommandHolder ImportCommandType::Create(std::unique_ptr &&target) { - return new ImportCommand(*this, target); + return std::make_shared(*this, std::move(target)); } bool ImportCommand::Apply(CommandExecutionContext context) @@ -66,9 +66,9 @@ void ExportCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("Channels"), 1, channelsValidator); } -Command *ExportCommandType::Create(CommandOutputTarget *target) +CommandHolder ExportCommandType::Create(std::unique_ptr &&target) { - return new ExportCommand(*this, target); + return std::make_shared(*this, std::move(target)); } bool ExportCommand::Apply(CommandExecutionContext context) diff --git a/src/commands/ImportExportCommands.h b/src/commands/ImportExportCommands.h index 3e1a4563e..9add2c4e8 100644 --- a/src/commands/ImportExportCommands.h +++ b/src/commands/ImportExportCommands.h @@ -27,15 +27,15 @@ class ImportCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class ImportCommand final : public CommandImplementation { public: ImportCommand(CommandType &type, - CommandOutputTarget *target) - : CommandImplementation(type, target) + std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } virtual ~ImportCommand(); @@ -49,15 +49,15 @@ class ExportCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class ExportCommand final : public CommandImplementation { public: ExportCommand(CommandType &type, - CommandOutputTarget *target) - : CommandImplementation(type, target) + std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } virtual ~ExportCommand(); diff --git a/src/commands/MessageCommand.cpp b/src/commands/MessageCommand.cpp index 83819c2c6..caa15e8a2 100644 --- a/src/commands/MessageCommand.cpp +++ b/src/commands/MessageCommand.cpp @@ -27,9 +27,9 @@ void MessageCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("MessageString"), wxT(""), stringValidator); } -Command *MessageCommandType::Create(CommandOutputTarget *target) +CommandHolder MessageCommandType::Create(std::unique_ptr &&target) { - return new MessageCommand(*this, target); + return std::make_shared(*this, std::move(target)); } bool MessageCommand::Apply(CommandExecutionContext WXUNUSED(context)) diff --git a/src/commands/MessageCommand.h b/src/commands/MessageCommand.h index f099fc7e0..4d6fd0168 100644 --- a/src/commands/MessageCommand.h +++ b/src/commands/MessageCommand.h @@ -29,15 +29,15 @@ class MessageCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class MessageCommand final : public CommandImplementation { public: MessageCommand(CommandType &type, - CommandOutputTarget *target) - : CommandImplementation(type, target) {} + std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) {} bool Apply(CommandExecutionContext context) override; }; diff --git a/src/commands/OpenSaveCommands.cpp b/src/commands/OpenSaveCommands.cpp index 749836fc4..af181ab0e 100644 --- a/src/commands/OpenSaveCommands.cpp +++ b/src/commands/OpenSaveCommands.cpp @@ -32,9 +32,9 @@ void OpenProjectCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("Filename"), wxT(""), filenameValidator); } -Command *OpenProjectCommandType::Create(CommandOutputTarget *target) +CommandHolder OpenProjectCommandType::Create(std::unique_ptr &&target) { - return new OpenProjectCommand(*this, target); + return std::make_shared(*this, std::move(target)); } bool OpenProjectCommand::Apply(CommandExecutionContext context) @@ -80,9 +80,9 @@ void SaveProjectCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("Filename"), wxT(""), filenameValidator); } -Command *SaveProjectCommandType::Create(CommandOutputTarget *target) +CommandHolder SaveProjectCommandType::Create(std::unique_ptr &&target) { - return new SaveProjectCommand(*this, target); + return std::make_shared(*this, std::move(target)); } bool SaveProjectCommand::Apply(CommandExecutionContext context) diff --git a/src/commands/OpenSaveCommands.h b/src/commands/OpenSaveCommands.h index 90a0d2d0c..339efb999 100644 --- a/src/commands/OpenSaveCommands.h +++ b/src/commands/OpenSaveCommands.h @@ -27,15 +27,15 @@ class OpenProjectCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class OpenProjectCommand final : public CommandImplementation { public: OpenProjectCommand(CommandType &type, - CommandOutputTarget *target) - : CommandImplementation(type, target) + std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } virtual ~OpenProjectCommand(); @@ -49,15 +49,15 @@ class SaveProjectCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class SaveProjectCommand final : public CommandImplementation { public: SaveProjectCommand(CommandType &type, - CommandOutputTarget *target) - : CommandImplementation(type, target) + std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } virtual ~SaveProjectCommand(); diff --git a/src/commands/PreferenceCommands.cpp b/src/commands/PreferenceCommands.cpp index 540194234..13e52780c 100644 --- a/src/commands/PreferenceCommands.cpp +++ b/src/commands/PreferenceCommands.cpp @@ -30,9 +30,9 @@ void GetPreferenceCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("PrefName"), wxT(""), prefNameValidator); } -Command *GetPreferenceCommandType::Create(CommandOutputTarget *target) +CommandHolder GetPreferenceCommandType::Create(std::unique_ptr &&target) { - return new GetPreferenceCommand(*this, target); + return std::make_shared(*this, std::move(target)); } bool GetPreferenceCommand::Apply(CommandExecutionContext WXUNUSED(context)) @@ -65,9 +65,9 @@ void SetPreferenceCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("PrefValue"), wxT(""), prefValueValidator); } -Command *SetPreferenceCommandType::Create(CommandOutputTarget *target) +CommandHolder SetPreferenceCommandType::Create(std::unique_ptr &&target) { - return new SetPreferenceCommand(*this, target); + return std::make_shared(*this, std::move(target)); } bool SetPreferenceCommand::Apply(CommandExecutionContext WXUNUSED(context)) diff --git a/src/commands/PreferenceCommands.h b/src/commands/PreferenceCommands.h index a5699a2e3..27955ddc0 100644 --- a/src/commands/PreferenceCommands.h +++ b/src/commands/PreferenceCommands.h @@ -30,15 +30,15 @@ class GetPreferenceCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class GetPreferenceCommand final : public CommandImplementation { public: GetPreferenceCommand(CommandType &type, - CommandOutputTarget *target) - : CommandImplementation(type, target) + std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } virtual ~GetPreferenceCommand(); @@ -52,15 +52,15 @@ class SetPreferenceCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class SetPreferenceCommand final : public CommandImplementation { public: SetPreferenceCommand(CommandType &type, - CommandOutputTarget *target) - : CommandImplementation(type, target) + std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } virtual ~SetPreferenceCommand(); diff --git a/src/commands/ScreenshotCommand.cpp b/src/commands/ScreenshotCommand.cpp index 8edae5cc2..e0d7ddc02 100644 --- a/src/commands/ScreenshotCommand.cpp +++ b/src/commands/ScreenshotCommand.cpp @@ -306,9 +306,9 @@ void ScreenshotCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("FilePath"), wxT(""), filePathValidator); } -Command *ScreenshotCommandType::Create(CommandOutputTarget *target) +CommandHolder ScreenshotCommandType::Create(std::unique_ptr &&target) { - return new ScreenshotCommand(*this, target); + return std::make_shared(*this, std::move(target)); } wxString ScreenshotCommand::MakeFileName(const wxString &path, const wxString &basename) diff --git a/src/commands/ScreenshotCommand.h b/src/commands/ScreenshotCommand.h index fe8a689b9..4a7e06926 100644 --- a/src/commands/ScreenshotCommand.h +++ b/src/commands/ScreenshotCommand.h @@ -28,7 +28,7 @@ class ScreenshotCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class ScreenshotCommand final : public CommandImplementation @@ -53,9 +53,9 @@ private: public: wxTopLevelWindow *GetFrontWindow(AudacityProject *project); ScreenshotCommand(CommandType &type, - CommandOutputTarget *output, + std::unique_ptr &&output, wxWindow *ignore = NULL) - : CommandImplementation(type, output), + : CommandImplementation(type, std::move(output)), mIgnore(ignore), mBackground(false) { } diff --git a/src/commands/ScriptCommandRelay.cpp b/src/commands/ScriptCommandRelay.cpp index b326c8773..a6a2b4947 100644 --- a/src/commands/ScriptCommandRelay.cpp +++ b/src/commands/ScriptCommandRelay.cpp @@ -51,7 +51,7 @@ void ScriptCommandRelay::Run() } /// Send a command to a project, to be applied in that context. -void ScriptCommandRelay::PostCommand(AudacityProject *project, Command *cmd) +void ScriptCommandRelay::PostCommand(AudacityProject *project, const CommandHolder &cmd) { wxASSERT(project != NULL); wxASSERT(cmd != NULL); @@ -66,20 +66,22 @@ void ScriptCommandRelay::PostCommand(AudacityProject *project, Command *cmd) /// the GUI at a time causes problems with wxwidgets. int ExecCommand(wxString *pIn, wxString *pOut) { - CommandBuilder builder(*pIn); - if (builder.WasValid()) { - AudacityProject *project = GetActiveProject(); - project->SafeDisplayStatusMessage(wxT("Received script command")); - Command *cmd = builder.GetCommand(); - ScriptCommandRelay::PostCommand(project, cmd); + CommandBuilder builder(*pIn); + if (builder.WasValid()) + { + AudacityProject *project = GetActiveProject(); + project->SafeDisplayStatusMessage(wxT("Received script command")); + CommandHolder cmd = builder.GetCommand(); + ScriptCommandRelay::PostCommand(project, cmd); - *pOut = wxEmptyString; - } else - { - *pOut = wxT("Syntax error!\n"); - *pOut += builder.GetErrorMessage() + wxT("\n"); - builder.Cleanup(); + *pOut = wxEmptyString; + } + else + { + *pOut = wxT("Syntax error!\n"); + *pOut += builder.GetErrorMessage() + wxT("\n"); + } } // Wait until all responses from the command have been received. diff --git a/src/commands/ScriptCommandRelay.h b/src/commands/ScriptCommandRelay.h index 730614673..dd31a1d39 100644 --- a/src/commands/ScriptCommandRelay.h +++ b/src/commands/ScriptCommandRelay.h @@ -17,6 +17,7 @@ #define __SCRIPTCOMMANDRELAY__ #include "../Audacity.h" +#include "../MemoryX.h" class CommandHandler; class ResponseQueue; @@ -24,6 +25,7 @@ class Response; class ResponseQueueTarget; class AudacityProject; class Command; +using CommandHolder = std::shared_ptr; class wxString; typedef int (*tpExecScriptServerFunc)( wxString * pIn, wxString * pOut); @@ -47,7 +49,7 @@ class ScriptCommandRelay static void SetCommandHandler(CommandHandler &ch); static void Run(); - static void PostCommand(AudacityProject *project, Command *cmd); + static void PostCommand(AudacityProject *project, const CommandHolder &cmd); static void SendResponse(const wxString &response); static Response ReceiveResponse(); static ResponseQueueTarget *GetResponseTarget(); diff --git a/src/commands/SelectCommand.cpp b/src/commands/SelectCommand.cpp index 35c1b8b2b..60a3d555f 100644 --- a/src/commands/SelectCommand.cpp +++ b/src/commands/SelectCommand.cpp @@ -49,9 +49,9 @@ void SelectCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("TrackName"), 0, trackNameValidator); } -Command *SelectCommandType::Create(CommandOutputTarget *target) +CommandHolder SelectCommandType::Create(std::unique_ptr &&target) { - return new SelectCommand(*this, target); + return std::make_shared(*this, std::move(target)); } bool SelectCommand::Apply(CommandExecutionContext context) diff --git a/src/commands/SelectCommand.h b/src/commands/SelectCommand.h index eae1dd7d7..195d9514a 100644 --- a/src/commands/SelectCommand.h +++ b/src/commands/SelectCommand.h @@ -24,14 +24,14 @@ class SelectCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class SelectCommand final : public CommandImplementation { public: - SelectCommand(SelectCommandType &type, CommandOutputTarget *target) - : CommandImplementation(type, target) { } + SelectCommand(SelectCommandType &type, std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } bool Apply(CommandExecutionContext context) override; }; diff --git a/src/commands/SetProjectInfoCommand.cpp b/src/commands/SetProjectInfoCommand.cpp index b95132ca5..b7372feba 100644 --- a/src/commands/SetProjectInfoCommand.cpp +++ b/src/commands/SetProjectInfoCommand.cpp @@ -41,9 +41,9 @@ void SetProjectInfoCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT(kSetOfTracksStr), wxT("x"), TracksSetValidator); } -Command *SetProjectInfoCommandType::Create(CommandOutputTarget *target) +CommandHolder SetProjectInfoCommandType::Create(std::unique_ptr &&target) { - return new SetProjectInfoCommand(*this, target); + return std::make_shared(*this, std::move(target)); } diff --git a/src/commands/SetProjectInfoCommand.h b/src/commands/SetProjectInfoCommand.h index 9a5c216d6..01e335131 100644 --- a/src/commands/SetProjectInfoCommand.h +++ b/src/commands/SetProjectInfoCommand.h @@ -28,15 +28,15 @@ class SetProjectInfoCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class SetProjectInfoCommand final : public CommandImplementation { public: - SetProjectInfoCommand(CommandType &type, CommandOutputTarget *target) - : CommandImplementation(type, target) + SetProjectInfoCommand(CommandType &type, std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } virtual ~SetProjectInfoCommand() { } diff --git a/src/commands/SetTrackInfoCommand.cpp b/src/commands/SetTrackInfoCommand.cpp index 95c8d3288..288b8f903 100644 --- a/src/commands/SetTrackInfoCommand.cpp +++ b/src/commands/SetTrackInfoCommand.cpp @@ -37,9 +37,9 @@ void SetTrackInfoCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("Name"), wxT("Unnamed"), nameValidator); } -Command *SetTrackInfoCommandType::Create(CommandOutputTarget *target) +CommandHolder SetTrackInfoCommandType::Create(std::unique_ptr &&target) { - return new SetTrackInfoCommand(*this, target); + return std::make_shared(*this, std::move(target)); } bool SetTrackInfoCommand::Apply(CommandExecutionContext context) diff --git a/src/commands/SetTrackInfoCommand.h b/src/commands/SetTrackInfoCommand.h index eaee0d651..0213a678d 100644 --- a/src/commands/SetTrackInfoCommand.h +++ b/src/commands/SetTrackInfoCommand.h @@ -24,14 +24,14 @@ class SetTrackInfoCommandType final : public CommandType public: wxString BuildName() override; void BuildSignature(CommandSignature &signature) override; - Command *Create(CommandOutputTarget *target) override; + CommandHolder Create(std::unique_ptr &&target) override; }; class SetTrackInfoCommand final : public CommandImplementation { public: - SetTrackInfoCommand(CommandType &type, CommandOutputTarget *target) - : CommandImplementation(type, target) + SetTrackInfoCommand(CommandType &type, std::unique_ptr &&target) + : CommandImplementation(type, std::move(target)) { } virtual ~SetTrackInfoCommand() { }