Ported test_config.cpp from upstream Slic3r.

Extended ConfigBase with set() functions similar to the upstream Slic3r.
ConfigBase::set_deserialize() newly throws if the operation fails.
Extrusion width parameters are newly tested for negative values.
This commit is contained in:
bubnikv 2019-10-18 11:53:19 +02:00
parent b7af51fd6d
commit 13cc74ef0a
6 changed files with 298 additions and 15 deletions

View file

@ -425,7 +425,30 @@ std::string ConfigBase::opt_serialize(const t_config_option_key &opt_key) const
return opt->serialize();
}
bool ConfigBase::set_deserialize(const t_config_option_key &opt_key_src, const std::string &value_src, bool append)
void ConfigBase::set(const std::string &opt_key, int value, bool create)
{
ConfigOption *opt = this->option_throw(opt_key, create);
switch (opt->type()) {
case coInt: static_cast<ConfigOptionInt*>(opt)->value = value; break;
case coFloat: static_cast<ConfigOptionFloat*>(opt)->value = value; break;
case coFloatOrPercent: static_cast<ConfigOptionFloatOrPercent*>(opt)->value = value; static_cast<ConfigOptionFloatOrPercent*>(opt)->percent = false; break;
case coString: static_cast<ConfigOptionString*>(opt)->value = std::to_string(value); break;
default: throw BadOptionTypeException("Configbase::set() - conversion from int not possible");
}
}
void ConfigBase::set(const std::string &opt_key, double value, bool create)
{
ConfigOption *opt = this->option_throw(opt_key, create);
switch (opt->type()) {
case coFloat: static_cast<ConfigOptionFloat*>(opt)->value = value; break;
case coFloatOrPercent: static_cast<ConfigOptionFloatOrPercent*>(opt)->value = value; static_cast<ConfigOptionFloatOrPercent*>(opt)->percent = false; break;
case coString: static_cast<ConfigOptionString*>(opt)->value = std::to_string(value); break;
default: throw BadOptionTypeException("Configbase::set() - conversion from float not possible");
}
}
bool ConfigBase::set_deserialize_nothrow(const t_config_option_key &opt_key_src, const std::string &value_src, bool append)
{
t_config_option_key opt_key = opt_key_src;
std::string value = value_src;
@ -438,12 +461,16 @@ bool ConfigBase::set_deserialize(const t_config_option_key &opt_key_src, const s
return this->set_deserialize_raw(opt_key, value, append);
}
bool ConfigBase::set_deserialize(std::initializer_list<SetDeserializeItem> items)
void ConfigBase::set_deserialize(const t_config_option_key &opt_key_src, const std::string &value_src, bool append)
{
if (! this->set_deserialize_nothrow(opt_key_src, value_src, append))
throw BadOptionTypeException("ConfigBase::set_deserialize() failed");
}
void ConfigBase::set_deserialize(std::initializer_list<SetDeserializeItem> items)
{
bool deserialized = true;
for (const SetDeserializeItem &item : items)
deserialized &= this->set_deserialize(item.opt_key, item.opt_value, item.append);
return deserialized;
this->set_deserialize(item.opt_key, item.opt_value, item.append);
}
bool ConfigBase::set_deserialize_raw(const t_config_option_key &opt_key_src, const std::string &value, bool append)
@ -831,7 +858,7 @@ bool DynamicConfig::read_cli(int argc, char** argv, t_config_option_keys* extra,
static_cast<ConfigOptionString*>(opt_base)->value = value;
} else {
// Any scalar value of a type different from Bool and String.
if (! this->set_deserialize(opt_key, value, false)) {
if (! this->set_deserialize_nothrow(opt_key, value, false)) {
boost::nowide::cerr << "Invalid value supplied for --" << token.c_str() << std::endl;
return false;
}