Audacity 3.2.0
CommandTargets.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity - A Digital Audio Editor
4 Copyright 1999-2009 Audacity Team
5 License: GPL v2 or later - see LICENSE.txt
6
7 Dan Horgan
8
9******************************************************************//*******************************************************************/
50
51#ifndef __COMMANDTARGETS__
52#define __COMMANDTARGETS__
53
54#include <memory>
55#include <vector>
56#include <wx/string.h>
57#include <wx/thread.h>
58
59class wxStatusBar;
60
62class CommandProgressTarget /* not final */
63{
64public:
66 virtual void Update(double completed) = 0;
67};
68
70class MENUS_API CommandMessageTarget /* not final */
71{
72public:
73 CommandMessageTarget() {mCounts.push_back(0);}
74 virtual ~CommandMessageTarget();
75 virtual void Update(const wxString &message) = 0;
76 virtual void StartArray();
77 virtual void EndArray();
78 virtual void StartStruct();
79 virtual void EndStruct();
80 virtual void AddItem(const wxString &value , const wxString &name = {} );
81 virtual void AddBool(const bool value , const wxString &name = {} );
82 virtual void AddItem(const double value , const wxString &name = {} );
83 virtual void StartField( const wxString &name = {} );
84 virtual void EndField( );
85 virtual void Flush();
86 wxString Escaped( const wxString & str);
87 std::vector<int> mCounts;
88};
89
91{
92public:
94 : mTarget(target) {}
96 void Update(const wxString &message) override { mTarget.Update( message );}
97 void StartArray() override { mTarget.StartArray();}
98 void EndArray() override { mTarget.EndArray();}
99 void StartStruct() override { mTarget.StartStruct();}
100 void EndStruct() override { mTarget.EndStruct();}
101 void AddItem(const wxString &value , const wxString &name = {} ) override
102 { mTarget.AddItem(value,name);}
103 void AddBool(const bool value , const wxString &name = {} ) override
104 { mTarget.AddBool(value,name);}
105 void AddItem(const double value , const wxString &name = {} ) override
106 { mTarget.AddItem(value,name);}
107 void StartField( const wxString &name = {} ) override
108 { mTarget.StartField(name);}
109 void EndField( ) override
110 { mTarget.EndField();}
111 void Flush() override
112 { mTarget.Flush();}
114};
115
117 : public CommandMessageTargetDecorator /* not final */
118{
119public:
123 virtual void StartArray() override;
124 virtual void EndArray() override;
125 virtual void StartStruct() override;
126 virtual void EndStruct() override;
127 virtual void AddItem(const wxString &value , const wxString &name = {})
128 override;
129 virtual void AddBool(const bool value , const wxString &name = {})
130 override;
131 virtual void AddItem(const double value , const wxString &name = {})
132 override;
133 virtual void StartField( const wxString &name = {} )override;
134 virtual void EndField( ) override;
135};
136
138 : public CommandMessageTargetDecorator /* not final */
139{
140public:
144 virtual void StartArray() override;
145 virtual void EndArray() override;
146 virtual void StartStruct() override;
147 virtual void EndStruct() override;
148 virtual void AddItem(const wxString &value , const wxString &name = {})
149 override;
150 virtual void AddBool(const bool value , const wxString &name = {})
151 override;
152 virtual void AddItem(const double value , const wxString &name = {})
153 override;
154 virtual void StartField( const wxString &name = {} )override;
155 virtual void EndField( ) override;
156};
157
159class MENUS_API NullProgressTarget final : public CommandProgressTarget
160{
161public:
163 void Update(double WXUNUSED(completed)) override {}
164};
165
167class MENUS_API ProgressToMessageTarget final : public CommandProgressTarget
168{
169private:
170 std::unique_ptr<CommandMessageTarget> mTarget;
171public:
172 ProgressToMessageTarget(std::unique_ptr<CommandMessageTarget> &&target)
173 : mTarget(std::move(target))
174 { }
176 void Update(double completed) override
177 {
178 mTarget->Update(wxString::Format(wxT("%.2f%%"), completed*100));
179 }
180};
181
183class MENUS_API NullMessageTarget final : public CommandMessageTarget
184{
185public:
187 void Update(const wxString &) override {}
188};
189
191class MENUS_API MessageBoxTarget final : public CommandMessageTarget
192{
193public:
195 void Update(const wxString &message) override;
196};
197
199class MENUS_API ResponseTarget final : public CommandMessageTarget
200{
201private:
202 wxSemaphore mSemaphore;
203 wxString mBuffer;
204public:
206 : mSemaphore(0, 1),
207 mBuffer(wxEmptyString)
208 {
209 // Cater for handling long responses quickly.
210 mBuffer.Alloc(40000);
211 }
212 ~ResponseTarget() override;
213 void Update(const wxString &message) override
214 {
215 mBuffer += message;
216 }
217 virtual void Flush() override
218 {
219 mSemaphore.Post();
220 }
221 wxString GetResponse()
222 {
223 mSemaphore.Wait();
224 return mBuffer;
225 }
226};
227
229class MENUS_API CombinedMessageTarget final : public CommandMessageTarget
230{
231private:
232 std::unique_ptr<CommandMessageTarget> m1, m2;
233public:
234 CombinedMessageTarget(std::unique_ptr<CommandMessageTarget> &&t1,
235 std::unique_ptr<CommandMessageTarget> &&t2)
236 : m1(std::move(t1)), m2(std::move(t2))
237 {
238 wxASSERT(m1);
239 wxASSERT(m2);
240 }
242 void Update(const wxString &message) override
243 {
244 m1->Update(message);
245 m2->Update(message);
246 }
247};
248
249
256{
257public:
258 static std::unique_ptr<CommandProgressTarget> ProgressDefault()
259 {
260 return std::make_unique<NullProgressTarget>();
261 }
262
263 static std::shared_ptr<CommandMessageTarget> MessageDefault()
264 {
265 return std::make_shared<MessageBoxTarget>();
266 }
267};
268
274class CommandOutputTargets /* not final */
275{
276public:
277 std::unique_ptr<CommandProgressTarget> mProgressTarget;
278 std::shared_ptr<CommandMessageTarget> mStatusTarget;
279 std::shared_ptr<CommandMessageTarget> mErrorTarget;
280public:
282 std::unique_ptr<CommandProgressTarget> pt =
284 std::shared_ptr<CommandMessageTarget> st =
286 std::shared_ptr<CommandMessageTarget> et =
288 : mProgressTarget(std::move(pt)), mStatusTarget(st), mErrorTarget(et)
289 { }
291 {
292 }
293 // Lots of forwarding...
294 void Progress(double completed)
295 {
296 if (mProgressTarget)
297 mProgressTarget->Update(completed);
298 }
299 void Status(const wxString &status, bool bFlush=false)
300 {
301 if (mStatusTarget){
302 mStatusTarget->Update(status);
303 if( bFlush )
304 mStatusTarget->Flush();
305 }
306 }
307 void Error(const wxString &message)
308 {
309 if (mErrorTarget)
310 mErrorTarget->Update(message);
311 }
313 {
314 if (mStatusTarget)
315 mStatusTarget->StartArray();
316 }
317 void EndArray()
318 {
319 if (mStatusTarget)
320 mStatusTarget->EndArray();
321 }
323 {
324 if (mStatusTarget)
325 mStatusTarget->StartStruct();
326 }
328 {
329 if (mStatusTarget)
330 mStatusTarget->EndStruct();
331 }
332 void StartField(const wxString &name)
333 {
334 if (mStatusTarget)
335 mStatusTarget->StartField(name);
336 }
337 void EndField()
338 {
339 if (mStatusTarget)
340 mStatusTarget->EndField();
341 }
342 void AddItem(const wxString &value , const wxString &name = {} )
343 {
344 if (mStatusTarget)
345 mStatusTarget->AddItem( value, name );
346 }
347 void AddBool(const bool value , const wxString &name = {} )
348 {
349 if (mStatusTarget)
350 mStatusTarget->AddItem( value, name );
351 }
352 void AddItem(const double value , const wxString &name = {} )
353 {
354 if (mStatusTarget)
355 mStatusTarget->AddItem( value, name );
356 }
357};
358
360 : public CommandOutputTargets
361{
362public :
365private:
367};
368
370{
371public :
374private:
376};
377
378#endif /* End of include guard: __COMMANDTARGETS__ */
wxT("CloseDown"))
#define str(a)
wxString name
Definition: TagsEditor.cpp:166
BriefCommandMessageTarget is a CommandOutputTarget that provides status in a briefer listing.
BriefCommandMessageTarget(CommandMessageTarget &target)
virtual void AddItem(const double value, const wxString &name={}) override
virtual void AddItem(const wxString &value, const wxString &name={}) override
~BriefCommandMessageTarget() override
BriefCommandOutputTargets is a CommandOutputTargets that replaces the status message target with the ...
CommandOutputTargets * pToRestore
Sends messages to two message targets at once.
CombinedMessageTarget(std::unique_ptr< CommandMessageTarget > &&t1, std::unique_ptr< CommandMessageTarget > &&t2)
std::unique_ptr< CommandMessageTarget > m1
~CombinedMessageTarget() override
void Update(const wxString &message) override
CommandMessageTargetDecorator is a CommandOutputTarget that forwards its work on to another one....
void AddBool(const bool value, const wxString &name={}) override
void StartField(const wxString &name={}) override
void AddItem(const double value, const wxString &name={}) override
void AddItem(const wxString &value, const wxString &name={}) override
~CommandMessageTargetDecorator() override
void Update(const wxString &message) override
CommandMessageTargetDecorator(CommandMessageTarget &target)
CommandMessageTarget & mTarget
Interface for objects that can receive (string) messages from a command.
std::vector< int > mCounts
virtual void Update(const wxString &message)=0
CommandOutputTargets a mix of three output classes to output progress indication, status messages and...
std::shared_ptr< CommandMessageTarget > mStatusTarget
void AddItem(const double value, const wxString &name={})
std::unique_ptr< CommandProgressTarget > mProgressTarget
void StartField(const wxString &name)
CommandOutputTargets(std::unique_ptr< CommandProgressTarget > pt=TargetFactory::ProgressDefault(), std::shared_ptr< CommandMessageTarget > st=TargetFactory::MessageDefault(), std::shared_ptr< CommandMessageTarget > et=TargetFactory::MessageDefault())
std::shared_ptr< CommandMessageTarget > mErrorTarget
void AddBool(const bool value, const wxString &name={})
void Error(const wxString &message)
void AddItem(const wxString &value, const wxString &name={})
void Progress(double completed)
void Status(const wxString &status, bool bFlush=false)
Interface for objects that can receive command progress information.
virtual ~CommandProgressTarget()
virtual void Update(double completed)=0
LispifiedCommandOutputTargets is a CommandOutputTargets that replaces the status message target with ...
CommandOutputTargets * pToRestore
LispyCommandMessageTarget is a CommandOutputTarget that provides status in a lispy style.
LispyCommandMessageTarget(CommandMessageTarget &target)
~LispyCommandMessageTarget() override
Displays messages from a command in a BasicUI::MessageBox.
~MessageBoxTarget() override
Used to ignore a command's message updates.
void Update(const wxString &) override
~NullMessageTarget() override
Used to ignore a command's progress updates.
~NullProgressTarget() override
void Update(double WXUNUSED(completed)) override
ProgressToMessageTarget formats the percentage complete text as a message and sends it to that messag...
void Update(double completed) override
~ProgressToMessageTarget() override
ProgressToMessageTarget(std::unique_ptr< CommandMessageTarget > &&target)
std::unique_ptr< CommandMessageTarget > mTarget
Constructs a response (to be sent back to a script)
~ResponseTarget() override
virtual void Flush() override
void Update(const wxString &message) override
wxSemaphore mSemaphore
wxString GetResponse()
TargetFactory makes Command output targets. By default, we ignore progress updates but display all ot...
static std::unique_ptr< CommandProgressTarget > ProgressDefault()
static std::shared_ptr< CommandMessageTarget > MessageDefault()
STL namespace.