Audacity  3.0.3
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 - 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 
63 class wxStatusBar;
64 
66 class CommandProgressTarget /* not final */
67 {
68 public:
70  virtual void Update(double completed) = 0;
71 };
72 
74 class AUDACITY_DLL_API CommandMessageTarget /* not final */
75 {
76 public:
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 {
96 public:
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
111  { mTarget.StartField(name);}
112  void EndField( ) override
113  { mTarget.EndField();}
114  void Flush() override
115  { mTarget.Flush();}
117 };
118 
120 {
121 public:
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 {
136 public:
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 {
152 public:
153  virtual ~NullProgressTarget() {}
154  void Update(double WXUNUSED(completed)) override {}
155 };
156 
157 #if 0
158 
159 //#include "../widgets/ProgressDialog.h" // Member variable
160 
162 class GUIProgressTarget final : public CommandProgressTarget
163 {
164 private:
165  ProgressDialog &mProgress;
166 public:
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 {
182 private:
183  std::unique_ptr<CommandMessageTarget> mTarget;
184 public:
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 {
200 public:
201  virtual ~NullMessageTarget() {}
202  void Update(const wxString &) override {}
203 };
204 
206 class AUDACITY_DLL_API MessageBoxTarget final : public CommandMessageTarget
207 {
208 public:
209  virtual ~MessageBoxTarget() {}
210  void Update(const wxString &message) override;
211 };
212 
214 class AUDACITY_DLL_API StatusBarTarget final : public CommandMessageTarget
215 {
216 private:
217  wxStatusBar &mStatus;
218 public:
219  StatusBarTarget(wxStatusBar &sb)
220  : mStatus(sb)
221  {}
222  void Update(const wxString &message) override;
223 };
224 
227 {
228 private:
229  wxSemaphore mSemaphore;
230  wxString mBuffer;
231 public:
233  : mBuffer(wxEmptyString),
234  mSemaphore(0, 1)
235  {
236  // Cater for handling long responses quickly.
237  mBuffer.Alloc(40000);
238  }
239  virtual ~ResponseTarget()
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 {
260 private:
261  std::unique_ptr<CommandMessageTarget> m1, m2;
262 public:
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 {
288 public:
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 
305 class CommandOutputTargets /* not final */
306 {
307 public:
308  std::unique_ptr<CommandProgressTarget> mProgressTarget;
309  std::shared_ptr<CommandMessageTarget> mStatusTarget;
310  std::shared_ptr<CommandMessageTarget> mErrorTarget;
311 public:
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  }
342  void StartArray()
343  {
344  if (mStatusTarget)
345  mStatusTarget->StartArray();
346  }
347  void EndArray()
348  {
349  if (mStatusTarget)
350  mStatusTarget->EndArray();
351  }
352  void StartStruct()
353  {
354  if (mStatusTarget)
355  mStatusTarget->StartStruct();
356  }
357  void EndStruct()
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 
389 class AUDACITY_DLL_API LispifiedCommandOutputTargets
390  : public CommandOutputTargets
391 {
392 public :
395 private:
397 };
398 
399 class AUDACITY_DLL_API BriefCommandOutputTargets : public CommandOutputTargets
400 {
401 public :
404 private:
406 };
407 
409 {
410 public:
412 
413 };
414 
415 #endif /* End of include guard: __COMMANDTARGETS__ */
CommandOutputTargets::StartStruct
void StartStruct()
Definition: CommandTargets.h:352
CommandOutputTargets::EndArray
void EndArray()
Definition: CommandTargets.h:347
CombinedMessageTarget::~CombinedMessageTarget
~CombinedMessageTarget()
Definition: CommandTargets.h:270
CommandMessageTarget::StartField
virtual void StartField(const wxString &name={})
Definition: CommandTargets.cpp:99
InteractiveOutputTargets
InteractiveOutputTargets is an output target that pops up a dialog, if necessary.
Definition: CommandTargets.h:409
ProgressToMessageTarget
ProgressToMessageTarget formats the percentage complete text as a message and sends it to that messag...
Definition: CommandTargets.h:181
CommandMessageTargetDecorator::StartField
void StartField(const wxString &name={}) override
Definition: CommandTargets.h:110
ResponseTarget
Constructs a response (to be sent back to a script)
Definition: CommandTargets.h:227
LispyCommandMessageTarget::StartArray
virtual void StartArray() override
Definition: CommandTargets.cpp:125
str
#define str(a)
Definition: DBConnection.cpp:30
CommandMessageTarget::StartArray
virtual void StartArray()
Definition: CommandTargets.cpp:38
ProgressToMessageTarget::ProgressToMessageTarget
ProgressToMessageTarget(std::unique_ptr< CommandMessageTarget > &&target)
Definition: CommandTargets.h:185
CommandMessageTargetDecorator::~CommandMessageTargetDecorator
~CommandMessageTargetDecorator() override
Definition: CommandTargets.h:98
CommandMessageTargetDecorator::EndStruct
void EndStruct() override
Definition: CommandTargets.h:103
LispyCommandMessageTarget::StartStruct
virtual void StartStruct() override
Definition: CommandTargets.cpp:140
BriefCommandOutputTargets
BriefCommandOutputTargets is a CommandOutputTargets that replaces the status message target with the ...
Definition: CommandTargets.h:400
LispyCommandMessageTarget::AddBool
virtual void AddBool(const bool value, const wxString &name={}) override
Definition: CommandTargets.cpp:161
TargetFactory::ProgressDefault
static std::unique_ptr< CommandProgressTarget > ProgressDefault()
Definition: CommandTargets.h:289
LispyCommandMessageTarget::EndField
virtual void EndField() override
Definition: CommandTargets.cpp:182
CommandMessageTarget::EndStruct
virtual void EndStruct()
Definition: CommandTargets.cpp:60
LispyCommandMessageTarget::AddItem
virtual void AddItem(const wxString &value, const wxString &name={}) override
Definition: CommandTargets.cpp:153
BriefCommandMessageTarget::BriefCommandMessageTarget
BriefCommandMessageTarget(CommandMessageTarget &target)
Definition: CommandTargets.h:137
CommandMessageTargetDecorator::Flush
void Flush() override
Definition: CommandTargets.h:114
MessageBoxTarget
Displays messages from a command in an AudacityMessageBox.
Definition: CommandTargets.h:207
CommandProgressTarget
Interface for objects that can receive command progress information.
Definition: CommandTargets.h:67
LispifiedCommandOutputTargets
LispifiedCommandOutputTargets is a CommandOutputTargets that replaces the status message target with ...
Definition: CommandTargets.h:391
ResponseTarget::ResponseTarget
ResponseTarget()
Definition: CommandTargets.h:232
CommandOutputTargets::AddItem
void AddItem(const double value, const wxString &name={})
Definition: CommandTargets.h:382
CommandMessageTargetDecorator::AddBool
void AddBool(const bool value, const wxString &name={}) override
Definition: CommandTargets.h:106
CommandMessageTarget::StartStruct
virtual void StartStruct()
Definition: CommandTargets.cpp:53
ProgressToMessageTarget::Update
void Update(double completed) override
Definition: CommandTargets.h:191
CommandMessageTarget::Flush
virtual void Flush()
Definition: CommandTargets.cpp:114
CommandOutputTargets::mErrorTarget
std::shared_ptr< CommandMessageTarget > mErrorTarget
Definition: CommandTargets.h:310
CommandMessageTargetDecorator::AddItem
void AddItem(const wxString &value, const wxString &name={}) override
Definition: CommandTargets.h:104
CommandMessageTarget
Interface for objects that can receive (string) messages from a command.
Definition: CommandTargets.h:75
CommandOutputTargets::mProgressTarget
std::unique_ptr< CommandProgressTarget > mProgressTarget
Definition: CommandTargets.h:308
ProgressToMessageTarget::mTarget
std::unique_ptr< CommandMessageTarget > mTarget
Definition: CommandTargets.h:183
NullMessageTarget::Update
void Update(const wxString &) override
Definition: CommandTargets.h:202
CommandProgressTarget::~CommandProgressTarget
virtual ~CommandProgressTarget()
Definition: CommandTargets.h:69
StatusBarTarget::mStatus
wxStatusBar & mStatus
Definition: CommandTargets.h:217
CommandOutputTargets::EndStruct
void EndStruct()
Definition: CommandTargets.h:357
NullMessageTarget::~NullMessageTarget
virtual ~NullMessageTarget()
Definition: CommandTargets.h:201
CommandMessageTargetDecorator::EndField
void EndField() override
Definition: CommandTargets.h:112
CombinedMessageTarget::CombinedMessageTarget
CombinedMessageTarget(std::unique_ptr< CommandMessageTarget > &&t1, std::unique_ptr< CommandMessageTarget > &&t2)
Definition: CommandTargets.h:263
CommandMessageTargetDecorator
CommandMessageTargetDecorator is a CommandOutputTarget that forwards its work on to another one....
Definition: CommandTargets.h:95
CombinedMessageTarget::Update
void Update(const wxString &message) override
Definition: CommandTargets.h:273
CombinedMessageTarget::m1
std::unique_ptr< CommandMessageTarget > m1
Definition: CommandTargets.h:261
CommandOutputTargets::AddItem
void AddItem(const wxString &value, const wxString &name={})
Definition: CommandTargets.h:372
CommandMessageTarget::EndField
virtual void EndField()
Definition: CommandTargets.cpp:108
CommandMessageTarget::AddBool
virtual void AddBool(const bool value, const wxString &name={})
Definition: CommandTargets.cpp:77
BriefCommandMessageTarget::EndStruct
virtual void EndStruct() override
Definition: CommandTargets.cpp:219
ProgressDialog
ProgressDialog Class.
Definition: ProgressDialog.h:51
CommandOutputTargets::StartField
void StartField(const wxString &name)
Definition: CommandTargets.h:362
CommandOutputTargets::Error
void Error(const wxString &message)
Definition: CommandTargets.h:337
CommandMessageTarget::AddItem
virtual void AddItem(const wxString &value, const wxString &name={})
Definition: CommandTargets.cpp:66
CommandOutputTargets::mStatusTarget
std::shared_ptr< CommandMessageTarget > mStatusTarget
Definition: CommandTargets.h:309
CommandMessageTargetDecorator::EndArray
void EndArray() override
Definition: CommandTargets.h:101
ResponseTarget::Flush
virtual void Flush() override
Definition: CommandTargets.h:246
BriefCommandMessageTarget::StartStruct
virtual void StartStruct() override
Definition: CommandTargets.cpp:211
InteractiveOutputTargets::InteractiveOutputTargets
InteractiveOutputTargets()
Definition: CommandTargets.cpp:453
CommandOutputTargets::Progress
void Progress(double completed)
Definition: CommandTargets.h:324
CommandOutputTargets::Status
void Status(const wxString &status, bool bFlush=false)
Definition: CommandTargets.h:329
ProgressToMessageTarget::~ProgressToMessageTarget
virtual ~ProgressToMessageTarget()
Definition: CommandTargets.h:188
name
const TranslatableString name
Definition: Distortion.cpp:98
BriefCommandMessageTarget::StartField
virtual void StartField(const wxString &name={}) override
Definition: CommandTargets.cpp:242
NullProgressTarget::Update
void Update(double WXUNUSED(completed)) override
Definition: CommandTargets.h:154
BriefCommandMessageTarget::AddItem
virtual void AddItem(const double value, const wxString &name={}) override
ProgressDialog::Update
ProgressResult Update(int value, const TranslatableString &message={})
Definition: ProgressDialog.cpp:1327
BriefCommandMessageTarget::AddBool
virtual void AddBool(const bool value, const wxString &name={}) override
Definition: CommandTargets.cpp:231
BriefCommandMessageTarget::StartArray
virtual void StartArray() override
Definition: CommandTargets.cpp:194
CommandOutputTargets::~CommandOutputTargets
~CommandOutputTargets()
Definition: CommandTargets.h:320
NullProgressTarget::~NullProgressTarget
virtual ~NullProgressTarget()
Definition: CommandTargets.h:153
ResponseTarget::~ResponseTarget
virtual ~ResponseTarget()
Definition: CommandTargets.h:239
NullMessageTarget
Used to ignore a command's message updates.
Definition: CommandTargets.h:199
CommandMessageTargetDecorator::StartArray
void StartArray() override
Definition: CommandTargets.h:100
MessageBoxTarget::~MessageBoxTarget
virtual ~MessageBoxTarget()
Definition: CommandTargets.h:209
CommandMessageTarget::CommandMessageTarget
CommandMessageTarget()
Definition: CommandTargets.h:77
TargetFactory::MessageDefault
static std::shared_ptr< CommandMessageTarget > MessageDefault()
Definition: CommandTargets.h:294
TargetFactory
TargetFactory makes Command output targets. By default, we ignore progress updates but display all ot...
Definition: CommandTargets.h:287
CombinedMessageTarget::m2
std::unique_ptr< CommandMessageTarget > m2
Definition: CommandTargets.h:261
BriefCommandMessageTarget::AddItem
virtual void AddItem(const wxString &value, const wxString &name={}) override
ResponseTarget::GetResponse
wxString GetResponse()
Definition: CommandTargets.h:250
CommandOutputTargets::EndField
void EndField()
Definition: CommandTargets.h:367
CombinedMessageTarget
Sends messages to two message targets at once.
Definition: CommandTargets.h:259
LispyCommandMessageTarget::EndArray
virtual void EndArray() override
Definition: CommandTargets.cpp:134
BriefCommandMessageTarget::EndArray
virtual void EndArray() override
Definition: CommandTargets.cpp:204
CommandOutputTargets::StartArray
void StartArray()
Definition: CommandTargets.h:342
BriefCommandOutputTargets::pToRestore
CommandOutputTargets * pToRestore
Definition: CommandTargets.h:405
CommandMessageTargetDecorator::StartStruct
void StartStruct() override
Definition: CommandTargets.h:102
CommandMessageTargetDecorator::Update
void Update(const wxString &message) override
Definition: CommandTargets.h:99
ResponseTarget::mSemaphore
wxSemaphore mSemaphore
Definition: CommandTargets.h:229
CommandMessageTargetDecorator::AddItem
void AddItem(const double value, const wxString &name={}) override
Definition: CommandTargets.h:108
StatusBarTarget::StatusBarTarget
StatusBarTarget(wxStatusBar &sb)
Definition: CommandTargets.h:219
ResponseTarget::Update
void Update(const wxString &message) override
Definition: CommandTargets.h:242
CommandMessageTargetDecorator::CommandMessageTargetDecorator
CommandMessageTargetDecorator(CommandMessageTarget &target)
Definition: CommandTargets.h:97
CommandOutputTargets::CommandOutputTargets
CommandOutputTargets(std::unique_ptr< CommandProgressTarget > &&pt=TargetFactory::ProgressDefault(), std::shared_ptr< CommandMessageTarget > &&st=TargetFactory::MessageDefault(), std::shared_ptr< CommandMessageTarget > &&et=TargetFactory::MessageDefault())
Definition: CommandTargets.h:315
CommandMessageTarget::~CommandMessageTarget
virtual ~CommandMessageTarget()
Definition: CommandTargets.h:78
CommandProgressTarget::Update
virtual void Update(double completed)=0
LispyCommandMessageTarget::StartField
virtual void StartField(const wxString &name={}) override
Definition: CommandTargets.cpp:176
NullProgressTarget
Used to ignore a command's progress updates.
Definition: CommandTargets.h:151
BriefCommandMessageTarget
BriefCommandMessageTarget is a CommandOutputTarget that provides status in a briefer listing.
Definition: CommandTargets.h:135
CommandMessageTargetDecorator::mTarget
CommandMessageTarget & mTarget
Definition: CommandTargets.h:116
CommandMessageTarget::Update
virtual void Update(const wxString &message)=0
StatusBarTarget
Displays messages from a command in a wxStatusBar.
Definition: CommandTargets.h:215
LispyCommandMessageTarget::LispyCommandMessageTarget
LispyCommandMessageTarget(CommandMessageTarget &target)
Definition: CommandTargets.h:122
LispyCommandMessageTarget
LispyCommandMessageTarget is a CommandOutputTarget that provides status in a lispy style.
Definition: CommandTargets.h:120
CommandMessageTarget::EndArray
virtual void EndArray()
Definition: CommandTargets.cpp:47
BriefCommandMessageTarget::EndField
virtual void EndField() override
Definition: CommandTargets.cpp:247
LispyCommandMessageTarget::EndStruct
virtual void EndStruct() override
Definition: CommandTargets.cpp:147
CommandMessageTarget::mCounts
std::vector< int > mCounts
Definition: CommandTargets.h:91
ResponseTarget::mBuffer
wxString mBuffer
Definition: CommandTargets.h:230
CommandOutputTargets
CommandOutputTargets a mix of three output classes to output progress indication, status messages and...
Definition: CommandTargets.h:306
LispifiedCommandOutputTargets::pToRestore
CommandOutputTargets * pToRestore
Definition: CommandTargets.h:396
CommandOutputTargets::AddBool
void AddBool(const bool value, const wxString &name={})
Definition: CommandTargets.h:377