1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-11-23 17:30:17 +01:00

Break cycle of ScriptCommandRelay and CommandBuilder

This commit is contained in:
Paul Licameli
2019-07-09 13:11:42 -04:00
parent 7e1c469d7f
commit f2f6ff3a1b
5 changed files with 17 additions and 31 deletions

View File

@@ -26,7 +26,6 @@ system by constructing BatchCommandEval objects.
#include "CommandDirectory.h" #include "CommandDirectory.h"
#include "Command.h" #include "Command.h"
#include "ScriptCommandRelay.h"
#include "CommandContext.h" #include "CommandContext.h"
#include "CommandTargets.h" #include "CommandTargets.h"
#include "../Shuttle.h" #include "../Shuttle.h"
@@ -83,7 +82,7 @@ void CommandBuilder::BuildCommand(const wxString &cmdName,
{ {
// Stage 1: create a Command object of the right type // Stage 1: create a Command object of the right type
auto scriptOutput = ScriptCommandRelay::GetResponseTarget(); auto scriptOutput = std::make_shared< ResponseQueueTarget >();
auto output auto output
= std::make_unique<CommandOutputTargets>(std::make_unique<NullProgressTarget>(), = std::make_unique<CommandOutputTargets>(std::make_unique<NullProgressTarget>(),
scriptOutput, scriptOutput,
@@ -192,7 +191,8 @@ void CommandBuilder::BuildCommand(const wxString &cmdStringArg)
int splitAt = cmdString.Find(wxT(':')); int splitAt = cmdString.Find(wxT(':'));
if (splitAt < 0 && cmdString.Find(wxT(' ')) >= 0) { if (splitAt < 0 && cmdString.Find(wxT(' ')) >= 0) {
mError = wxT("Command is missing ':'"); mError = wxT("Command is missing ':'");
ScriptCommandRelay::SendResponse(wxT("\n")); ResponseQueueTarget::sResponseQueue().AddResponse(
Response{wxT("\n")});
mValid = false; mValid = false;
return; return;
} }

View File

@@ -458,3 +458,9 @@ void StatusBarTarget::Update(const wxString &message)
{ {
mStatus.SetStatusText(message, 0); mStatus.SetStatusText(message, 0);
} }
ResponseQueue &ResponseQueueTarget::sResponseQueue()
{
static ResponseQueue queue;
return queue;
}

View File

@@ -221,23 +221,23 @@ public:
void Update(const wxString &message) override; void Update(const wxString &message) override;
}; };
/// Adds messages to a response queue (to be sent back to a script) /// Adds messages to the global response queue (to be sent back to a script)
class ResponseQueueTarget final : public CommandMessageTarget class ResponseQueueTarget final : public CommandMessageTarget
{ {
private: private:
ResponseQueue &mResponseQueue;
wxString mBuffer; wxString mBuffer;
public: public:
ResponseQueueTarget(ResponseQueue &responseQueue) static ResponseQueue &sResponseQueue();
: mResponseQueue(responseQueue),
mBuffer( wxEmptyString ) ResponseQueueTarget()
: mBuffer( wxEmptyString )
{ } { }
virtual ~ResponseQueueTarget() virtual ~ResponseQueueTarget()
{ {
if( mBuffer.StartsWith("\n" ) ) if( mBuffer.StartsWith("\n" ) )
mBuffer = mBuffer.Mid( 1 ); mBuffer = mBuffer.Mid( 1 );
mResponseQueue.AddResponse( mBuffer ); sResponseQueue().AddResponse( mBuffer );
mResponseQueue.AddResponse(wxString(wxT("\n"))); sResponseQueue().AddResponse(wxString(wxT("\n")));
} }
void Update(const wxString &message) override void Update(const wxString &message) override
{ {

View File

@@ -31,7 +31,6 @@ code out of ModuleManager.
// Declare static class members // Declare static class members
CommandHandler *ScriptCommandRelay::sCmdHandler; CommandHandler *ScriptCommandRelay::sCmdHandler;
tpRegScriptServerFunc ScriptCommandRelay::sScriptFn; tpRegScriptServerFunc ScriptCommandRelay::sScriptFn;
ResponseQueue ScriptCommandRelay::sResponseQueue;
void ScriptCommandRelay::SetRegScriptServerFunc(tpRegScriptServerFunc scriptFn) void ScriptCommandRelay::SetRegScriptServerFunc(tpRegScriptServerFunc scriptFn)
{ {
@@ -162,22 +161,8 @@ void * ExecForLisp( char * pIn ){
}; };
/// Adds a response to the queue to be sent back to the script
void ScriptCommandRelay::SendResponse(const wxString &response)
{
sResponseQueue.AddResponse(response);
}
/// Gets a response from the queue (may block) /// Gets a response from the queue (may block)
Response ScriptCommandRelay::ReceiveResponse() Response ScriptCommandRelay::ReceiveResponse()
{ {
return ScriptCommandRelay::sResponseQueue.WaitAndGetResponse(); return ResponseQueueTarget::sResponseQueue().WaitAndGetResponse();
}
/// Get a pointer to a message target which allows commands to send responses
/// back to a script.
std::shared_ptr<ResponseQueueTarget> ScriptCommandRelay::GetResponseTarget()
{
// This should be deleted by a Command destructor
return std::make_shared<ResponseQueueTarget>(sResponseQueue);
} }

View File

@@ -22,9 +22,7 @@
class wxWindow; class wxWindow;
class CommandHandler; class CommandHandler;
class ResponseQueue;
class Response; class Response;
class ResponseQueueTarget;
class OldStyleCommand; class OldStyleCommand;
using OldStyleCommandPointer = std::shared_ptr<OldStyleCommand>; using OldStyleCommandPointer = std::shared_ptr<OldStyleCommand>;
class wxString; class wxString;
@@ -42,7 +40,6 @@ class ScriptCommandRelay
// N.B. Static class members also have to be declared in the .cpp file // N.B. Static class members also have to be declared in the .cpp file
static CommandHandler *sCmdHandler; static CommandHandler *sCmdHandler;
static tpRegScriptServerFunc sScriptFn; static tpRegScriptServerFunc sScriptFn;
static ResponseQueue sResponseQueue;
public: public:
@@ -52,9 +49,7 @@ class ScriptCommandRelay
static void Run(); static void Run();
static void PostCommand( static void PostCommand(
wxWindow *pWindow, const OldStyleCommandPointer &cmd); wxWindow *pWindow, const OldStyleCommandPointer &cmd);
static void SendResponse(const wxString &response);
static Response ReceiveResponse(); static Response ReceiveResponse();
static std::shared_ptr<ResponseQueueTarget> GetResponseTarget();
}; };
#endif /* End of include guard: __SCRIPT_COMMAND_RELAY__ */ #endif /* End of include guard: __SCRIPT_COMMAND_RELAY__ */