2013-12-07 15:14:30 +00:00
|
|
|
#include "Config.hpp"
|
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
|
2013-12-21 20:06:45 +00:00
|
|
|
bool
|
|
|
|
ConfigBase::has(const t_config_option_key opt_key) {
|
|
|
|
return (this->option(opt_key, false) != NULL);
|
|
|
|
}
|
|
|
|
|
2013-12-20 15:37:28 +00:00
|
|
|
void
|
|
|
|
ConfigBase::apply(ConfigBase &other, bool ignore_nonexistent) {
|
|
|
|
// get list of option keys to apply
|
|
|
|
t_config_option_keys opt_keys;
|
|
|
|
other.keys(&opt_keys);
|
|
|
|
|
|
|
|
// loop through options and apply them
|
|
|
|
for (t_config_option_keys::const_iterator it = opt_keys.begin(); it != opt_keys.end(); ++it) {
|
2013-12-21 20:06:45 +00:00
|
|
|
ConfigOption* my_opt = this->option(*it, true);
|
2013-12-21 15:32:11 +00:00
|
|
|
if (my_opt == NULL) {
|
|
|
|
if (ignore_nonexistent == false) throw "Attempt to apply non-existent option";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not the most efficient way, but easier than casting pointers to subclasses
|
|
|
|
my_opt->deserialize( other.option(*it)->serialize() );
|
2013-12-20 15:37:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:54:11 +00:00
|
|
|
std::string
|
|
|
|
ConfigBase::serialize(const t_config_option_key opt_key) {
|
|
|
|
ConfigOption* opt = this->option(opt_key);
|
|
|
|
assert(opt != NULL);
|
|
|
|
return opt->serialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConfigBase::set_deserialize(const t_config_option_key opt_key, std::string str) {
|
|
|
|
ConfigOption* opt = this->option(opt_key);
|
|
|
|
assert(opt != NULL);
|
|
|
|
opt->deserialize(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
ConfigBase::get_abs_value(const t_config_option_key opt_key) {
|
|
|
|
// get option definition
|
2013-12-21 15:15:41 +00:00
|
|
|
assert(this->def->count(opt_key) != 0);
|
|
|
|
ConfigOptionDef* def = &(*this->def)[opt_key];
|
2013-12-20 19:54:11 +00:00
|
|
|
assert(def->type == coFloatOrPercent);
|
|
|
|
|
|
|
|
// get stored option value
|
|
|
|
ConfigOptionFloatOrPercent* opt = dynamic_cast<ConfigOptionFloatOrPercent*>(this->option(opt_key));
|
|
|
|
assert(opt != NULL);
|
|
|
|
|
|
|
|
// compute absolute value
|
|
|
|
if (opt->percent) {
|
|
|
|
ConfigOptionFloat* optbase = dynamic_cast<ConfigOptionFloat*>(this->option(def->ratio_over));
|
|
|
|
assert(optbase != NULL);
|
|
|
|
return optbase->value * opt->value / 100;
|
|
|
|
} else {
|
|
|
|
return opt->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 15:37:28 +00:00
|
|
|
#ifdef SLIC3RXS
|
2013-12-21 20:06:45 +00:00
|
|
|
SV*
|
|
|
|
ConfigBase::as_hash() {
|
|
|
|
HV* hv = newHV();
|
|
|
|
|
|
|
|
t_config_option_keys opt_keys;
|
|
|
|
this->keys(&opt_keys);
|
|
|
|
|
|
|
|
for (t_config_option_keys::const_iterator it = opt_keys.begin(); it != opt_keys.end(); ++it)
|
|
|
|
(void)hv_store( hv, it->c_str(), it->length(), this->get(*it), 0 );
|
|
|
|
|
|
|
|
return newRV_noinc((SV*)hv);
|
|
|
|
}
|
|
|
|
|
2013-12-20 15:37:28 +00:00
|
|
|
SV*
|
|
|
|
ConfigBase::get(t_config_option_key opt_key) {
|
|
|
|
ConfigOption* opt = this->option(opt_key);
|
|
|
|
if (opt == NULL) return &PL_sv_undef;
|
2013-12-20 19:54:11 +00:00
|
|
|
if (ConfigOptionFloat* optv = dynamic_cast<ConfigOptionFloat*>(opt)) {
|
|
|
|
return newSVnv(optv->value);
|
2013-12-21 13:27:58 +00:00
|
|
|
} else if (ConfigOptionFloats* optv = dynamic_cast<ConfigOptionFloats*>(opt)) {
|
|
|
|
AV* av = newAV();
|
|
|
|
av_fill(av, optv->values.size()-1);
|
|
|
|
for (std::vector<float>::iterator it = optv->values.begin(); it != optv->values.end(); ++it)
|
|
|
|
av_store(av, it - optv->values.begin(), newSVnv(*it));
|
|
|
|
return newRV_noinc((SV*)av);
|
2013-12-20 19:54:11 +00:00
|
|
|
} else if (ConfigOptionInt* optv = dynamic_cast<ConfigOptionInt*>(opt)) {
|
|
|
|
return newSViv(optv->value);
|
2013-12-21 13:27:58 +00:00
|
|
|
} else if (ConfigOptionInts* optv = dynamic_cast<ConfigOptionInts*>(opt)) {
|
|
|
|
AV* av = newAV();
|
|
|
|
av_fill(av, optv->values.size()-1);
|
|
|
|
for (std::vector<int>::iterator it = optv->values.begin(); it != optv->values.end(); ++it)
|
|
|
|
av_store(av, it - optv->values.begin(), newSViv(*it));
|
|
|
|
return newRV_noinc((SV*)av);
|
2013-12-20 19:54:11 +00:00
|
|
|
} else if (ConfigOptionString* optv = dynamic_cast<ConfigOptionString*>(opt)) {
|
|
|
|
// we don't serialize() because that would escape newlines
|
|
|
|
return newSVpvn(optv->value.c_str(), optv->value.length());
|
|
|
|
} else if (ConfigOptionPoint* optv = dynamic_cast<ConfigOptionPoint*>(opt)) {
|
|
|
|
return optv->point.to_SV_pureperl();
|
2013-12-21 13:27:58 +00:00
|
|
|
} else if (ConfigOptionPoints* optv = dynamic_cast<ConfigOptionPoints*>(opt)) {
|
|
|
|
AV* av = newAV();
|
|
|
|
av_fill(av, optv->points.size()-1);
|
|
|
|
for (Pointfs::iterator it = optv->points.begin(); it != optv->points.end(); ++it)
|
|
|
|
av_store(av, it - optv->points.begin(), it->to_SV_pureperl());
|
|
|
|
return newRV_noinc((SV*)av);
|
2013-12-20 20:32:18 +00:00
|
|
|
} else if (ConfigOptionBool* optv = dynamic_cast<ConfigOptionBool*>(opt)) {
|
|
|
|
return newSViv(optv->value ? 1 : 0);
|
2013-12-21 13:27:58 +00:00
|
|
|
} else if (ConfigOptionBools* optv = dynamic_cast<ConfigOptionBools*>(opt)) {
|
|
|
|
AV* av = newAV();
|
|
|
|
av_fill(av, optv->values.size()-1);
|
|
|
|
for (std::vector<bool>::iterator it = optv->values.begin(); it != optv->values.end(); ++it)
|
|
|
|
av_store(av, it - optv->values.begin(), newSViv(*it ? 1 : 0));
|
|
|
|
return newRV_noinc((SV*)av);
|
2013-12-20 19:54:11 +00:00
|
|
|
} else {
|
|
|
|
std::string serialized = opt->serialize();
|
|
|
|
return newSVpvn(serialized.c_str(), serialized.length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConfigBase::set(t_config_option_key opt_key, SV* value) {
|
|
|
|
ConfigOption* opt = this->option(opt_key, true);
|
|
|
|
assert(opt != NULL);
|
|
|
|
|
|
|
|
if (ConfigOptionFloat* optv = dynamic_cast<ConfigOptionFloat*>(opt)) {
|
|
|
|
optv->value = SvNV(value);
|
2013-12-21 13:27:58 +00:00
|
|
|
} else if (ConfigOptionFloats* optv = dynamic_cast<ConfigOptionFloats*>(opt)) {
|
|
|
|
optv->values.clear();
|
|
|
|
AV* av = (AV*)SvRV(value);
|
|
|
|
const size_t len = av_len(av)+1;
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
SV** elem = av_fetch(av, i, 0);
|
|
|
|
optv->values.push_back(SvNV(*elem));
|
|
|
|
}
|
2013-12-20 19:54:11 +00:00
|
|
|
} else if (ConfigOptionInt* optv = dynamic_cast<ConfigOptionInt*>(opt)) {
|
|
|
|
optv->value = SvIV(value);
|
2013-12-21 13:27:58 +00:00
|
|
|
} else if (ConfigOptionInts* optv = dynamic_cast<ConfigOptionInts*>(opt)) {
|
|
|
|
optv->values.clear();
|
|
|
|
AV* av = (AV*)SvRV(value);
|
|
|
|
const size_t len = av_len(av)+1;
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
SV** elem = av_fetch(av, i, 0);
|
|
|
|
optv->values.push_back(SvIV(*elem));
|
|
|
|
}
|
2013-12-20 19:54:11 +00:00
|
|
|
} else if (ConfigOptionString* optv = dynamic_cast<ConfigOptionString*>(opt)) {
|
|
|
|
optv->value = std::string(SvPV_nolen(value), SvCUR(value));
|
|
|
|
} else if (ConfigOptionPoint* optv = dynamic_cast<ConfigOptionPoint*>(opt)) {
|
|
|
|
optv->point.from_SV(value);
|
2013-12-21 13:27:58 +00:00
|
|
|
} else if (ConfigOptionPoints* optv = dynamic_cast<ConfigOptionPoints*>(opt)) {
|
|
|
|
optv->points.clear();
|
|
|
|
AV* av = (AV*)SvRV(value);
|
|
|
|
const size_t len = av_len(av)+1;
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
SV** elem = av_fetch(av, i, 0);
|
|
|
|
Pointf point;
|
|
|
|
point.from_SV(*elem);
|
|
|
|
optv->points.push_back(point);
|
|
|
|
}
|
2013-12-20 20:32:18 +00:00
|
|
|
} else if (ConfigOptionBool* optv = dynamic_cast<ConfigOptionBool*>(opt)) {
|
|
|
|
optv->value = SvTRUE(value);
|
2013-12-21 13:27:58 +00:00
|
|
|
} else if (ConfigOptionBools* optv = dynamic_cast<ConfigOptionBools*>(opt)) {
|
|
|
|
optv->values.clear();
|
|
|
|
AV* av = (AV*)SvRV(value);
|
|
|
|
const size_t len = av_len(av)+1;
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
SV** elem = av_fetch(av, i, 0);
|
|
|
|
optv->values.push_back(SvTRUE(*elem));
|
|
|
|
}
|
2013-12-20 15:37:28 +00:00
|
|
|
} else {
|
2013-12-20 19:54:11 +00:00
|
|
|
opt->deserialize( std::string(SvPV_nolen(value)) );
|
2013-12-20 15:37:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-20 19:54:11 +00:00
|
|
|
DynamicConfig::~DynamicConfig () {
|
|
|
|
for (t_options_map::iterator it = this->options.begin(); it != this->options.end(); ++it) {
|
|
|
|
ConfigOption* opt = it->second;
|
|
|
|
if (opt != NULL) delete opt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 15:37:28 +00:00
|
|
|
ConfigOption*
|
2013-12-20 19:54:11 +00:00
|
|
|
DynamicConfig::option(const t_config_option_key opt_key, bool create) {
|
2013-12-21 15:15:41 +00:00
|
|
|
if (this->options.count(opt_key) == 0) {
|
2013-12-20 19:54:11 +00:00
|
|
|
if (create) {
|
2013-12-21 15:15:41 +00:00
|
|
|
ConfigOptionDef* optdef = &(*this->def)[opt_key];
|
2013-12-20 19:54:11 +00:00
|
|
|
ConfigOption* opt;
|
2013-12-21 15:15:41 +00:00
|
|
|
if (optdef->type == coFloat) {
|
2013-12-20 19:54:11 +00:00
|
|
|
opt = new ConfigOptionFloat ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coFloats) {
|
2013-12-21 13:27:58 +00:00
|
|
|
opt = new ConfigOptionFloats ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coInt) {
|
2013-12-20 19:54:11 +00:00
|
|
|
opt = new ConfigOptionInt ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coInts) {
|
2013-12-21 13:27:58 +00:00
|
|
|
opt = new ConfigOptionInts ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coString) {
|
2013-12-20 19:54:11 +00:00
|
|
|
opt = new ConfigOptionString ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coFloatOrPercent) {
|
2013-12-20 19:54:11 +00:00
|
|
|
opt = new ConfigOptionFloatOrPercent ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coPoint) {
|
2013-12-20 19:54:11 +00:00
|
|
|
opt = new ConfigOptionPoint ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coPoints) {
|
2013-12-21 13:27:58 +00:00
|
|
|
opt = new ConfigOptionPoints ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coBool) {
|
2013-12-20 20:32:18 +00:00
|
|
|
opt = new ConfigOptionBool ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coBools) {
|
2013-12-21 13:27:58 +00:00
|
|
|
opt = new ConfigOptionBools ();
|
2013-12-21 15:15:41 +00:00
|
|
|
} else if (optdef->type == coEnum) {
|
|
|
|
ConfigOptionEnumGeneric* optv = new ConfigOptionEnumGeneric ();
|
|
|
|
optv->keys_map = &optdef->enum_keys_map;
|
|
|
|
opt = static_cast<ConfigOption*>(optv);
|
2013-12-20 19:54:11 +00:00
|
|
|
} else {
|
|
|
|
throw "Unknown option type";
|
|
|
|
}
|
|
|
|
this->options[opt_key] = opt;
|
|
|
|
return opt;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2013-12-21 15:15:41 +00:00
|
|
|
return this->options[opt_key];
|
2013-12-20 15:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DynamicConfig::keys(t_config_option_keys *keys) {
|
|
|
|
for (t_options_map::const_iterator it = this->options.begin(); it != this->options.end(); ++it)
|
|
|
|
keys->push_back(it->first);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StaticConfig::keys(t_config_option_keys *keys) {
|
2013-12-21 15:15:41 +00:00
|
|
|
for (t_optiondef_map::const_iterator it = this->def->begin(); it != this->def->end(); ++it) {
|
2013-12-20 15:37:28 +00:00
|
|
|
ConfigOption* opt = this->option(it->first);
|
|
|
|
if (opt != NULL) keys->push_back(it->first);
|
|
|
|
}
|
|
|
|
}
|
2013-12-07 15:14:30 +00:00
|
|
|
|
|
|
|
}
|