From a1eeb528b732bd145517d5334ee18c9eda1c272a Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Tue, 21 May 2019 21:20:58 -0400 Subject: [PATCH] Lift a call to GetActiveProject into ScriptCommandRelay... ... Don't do it at the low level of construction of a command object. Do it only at the highest possible level, where an external scripting module or Nyquist calls into the command framework. Pass the project pointer down where it is needed. --- src/commands/BatchEvalCommand.cpp | 5 +++-- src/commands/BatchEvalCommand.h | 3 ++- src/commands/CommandBuilder.cpp | 20 ++++++++++++-------- src/commands/CommandBuilder.h | 10 ++++++---- src/commands/CommandType.h | 3 ++- src/commands/ScriptCommandRelay.cpp | 5 +++-- 6 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/commands/BatchEvalCommand.cpp b/src/commands/BatchEvalCommand.cpp index e4f260f5e..696b6f51a 100644 --- a/src/commands/BatchEvalCommand.cpp +++ b/src/commands/BatchEvalCommand.cpp @@ -39,9 +39,10 @@ void BatchEvalCommandType::BuildSignature(CommandSignature &signature) signature.AddParameter(wxT("MacroName"), wxT(""), std::move(macroValidator)); } -OldStyleCommandPointer BatchEvalCommandType::Create(std::unique_ptr && WXUNUSED(target)) +OldStyleCommandPointer BatchEvalCommandType::Create( AudacityProject *project, + std::unique_ptr && WXUNUSED(target)) { - return std::make_shared(*GetActiveProject(), *this); + return std::make_shared(*project, *this); } bool BatchEvalCommand::Apply(const CommandContext & context) diff --git a/src/commands/BatchEvalCommand.h b/src/commands/BatchEvalCommand.h index 762a1fabc..bf6fbcee7 100644 --- a/src/commands/BatchEvalCommand.h +++ b/src/commands/BatchEvalCommand.h @@ -33,7 +33,8 @@ class BatchEvalCommandType final : public OldStyleCommandType public: ComponentInterfaceSymbol BuildName() override; void BuildSignature(CommandSignature &signature) override; - OldStyleCommandPointer Create(std::unique_ptr &&target) override; + OldStyleCommandPointer Create( AudacityProject *project, + std::unique_ptr &&target) override; }; class BatchEvalCommand final : public CommandImplementation diff --git a/src/commands/CommandBuilder.cpp b/src/commands/CommandBuilder.cpp index 41a729172..4bd0eeab6 100644 --- a/src/commands/CommandBuilder.cpp +++ b/src/commands/CommandBuilder.cpp @@ -30,16 +30,18 @@ system by constructing BatchCommandEval objects. #include "CommandTargets.h" #include "../Shuttle.h" -CommandBuilder::CommandBuilder(const wxString &cmdString) +CommandBuilder::CommandBuilder( + AudacityProject *project, const wxString &cmdString) : mValid(false) { - BuildCommand(cmdString); + BuildCommand(project, cmdString); } -CommandBuilder::CommandBuilder(const wxString &cmdName, const wxString ¶ms) +CommandBuilder::CommandBuilder(AudacityProject *project, + const wxString &cmdName, const wxString ¶ms) : mValid(false) { - BuildCommand(cmdName, params); + BuildCommand(project, cmdName, params); } CommandBuilder::~CommandBuilder() @@ -77,7 +79,8 @@ void CommandBuilder::Success(const OldStyleCommandPointer &cmd) mValid = true; } -void CommandBuilder::BuildCommand(const wxString &cmdName, +void CommandBuilder::BuildCommand(AudacityProject *project, + const wxString &cmdName, const wxString &cmdParamsArg) { // Stage 1: create a Command object of the right type @@ -97,7 +100,7 @@ void CommandBuilder::BuildCommand(const wxString &cmdName, #endif OldStyleCommandType *type = CommandDirectory::Get()->LookUp(wxT("BatchCommand")); wxASSERT(type != NULL); - mCommand = type->Create(nullptr); + mCommand = type->Create(project, nullptr); mCommand->SetParameter(wxT("CommandName"), cmdName); mCommand->SetParameter(wxT("ParamString"), cmdParamsArg); auto aCommand = std::make_shared(mCommand, output); @@ -181,7 +184,8 @@ void CommandBuilder::BuildCommand(const wxString &cmdName, #endif } -void CommandBuilder::BuildCommand(const wxString &cmdStringArg) +void CommandBuilder::BuildCommand( + AudacityProject *project, const wxString &cmdStringArg) { wxString cmdString(cmdStringArg); @@ -205,5 +209,5 @@ void CommandBuilder::BuildCommand(const wxString &cmdStringArg) cmdName.Trim(true); cmdParams.Trim(false); - BuildCommand(cmdName, cmdParams); + BuildCommand(project, cmdName, cmdParams); } diff --git a/src/commands/CommandBuilder.h b/src/commands/CommandBuilder.h index a0a3d879f..c9c9310d8 100644 --- a/src/commands/CommandBuilder.h +++ b/src/commands/CommandBuilder.h @@ -18,6 +18,7 @@ #include "../MemoryX.h" +class AudacityProject; class OldStyleCommand; using OldStyleCommandPointer = std::shared_ptr; class wxString; @@ -34,11 +35,12 @@ class CommandBuilder void Failure(const wxString &msg = {}); void Success(const OldStyleCommandPointer &cmd); - void BuildCommand(const wxString &cmdName, const wxString &cmdParams); - void BuildCommand(const wxString &cmdString); + void BuildCommand( AudacityProject *project, + const wxString &cmdName, const wxString &cmdParams); + void BuildCommand( AudacityProject *project, const wxString &cmdString); public: - CommandBuilder(const wxString &cmdString); - CommandBuilder(const wxString &cmdName, + CommandBuilder(AudacityProject *project, const wxString &cmdString); + CommandBuilder(AudacityProject *project, const wxString &cmdName, const wxString &cmdParams); ~CommandBuilder(); bool WasValid(); diff --git a/src/commands/CommandType.h b/src/commands/CommandType.h index 9fb343b2a..3ba5a9acc 100644 --- a/src/commands/CommandType.h +++ b/src/commands/CommandType.h @@ -63,7 +63,8 @@ public: virtual void BuildSignature(CommandSignature &signature) = 0; // Create a command instance with the specified output target - virtual OldStyleCommandPointer Create(std::unique_ptr &&target) = 0; + virtual OldStyleCommandPointer Create( + AudacityProject *project, std::unique_ptr &&target) = 0; }; #endif /* End of include guard: __COMMANDTYPE__ */ diff --git a/src/commands/ScriptCommandRelay.cpp b/src/commands/ScriptCommandRelay.cpp index 297633847..aace234e9 100644 --- a/src/commands/ScriptCommandRelay.cpp +++ b/src/commands/ScriptCommandRelay.cpp @@ -24,6 +24,7 @@ code out of ModuleManager. #include "CommandTargets.h" #include "CommandBuilder.h" #include "AppCommandEvent.h" +#include "../Project.h" #include #include #include @@ -70,7 +71,7 @@ void ScriptCommandRelay::PostCommand( int ExecCommand(wxString *pIn, wxString *pOut) { { - CommandBuilder builder(*pIn); + CommandBuilder builder(::GetActiveProject(), *pIn); if (builder.WasValid()) { OldStyleCommandPointer cmd = builder.GetCommand(); @@ -105,7 +106,7 @@ int ExecCommand(wxString *pIn, wxString *pOut) int ExecCommand2(wxString *pIn, wxString *pOut) { { - CommandBuilder builder(*pIn); + CommandBuilder builder(::GetActiveProject(), *pIn); if (builder.WasValid()) { OldStyleCommandPointer cmd = builder.GetCommand();