Audacity 3.2.0
Public Member Functions | Private Member Functions | Private Attributes | List of all members
CommandBuilder Class Reference

A type of factory for Commands of various sorts. More...

#include <CommandBuilder.h>

Collaboration diagram for CommandBuilder:
[legend]

Public Member Functions

 CommandBuilder (AudacityProject &project, const wxString &cmdString)
 
 CommandBuilder (AudacityProject &project, const wxString &cmdName, const wxString &cmdParams)
 
 ~CommandBuilder ()
 
bool WasValid ()
 
OldStyleCommandPointer GetCommand ()
 
wxString GetResponse ()
 

Private Member Functions

void Failure (const wxString &msg={})
 
void Success (const OldStyleCommandPointer &cmd)
 
void BuildCommand (AudacityProject &project, const wxString &cmdName, const wxString &cmdParams)
 
void BuildCommand (AudacityProject &project, const wxString &cmdString)
 

Private Attributes

bool mValid
 
ResponseTargetPointer mResponse
 
OldStyleCommandPointer mCommand
 
wxString mError
 

Detailed Description

A type of factory for Commands of various sorts.

CommandBuilder has the task of deciding what command is meant by a given command string, and producing a suitable command object. For now, it doesn't actually do any processing - it just passes everything on to the BatchCommand system by constructing BatchCommandEval objects.

Definition at line 32 of file CommandBuilder.h.

Constructor & Destructor Documentation

◆ CommandBuilder() [1/2]

CommandBuilder::CommandBuilder ( AudacityProject project,
const wxString &  cmdString 
)

Definition at line 33 of file CommandBuilder.cpp.

35 : mValid(false)
36{
37 BuildCommand(project, cmdString);
38}
const auto project
void BuildCommand(AudacityProject &project, const wxString &cmdName, const wxString &cmdParams)

References BuildCommand(), and project.

Here is the call graph for this function:

◆ CommandBuilder() [2/2]

CommandBuilder::CommandBuilder ( AudacityProject project,
const wxString &  cmdName,
const wxString &  cmdParams 
)

Definition at line 40 of file CommandBuilder.cpp.

42 : mValid(false)
43{
44 BuildCommand(project, cmdName, params);
45}
EffectDistortionSettings params

References BuildCommand(), params, and project.

Here is the call graph for this function:

◆ ~CommandBuilder()

CommandBuilder::~CommandBuilder ( )

Definition at line 47 of file CommandBuilder.cpp.

48{
49}

Member Function Documentation

◆ BuildCommand() [1/2]

void CommandBuilder::BuildCommand ( AudacityProject project,
const wxString &  cmdName,
const wxString &  cmdParams 
)
private

Definition at line 139 of file CommandBuilder.cpp.

142{
143 // Stage 1: create a Command object of the right type
144
145 mResponse = std::make_shared< ResponseTarget >();
146 auto output
147 = std::make_unique<CommandOutputTargets>(std::make_unique<NullProgressTarget>(),
148 mResponse,
149 mResponse);
150
151#ifdef OLD_BATCH_SYSTEM
153
154 if (factory == NULL)
155 {
156 // Fall back to hoping the Batch Command system can handle it
157#endif
158 OldStyleCommandType *type = CommandDirectory::Get()->LookUp(wxT("BatchCommand"));
159 wxASSERT(type != NULL);
160 mCommand = type->Create(project, nullptr);
161 mCommand->SetParameter(wxT("CommandName"), cmdName);
162 mCommand->SetParameter(wxT("ParamString"), cmdParamsArg);
163 auto aCommand = std::make_shared<ApplyAndSendResponse>(mCommand, output);
164 Success(aCommand);
165 return;
166#ifdef OLD_BATCH_SYSTEM
167 }
168
169 CommandSignature &signature = factory->GetSignature();
170 mCommand = factory->Create(nullptr);
171 //mCommand->SetOutput( std::move(output) );
172 // Stage 2: set the parameters
173
174 ShuttleCli shuttle;
175 shuttle.mParams = cmdParamsArg;
176
177 ParamValueMap::const_iterator iter;
178 ParamValueMap params = signature.GetDefaults();
179
180 // Iterate through the parameters defined by the command
181 for (iter = params.begin(); iter != params.end(); ++iter)
182 {
183 wxString paramString;
184 // IF there is a match in the args actually used
185 if (shuttle.TransferString(iter->first, paramString, wxT("")))
186 {
187 // Then set that parameter.
188 if (!mCommand->SetParameter(iter->first, paramString))
189 {
190 Failure();
191 return;
192 }
193 }
194 }
195
196 // Check for unrecognised parameters
197
198 wxString cmdParams(cmdParamsArg);
199
200 while (!cmdParams.empty())
201 {
202 cmdParams.Trim(true);
203 cmdParams.Trim(false);
204 int splitAt = cmdParams.Find(wxT('='));
205 if (splitAt < 0 && !cmdParams.empty())
206 {
207 Failure(wxT("Parameter string is missing '='"));
208 return;
209 }
210 wxString paramName = cmdParams.Left(splitAt);
211 if (params.find(paramName) == params.end())
212 {
213 Failure(wxT("Unrecognized parameter: '") + paramName + wxT("'"));
214 return;
215 }
216 // Handling of quoted strings is quite limited.
217 // You start and end with a " or a '.
218 // There is no escaping in the string.
219 cmdParams = cmdParams.Mid(splitAt+1);
220 if( cmdParams.empty() )
221 splitAt =-1;
222 else if( cmdParams[0] == '\"' ){
223 cmdParams = cmdParams.Mid(1);
224 splitAt = cmdParams.Find(wxT('\"'))+1;
225 }
226 else if( cmdParams[0] == '\'' ){
227 cmdParams = cmdParams.Mid(1);
228 splitAt = cmdParams.Find(wxT('\''))+1;
229 }
230 else
231 splitAt = cmdParams.Find(wxT(' '))+1;
232 if (splitAt < 1)
233 {
234 splitAt = cmdParams.length();
235 }
236 cmdParams = cmdParams.Mid(splitAt);
237 }
238 auto aCommand = std::make_shared<ApplyAndSendResponse>(mCommand, output);
239 Success(aCommand);
240#endif
241}
wxT("CloseDown"))
static RegisteredToolbarFactory factory
std::map< wxString, wxVariant > ParamValueMap
Definition: CommandMisc.h:23
void Failure(const wxString &msg={})
void Success(const OldStyleCommandPointer &cmd)
ResponseTargetPointer mResponse
OldStyleCommandPointer mCommand
OldStyleCommandType * LookUp(const wxString &cmdName) const
static CommandDirectory * Get()
Get a pointer to the singleton instance.
Class that maps parameter names to default values and validators.
ParamValueMap GetDefaults() const
Base class for containing data common to all commands of a given type. Also acts as a factory.
Definition: CommandType.h:45
virtual OldStyleCommandPointer Create(AudacityProject &project, std::unique_ptr< CommandOutputTargets > &&target)=0

