Manage Commands and CommandOutputTarget objects with smart pointers

This commit is contained in:
Paul Licameli 2016-03-31 07:40:05 -04:00
parent f4441d7476
commit e8ad90b3c9
42 changed files with 164 additions and 172 deletions

View File

@ -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<CommandOutputTarget>(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);

View File

@ -97,7 +97,7 @@ class ScreenFrame final : public wxFrame
void OnCaptureFirstTrack(wxCommandEvent & event);
void OnCaptureSecondTrack(wxCommandEvent & event);
ScreenshotCommand *CreateCommand();
std::unique_ptr<ScreenshotCommand> CreateCommand();
wxCheckBox *mDelayCheckBox;
wxTextCtrl *mDirectoryTextBox;
@ -105,7 +105,7 @@ class ScreenFrame final : public wxFrame
wxToggleButton *mWhite;
wxStatusBar *mStatus;
ScreenshotCommand *mCommand;
std::unique_ptr<ScreenshotCommand> 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<ScreenshotCommand> ScreenFrame::CreateCommand()
{
wxASSERT(mStatus != NULL);
CommandOutputTarget *output =
new CommandOutputTarget(new NullProgressTarget(),
auto output =
std::make_unique<CommandOutputTarget>(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<ScreenshotCommand>(*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()

View File

@ -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;
}

View File

@ -18,15 +18,17 @@
#include <wx/event.h>
#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<Command>;
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)

View File

@ -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<CommandOutputTarget> &&target)
{
return new BatchEvalCommand(*this, target);
return std::make_shared<BatchEvalCommand>(*this, std::move(target));
}
bool BatchEvalCommand::Apply(CommandExecutionContext WXUNUSED(context))

View File

@ -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<CommandOutputTarget> &&target) override;
};
class BatchEvalCommand final : public CommandImplementation
{
public:
BatchEvalCommand(CommandType &type,
CommandOutputTarget *target)
: CommandImplementation(type, target)
std::unique_ptr<CommandOutputTarget> &&target)
: CommandImplementation(type, std::move(target))
{ }
virtual ~BatchEvalCommand();

View File

@ -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<CommandOutputTarget> &&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,

View File

@ -70,17 +70,19 @@ public:
virtual bool Apply(CommandExecutionContext context) = 0;
};
using CommandHolder = std::shared_ptr<Command>;
// 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 &paramName, const wxVariant &paramValue);
protected:
CommandOutputTarget *mOutput;
std::unique_ptr<CommandOutputTarget> 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<CommandOutputTarget> &&output);
virtual ~CommandImplementation();

View File

@ -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 &params)
: 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<CommandOutputTarget>(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<ApplyAndSendResponse>(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<ApplyAndSendResponse>(mCommand));
}
void CommandBuilder::BuildCommand(const wxString &cmdStringArg)

View File

@ -17,6 +17,7 @@
#define __COMMANDBUILDER__
class Command;
using CommandHolder = std::shared_ptr<Command>;
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__ */

View File

@ -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();
}

View File

@ -17,8 +17,10 @@
#define __COMMANDTYPE__
#include "CommandMisc.h"
#include "../MemoryX.h"
class Command;
using CommandHolder = std::shared_ptr<Command>;
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<CommandOutputTarget> &&target) = 0;
};
#endif /* End of include guard: __COMMANDTYPE__ */

View File

@ -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<CommandOutputTarget> &&target)
{
return new CompareAudioCommand(*this, target);
return std::make_shared<CompareAudioCommand>(*this, std::move(target));
}
// Update member variables with project selection data (and validate)

View File

@ -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<CommandOutputTarget> &&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<CommandOutputTarget> &&target)
: CommandImplementation(type, std::move(target))
{ }
bool Apply(CommandExecutionContext context) override;
};

View File

@ -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<CommandOutputTarget> &&target)
{
return new ExecMenuCommand(*this, target);
return std::make_shared<ExecMenuCommand>(*this, std::move(target));
}
bool ExecMenuCommand::Apply(CommandExecutionContext context)

View File

@ -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<CommandOutputTarget> &&target) override;
};
class ExecMenuCommand final : public CommandImplementation
{
public:
ExecMenuCommand(CommandType &type,
CommandOutputTarget *target)
: CommandImplementation(type, target)
std::unique_ptr<CommandOutputTarget> &&target)
: CommandImplementation(type, std::move(target))
{ }
virtual ~ExecMenuCommand() { }
bool Apply(CommandExecutionContext context) override;

View File

