jeudi 20 avril 2017

Boost custom validator for std::vector

I have written the following custom validator for std::vector<double>.

typedef vector<double> coordinate;
void validate(boost::any& v,
  const vector<string>& values,
  coordinate*, int) {
  std::cout << "Custom validator called\n";
  coordinate c;
  vector<double> dvalues;
  for(vector<string>::const_iterator it = values.begin();
    it != values.end();
    ++it) {
    stringstream ss(*it);
    copy(istream_iterator<double>(ss), istream_iterator<double>(),
      back_inserter(dvalues));
    if(!ss.eof()) {
      std::cerr << "SS EOF\n";
      throw po::invalid_option_value("Invalid coordinate specification sseof");
    }
  }
  if(dvalues.size() != 2) {
    std::cerr << "dvalues size\n";
    throw po::invalid_option_value("Invalid coordinate specification dvalues size");
  }
  c.push_back(dvalues[0]);
  c.push_back(dvalues[1]);
  v = c;
}

And I add options in the following manner:

coordinate c;
// Setup options.
po::options_description desc("Options");
desc.add_options()
  ("instruments.prop", po::value<coordinate>( &c )->multitoken(),
   "plugin names" );

The custom validator is not being used by the program at all. I don't get the message "Custom validator called" which should have been printed if my validator was being used. Instead I get this error:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl

' what(): the argument ('1 2.9') for option 'instruments.name' is invalid Aborted (core dumped)

My config file looks like:

[instruments]
prop= 1 2.9

Any ideas on how to parse multiple arguments from config file without writing them in separate lines like this:

[instruments]
prop = 1
prop = 2.9

Aucun commentaire:

Enregistrer un commentaire