Config.cpp/h - inlined short functions, converted loops to C++11,
fixed some constness issues.
This commit is contained in:
parent
2178180a19
commit
e32632b9d9
2 changed files with 71 additions and 159 deletions
|
@ -162,48 +162,8 @@ bool unescape_strings_cstyle(const std::string &str, std::vector<std::string> &o
|
|||
}
|
||||
}
|
||||
|
||||
bool
|
||||
operator== (const ConfigOption &a, const ConfigOption &b)
|
||||
void ConfigBase::apply(const ConfigBase &other, bool ignore_nonexistent)
|
||||
{
|
||||
return a.serialize().compare(b.serialize()) == 0;
|
||||
}
|
||||
|
||||
bool
|
||||
operator!= (const ConfigOption &a, const ConfigOption &b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
ConfigOptionDef*
|
||||
ConfigDef::add(const t_config_option_key &opt_key, ConfigOptionType type)
|
||||
{
|
||||
ConfigOptionDef* opt = &this->options[opt_key];
|
||||
opt->type = type;
|
||||
return opt;
|
||||
}
|
||||
|
||||
const ConfigOptionDef*
|
||||
ConfigDef::get(const t_config_option_key &opt_key) const
|
||||
{
|
||||
t_optiondef_map::iterator it = const_cast<ConfigDef*>(this)->options.find(opt_key);
|
||||
return (it == this->options.end()) ? NULL : &it->second;
|
||||
}
|
||||
|
||||
bool
|
||||
ConfigBase::has(const t_config_option_key &opt_key) {
|
||||
return (this->option(opt_key, false) != NULL);
|
||||
}
|
||||
|
||||
void
|
||||
ConfigBase::apply(const ConfigBase &other, bool ignore_nonexistent) {
|
||||
// get list of option keys to apply
|
||||
t_config_option_keys opt_keys = other.keys();
|
||||
|
||||
|
@ -224,35 +184,25 @@ ConfigBase::apply(const ConfigBase &other, bool ignore_nonexistent) {
|
|||
}
|
||||
}
|
||||
|
||||
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 ConfigBase::diff(const ConfigBase &other) const
|
||||
{
|
||||
t_config_option_keys diff;
|
||||
|
||||
t_config_option_keys my_keys = this->keys();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
for (const t_config_option_key &opt_key : this->keys())
|
||||
if (other.has(opt_key) && other.serialize(opt_key) != this->serialize(opt_key))
|
||||
diff.push_back(opt_key);
|
||||
return diff;
|
||||
}
|
||||
|
||||
std::string
|
||||
ConfigBase::serialize(const t_config_option_key &opt_key) const {
|
||||
std::string ConfigBase::serialize(const t_config_option_key &opt_key) const
|
||||
{
|
||||
const ConfigOption* opt = this->option(opt_key);
|
||||
assert(opt != NULL);
|
||||
assert(opt != nullptr);
|
||||
return opt->serialize();
|
||||
}
|
||||
|
||||
bool
|
||||
ConfigBase::set_deserialize(const t_config_option_key &opt_key, std::string str) {
|
||||
bool ConfigBase::set_deserialize(const t_config_option_key &opt_key, std::string str)
|
||||
{
|
||||
const ConfigOptionDef* optdef = this->def->get(opt_key);
|
||||
if (optdef == NULL) throw "Calling set_deserialize() on unknown option";
|
||||
if (!optdef->shortcut.empty()) {
|
||||
|
@ -261,22 +211,20 @@ ConfigBase::set_deserialize(const t_config_option_key &opt_key, std::string str)
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
ConfigOption* opt = this->option(opt_key, true);
|
||||
assert(opt != NULL);
|
||||
assert(opt != nullptr);
|
||||
return opt->deserialize(str);
|
||||
}
|
||||
|
||||
// Return an absolute value of a possibly relative config variable.
|
||||
// For example, return absolute infill extrusion width, either from an absolute value, or relative to the layer height.
|
||||
double
|
||||
ConfigBase::get_abs_value(const t_config_option_key &opt_key) const {
|
||||
double ConfigBase::get_abs_value(const t_config_option_key &opt_key) const
|
||||
{
|
||||
const ConfigOption* opt = this->option(opt_key);
|
||||
if (const ConfigOptionFloatOrPercent* optv = dynamic_cast<const ConfigOptionFloatOrPercent*>(opt)) {
|
||||
// get option definition
|
||||
const ConfigOptionDef* def = this->def->get(opt_key);
|
||||
assert(def != NULL);
|
||||
|
||||
assert(def != nullptr);
|
||||
// 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 (const ConfigOptionFloat* optv = dynamic_cast<const ConfigOptionFloat*>(opt)) {
|
||||
|
@ -288,18 +236,16 @@ ConfigBase::get_abs_value(const t_config_option_key &opt_key) const {
|
|||
|
||||
// Return an absolute value of a possibly relative config variable.
|
||||
// For example, return absolute infill extrusion width, either from an absolute value, or relative to a provided value.
|
||||
double
|
||||
ConfigBase::get_abs_value(const t_config_option_key &opt_key, double ratio_over) const {
|
||||
double ConfigBase::get_abs_value(const t_config_option_key &opt_key, double ratio_over) const
|
||||
{
|
||||
// get stored option value
|
||||
const ConfigOptionFloatOrPercent* opt = dynamic_cast<const ConfigOptionFloatOrPercent*>(this->option(opt_key));
|
||||
assert(opt != NULL);
|
||||
|
||||
assert(opt != nullptr);
|
||||
// compute absolute value
|
||||
return opt->get_abs_value(ratio_over);
|
||||
}
|
||||
|
||||
void
|
||||
ConfigBase::setenv_()
|
||||
void ConfigBase::setenv_()
|
||||
{
|
||||
#ifdef setenv
|
||||
t_config_option_keys opt_keys = this->keys();
|
||||
|
@ -319,43 +265,7 @@ ConfigBase::setenv_()
|
|||
#endif
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
DynamicConfig& DynamicConfig::operator= (DynamicConfig other)
|
||||
{
|
||||
this->swap(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void
|
||||
DynamicConfig::swap(DynamicConfig &other)
|
||||
{
|
||||
std::swap(this->def, other.def);
|
||||
std::swap(this->options, other.options);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
DynamicConfig::DynamicConfig (const DynamicConfig& other) {
|
||||
this->def = other.def;
|
||||
this->apply(other, false);
|
||||
}
|
||||
|
||||
ConfigOption*
|
||||
DynamicConfig::optptr(const t_config_option_key &opt_key, bool create) {
|
||||
ConfigOption* DynamicConfig::optptr(const t_config_option_key &opt_key, bool create) {
|
||||
t_options_map::iterator it = options.find(opt_key);
|
||||
if (it == options.end()) {
|
||||
if (create) {
|
||||
|
@ -405,8 +315,7 @@ DynamicConfig::optptr(const t_config_option_key &opt_key, bool create) {
|
|||
}
|
||||
|
||||
template<class T>
|
||||
T*
|
||||
DynamicConfig::opt(const t_config_option_key &opt_key, bool create) {
|
||||
T* DynamicConfig::opt(const t_config_option_key &opt_key, bool create) {
|
||||
return dynamic_cast<T*>(this->option(opt_key, create));
|
||||
}
|
||||
template ConfigOptionInt* DynamicConfig::opt<ConfigOptionInt>(const t_config_option_key &opt_key, bool create);
|
||||
|
@ -414,40 +323,34 @@ template ConfigOptionBool* DynamicConfig::opt<ConfigOptionBool>(const t_config_o
|
|||
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);
|
||||
|
||||
t_config_option_keys
|
||||
DynamicConfig::keys() const {
|
||||
t_config_option_keys DynamicConfig::keys() const
|
||||
{
|
||||
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);
|
||||
keys.reserve(this->options.size());
|
||||
for (const auto &opt : this->options)
|
||||
keys.emplace_back(opt.first);
|
||||
return keys;
|
||||
}
|
||||
|
||||
void
|
||||
DynamicConfig::erase(const t_config_option_key &opt_key) {
|
||||
this->options.erase(opt_key);
|
||||
}
|
||||
|
||||
void
|
||||
StaticConfig::set_defaults()
|
||||
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);
|
||||
if (this->def != nullptr) {
|
||||
for (const std::string &key : this->keys()) {
|
||||
const ConfigOptionDef* def = this->def->get(key);
|
||||
if (def->default_value != nullptr)
|
||||
this->option(key)->set(*def->default_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t_config_option_keys
|
||||
StaticConfig::keys() const {
|
||||
t_config_option_keys StaticConfig::keys() const
|
||||
{
|
||||
t_config_option_keys keys;
|
||||
assert(this->def != NULL);
|
||||
for (t_optiondef_map::const_iterator it = this->def->options.begin(); it != this->def->options.end(); ++it) {
|
||||
const ConfigOption* opt = this->option(it->first);
|
||||
if (opt != NULL) keys.push_back(it->first);
|
||||
}
|
||||
assert(this->def != nullptr);
|
||||
for (const auto &opt_def : this->def->options)
|
||||
if (this->option(opt_def.first) != nullptr)
|
||||
keys.push_back(opt_def.first);
|
||||
return keys;
|
||||
}
|
||||
|
||||
|
|
|
@ -35,8 +35,8 @@ class ConfigOption {
|
|||
virtual double getFloat() const { return 0; };
|
||||
virtual bool getBool() const { return false; };
|
||||
virtual void setInt(int /* val */) { };
|
||||
friend bool operator== (const ConfigOption &a, const ConfigOption &b);
|
||||
friend bool operator!= (const ConfigOption &a, const ConfigOption &b);
|
||||
bool operator==(const ConfigOption &rhs) { return this->serialize().compare(rhs.serialize()) == 0; }
|
||||
bool operator!=(const ConfigOption &rhs) { return this->serialize().compare(rhs.serialize()) != 0; }
|
||||
};
|
||||
|
||||
// Value of a single valued option (bool, int, float, string, point, enum)
|
||||
|
@ -263,7 +263,7 @@ class ConfigOptionPercents : public ConfigOptionFloats
|
|||
public:
|
||||
std::string serialize() const {
|
||||
std::ostringstream ss;
|
||||
for (const auto &v : this->values) {
|
||||
for (const auto v : this->values) {
|
||||
if (&v != &this->values.front()) ss << ",";
|
||||
ss << v << "%";
|
||||
}
|
||||
|
@ -627,33 +627,42 @@ typedef std::map<t_config_option_key,ConfigOptionDef> t_optiondef_map;
|
|||
// but it carries the defaults of the configuration values.
|
||||
class ConfigDef
|
||||
{
|
||||
public:
|
||||
public:
|
||||
t_optiondef_map options;
|
||||
~ConfigDef();
|
||||
ConfigOptionDef* add(const t_config_option_key &opt_key, ConfigOptionType type);
|
||||
const ConfigOptionDef* get(const t_config_option_key &opt_key) const;
|
||||
~ConfigDef() { for (auto &opt : this->options) delete opt.second.default_value; }
|
||||
ConfigOptionDef* add(const t_config_option_key &opt_key, ConfigOptionType type) {
|
||||
ConfigOptionDef* opt = &this->options[opt_key];
|
||||
opt->type = type;
|
||||
return opt;
|
||||
}
|
||||
const ConfigOptionDef* get(const t_config_option_key &opt_key) const {
|
||||
t_optiondef_map::iterator it = const_cast<ConfigDef*>(this)->options.find(opt_key);
|
||||
return (it == this->options.end()) ? nullptr : &it->second;
|
||||
}
|
||||
};
|
||||
|
||||
// An abstract configuration store.
|
||||
class ConfigBase
|
||||
{
|
||||
public:
|
||||
public:
|
||||
// Definition of configuration values for the purpose of GUI presentation, editing, value mapping and config file handling.
|
||||
// The configuration definition is static: It does not carry the actual configuration values,
|
||||
// but it carries the defaults of the configuration values.
|
||||
// ConfigBase does not own ConfigDef, it only references it.
|
||||
const ConfigDef* def;
|
||||
|
||||
ConfigBase() : def(NULL) {};
|
||||
ConfigBase(const ConfigDef *def = nullptr) : def(def) {};
|
||||
virtual ~ConfigBase() {};
|
||||
bool has(const t_config_option_key &opt_key);
|
||||
const ConfigOption* option(const t_config_option_key &opt_key) const;
|
||||
ConfigOption* option(const t_config_option_key &opt_key, bool create = false);
|
||||
bool has(const t_config_option_key &opt_key) const { return this->option(opt_key) != nullptr; }
|
||||
const ConfigOption* option(const t_config_option_key &opt_key) const
|
||||
{ return const_cast<ConfigBase*>(this)->option(opt_key, false); }
|
||||
ConfigOption* option(const t_config_option_key &opt_key, bool create = false)
|
||||
{ return this->optptr(opt_key, create); }
|
||||
virtual ConfigOption* optptr(const t_config_option_key &opt_key, bool create = false) = 0;
|
||||
virtual t_config_option_keys keys() const = 0;
|
||||
void apply(const ConfigBase &other, bool ignore_nonexistent = false);
|
||||
bool equals(ConfigBase &other);
|
||||
t_config_option_keys diff(ConfigBase &other);
|
||||
bool equals(const ConfigBase &other) const { return this->diff(other).empty(); }
|
||||
t_config_option_keys diff(const ConfigBase &other) const;
|
||||
std::string serialize(const t_config_option_key &opt_key) const;
|
||||
bool set_deserialize(const t_config_option_key &opt_key, std::string str);
|
||||
|
||||
|
@ -666,18 +675,18 @@ class ConfigBase
|
|||
// In Slic3r, the dynamic config is mostly used at the user interface layer.
|
||||
class DynamicConfig : public virtual ConfigBase
|
||||
{
|
||||
public:
|
||||
public:
|
||||
DynamicConfig() {};
|
||||
DynamicConfig(const DynamicConfig& other);
|
||||
DynamicConfig& operator= (DynamicConfig other);
|
||||
void swap(DynamicConfig &other);
|
||||
virtual ~DynamicConfig();
|
||||
DynamicConfig(const DynamicConfig& other) : ConfigBase(other.def) { this->apply(other, false); }
|
||||
DynamicConfig& operator= (DynamicConfig other) { this->swap(other); return *this; }
|
||||
virtual ~DynamicConfig() { for (auto &opt : this->options) delete opt.second; }
|
||||
void swap(DynamicConfig &other) { std::swap(this->def, other.def); std::swap(this->options, other.options); }
|
||||
template<class T> T* opt(const t_config_option_key &opt_key, bool create = false);
|
||||
virtual ConfigOption* optptr(const t_config_option_key &opt_key, bool create = false);
|
||||
t_config_option_keys keys() const;
|
||||
void erase(const t_config_option_key &opt_key);
|
||||
void erase(const t_config_option_key &opt_key) { this->options.erase(opt_key); }
|
||||
|
||||
private:
|
||||
private:
|
||||
typedef std::map<t_config_option_key,ConfigOption*> t_options_map;
|
||||
t_options_map options;
|
||||
};
|
||||
|
@ -687,7 +696,7 @@ class DynamicConfig : public virtual ConfigBase
|
|||
// because the configuration values could be accessed directly.
|
||||
class StaticConfig : public virtual ConfigBase
|
||||
{
|
||||
public:
|
||||
public:
|
||||
StaticConfig() : ConfigBase() {};
|
||||
// Gets list of config option names for each config option of this->def, which has a static counter-part defined by the derived object
|
||||
// and which could be resolved by this->optptr(key) call.
|
||||
|
|
Loading…
Reference in a new issue