Audacity  2.2.2
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>

Public Member Functions

 CommandBuilder (const wxString &cmdString)
 
 CommandBuilder (const wxString &cmdName, const wxString &cmdParams)
 
 ~CommandBuilder ()
 
bool WasValid ()
 
OldStyleCommandPointer GetCommand ()
 
const wxString & GetErrorMessage ()
 

Private Member Functions

void Failure (const wxString &msg=wxEmptyString)
 
void Success (const OldStyleCommandPointer &cmd)
 
void BuildCommand (const wxString &cmdName, const wxString &cmdParams)
 
void BuildCommand (const wxString &cmdString)
 

Private Attributes

bool mValid
 
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 29 of file CommandBuilder.h.

Constructor & Destructor Documentation

CommandBuilder::CommandBuilder ( const wxString &  cmdString)

Definition at line 34 of file CommandBuilder.cpp.

References BuildCommand().

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

Definition at line 40 of file CommandBuilder.cpp.

References BuildCommand().

41  : mValid(false)
42 {
43  BuildCommand(cmdName, params);
44 }
void BuildCommand(const wxString &cmdName, const wxString &cmdParams)
EffectDistortion::Params params
Definition: Distortion.cpp:95
CommandBuilder::~CommandBuilder ( )

Definition at line 46 of file CommandBuilder.cpp.

47 {
48 }

Member Function Documentation

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

Definition at line 81 of file CommandBuilder.cpp.

References OldStyleCommandType::Create(), Failure(), CommandDirectory::Get(), CommandSignature::GetDefaults(), ScriptCommandRelay::GetResponseTarget(), OldStyleCommandType::GetSignature(), CommandDirectory::LookUp(), Shuttle::mbStoreInClient, mCommand, ShuttleCli::mParams, params, Success(), and Shuttle::TransferString().

Referenced by BuildCommand(), and CommandBuilder().

83 {
84  // Stage 1: create a Command object of the right type
85 
86  auto scriptOutput = ScriptCommandRelay::GetResponseTarget();
87  auto output
88  = std::make_unique<CommandOutputTargets>(std::make_unique<NullProgressTarget>(),
89  scriptOutput,
90  scriptOutput);
91 
92 #ifdef OLD_BATCH_SYSTEM
93  OldStyleCommandType *factory = CommandDirectory::Get()->LookUp(cmdName);
94 
95  if (factory == NULL)
96  {
97  // Fall back to hoping the Batch Command system can handle it
98 #endif
99  OldStyleCommandType *type = CommandDirectory::Get()->LookUp(wxT("BatchCommand"));
100  wxASSERT(type != NULL);
101  mCommand = type->Create(nullptr);
102  mCommand->SetParameter(wxT("CommandName"), cmdName);
103  mCommand->SetParameter(wxT("ParamString"), cmdParamsArg);
104  auto aCommand = std::make_shared<ApplyAndSendResponse>(mCommand, output);
105  Success(aCommand);
106  return;
107 #ifdef OLD_BATCH_SYSTEM
108  }
109 
110  CommandSignature &signature = factory->GetSignature();
111  mCommand = factory->Create(nullptr);
112  //mCommand->SetOutput( std::move(output) );
113  // Stage 2: set the parameters
114 
115  ShuttleCli shuttle;
116  shuttle.mParams = cmdParamsArg;
117  shuttle.mbStoreInClient = true;
118 
119  ParamValueMap::const_iterator iter;
120  ParamValueMap params = signature.GetDefaults();
121 
122  // Iterate through the parameters defined by the command
123  for (iter = params.begin(); iter != params.end(); ++iter)
124  {
125  wxString paramString;
126  // IF there is a match in the args actually used
127  if (shuttle.TransferString(iter->first, paramString, wxT("")))
128  {
129  // Then set that parameter.
130  if (!mCommand->SetParameter(iter->first, paramString))
131  {
132  Failure();
133  return;
134  }
135  }
136  }
137 
138  // Check for unrecognised parameters
139 
140  wxString cmdParams(cmdParamsArg);
141 
142  while (cmdParams != wxEmptyString)
143  {
144  cmdParams.Trim(true);
145  cmdParams.Trim(false);
146  int splitAt = cmdParams.Find(wxT('='));
147  if (splitAt < 0 && cmdParams != wxEmptyString)
148  {
149  Failure(wxT("Parameter string is missing '='"));
150  return;
151  }
152  wxString paramName = cmdParams.Left(splitAt);
153  if (params.find(paramName) == params.end())
154  {
155  Failure(wxT("Unrecognized parameter: '") + paramName + wxT("'"));
156  return;
157  }
158  // Handling of quoted strings is quite limitted.
159  // You start and end with a " or a '.
160  // There is no escaping in the string.
161  cmdParams = cmdParams.Mid(splitAt+1);
162  if( cmdParams.IsEmpty() )
163  splitAt =-1;
164  else if( cmdParams[0] == '\"' ){
165  cmdParams = cmdParams.Mid(1);
166  splitAt = cmdParams.Find(wxT('\"'))+1;
167  }
168  else if( cmdParams[0] == '\'' ){
169  cmdParams = cmdParams.Mid(1);
170  splitAt = cmdParams.Find(wxT('\''))+1;
171  }
172  else
173  splitAt = cmdParams.Find(wxT(' '))+1;
174  if (splitAt < 1)
175  {
176  splitAt = cmdParams.Len();
177  }
178  cmdParams = cmdParams.Mid(splitAt);
179  }
180  auto aCommand = std::make_shared<ApplyAndSendResponse>(mCommand, output);
181  Success(aCommand);
182 #endif
183 }
static CommandDirectory * Get()
Get a pointer to the singleton instance.
std::map< wxString, wxVariant > ParamValueMap
Definition: CommandMisc.h:23
Base class for containing data common to all commands of a given type. Also acts as a factory...
Definition: CommandType.h:44
OldStyleCommandPointer mCommand
wxString mParams
Definition: Shuttle.h:49
Derived from Shuttle, this class exchanges string parameters with a binary representation.
Definition: Shuttle.h:46
ParamValueMap GetDefaults() const
static std::shared_ptr< ResponseQueueTarget > GetResponseTarget()
virtual bool TransferString(const wxString &Name, wxString &strValue, const wxString &strDefault)
Definition: Shuttle.cpp:223
CommandSignature & GetSignature()
Definition: CommandType.cpp:41
void Failure(const wxString &msg=wxEmptyString)
OldStyleCommandType * LookUp(const wxString &cmdName) const
bool mbStoreInClient
Definition: Shuttle.h:27
void Success(const OldStyleCommandPointer &cmd)
virtual OldStyleCommandPointer Create(std::unique_ptr< CommandOutputTargets > &&target)=0
EffectDistortion::Params params
Definition: Distortion.cpp:95
Class that maps parameter names to default values and validators.
void CommandBuilder::BuildCommand ( const wxString &  cmdString)
private

