diff --git a/src/commands/CommandBuilder.cpp b/src/commands/CommandBuilder.cpp index 6d549123c..41a729172 100644 --- a/src/commands/CommandBuilder.cpp +++ b/src/commands/CommandBuilder.cpp @@ -26,7 +26,6 @@ system by constructing BatchCommandEval objects. #include "CommandDirectory.h" #include "Command.h" -#include "ScriptCommandRelay.h" #include "CommandContext.h" #include "CommandTargets.h" #include "../Shuttle.h" @@ -83,7 +82,7 @@ void CommandBuilder::BuildCommand(const wxString &cmdName, { // Stage 1: create a Command object of the right type - auto scriptOutput = ScriptCommandRelay::GetResponseTarget(); + auto scriptOutput = std::make_shared< ResponseQueueTarget >(); auto output = std::make_unique(std::make_unique(), scriptOutput, @@ -192,7 +191,8 @@ void CommandBuilder::BuildCommand(const wxString &cmdStringArg) int splitAt = cmdString.Find(wxT(':')); if (splitAt < 0 && cmdString.Find(wxT(' ')) >= 0) { mError = wxT("Command is missing ':'"); - ScriptCommandRelay::SendResponse(wxT("\n")); + ResponseQueueTarget::sResponseQueue().AddResponse( + Response{wxT("\n")}); mValid = false; return; } diff --git a/src/commands/CommandTargets.cpp b/src/commands/CommandTargets.cpp index edccfbb6d..d7ca1057b 100644 --- a/src/commands/CommandTargets.cpp +++ b/src/commands/CommandTargets.cpp @@ -458,3 +458,9 @@ void StatusBarTarget::Update(const wxString &message) { mStatus.SetStatusText(message, 0); } + +ResponseQueue &ResponseQueueTarget::sResponseQueue() +{ + static ResponseQueue queue; + return queue; +} diff --git a/src/commands/CommandTargets.h b/src/commands/CommandTargets.h index 75471be98..c5ad18dfb 100644 --- a/src/commands/CommandTargets.h +++ b/src/commands/CommandTargets.h @@ -221,23 +221,23 @@ public: 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 { private: - ResponseQueue &mResponseQueue; wxString mBuffer; public: - ResponseQueueTarget(ResponseQueue &responseQueue) - : mResponseQueue(responseQueue), - mBuffer( wxEmptyString ) + static ResponseQueue &sResponseQueue(); + + ResponseQueueTarget() + : mBuffer( wxEmptyString ) { } virtual ~ResponseQueueTarget() { if( mBuffer.StartsWith("\n" ) ) mBuffer = mBuffer.Mid( 1 ); - mResponseQueue.AddResponse( mBuffer ); - mResponseQueue.AddResponse(wxString(wxT("\n"))); + sResponseQueue().AddResponse( mBuffer ); + sResponseQueue().AddResponse(wxString(wxT("\n"))); } void Update(const wxString &message) override { diff --git a/src/commands/ScriptCommandRelay.cpp b/src/commands/ScriptCommandRelay.cpp index a0d57426b..297633847 100644 --- a/src/commands/ScriptCommandRelay.cpp +++ b/src/commands/ScriptCommandRelay.cpp @@ -31,7 +31,6 @@ code out of ModuleManager. // Declare static class members CommandHandler *ScriptCommandRelay::sCmdHandler; tpRegScriptServerFunc ScriptCommandRelay::sScriptFn; -ResponseQueue ScriptCommandRelay::sResponseQueue; 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) Response ScriptCommandRelay::ReceiveResponse() { - return ScriptCommandRelay::sResponseQueue.WaitAndGetResponse(); -} - -/// Get a pointer to a message target which allows commands to send responses -/// back to a script. -std::shared_ptr ScriptCommandRelay::GetResponseTarget() -{ - // This should be deleted by a Command destructor - return std::make_shared(sResponseQueue); + return ResponseQueueTarget::sResponseQueue().WaitAndGetResponse(); } diff --git a/src/commands/ScriptCommandRelay.h b/src/commands/ScriptCommandRelay.h index 999b0fdac..cb9223709 100644 --- a/src/commands/ScriptCommandRelay.h +++ b/src/commands/ScriptCommandRelay.h @@ -22,9 +22,7 @@ class wxWindow; class CommandHandler; -class ResponseQueue; class Response; -class ResponseQueueTarget; class OldStyleCommand; using OldStyleCommandPointer = std::shared_ptr; class wxString; @@ -42,7 +40,6 @@ class ScriptCommandRelay // N.B. Static class members also have to be declared in the .cpp file static CommandHandler *sCmdHandler; static tpRegScriptServerFunc sScriptFn; - static ResponseQueue sResponseQueue; public: @@ -52,9 +49,7 @@ class ScriptCommandRelay static void Run(); static void PostCommand( wxWindow *pWindow, const OldStyleCommandPointer &cmd); - static void SendResponse(const wxString &response); static Response ReceiveResponse(); - static std::shared_ptr GetResponseTarget(); }; #endif /* End of include guard: __SCRIPT_COMMAND_RELAY__ */