Exemplo n.º 1
0
CommandImplementation::CommandImplementation(CommandType &type,
      std::unique_ptr<CommandOutputTarget> &&output)
: mType(type),
   mParams(type.GetSignature().GetDefaults()),
   mOutput(std::move(output))
{
   wxASSERT(mOutput);
}
Exemplo n.º 2
0
void CommandBuilder::BuildCommand(const wxString &cmdName,
                                  wxString cmdParams)
{
   // Stage 1: create a Command object of the right type

   CommandMessageTarget *scriptOutput = ScriptCommandRelay::GetResponseTarget();
   CommandOutputTarget *output
      = new CommandOutputTarget(new NullProgressTarget(),
                                scriptOutput,
                                scriptOutput);

   CommandType *factory = CommandDirectory::Get()->LookUp(cmdName);

   if (factory == NULL)
   {
      // Fall back to hoping the Batch Command system can handle it
      CommandType *type = CommandDirectory::Get()->LookUp(wxT("BatchCommand"));
      wxASSERT(type != NULL);
      mCommand = type->Create(output);
      mCommand->SetParameter(wxT("CommandName"), cmdName);
      mCommand->SetParameter(wxT("ParamString"), cmdParams);
      Success(new ApplyAndSendResponse(mCommand));
      return;
   }

   CommandSignature &signature = factory->GetSignature();
   mCommand = factory->Create(output);

   // Stage 2: set the parameters

   ShuttleCli shuttle;
   shuttle.mParams = cmdParams;
   shuttle.mbStoreInClient = true;

   ParamValueMap::const_iterator iter;
   ParamValueMap params = signature.GetDefaults();

   for (iter = params.begin(); iter != params.end(); ++iter)
   {
      wxString paramString;
      if (shuttle.TransferString(iter->first, paramString, wxT("")))
      {
         if (!mCommand->SetParameter(iter->first, paramString))
         {
            Failure();
            return;
         }
      }
   }

   // Check for unrecognised parameters

   while (cmdParams != wxEmptyString)
   {
      cmdParams.Trim(true);
      cmdParams.Trim(false);
      int splitAt = cmdParams.Find(wxT('='));
      if (splitAt < 0 && cmdParams != wxEmptyString)
      {
         Failure(wxT("Parameter string is missing '='"));
         return;
      }
      wxString paramName = cmdParams.Left(splitAt);
      if (params.find(paramName) == params.end())
      {
         Failure(wxT("Unrecognized parameter: '") + paramName + wxT("'"));
         return;
      }
      cmdParams = cmdParams.Mid(splitAt+1);
      splitAt = cmdParams.Find(wxT(' '));
      if (splitAt < 0)
      {
         splitAt = cmdParams.Len();
      }
      cmdParams = cmdParams.Mid(splitAt);
   }

   Success(new ApplyAndSendResponse(mCommand));
}