2013-12-07 15:14:30 +00:00
|
|
|
#include "Config.hpp"
|
2015-07-01 16:18:25 +00:00
|
|
|
#include <stdlib.h> // for setenv()
|
2015-12-11 20:36:51 +00:00
|
|
|
#include <assert.h>
|
2013-12-07 15:14:30 +00:00
|
|
|
|
2015-11-06 10:34:37 +00:00
|
|
|
#if defined(_WIN32) && !defined(setenv) && defined(_putenv_s)
|
2015-07-10 14:01:45 +00:00
|
|
|
#define setenv(k, v, o) _putenv_s(k, v)
|
|
|
|
#endif
|
|
|
|
|
2013-12-07 15:14:30 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
2015-12-02 18:32:57 +00:00
|
|
|
bool
|
|
|
|
operator== (const ConfigOption &a, const ConfigOption &b)
|
|
|
|
{
|
|
|
|
return a.serialize().compare(b.serialize()) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator!= (const ConfigOption &a, const ConfigOption &b)
|
|
|
|
{
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
2015-12-07 18:39:49 +00:00
|
|
|
ConfigDef::~ConfigDef()
|
|
|
|
{
|
|
|
|
for (t_optiondef_map::iterator it = this->options.begin(); it != this->options.end(); ++it) {
|
|
|
|
if (it->second.default_value != NULL)
|
|
|
|
delete it->second.default_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 12:09:25 +00:00
|
|
|
ConfigOptionDef*
|
|
|
|
ConfigDef::add(const t_config_option_key &opt_key, ConfigOptionType type)
|
|
|
|
{
|
|
|
|
ConfigOptionDef* opt = &this->options[opt_key];
|
|
|
|
opt->type = type;
|
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
|
2015-12-07 18:39:49 +00:00
|
|
|
const ConfigOptionDef*
|
|
|
|
ConfigDef::get(const t_config_option_key &opt_key) const
|
|
|
|
{
|
|
|
|
if (this->options.count(opt_key) == 0) return NULL;
|
|
|
|
return &const_cast<ConfigDef*>(this)->options[opt_key];
|
|
|
|
}
|
|
|
|
|
2013-12-21 20:06:45 +00:00
|
|
|
bool
|
2015-08-11 23:03:43 +00:00
|
|
|
ConfigBase::has(const t_config_option_key &opt_key) {
|
2013-12-21 20:06:45 +00:00
|
|
|
return (this->option(opt_key, false) != NULL);
|
|
|
|
}
|
|
|
|
|
2013-12-20 15:37:28 +00:00
|
|
|
void
|
2014-03-22 19:12:54 +00:00
|
|
|
ConfigBase::apply(const ConfigBase &other, bool ignore_nonexistent) {
|
2013-12-20 15:37:28 +00:00
|
|
|
// get list of option keys to apply
|
2015-07-01 16:18:25 +00:00
|
|
|
t_config_option_keys opt_keys = other.keys();
|
2013-12-20 15:37:28 +00:00
|
|
|
|
|
|
|
// 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
|
2014-03-24 13:16:37 +00:00
|
|
|
bool res = my_opt->deserialize( other.option(*it)->serialize() );
|
2016-03-13 14:25:50 +00:00
|
|
|
if (!res) {
|
|
|
|
std::string error = "Unexpected failure when deserializing serialized value for " + *it;
|
|
|
|
CONFESS(error.c_str());
|
|
|
|
}
|
2013-12-20 15:37:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-09 11:25:59 +00:00
|
|
|
bool
|
|
|
|
ConfigBase::equals(ConfigBase &other) {
|
|
|
|
return this->diff(other).empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// this will *ignore* options not present in both configs
|
|
|
|
t_config_option_keys
|
|
|
|
ConfigBase::diff(ConfigBase &other) {
|
|
|
|
t_config_option_keys diff;
|
|
|
|
|
2015-07-01 16:18:25 +00:00
|
|
|
t_config_option_keys my_keys = this->keys();
|
2014-11-09 11:25:59 +00:00
|
|
|
for (t_config_option_keys::const_iterator opt_key = my_keys.begin(); opt_key != my_keys.end(); ++opt_key) {
|
|
|
|
if (other.has(*opt_key) && other.serialize(*opt_key) != this->serialize(*opt_key)) {
|
|
|
|
diff.push_back(*opt_key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:54:11 +00:00
|
|
|
std::string
|
2015-12-02 18:32:57 +00:00
|
|
|
ConfigBase::serialize(const t_config_option_key &opt_key) const {
|
|
|
|
const ConfigOption* opt = this->option(opt_key);
|
2013-12-20 19:54:11 +00:00
|
|
|
assert(opt != NULL);
|
|
|
|
return opt->serialize();
|
|
|
|
}
|
|
|
|
|
2014-03-24 00:07:30 +00:00
|
|
|
bool
|
2015-08-11 23:03:43 +00:00
|
|
|
ConfigBase::set_deserialize(const t_config_option_key &opt_key, std::string str) {
|
2015-12-07 18:39:49 +00:00
|
|
|
const ConfigOptionDef* optdef = this->def->get(opt_key);
|
|
|
|
if (optdef == NULL) throw "Calling set_deserialize() on unknown option";
|
2014-01-01 16:29:15 +00:00
|
|
|
if (!optdef->shortcut.empty()) {
|
2015-12-07 18:39:49 +00:00
|
|
|
for (std::vector<t_config_option_key>::const_iterator it = optdef->shortcut.begin(); it != optdef->shortcut.end(); ++it) {
|
2014-03-24 00:07:30 +00:00
|
|
|
if (!this->set_deserialize(*it, str)) return false;
|
|
|
|
}
|
|
|
|
return true;
|
2014-01-01 16:29:15 +00:00
|
|
|
}
|
|
|
|
|
2013-12-23 23:30:51 +00:00
|
|
|
ConfigOption* opt = this->option(opt_key, true);
|
2013-12-20 19:54:11 +00:00
|
|
|
assert(opt != NULL);
|
2014-03-24 00:07:30 +00:00
|
|
|
return opt->deserialize(str);
|
2013-12-20 19:54:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-22 00:38:10 +00:00
|
|
|
double
|
2015-08-11 23:03:43 +00:00
|
|
|
ConfigBase::get_abs_value(const t_config_option_key &opt_key) {
|
2014-01-05 12:16:13 +00:00
|
|
|
ConfigOption* opt = this->option(opt_key, false);
|
|
|
|
if (ConfigOptionFloatOrPercent* optv = dynamic_cast<ConfigOptionFloatOrPercent*>(opt)) {
|
|
|
|
// get option definition
|
2015-12-07 18:39:49 +00:00
|
|
|
const ConfigOptionDef* def = this->def->get(opt_key);
|
|
|
|
assert(def != NULL);
|
2014-01-05 12:16:13 +00:00
|
|
|
|
|
|
|
// compute absolute value over the absolute value of the base option
|
|
|
|
return optv->get_abs_value(this->get_abs_value(def->ratio_over));
|
|
|
|
} else if (ConfigOptionFloat* optv = dynamic_cast<ConfigOptionFloat*>(opt)) {
|
|
|
|
return optv->value;
|
2013-12-20 19:54:11 +00:00
|
|
|
} else {
|
2014-01-05 12:16:13 +00:00
|
|
|
throw "Not a valid option type for get_abs_value()";
|
2013-12-20 19:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-01 16:29:15 +00:00
|
|
|
double
|
2015-08-11 23:03:43 +00:00
|
|
|
ConfigBase::get_abs_value(const t_config_option_key &opt_key, double ratio_over) {
|
2014-01-01 16:29:15 +00:00
|
|
|
// get stored option value
|
|
|
|
ConfigOptionFloatOrPercent* opt = dynamic_cast<ConfigOptionFloatOrPercent*>(this->option(opt_key));
|
|
|
|
assert(opt != NULL);
|
|
|
|
|
|
|
|
// compute absolute value
|
2014-01-05 12:16:13 +00:00
|
|
|
return opt->get_abs_value(ratio_over);
|
2014-01-01 16:29:15 +00:00
|
|
|
}
|
|
|
|
|
2015-07-01 16:18:25 +00:00
|
|
|
void
|
|
|
|
ConfigBase::setenv_()
|
|
|
|
{
|
2015-11-06 10:34:37 +00:00
|
|
|
#ifdef setenv
|
2015-07-01 16:18:25 +00:00
|
|
|
t_config_option_keys opt_keys = this->keys();
|
|
|
|
for (t_config_option_keys::const_iterator it = opt_keys.begin(); it != opt_keys.end(); ++it) {
|
|
|
|
// prepend the SLIC3R_ prefix
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << "SLIC3R_";
|
|
|
|
ss << *it;
|
|
|
|
std::string envname = ss.str();
|
|
|
|
|
|
|
|
// capitalize environment variable name
|
|
|
|
for (size_t i = 0; i < envname.size(); ++i)
|
|
|
|
envname[i] = (envname[i] <= 'z' && envname[i] >= 'a') ? envname[i]-('a'-'A') : envname[i];
|
|
|
|
|
|
|
|
setenv(envname.c_str(), this->serialize(*it).c_str(), 1);
|
|
|
|
}
|
2015-11-06 10:34:37 +00:00
|
|
|
#endif
|
2015-07-01 16:18:25 +00:00
|
|
|
}
|
|
|
|
|
2015-12-16 11:33:19 +00:00
|
|
|
const ConfigOption*
|
|
|
|
ConfigBase::option(const t_config_option_key &opt_key) const {
|
|
|
|
return const_cast<ConfigBase*>(this)->option(opt_key, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfigOption*
|
|
|
|
ConfigBase::option(const t_config_option_key &opt_key, bool create) {
|
|
|
|
return this->optptr(opt_key, create);
|
|
|
|
}
|
|
|
|
|
2014-05-08 11:33:43 +00:00
|
|
|
DynamicConfig& DynamicConfig::operator= (DynamicConfig other)
|
|
|
|
{
|
|
|
|
this->swap(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DynamicConfig::swap(DynamicConfig &other)
|
|
|
|
{
|
|
|
|
std::swap(this->options, other.options);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 19:12:54 +00:00
|
|
|
DynamicConfig::DynamicConfig (const DynamicConfig& other) {
|
|
|
|
this->def = other.def;
|
|
|
|
this->apply(other, false);
|
|
|
|
}
|
|
|
|
|
2013-12-20 15:37:28 +00:00
|
|
|
ConfigOption*
|
2015-12-16 11:33:19 +00:00
|
|
|
DynamicConfig::optptr(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) {
|
2015-12-07 18:39:49 +00:00
|
|
|
const ConfigOptionDef* optdef = this->def->get(opt_key);
|
|
|
|
assert(optdef != NULL);
|
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 23:39:03 +00:00
|
|
|
} else if (optdef->type == coStrings) {
|
|
|
|
opt = new ConfigOptionStrings ();
|
2014-03-22 15:23:33 +00:00
|
|
|
} else if (optdef->type == coPercent) {
|
|
|
|
opt = new ConfigOptionPercent ();
|
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
|
|
|
}
|
|
|
|
|
2014-05-26 21:51:58 +00:00
|
|
|
template<class T>
|
|
|
|
T*
|
2015-08-11 23:03:43 +00:00
|
|
|
DynamicConfig::opt(const t_config_option_key &opt_key, bool create) {
|
2014-05-26 21:51:58 +00:00
|
|
|
return dynamic_cast<T*>(this->option(opt_key, create));
|
|
|
|
}
|
2015-08-11 23:03:43 +00:00
|
|
|
template ConfigOptionInt* DynamicConfig::opt<ConfigOptionInt>(const t_config_option_key &opt_key, bool create);
|
|
|
|
template ConfigOptionBool* DynamicConfig::opt<ConfigOptionBool>(const t_config_option_key &opt_key, bool create);
|
|
|
|
template ConfigOptionBools* DynamicConfig::opt<ConfigOptionBools>(const t_config_option_key &opt_key, bool create);
|
|
|
|
template ConfigOptionPercent* DynamicConfig::opt<ConfigOptionPercent>(const t_config_option_key &opt_key, bool create);
|
2014-05-26 21:51:58 +00:00
|
|
|
|
2015-07-01 16:18:25 +00:00
|
|
|
t_config_option_keys
|
|
|
|
DynamicConfig::keys() const {
|
|
|
|
t_config_option_keys keys;
|
2013-12-20 15:37:28 +00:00
|
|
|
for (t_options_map::const_iterator it = this->options.begin(); it != this->options.end(); ++it)
|
2015-07-01 16:18:25 +00:00
|
|
|
keys.push_back(it->first);
|
|
|
|
return keys;
|
2013-12-20 15:37:28 +00:00
|
|
|
}
|
|
|
|
|
2014-01-22 20:14:47 +00:00
|
|
|
void
|
2015-08-11 23:03:43 +00:00
|
|
|
DynamicConfig::erase(const t_config_option_key &opt_key) {
|
2014-01-22 20:14:47 +00:00
|
|
|
this->options.erase(opt_key);
|
|
|
|
}
|
|
|
|
|
2015-12-07 18:39:49 +00:00
|
|
|
void
|
|
|
|
StaticConfig::set_defaults()
|
|
|
|
{
|
|
|
|
// use defaults from definition
|
|
|
|
if (this->def == NULL) return;
|
|
|
|
t_config_option_keys keys = this->keys();
|
|
|
|
for (t_config_option_keys::const_iterator it = keys.begin(); it != keys.end(); ++it) {
|
|
|
|
const ConfigOptionDef* def = this->def->get(*it);
|
|
|
|
|
|
|
|
if (def->default_value != NULL)
|
|
|
|
this->option(*it)->set(*def->default_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-01 16:18:25 +00:00
|
|
|
t_config_option_keys
|
|
|
|
StaticConfig::keys() const {
|
|
|
|
t_config_option_keys keys;
|
2015-12-07 18:39:49 +00:00
|
|
|
for (t_optiondef_map::const_iterator it = this->def->options.begin(); it != this->def->options.end(); ++it) {
|
2014-03-22 19:12:54 +00:00
|
|
|
const ConfigOption* opt = this->option(it->first);
|
2015-07-01 16:18:25 +00:00
|
|
|
if (opt != NULL) keys.push_back(it->first);
|
2013-12-20 15:37:28 +00:00
|
|
|
}
|
2015-07-01 16:18:25 +00:00
|
|
|
return keys;
|
2013-12-20 15:37:28 +00:00
|
|
|
}
|
2013-12-07 15:14:30 +00:00
|
|
|
|
|
|
|
}
|