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******************************************************************//*******************************************************************/
54
55#ifndef __COMMANDTARGETS__
56#define __COMMANDTARGETS__
57
58#include <memory>
59#include <vector>
60#include <wx/string.h>
61#include <wx/thread.h>
62
63class wxStatusBar;
64
66class CommandProgressTarget /* not final */
67{
68public:
70 virtual void Update(double completed) = 0;
71};
72
74class AUDACITY_DLL_API CommandMessageTarget /* not final */
75{
76public:
77 CommandMessageTarget() {mCounts.push_back(0);}
78 virtual ~CommandMessageTarget() { Flush();}
79 virtual void Update(const wxString &message) = 0;
80 virtual void StartArray();
81 virtual void EndArray();
82 virtual void StartStruct();
83 virtual void EndStruct();
84 virtual void AddItem(const wxString &value , const wxString &name = {} );
85 virtual void AddBool(const bool value , const wxString &name = {} );
86 virtual void AddItem(const double value , const wxString &name = {} );
87 virtual void StartField( const wxString &name = {} );
88 virtual void EndField( );
89 virtual void Flush();
90 wxString Escaped( const wxString & str);
91 std::vector<int> mCounts;
92};
93
95{
96public:
99 void Update(const wxString &message) override { mTarget.Update( message );}
100 void StartArray() override { mTarget.StartArray();}
101 void EndArray() override { mTarget.EndArray();}
102 void StartStruct() override { mTarget.StartStruct();}
103 void EndStruct() override { mTarget.EndStruct();}
104 void AddItem(const wxString &value , const wxString &name = {} ) override
105 { mTarget.AddItem(value,name);}
106 void AddBool(const bool value , const wxString &name = {} ) override
107 { mTarget.AddBool(value,name);}
108 void AddItem(const double value , const wxString &name = {} ) override
109 { mTarget.AddItem(value,name);}
110 void StartField( const wxString &name = {} ) override
112 void EndField( ) override
113 { mTarget.EndField();}
114 void Flush() override
115 { mTarget.Flush();}
117};
118
120{
121public:
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 = {} )override;
128 virtual void AddBool(const bool value , const wxString &name = {} )override;
129 virtual void AddItem(const double value , const wxString &name = {} )override;
130 virtual void StartField( const wxString &name = {} )override;
131 virtual void EndField( ) override;
132};
133
135{
136public:
138 virtual void StartArray() override;
139 virtual void EndArray() override;
140 virtual void StartStruct() override;
141 virtual void EndStruct() override;
142 virtual void AddItem(const wxString &value , const wxString &name = {} )override;
143 virtual void AddBool(const bool value , const wxString &name = {} )override;
144 virtual void AddItem(const double value , const wxString &name = {} )override;
145 virtual void StartField( const wxString &name = {} )override;
146 virtual void EndField( ) override;
147};
148
151{
152public:
154 void Update(double WXUNUSED(completed)) override {}
155};
156
157#if 0
158
159//#include "../widgets/ProgressDialog.h" // Member variable
160
162class GUIProgressTarget final : public CommandProgressTarget
163{
164private:
165 ProgressDialog &mProgress;
166public:
167 GUIProgressTarget(ProgressDialog &pd)
168 : mProgress(pd)
169 {}
170 virtual ~GUIProgressTarget() {}
171 void Update(double completed) override
172 {
173 mProgress.Update(completed);
174 }
175};
176#endif
177
178
181{
182private:
183 std::unique_ptr<CommandMessageTarget> mTarget;
184public:
185 ProgressToMessageTarget(std::unique_ptr<CommandMessageTarget> &&target)
186 : mTarget(std::move(target))
187 { }
189 {
190 }
191 void Update(double completed) override
192 {
193 mTarget->Update(wxString::Format(wxT("%.2f%%"), completed*100));
194 }
195};
196
199{
200public:
202 void Update(const wxString &) override {}
203};
204
206class AUDACITY_DLL_API MessageBoxTarget final : public CommandMessageTarget
207{
208public:
209 virtual ~MessageBoxTarget() {}
210 void Update(const wxString &message) override;
211};
212
214class AUDACITY_DLL_API StatusBarTarget final : public CommandMessageTarget
215{
216private:
217 wxStatusBar &mStatus;
218public:
219 StatusBarTarget(wxStatusBar &sb)
220 : mStatus(sb)
221 {}
222 void Update(const wxString &message) override;
223};
224
227{
228private:
229 wxSemaphore mSemaphore;
230 wxString mBuffer;
231public:
233 : mSemaphore(0, 1),
234 mBuffer(wxEmptyString)
235 {
236 // Cater for handling long responses quickly.
237 mBuffer.Alloc(40000);
238 }
240 {
241 }
242 void Update(const wxString &message) override
243 {
244 mBuffer += message;
245 }
246 virtual void Flush() override
247 {
248 mSemaphore.Post();
249 }
250 wxString GetResponse()
251 {
252 mSemaphore.Wait();
253 return mBuffer;
254 }
255};
256
259{
260private:
261 std::unique_ptr<CommandMessageTarget> m1, m2;
262public:
263 CombinedMessageTarget(std::unique_ptr<CommandMessageTarget> &&t1,
264 std::unique_ptr<CommandMessageTarget> &&t2)
265 : m1(std::move(t1)), m2(std::move(t2))
266 {
267 wxASSERT(m1);
268 wxASSERT(m2);
269 }
271 {
272 }
273 void Update(const wxString &message) override
274 {
275 m1->Update(message);
276 m2->Update(message);
277 }
278};
279
280
287{
288public:
289 static std::unique_ptr<CommandProgressTarget> ProgressDefault()
290 {
291 return std::make_unique<NullProgressTarget>();
292 }
293
294 static std::shared_ptr<CommandMessageTarget> MessageDefault()
295 {
296 return std::make_shared<MessageBoxTarget>();
297 }
298};
299
305class CommandOutputTargets /* not final */
306{
307public:
308 std::unique_ptr<CommandProgressTarget> mProgressTarget;
309 std::shared_ptr<CommandMessageTarget> mStatusTarget;
310 std::shared_ptr<CommandMessageTarget> mErrorTarget;
311public:
312 // && is not a reference to a reference, but rather a way to allow reference to a temporary
313 // that will be gone or transferred after we have taken it. It's a reference to an xvalue,
314 // or 'expiring value'.
315 CommandOutputTargets(std::unique_ptr<CommandProgressTarget> &&pt = TargetFactory::ProgressDefault(),
316 std::shared_ptr<CommandMessageTarget> &&st = TargetFactory::MessageDefault(),
317 std::shared_ptr<CommandMessageTarget> &&et = TargetFactory::MessageDefault())
318 : mProgressTarget(std::move(pt)), mStatusTarget(st), mErrorTarget(et)
319 { }
321 {
322 }
323 // Lots of forwarding...
324 void Progress(double completed)
325 {
326 if (mProgressTarget)
327 mProgressTarget->Update(completed);
328 }
329 void Status(const wxString &status, bool bFlush=false)
330 {
331 if (mStatusTarget){
332 mStatusTarget->Update(status);
333 if( bFlush )
334 mStatusTarget->Flush();
335 }
336 }
337 void Error(const wxString &message)
338 {
339 if (mErrorTarget)
340 mErrorTarget->Update(message);
341 }
343 {
344 if (mStatusTarget)
345 mStatusTarget->StartArray();
346 }
347 void EndArray()
348 {
349 if (mStatusTarget)
350 mStatusTarget->EndArray();
351 }
353 {
354 if (mStatusTarget)
355 mStatusTarget->StartStruct();
356 }
358 {
359 if (mStatusTarget)
360 mStatusTarget->EndStruct();
361 }
362 void StartField(const wxString &name)
363 {
364 if (mStatusTarget)
365 mStatusTarget->StartField(name);
366 }
367 void EndField()
368 {
369 if (mStatusTarget)
370 mStatusTarget->EndField();
371 }
372 void AddItem(const wxString &value , const wxString &name = {} )
373 {
374 if (mStatusTarget)
375 mStatusTarget->AddItem( value, name );
376 }
377 void AddBool(const bool value , const wxString &name = {} )
378 {
379 if (mStatusTarget)
380 mStatusTarget->AddItem( value, name );
381 }
382 void AddItem(const double value , const wxString &name = {} )
383 {
384 if (mStatusTarget)
385 mStatusTarget->AddItem( value, name );
386 }
387};
388
389class AUDACITY_DLL_API LispifiedCommandOutputTargets
390 : public CommandOutputTargets
391{
392public :
395private:
397};
398
399class AUDACITY_DLL_API BriefCommandOutputTargets : public CommandOutputTargets
400{
401public :
404private:
406};
407
409{
410public:
412
413};
414
415#endif /* End of include guard: __COMMANDTARGETS__ */
#define str(a)
const TranslatableString name
Definition: Distortion.cpp:82
BriefCommandMessageTarget is a CommandOutputTarget that provides status in a briefer listing.
virtual void StartStruct() override
virtual void EndArray() override
BriefCommandMessageTarget(CommandMessageTarget &target)
virtual void EndStruct() override
virtual void AddBool(const bool value, const wxString &name={}) override
virtual void StartField(const wxString &name={}) override
virtual void AddItem(const double value, const wxString &name={}) override
virtual void EndField() override
virtual void AddItem(const wxString &value, const wxString &name={}) override
virtual void StartArray() 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 > m2
std::unique_ptr< CommandMessageTarget > m1
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
void Update(const wxString &message) override
CommandMessageTargetDecorator(CommandMessageTarget &target)
CommandMessageTarget & mTarget
Interface for objects that can receive (string) messages from a command.
virtual void AddBool(const bool value, const wxString &name={})
std::vector< int > mCounts
virtual ~CommandMessageTarget()
virtual void AddItem(const wxString &value, const wxString &name={})
virtual void Update(const wxString &message)=0
virtual void StartField(const wxString &name={})
virtual void EndArray()
virtual void EndField()
virtual void StartStruct()
virtual void EndStruct()
virtual void StartArray()
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)
std::shared_ptr< CommandMessageTarget > mErrorTarget
void AddBool(const bool value, const wxString &name={})
void Error(const wxString &message)
CommandOutputTargets(std::unique_ptr< CommandProgressTarget > &&pt=TargetFactory::ProgressDefault(), std::shared_ptr< CommandMessageTarget > &&st=TargetFactory::MessageDefault(), std::shared_ptr< CommandMessageTarget > &&et=TargetFactory::MessageDefault())
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
InteractiveOutputTargets is an output target that pops up a dialog, if necessary.
LispifiedCommandOutputTargets is a CommandOutputTargets that replaces the status message target with ...
CommandOutputTargets * pToRestore
LispyCommandMessageTarget is a CommandOutputTarget that provides status in a lispy style.
virtual void StartStruct() override
virtual void StartField(const wxString &name={}) override
LispyCommandMessageTarget(CommandMessageTarget &target)
virtual void EndArray() override
virtual void AddBool(const bool value, const wxString &name={}) override
virtual void EndField() override
virtual void EndStruct() override
virtual void StartArray() override
virtual void AddItem(const wxString &value, const wxString &name={}) override
Displays messages from a command in an AudacityMessageBox.
virtual ~MessageBoxTarget()
Used to ignore a command's message updates.
void Update(const wxString &) override
virtual ~NullMessageTarget()
Used to ignore a command's progress updates.
virtual ~NullProgressTarget()
void Update(double WXUNUSED(completed)) override
ProgressDialog Class.
ProgressResult Update(int value, const TranslatableString &message={})
ProgressToMessageTarget formats the percentage complete text as a message and sends it to that messag...
void Update(double completed) override
ProgressToMessageTarget(std::unique_ptr< CommandMessageTarget > &&target)
virtual ~ProgressToMessageTarget()
std::unique_ptr< CommandMessageTarget > mTarget
Constructs a response (to be sent back to a script)
virtual void Flush() override
void Update(const wxString &message) override
virtual ~ResponseTarget()
wxSemaphore mSemaphore
wxString GetResponse()
Displays messages from a command in a wxStatusBar.
wxStatusBar & mStatus
StatusBarTarget(wxStatusBar &sb)
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.