Definition at line 185 of file CommandBuilder.cpp.

References BuildCommand(), mError, mValid, and ScriptCommandRelay::SendResponse().

186 {
187  wxString cmdString(cmdStringArg);
188 
189  // Find the command name terminator... If there is more than one word and
190  // no terminator, the command is badly formed
191  cmdString.Trim(true); cmdString.Trim(false);
192  int splitAt = cmdString.Find(wxT(':'));
193  if (splitAt < 0 && cmdString.Find(wxT(' ')) >= 0) {
194  mError = wxT("Command is missing ':'");
196  mValid = false;
197  return;
198  }
199 
200  wxString cmdName = cmdString.Left(splitAt);
201  wxString cmdParams = cmdString.Mid(splitAt+1);
202  if( splitAt < 0 )
203  cmdParams = "";
204 
205  cmdName.Trim(true);
206  cmdParams.Trim(false);
207 
208  BuildCommand(cmdName, cmdParams);
209 }
void BuildCommand(const wxString &cmdName, const wxString &cmdParams)
static void SendResponse(const wxString &response)
Adds a response to the queue to be sent back to the script.
void CommandBuilder::Failure ( const wxString &  msg = wxEmptyString)
private

Definition at line 69 of file CommandBuilder.cpp.

References mError, and mValid.

Referenced by BuildCommand().

70 {
71  mError = msg;
72  mValid = false;
73 }
OldStyleCommandPointer CommandBuilder::GetCommand ( )

Definition at line 60 of file CommandBuilder.cpp.

References mCommand, and mValid.

Referenced by ExecCommand().

61 {
62  wxASSERT(mValid);
63  wxASSERT(mCommand);
64  auto result = mCommand;
65  mCommand.reset();
66  return result;
67 }
OldStyleCommandPointer mCommand
const wxString & CommandBuilder::GetErrorMessage ( )

Definition at line 55 of file CommandBuilder.cpp.

References mError.

Referenced by ExecCommand().

56 {
57  return mError;
58 }
void CommandBuilder::Success ( const OldStyleCommandPointer cmd)
private

Definition at line 75 of file CommandBuilder.cpp.

References mCommand, and mValid.

Referenced by BuildCommand().

76 {
77  mCommand = cmd;
78  mValid = true;
79 }
OldStyleCommandPointer mCommand
bool CommandBuilder::WasValid ( )

Definition at line 50 of file CommandBuilder.cpp.

References mValid.

Referenced by ExecCommand().

51 {
52  return mValid;
53 }

Member Data Documentation

OldStyleCommandPointer CommandBuilder::mCommand
private

Definition at line 33 of file CommandBuilder.h.

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

wxString CommandBuilder::mError
private

Definition at line 34 of file CommandBuilder.h.

Referenced by BuildCommand(), Failure(), and GetErrorMessage().

bool CommandBuilder::mValid
private

Definition at line 32 of file CommandBuilder.h.

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


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