References OldStyleCommandType::Create(), factory, Failure(), CommandDirectory::Get(), CommandSignature::GetDefaults(), CommandDirectory::LookUp(), mCommand, mResponse, params, project, Success(), and wxT().

Referenced by BuildCommand(), and CommandBuilder().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ BuildCommand() [2/2]

void CommandBuilder::BuildCommand ( AudacityProject project,
const wxString &  cmdString 
)
private

Definition at line 243 of file CommandBuilder.cpp.

245{
246 wxString cmdString(cmdStringArg);
247
248 // Find the command name terminator... If there is more than one word and
249 // no terminator, the command is badly formed
250 cmdString.Trim(true); cmdString.Trim(false);
251 int splitAt = cmdString.Find(wxT(':'));
252 if (splitAt < 0 && cmdString.Find(wxT(' ')) >= 0) {
253 Failure(wxT("Syntax error!\nCommand is missing ':'"));
254 return;
255 }
256
257 wxString cmdName = cmdString.Left(splitAt);
258 wxString cmdParams = cmdString.Mid(splitAt+1);
259 if( splitAt < 0 )
260 cmdParams = "";
261
262 cmdName.Trim(true);
263 cmdParams.Trim(false);
264
265 BuildCommand(project, cmdName, cmdParams);
266}

References BuildCommand(), Failure(), project, and wxT().

Here is the call graph for this function:

◆ Failure()

void CommandBuilder::Failure ( const wxString &  msg = {})
private

Definition at line 73 of file CommandBuilder.cpp.

74{
75 mError = msg;
76 mValid = false;
77}

References mError, and mValid.

Referenced by BuildCommand().

Here is the caller graph for this function:

◆ GetCommand()

OldStyleCommandPointer CommandBuilder::GetCommand ( )

Definition at line 56 of file CommandBuilder.cpp.

57{
58 wxASSERT(mValid);
59 wxASSERT(mCommand);
60 auto result = mCommand;
61 mCommand.reset();
62 return result;
63}

References mCommand, and mValid.

Referenced by ExecCommand().

Here is the caller graph for this function:

◆ GetResponse()

wxString CommandBuilder::GetResponse ( )

Definition at line 65 of file CommandBuilder.cpp.

66{
67 if (!mValid && !mError.empty()) {
68 return mError + wxT("\n");
69 }
70 return mResponse->GetResponse() + wxT("\n");
71}

References mError, mResponse, mValid, and wxT().

Referenced by ExecCommand().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Success()

void CommandBuilder::Success ( const OldStyleCommandPointer cmd)
private

Definition at line 79 of file CommandBuilder.cpp.

80{
81 mCommand = cmd;
82 mValid = true;
83}

References mCommand, and mValid.

Referenced by BuildCommand().

Here is the caller graph for this function:

◆ WasValid()

bool CommandBuilder::WasValid ( )

Definition at line 51 of file CommandBuilder.cpp.

52{
53 return mValid;
54}

References mValid.

Referenced by ExecCommand().

Here is the caller graph for this function:

Member Data Documentation

◆ mCommand

OldStyleCommandPointer CommandBuilder::mCommand
private

Definition at line 37 of file CommandBuilder.h.

Referenced by BuildCommand(), GetCommand(), and Success().

◆ mError

wxString CommandBuilder::mError
private

Definition at line 38 of file CommandBuilder.h.

Referenced by Failure(), and GetResponse().

◆ mResponse

ResponseTargetPointer CommandBuilder::mResponse
private

Definition at line 36 of file CommandBuilder.h.

Referenced by BuildCommand(), and GetResponse().

◆ mValid

bool CommandBuilder::mValid
private

Definition at line 35 of file CommandBuilder.h.

Referenced by Failure(), GetCommand(), GetResponse(), Success(), and WasValid().


The documentation for this class was generated from the following files: