Audacity  2.2.2
Command.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity - A Digital Audio Editor
4  Copyright 1999-2009 Audacity Team
5  License: wxwidgets
6 
7  Dan Horgan
8 
9 ******************************************************************//*******************************************************************/
16 
17 #include "../Audacity.h"
18 #include "Command.h"
19 #include <map>
20 #include <wx/string.h>
21 #include <wx/variant.h>
22 #include <wx/arrstr.h>
23 
24 #include "../AudacityException.h"
25 #include "Validators.h"
26 #include "CommandType.h"
27 #include "CommandMisc.h"
28 #include "CommandBuilder.h"
29 #include "CommandTargets.h"
30 #include "CommandDirectory.h"
31 
32 bool Command::SetParameter(const wxString & WXUNUSED(paramName),
33  const wxVariant & WXUNUSED(paramValue))
34 {
35  wxASSERT_MSG(false, wxT("Tried to set parameter for command which doesn't support parameters!"));
36  return false;
37 }
38 
39 void DecoratedCommand::Progress(double completed)
40 {
41  mCommand->Progress(completed);
42 }
43 
44 void DecoratedCommand::Status(const wxString &message)
45 {
46  mCommand->Status(message);
47 }
48 
49 void DecoratedCommand::Error(const wxString &message)
50 {
51  mCommand->Error(message);
52 }
53 
55 {
56 }
57 
59 {
60  return mCommand->GetName();
61 }
62 
64 {
65  return mCommand->GetSignature();
66 }
67 
68 bool DecoratedCommand::SetParameter(const wxString &paramName,
69  const wxVariant &paramValue)
70 {
71  return mCommand->SetParameter(paramName, paramValue);
72 }
73 
75 {
76  auto result = GuardedCall<bool>(
77  [&] { return mCommand->Apply(context); }
78  );
79  wxString response = GetName();
80  // These three strings are deliberately not localised.
81  // They are used in script responses and always happen in English.
82  response += wxT(" finished: ");
83  if (result)
84  {
85  response += wxT("OK");
86  }
87  else
88  {
89  response += wxT("Failed!");
90  }
91  Status(response);
92  return result;
93 }
94 
96  std::unique_ptr<CommandOutputTarget> &&output)
97 : mType(type),
98  mParams(type.GetSignature().GetDefaults()),
99  mSetParams(),
100  mOutput(std::move(output))
101 {
102  wxASSERT(mOutput);
103 }
104 
106 {
107 }
108 
109 void CommandImplementation::TypeCheck(const wxString &typeName,
110  const wxString &paramName,
111  const wxVariant &param)
112 {
113  // this macro is empty if wxWidgets is not compiled in debug mode
114  wxASSERT_MSG(param.IsType(typeName),
115  GetName()
116  + wxT("command tried to get '")
117  + paramName
118  + wxT("' parameter as a ")
119  + typeName
120  + wxT(", but that wasn't enforced by the command signature."));
121 }
122 
123 void CommandImplementation::CheckParam(const wxString &paramName)
124 {
125  // this macro is empty if wxWidgets is not compiled in debug mode
126  wxASSERT_MSG(mParams.find(paramName) != mParams.end(),
127  GetName()
128  + wxT("command tried to get '")
129  + paramName
130  + wxT("' parameter, but that parameter doesn't exist in the command signature!"));
131 }
132 
133 bool CommandImplementation::HasParam( const wxString &paramName)
134 {
135  // Test for not even in map...
136  if( mParams.count(paramName) < 1)
137  return false;
138  return mSetParams[paramName];
139 }
140 
141 bool CommandImplementation::GetBool(const wxString &paramName)
142 {
143  CheckParam(paramName);
144  const wxVariant &v = mParams[paramName];
145  TypeCheck(wxT("bool"), paramName, v);
146  return v.GetBool();
147 }
148 
149 long CommandImplementation::GetLong(const wxString &paramName)
150 {
151  CheckParam(paramName);
152  const wxVariant &v = mParams[paramName];
153  TypeCheck(wxT("double"), paramName, v);
154  return (long)v.GetDouble();
155 }
156 
157 double CommandImplementation::GetDouble(const wxString &paramName)
158 {
159  CheckParam(paramName);
160  const wxVariant &v = mParams[paramName];
161  TypeCheck(wxT("double"), paramName, v);
162  return v.GetDouble();
163 }
164 
165 wxString CommandImplementation::GetString(const wxString &paramName)
166 {
167  CheckParam(paramName);
168  const wxVariant &v = mParams[paramName];
169  TypeCheck(wxT("string"), paramName, v);
170  return v.GetString();
171 }
172 
173 // Convenience methods for passing messages to the output target
174 void CommandImplementation::Progress(double completed)
175 {
176  mOutput->Progress(completed);
177 }
178 
179 void CommandImplementation::Status(const wxString &status)
180 {
181  mOutput->Status(status);
182 }
183 
184 void CommandImplementation::Error(const wxString &message)
185 {
186  mOutput->Error(message);
187 }
188 
191 {
192  return mType.GetName();
193 }
194 
197 {
198  return mType.GetSignature();
199 }
200 
201 bool CommandImplementation::SetParameter(const wxString &paramName, const wxVariant &paramValue)
202 {
203  wxASSERT(!paramValue.IsType(wxT("null")));
204 
205  ParamValueMap::iterator iter = mParams.find(paramName);
206  if (iter == mParams.end())
207  {
208  Error(paramName + wxT(" is not a parameter accepted by ") + GetName());
209  return false;
210  }
211 
212  Validator &validator = mType.GetSignature().GetValidator(iter->first);
213  if (!validator.Validate(paramValue))
214  {
215  Error(wxT("Invalid value for parameter '")
216  + paramName + wxT("': should be ")
217  + validator.GetDescription());
218  return false;
219  }
220  mParams[paramName] = validator.GetConverted();
221  mSetParams[ paramName ] = true;
222 
223  // (debug)
224  // Status(wxT("Set parameter ") + paramName + wxT(" to type ") + mParams[paramName].GetType() + wxT(", value ") + mParams[paramName].MakeString());
225 
226  return true;
227 }
228 
230 {
231  return true;
232 }
ParamValueMap mParams
Definition: Command.h:122
const wxVariant & GetConverted()
Definition: Validators.h:60
void Status(const wxString &message) override
Definition: Command.cpp:44
const wxString & GetName()
Definition: CommandType.cpp:34
virtual ~CommandImplementation()
Definition: Command.cpp:105
virtual ~DecoratedCommand()
Definition: Command.cpp:54
Contains declarations for CommandType class.
wxString GetString(const wxString &paramName)
Definition: Command.cpp:165
Contains declaration of Command base class.
wxString GetName() override
Definition: Command.cpp:58
CommandType & mType
Definition: Command.h:121
CommandSignature & GetSignature() override
Definition: Command.cpp:63
CommandSignature & GetSignature()
Definition: CommandType.cpp:41
Contains declarations for CommandDirectory class.
void TypeCheck(const wxString &typeName, const wxString &paramName, const wxVariant &param)
Definition: Command.cpp:109
Contains declarations and definitions for Validator, OptionValidator, BoolValidator, DoubleValidator, RangeValidator, IntValidator and AndValidator classes.
ParamBoolMap mSetParams
Definition: Command.h:123
virtual wxString GetDescription() const
Definition: Validators.h:74
double GetDouble(const wxString &paramName)
Definition: Command.cpp:157
std::unique_ptr< CommandOutputTarget > mOutput
Definition: Command.h:130
bool HasParam(const wxString &paramName)
Definition: Command.cpp:133
Base class for containing data common to all commands of a given type. Also acts as a factory...
Definition: CommandType.h:29
long GetLong(const wxString &paramName)
Definition: Command.cpp:149
void CheckParam(const wxString &paramName)
Definition: Command.cpp:123
wxString GetName()
An instance method for getting the command name (for consistency)
Definition: Command.cpp:190
void Progress(double completed)
Definition: Command.cpp:174
bool Apply(CommandExecutionContext context) override
Definition: Command.cpp:74
void Progress(double completed) override
Definition: Command.cpp:39
void Status(const wxString &status) override
Definition: Command.cpp:179
bool SetParameter(const wxString &paramName, const wxVariant &paramValue) override
Definition: Command.cpp:68
void Error(const wxString &message) override
Definition: Command.cpp:49
CommandSignature & GetSignature()
Get the signature of the command.
Definition: Command.cpp:196
virtual bool Validate(const wxVariant &v)
Judge whether the passed value satisfies the Validator.
Definition: Validators.h:66
Represents a context to which a command may be applied.
Definition: Command.h:50
virtual bool SetParameter(const wxString &paramName, const wxVariant &paramValue)
Definition: Command.cpp:32
CommandHolder mCommand
Definition: Command.h:89
CommandImplementation(CommandType &type, std::unique_ptr< CommandOutputTarget > &&output)
Definition: Command.cpp:95
bool SetParameter(const wxString &paramName, const wxVariant &paramValue)
Definition: Command.cpp:201
Contains declaration of CommandBuilder class.
bool Apply(CommandExecutionContext context) override
Definition: Command.cpp:229
Validator & GetValidator(const wxString &paramName)
bool GetBool(const wxString &paramName)
Definition: Command.cpp:141
Class that maps parameter names to default values and validators.
void Error(const wxString &message) override
Definition: Command.cpp:184
A Validator is an object which checks whether a wxVariant satisfies a certain criterion. This is a base validator which allows anything.
Definition: Validators.h:48