Config.cpp/h - inlined short functions, converted loops to C++11,

fixed some constness issues.
This commit is contained in:
bubnikv 2017-05-30 17:04:36 +02:00
parent 2178180a19
commit e32632b9d9
2 changed files with 71 additions and 159 deletions

View file

@ -162,48 +162,8 @@ bool unescape_strings_cstyle(const std::string &str, std::vector<std::string> &o
} }
} }
bool void ConfigBase::apply(const ConfigBase &other, bool ignore_nonexistent)
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);
}
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 // get list of option keys to apply
t_config_option_keys opt_keys = other.keys(); 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 // this will *ignore* options not present in both configs
t_config_option_keys t_config_option_keys ConfigBase::diff(const ConfigBase &other) const
ConfigBase::diff(ConfigBase &other) { {
t_config_option_keys diff; t_config_option_keys diff;
for (const t_config_option_key &opt_key : this->keys())
t_config_option_keys my_keys = this->keys(); if (other.has(opt_key) && other.serialize(opt_key) != this->serialize(opt_key))
for (t_config_option_keys::const_iterator opt_key = my_keys.begin(); opt_key != my_keys.end(); ++opt_key) { diff.push_back(opt_key);
if (other.has(*opt_key) && other.serialize(*opt_key) != this->serialize(*opt_key)) {
diff.push_back(*opt_key);
}
}
return diff; return diff;
} }
std::string std::string ConfigBase::serialize(const t_config_option_key &opt_key) const
ConfigBase::serialize(const t_config_option_key &opt_key) const { {
const ConfigOption* opt = this->option(opt_key); const ConfigOption* opt = this->option(opt_key);
assert(opt != NULL); assert(opt != nullptr);
return opt->serialize(); return opt->serialize();
} }
bool bool ConfigBase::set_deserialize(const t_config_option_key &opt_key, std::string str)
ConfigBase::set_deserialize(const t_config_option_key &opt_key, std::string str) { {
const ConfigOptionDef* optdef = this->def->get(opt_key); const ConfigOptionDef* optdef = this->def->get(opt_key);
if (optdef == NULL) throw "Calling set_deserialize() on unknown option"; if (optdef == NULL) throw "Calling set_deserialize() on unknown option";
if (!optdef->shortcut.empty()) { if (!optdef->shortcut.empty()) {
@ -261,22 +211,20 @@ ConfigBase::set_deserialize(const t_config_option_key &opt_key, std::string str)
} }
return true; return true;
} }
ConfigOption* opt = this->option(opt_key, true); ConfigOption* opt = this->option(opt_key, true);
assert(opt != NULL); assert(opt != nullptr);
return opt->deserialize(str); return opt->deserialize(str);
} }
// Return an absolute value of a possibly relative config variable. // 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. // For example, return absolute infill extrusion width, either from an absolute value, or relative to the layer height.
double double ConfigBase::get_abs_value(const t_config_option_key &opt_key) const
ConfigBase::get_abs_value(const t_config_option_key &opt_key) const { {
const ConfigOption* opt = this->option(opt_key); const ConfigOption* opt = this->option(opt_key);
if (const ConfigOptionFloatOrPercent* optv = dynamic_cast<const ConfigOptionFloatOrPercent*>(opt)) { if (const ConfigOptionFloatOrPercent* optv = dynamic_cast<const ConfigOptionFloatOrPercent*>(opt)) {
// get option definition // get option definition
const ConfigOptionDef* def = this->def->get(opt_key); 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 // compute absolute value over the absolute value of the base option
return optv->get_abs_value(this->get_abs_value(def->ratio_over)); return optv->get_abs_value(this->get_abs_value(def->ratio_over));
} else if (const ConfigOptionFloat* optv = dynamic_cast<const ConfigOptionFloat*>(opt)) { } 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. // 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. // For example, return absolute infill extrusion width, either from an absolute value, or relative to a provided value.
double double ConfigBase::get_abs_value(const t_config_option_key &opt_key, double ratio_over) const
ConfigBase::get_abs_value(const t_config_option_key &opt_key, double ratio_over) const { {
// get stored option value // get stored option value
const ConfigOptionFloatOrPercent* opt = dynamic_cast<const ConfigOptionFloatOrPercent*>(this->option(opt_key)); const ConfigOptionFloatOrPercent* opt = dynamic_cast<const ConfigOptionFloatOrPercent*>(this->option(opt_key));
assert(opt != NULL); assert(opt != nullptr);
// compute absolute value // compute absolute value
return opt->get_abs_value(ratio_over); return opt->get_abs_value(ratio_over);
} }
void void ConfigBase::setenv_()
ConfigBase::setenv_()
{ {
#ifdef setenv #ifdef setenv
t_config_option_keys opt_keys = this->keys(); t_config_option_keys opt_keys = this->keys();
@ -319,43 +265,7 @@ ConfigBase::setenv_()
#endif #endif
} }
const ConfigOption* ConfigOption* DynamicConfig::optptr(const t_config_option_key &opt_key, bool create) {
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) {
t_options_map::iterator it = options.find(opt_key); t_options_map::iterator it = options.find(opt_key);
if (it == options.end()) { if (it == options.end()) {
if (create) { if (create) {
@ -405,8 +315,7 @@ DynamicConfig::optptr(const t_config_option_key &opt_key, bool create) {
} }
template<class T> template<class T>
T* T* DynamicConfig::opt(const t_config_option_key &opt_key, bool create) {
DynamicConfig::opt(const t_config_option_key &opt_key, bool create) {
return dynamic_cast<T*>(this->option(opt_key, create)); return dynamic_cast<T*>(this->option(opt_key, create));
} }
template ConfigOptionInt* DynamicConfig::opt<ConfigOptionInt>(const t_config_option_key &opt_key, bool 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 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); template ConfigOptionPercent* DynamicConfig::opt<ConfigOptionPercent>(const t_config_option_key &opt_key, bool create);
t_config_option_keys t_config_option_keys DynamicConfig::keys() const
DynamicConfig::keys() const { {
t_config_option_keys keys; t_config_option_keys keys;
for (t_options_map::const_iterator it = this->options.begin(); it != this->options.end(); ++it) keys.reserve(this->options.size());
keys.push_back(it->first); for (const auto &opt : this->options)
keys.emplace_back(opt.first);
return keys; return keys;
} }
void void StaticConfig::set_defaults()
DynamicConfig::erase(const t_config_option_key &opt_key) {
this->options.erase(opt_key);
}
void
StaticConfig::set_defaults()
{ {
// use defaults from definition // use defaults from definition
if (this->def == NULL) return; if (this->def != nullptr) {
t_config_option_keys keys = this->keys(); for (const std::string &key : this->keys()) {
for (t_config_option_keys::const_iterator it = keys.begin(); it != keys.end(); ++it) { const ConfigOptionDef* def = this->def->get(key);
const ConfigOptionDef* def = this->def->get(*it); if (def->default_value != nullptr)
if (def->default_value != NULL) this->option(key)->set(*def->default_value);
this->option(*it)->set(*def->default_value); }
} }
} }
t_config_option_keys t_config_option_keys StaticConfig::keys() const
StaticConfig::keys() const { {
t_config_option_keys keys; t_config_option_keys keys;
assert(this->def != NULL); assert(this->def != nullptr);
for (t_optiondef_map::const_iterator it = this->def->options.begin(); it != this->def->options.end(); ++it) { for (const auto &opt_def : this->def->options)
const ConfigOption* opt = this->option(it->first); if (this->option(opt_def.first) != nullptr)
if (opt != NULL) keys.push_back(it->first); keys.push_back(opt_def.first);
}
return keys; return keys;
} }

View file

@ -35,8 +35,8 @@ class ConfigOption {
virtual double getFloat() const { return 0; }; virtual double getFloat() const { return 0; };
virtual bool getBool() const { return false; }; virtual bool getBool() const { return false; };
virtual void setInt(int /* val */) { }; virtual void setInt(int /* val */) { };
friend bool operator== (const ConfigOption &a, const ConfigOption &b); bool operator==(const ConfigOption &rhs) { return this->serialize().compare(rhs.serialize()) == 0; }
friend bool operator!= (const ConfigOption &a, const ConfigOption &b); bool operator!=(const ConfigOption &rhs) { return this->serialize().compare(rhs.serialize()) != 0; }
}; };
// Value of a single valued option (bool, int, float, string, point, enum) // Value of a single valued option (bool, int, float, string, point, enum)
@ -263,7 +263,7 @@ class ConfigOptionPercents : public ConfigOptionFloats
public: public:
std::string serialize() const { std::string serialize() const {
std::ostringstream ss; std::ostringstream ss;
for (const auto &v : this->values) { for (const auto v : this->values) {
if (&v != &this->values.front()) ss << ","; if (&v != &this->values.front()) ss << ",";
ss << v << "%"; 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. // but it carries the defaults of the configuration values.
class ConfigDef class ConfigDef
{ {
public: public:
t_optiondef_map options; t_optiondef_map options;
~ConfigDef(); ~ConfigDef() { for (auto &opt : this->options) delete opt.second.default_value; }
ConfigOptionDef* add(const t_config_option_key &opt_key, ConfigOptionType type); ConfigOptionDef* add(const t_config_option_key &opt_key, ConfigOptionType type) {
const ConfigOptionDef* get(const t_config_option_key &opt_key) const; 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. // An abstract configuration store.
class ConfigBase class ConfigBase
{ {
public: public:
// Definition of configuration values for the purpose of GUI presentation, editing, value mapping and config file handling. // 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, // The configuration definition is static: It does not carry the actual configuration values,
// but it carries the defaults of the configuration values. // but it carries the defaults of the configuration values.
// ConfigBase does not own ConfigDef, it only references it. // ConfigBase does not own ConfigDef, it only references it.
const ConfigDef* def; const ConfigDef* def;
ConfigBase() : def(NULL) {}; ConfigBase(const ConfigDef *def = nullptr) : def(def) {};
virtual ~ConfigBase() {}; virtual ~ConfigBase() {};
bool has(const t_config_option_key &opt_key); 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; const ConfigOption* option(const t_config_option_key &opt_key) const
ConfigOption* option(const t_config_option_key &opt_key, bool create = false); { 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 ConfigOption* optptr(const t_config_option_key &opt_key, bool create = false) = 0;
virtual t_config_option_keys keys() const = 0; virtual t_config_option_keys keys() const = 0;
void apply(const ConfigBase &other, bool ignore_nonexistent = false); void apply(const ConfigBase &other, bool ignore_nonexistent = false);
bool equals(ConfigBase &other); bool equals(const ConfigBase &other) const { return this->diff(other).empty(); }
t_config_option_keys diff(ConfigBase &other); t_config_option_keys diff(const ConfigBase &other) const;
std::string serialize(const t_config_option_key &opt_key) 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); 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. // In Slic3r, the dynamic config is mostly used at the user interface layer.
class DynamicConfig : public virtual ConfigBase class DynamicConfig : public virtual ConfigBase
{ {
public: public:
DynamicConfig() {}; DynamicConfig() {};
DynamicConfig(const DynamicConfig& other); DynamicConfig(const DynamicConfig& other) : ConfigBase(other.def) { this->apply(other, false); }
DynamicConfig& operator= (DynamicConfig other); DynamicConfig& operator= (DynamicConfig other) { this->swap(other); return *this; }
void swap(DynamicConfig &other); virtual ~DynamicConfig() { for (auto &opt : this->options) delete opt.second; }
virtual ~DynamicConfig(); 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); 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); virtual ConfigOption* optptr(const t_config_option_key &opt_key, bool create = false);
t_config_option_keys keys() const; 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; typedef std::map<t_config_option_key,ConfigOption*> t_options_map;
t_options_map options; t_options_map options;
}; };
@ -687,7 +696,7 @@ class DynamicConfig : public virtual ConfigBase
// because the configuration values could be accessed directly. // because the configuration values could be accessed directly.
class StaticConfig : public virtual ConfigBase class StaticConfig : public virtual ConfigBase
{ {
public: public:
StaticConfig() : ConfigBase() {}; 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 // 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. // and which could be resolved by this->optptr(key) call.