@ -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<CommandOutputTarget> &&target)
{
return new GetAllMenuCommands(*this, target);
return std::make_shared<GetAllMenuCommands>(*this, std::move(target));
}
bool GetAllMenuCommands::Apply(CommandExecutionContext context)

View File

@ -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<CommandOutputTarget> &&target) override;
};
class GetAllMenuCommands final : public CommandImplementation
{
public:
GetAllMenuCommands(CommandType &type,
CommandOutputTarget *target)
: CommandImplementation(type, target)
std::unique_ptr<CommandOutputTarget> &&target)
: CommandImplementation(type, std::move(target))
{ }
virtual ~GetAllMenuCommands()

View File

@ -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<CommandOutputTarget> &&target)
{
return new GetProjectInfoCommand(*this, target);
return std::make_shared<GetProjectInfoCommand>(*this, std::move(target));
}

View File

@ -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<CommandOutputTarget> &&target) override;
};
class GetProjectInfoCommand final : public CommandImplementation
{
public:
GetProjectInfoCommand(CommandType &type, CommandOutputTarget *target)
: CommandImplementation(type, target)
GetProjectInfoCommand(CommandType &type, std::unique_ptr<CommandOutputTarget> &&target)
: CommandImplementation(type, std::move(target))
{ }
virtual ~GetProjectInfoCommand()
{ }

View File

@ -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<CommandOutputTarget> &&target)
{
return new GetTrackInfoCommand(*this, target);
return std::make_shared<GetTrackInfoCommand>(*this, std::move(target));
}

View File

@ -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<CommandOutputTarget> &&target) override;
};
class GetTrackInfoCommand final : public CommandImplementation
{
public:
GetTrackInfoCommand(CommandType &type, CommandOutputTarget *target)
: CommandImplementation(type, target)
GetTrackInfoCommand(CommandType &type, std::unique_ptr<CommandOutputTarget> &&target)
: CommandImplementation(type, std::move(target))
{ }
virtual ~GetTrackInfoCommand()
{ }

View File

@ -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<CommandOutputTarget> &&target)
{
return new HelpCommand(*this, target);
return std::make_shared<HelpCommand>(*this, std::move(target));
}
bool HelpCommand::Apply(CommandExecutionContext WXUNUSED(context))

View File

@ -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<CommandOutputTarget> &&target) override;
};
class HelpCommand final : public CommandImplementation
{
public:
HelpCommand(HelpCommandType &type, CommandOutputTarget *target)
: CommandImplementation(type, target) { }
HelpCommand(HelpCommandType &type, std::unique_ptr<CommandOutputTarget> &&target)
: CommandImplementation(type, std::move(target)) { }
bool Apply(CommandExecutionContext context) override;
};

View File

@ -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<CommandOutputTarget> &&target)
{
return new ImportCommand(*this, target);
return std::make_shared<ImportCommand>(*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<CommandOutputTarget> &&target)
{
return new ExportCommand(*this, target);
return std::make_shared<ExportCommand>(*this, std::move(target));
}
bool ExportCommand::Apply(CommandExecutionContext context)

View File

@ -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<CommandOutputTarget> &&target) override;
};
class ImportCommand final : public CommandImplementation
{
public:
ImportCommand(CommandType &type,
CommandOutputTarget *target)
: CommandImplementation(type, target)
std::unique_ptr<CommandOutputTarget> &&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<CommandOutputTarget> &&target) override;
};
class ExportCommand final : public CommandImplementation
{
public:
ExportCommand(CommandType &type,
CommandOutputTarget *target)
: CommandImplementation(type, target)
std::unique_ptr<CommandOutputTarget> &&target)
: CommandImplementation(type, std::move(target))
{ }
virtual ~ExportCommand();

View File

@ -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<CommandOutputTarget> &&target)
{
return new MessageCommand(*this, target);
return std::make_shared<MessageCommand>(*this, std::move(target));
}
bool MessageCommand::Apply(CommandExecutionContext WXUNUSED(context))

View File

@ -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<CommandOutputTarget> &&target) override;
};
class MessageCommand final : public CommandImplementation
{
public:
MessageCommand(CommandType &type,
CommandOutputTarget *target)
: CommandImplementation(type, target) {}
std::unique_ptr<CommandOutputTarget> &&target)
: CommandImplementation(type, std::move(target)) {}
bool Apply(CommandExecutionContext context) override;
};

View File

@ -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<CommandOutputTarget> &&target)
{
return new OpenProjectCommand(*this, target);
return std::make_shared<OpenProjectCommand>(*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<CommandOutputTarget> &&target)
{
return new SaveProjectCommand(*this, target);
return std::make_shared<SaveProjectCommand>(*this, std::move(target));
}
bool SaveProjectCommand::Apply(CommandExecutionContext context)

View File

@ -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<CommandOutputTarget> &&target) override;
};
class OpenProjectCommand final : public CommandImplementation
{
public:
OpenProjectCommand(CommandType &type,
CommandOutputTarget *target)
: CommandImplementation(type, target)
std::unique_ptr<CommandOutputTarget> &&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<CommandOutputTarget> &&target) override;
};
class SaveProjectCommand final : public CommandImplementation
{
public:
SaveProjectCommand(CommandType &type,
CommandOutputTarget *target)
: CommandImplementation(type, target)
std::unique_ptr<CommandOutputTarget> &&target)
: CommandImplementation(type, std::move(target))
{ }
virtual ~SaveProjectCommand();

View File

@ -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<CommandOutputTarget> &&target)
{
return new GetPreferenceCommand(*this, target);
return std::make_shared<GetPreferenceCommand>(*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<CommandOutputTarget> &&target)
{
return new SetPreferenceCommand(*this, target);
return std::make_shared<SetPreferenceCommand>(*this, std::move(target));
}
bool SetPreferenceCommand::Apply(CommandExecutionContext WXUNUSED(context))

View File

@ -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<CommandOutputTarget> &&target) override;
};
class GetPreferenceCommand final : public CommandImplementation
{
public:
GetPreferenceCommand(CommandType &type,
CommandOutputTarget *target)
: CommandImplementation(type, target)
std::unique_ptr<CommandOutputTarget> &&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<CommandOutputTarget> &&target) override;
};
class SetPreferenceCommand final : public CommandImplementation
{
public:
SetPreferenceCommand(CommandType &type,
CommandOutputTarget *target)
: CommandImplementation(type, target)
std::unique_ptr<CommandOutputTarget> &&target)
: CommandImplementation(type, std::move(target))
{ }
virtual ~SetPreferenceCommand();

View File

@ -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<CommandOutputTarget> &&target)
{
return new ScreenshotCommand(*this, target);
return std::make_shared<ScreenshotCommand>(*this, std::move(target));
}
wxString ScreenshotCommand::MakeFileName(const wxString &path, const wxString &basename)

View File

@ -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<CommandOutputTarget> &&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<CommandOutputTarget> &&output,
wxWindow *ignore = NULL)
: CommandImplementation(type, output),
: CommandImplementation(type, std::move(output)),
mIgnore(ignore),
mBackground(false)
{ }

View File

@ -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.

View File

@ -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<Command>;
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();

View File

@ -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<CommandOutputTarget> &&target)
{
return new SelectCommand(*this, target);
return std::make_shared<SelectCommand>(*this, std::move(target));
}
bool SelectCommand::Apply(CommandExecutionContext context)

View File

@ -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<CommandOutputTarget> &&target) override;
};
class SelectCommand final : public CommandImplementation
{
public:
SelectCommand(SelectCommandType &type, CommandOutputTarget *target)
: CommandImplementation(type, target) { }
SelectCommand(SelectCommandType &type, std::unique_ptr<CommandOutputTarget> &&target)
: CommandImplementation(type, std::move(target)) { }
bool Apply(CommandExecutionContext context) override;
};

View File

@ -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<CommandOutputTarget> &&target)
{
return new SetProjectInfoCommand(*this, target);
return std::make_shared<SetProjectInfoCommand>(*this, std::move(target));
}

View File

@ -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<CommandOutputTarget> &&target) override;
};
class SetProjectInfoCommand final : public CommandImplementation
{
public:
SetProjectInfoCommand(CommandType &type, CommandOutputTarget *target)
: CommandImplementation(type, target)
SetProjectInfoCommand(CommandType &type, std::unique_ptr<CommandOutputTarget> &&target)
: CommandImplementation(type, std::move(target))
{ }
virtual ~SetProjectInfoCommand()
{ }

View File

@ -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<CommandOutputTarget> &&target)
{
return new SetTrackInfoCommand(*this, target);
return std::make_shared<SetTrackInfoCommand>(*this, std::move(target));
}
bool SetTrackInfoCommand::Apply(CommandExecutionContext context)

View File

@ -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<CommandOutputTarget> &&target) override;
};
class SetTrackInfoCommand final : public CommandImplementation
{
public:
SetTrackInfoCommand(CommandType &type, CommandOutputTarget *target)
: CommandImplementation(type, target)
SetTrackInfoCommand(CommandType &type, std::unique_ptr<CommandOutputTarget> &&target)
: CommandImplementation(type, std::move(target))
{ }
virtual ~SetTrackInfoCommand()
{ }