Ported Config::setenv() to XS

This commit is contained in:
Alessandro Ranellucci 2015-07-01 18:18:25 +02:00
parent f361d8ad43
commit 249088b4f8
4 changed files with 49 additions and 36 deletions

View File

@ -192,14 +192,6 @@ sub save {
__PACKAGE__->write_ini($file, $self->as_ini);
}
sub setenv {
my $self = shift;
foreach my $opt_key (@{$self->get_keys}) {
$ENV{"SLIC3R_" . uc $opt_key} = $self->serialize($opt_key);
}
}
# this method is idempotent by design and only applies to ::DynamicConfig or ::Full
# objects because it performs cross checks
sub validate {

View File

@ -1,4 +1,5 @@
#include "Config.hpp"
#include <stdlib.h> // for setenv()
namespace Slic3r {
@ -10,8 +11,7 @@ ConfigBase::has(const t_config_option_key opt_key) {
void
ConfigBase::apply(const ConfigBase &other, bool ignore_nonexistent) {
// get list of option keys to apply
t_config_option_keys opt_keys;
other.keys(&opt_keys);
t_config_option_keys opt_keys = other.keys();
// loop through options and apply them
for (t_config_option_keys::const_iterator it = opt_keys.begin(); it != opt_keys.end(); ++it) {
@ -37,8 +37,7 @@ t_config_option_keys
ConfigBase::diff(ConfigBase &other) {
t_config_option_keys diff;
t_config_option_keys my_keys;
this->keys(&my_keys);
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);
@ -98,14 +97,31 @@ ConfigBase::get_abs_value(const t_config_option_key opt_key, double ratio_over)
return opt->get_abs_value(ratio_over);
}
void
ConfigBase::setenv_()
{
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);
}
}
#ifdef SLIC3RXS
SV*
ConfigBase::as_hash() {
HV* hv = newHV();
t_config_option_keys opt_keys;
this->keys(&opt_keys);
t_config_option_keys opt_keys = this->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 );
@ -368,10 +384,12 @@ DynamicConfig::option(const t_config_option_key opt_key) const {
return const_cast<DynamicConfig*>(this)->option(opt_key, false);
}
void
DynamicConfig::keys(t_config_option_keys *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.push_back(it->first);
return keys;
}
void
@ -379,12 +397,14 @@ DynamicConfig::erase(const t_config_option_key opt_key) {
this->options.erase(opt_key);
}
void
StaticConfig::keys(t_config_option_keys *keys) const {
t_config_option_keys
StaticConfig::keys() const {
t_config_option_keys keys;
for (t_optiondef_map::const_iterator it = this->def->begin(); it != this->def->end(); ++it) {
const ConfigOption* opt = this->option(it->first);
if (opt != NULL) keys->push_back(it->first);
if (opt != NULL) keys.push_back(it->first);
}
return keys;
}
const ConfigOption*

View File

@ -515,7 +515,7 @@ class ConfigBase
bool has(const t_config_option_key opt_key);
virtual ConfigOption* option(const t_config_option_key opt_key, bool create = false) = 0;
virtual const ConfigOption* option(const t_config_option_key opt_key) const = 0;
virtual void keys(t_config_option_keys *keys) const = 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);
@ -524,6 +524,7 @@ class ConfigBase
void set_ifndef(t_config_option_key opt_key, SV* value, bool deserialize = false);
double get_abs_value(const t_config_option_key opt_key);
double get_abs_value(const t_config_option_key opt_key, double ratio_over);
void setenv_();
#ifdef SLIC3RXS
SV* as_hash();
@ -545,7 +546,7 @@ class DynamicConfig : public ConfigBase
template<class T> T* opt(const t_config_option_key opt_key, bool create = false);
ConfigOption* option(const t_config_option_key opt_key, bool create = false);
const ConfigOption* option(const t_config_option_key opt_key) const;
void keys(t_config_option_keys *keys) const;
t_config_option_keys keys() const;
void erase(const t_config_option_key opt_key);
private:
@ -556,7 +557,7 @@ class DynamicConfig : public ConfigBase
class StaticConfig : public ConfigBase
{
public:
void keys(t_config_option_keys *keys) const;
t_config_option_keys keys() const;
virtual ConfigOption* option(const t_config_option_key opt_key, bool create = false) = 0;
const ConfigOption* option(const t_config_option_key opt_key) const;

View File

@ -27,10 +27,10 @@
%code{% RETVAL = THIS->equals(*other); %};
void apply_static(FullPrintConfig* other)
%code{% THIS->apply(*other, true); %};
std::vector<std::string> get_keys()
%code{% THIS->keys(&RETVAL); %};
%name{get_keys} std::vector<std::string> keys();
void erase(t_config_option_key opt_key);
void normalize();
%name{setenv} void setenv_();
};
%name{Slic3r::Config::GCode} class GCodeConfig {
@ -51,9 +51,9 @@
%code{% THIS->apply(*other, true); %};
void apply_dynamic(DynamicPrintConfig* other)
%code{% THIS->apply(*other, true); %};
std::vector<std::string> get_keys()
%code{% THIS->keys(&RETVAL); %};
%name{get_keys} std::vector<std::string> keys();
std::string get_extrusion_axis();
%name{setenv} void setenv_();
};
%name{Slic3r::Config::Print} class PrintConfig {
@ -72,9 +72,9 @@
double get_abs_value(t_config_option_key opt_key, double ratio_over);
void apply_dynamic(DynamicPrintConfig* other)
%code{% THIS->apply(*other, true); %};
std::vector<std::string> get_keys()
%code{% THIS->keys(&RETVAL); %};
%name{get_keys} std::vector<std::string> keys();
std::string get_extrusion_axis();
%name{setenv} void setenv_();
};
%name{Slic3r::Config::PrintRegion} class PrintRegionConfig {
@ -95,8 +95,8 @@
%code{% THIS->apply(*other, true); %};
void apply_dynamic(DynamicPrintConfig* other)
%code{% THIS->apply(*other, true); %};
std::vector<std::string> get_keys()
%code{% THIS->keys(&RETVAL); %};
%name{get_keys} std::vector<std::string> keys();
%name{setenv} void setenv_();
};
%name{Slic3r::Config::PrintObject} class PrintObjectConfig {
@ -117,8 +117,8 @@
%code{% THIS->apply(*other, true); %};
void apply_dynamic(DynamicPrintConfig* other)
%code{% THIS->apply(*other, true); %};
std::vector<std::string> get_keys()
%code{% THIS->keys(&RETVAL); %};
%name{get_keys} std::vector<std::string> keys();
%name{setenv} void setenv_();
};
%name{Slic3r::Config::Full} class FullPrintConfig {
@ -143,9 +143,9 @@
%code{% THIS->apply(*other, true); %};
void apply_dynamic(DynamicPrintConfig* other)
%code{% THIS->apply(*other, true); %};
std::vector<std::string> get_keys()
%code{% THIS->keys(&RETVAL); %};
%name{get_keys} std::vector<std::string> keys();
std::string get_extrusion_axis();
%name{setenv} void setenv_();
};
%package{Slic3r::Config};