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.
This commit is contained in:
Paul Licameli 2019-05-21 21:20:58 -04:00
parent 1e3779c2f3
commit a1eeb528b7
6 changed files with 28 additions and 18 deletions

View File

@ -39,9 +39,10 @@ void BatchEvalCommandType::BuildSignature(CommandSignature &signature)
signature.AddParameter(wxT("MacroName"), wxT(""), std::move(macroValidator));
}
OldStyleCommandPointer BatchEvalCommandType::Create(std::unique_ptr<CommandOutputTargets> && WXUNUSED(target))
OldStyleCommandPointer BatchEvalCommandType::Create( AudacityProject *project,
std::unique_ptr<CommandOutputTargets> && WXUNUSED(target))
{
return std::make_shared<BatchEvalCommand>(*GetActiveProject(), *this);
return std::make_shared<BatchEvalCommand>(*project, *this);
}
bool BatchEvalCommand::Apply(const CommandContext & context)

View File

@ -33,7 +33,8 @@ class BatchEvalCommandType final : public OldStyleCommandType
public:
ComponentInterfaceSymbol BuildName() override;
void BuildSignature(CommandSignature &signature) override;
OldStyleCommandPointer Create(std::unique_ptr<CommandOutputTargets> &&target) override;
OldStyleCommandPointer Create( AudacityProject *project,
std::unique_ptr<CommandOutputTargets> &&target) override;
};
class BatchEvalCommand final : public CommandImplementation

View File

@ -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 &params)
CommandBuilder::CommandBuilder(AudacityProject *project,
const wxString &cmdName, const wxString &params)
: 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<ApplyAndSendResponse>(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);
}

View File

@ -18,6 +18,7 @@
#include "../MemoryX.h"
class AudacityProject;
class OldStyleCommand;
using OldStyleCommandPointer = std::shared_ptr<OldStyleCommand>;
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();

View File

@ -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<CommandOutputTargets> &&target) = 0;
virtual OldStyleCommandPointer Create(
AudacityProject *project, std::unique_ptr<CommandOutputTargets> &&target) = 0;
};
#endif /* End of include guard: __COMMANDTYPE__ */

View File

@ -24,6 +24,7 @@ code out of ModuleManager.
#include "CommandTargets.h"
#include "CommandBuilder.h"
#include "AppCommandEvent.h"
#include "../Project.h"
#include <wx/app.h>
#include <wx/window.h>
#include <wx/string.h>
@ -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();