Support bool options in XS

This commit is contained in:
Alessandro Ranellucci 2013-12-20 21:32:18 +01:00
parent 64e549a46f
commit f274287b0e
3 changed files with 81 additions and 14 deletions

View File

@ -75,6 +75,8 @@ ConfigBase::get(t_config_option_key opt_key) {
return newSVpvn(optv->value.c_str(), optv->value.length());
} else if (ConfigOptionPoint* optv = dynamic_cast<ConfigOptionPoint*>(opt)) {
return optv->point.to_SV_pureperl();
} else if (ConfigOptionBool* optv = dynamic_cast<ConfigOptionBool*>(opt)) {
return newSViv(optv->value ? 1 : 0);
} else {
std::string serialized = opt->serialize();
return newSVpvn(serialized.c_str(), serialized.length());
@ -94,6 +96,8 @@ ConfigBase::set(t_config_option_key opt_key, SV* value) {
optv->value = std::string(SvPV_nolen(value), SvCUR(value));
} else if (ConfigOptionPoint* optv = dynamic_cast<ConfigOptionPoint*>(opt)) {
optv->point.from_SV(value);
} else if (ConfigOptionBool* optv = dynamic_cast<ConfigOptionBool*>(opt)) {
optv->value = SvTRUE(value);
} else {
opt->deserialize( std::string(SvPV_nolen(value)) );
}
@ -123,6 +127,8 @@ DynamicConfig::option(const t_config_option_key opt_key, bool create) {
opt = new ConfigOptionFloatOrPercent ();
} else if (Options[opt_key].type == coPoint) {
opt = new ConfigOptionPoint ();
} else if (Options[opt_key].type == coBool) {
opt = new ConfigOptionBool ();
} else {
throw "Unknown option type";
}

View File

@ -126,6 +126,8 @@ class ConfigOptionPoint : public ConfigOption
Pointf point;
ConfigOptionPoint() : point(Pointf(0,0)) {};
operator Pointf() const { return this->point; };
std::string serialize() {
std::ostringstream ss;
ss << this->point.x;
@ -139,12 +141,62 @@ class ConfigOptionPoint : public ConfigOption
};
};
class ConfigOptionBool : public ConfigOption
{
public:
bool value;
ConfigOptionBool() : value(false) {};
operator bool() const { return this->value; };
std::string serialize() {
return std::string(this->value ? "1" : "0");
};
void deserialize(std::string str) {
this->value = (str.compare("1") == 0);
};
};
enum GCodeFlavor {
gcfRepRap, gcfTeacup, gcfMakerWare, gcfSailfish, gcfMach3, gcfNoExtrusion,
};
class ConfigOptionGCodeFlavor : public ConfigOption
{
public:
GCodeFlavor value;
ConfigOptionGCodeFlavor() : value(gcfRepRap) {};
operator GCodeFlavor() const { return this->value; };
std::string serialize() {
if (this->value == gfcRepRap) { return std::string("reprap"); }
else if (this->value == gcfTeacup) { return std::string("teacup"); }
else if (this->value == gcfMakerWare) { return std::string("makerware"); }
else if (this->value == gcfSailfish) { return std::string("sailfish"); }
else if (this->value == gcfMach3) { return std::string("mach3"); }
else if (this->value == gcfNoExtrusion) { return std::string("no-extrusion"); }
};
void deserialize(std::string str) {
if (str.compare("reprap") == 0) { this->value = gfcRepRap; }
else if (str.compare("teacup") == 0) { this->value = gcfTeacup; }
else if (str.compare("makerware") == 0) { this->value = gcfMakerWare; }
else if (str.compare("sailfish") == 0) { this->value = gcfSailfish; }
else if (str.compare("mach3") == 0) { this->value = gcfMach3; }
else if (str.compare("no-extrusion") == 0) { this->value = gcfNoExtrusion; }
};
};
enum ConfigOptionType {
coFloat,
coInt,
coString,
coFloatOrPercent,
coPoint,
coBool,
coGCodeFlavor,
};
class ConfigOptionDef
@ -201,12 +253,13 @@ class StaticConfig : public ConfigBase
class FullConfig : public StaticConfig
{
public:
ConfigOptionFloat layer_height;
ConfigOptionFloatOrPercent first_layer_height;
ConfigOptionInt perimeters;
ConfigOptionString extrusion_axis;
ConfigOptionPoint print_center;
ConfigOptionString notes;
ConfigOptionFloat layer_height;
ConfigOptionFloatOrPercent first_layer_height;
ConfigOptionInt perimeters;
ConfigOptionString extrusion_axis;
ConfigOptionPoint print_center;
ConfigOptionString notes;
ConfigOptionBool use_relative_e_distances;
ConfigOption* option(const t_config_option_key opt_key, bool create = false) {
assert(!create); // can't create options in StaticConfig
@ -216,6 +269,7 @@ class FullConfig : public StaticConfig
if (opt_key == "extrusion_axis") return &this->extrusion_axis;
if (opt_key == "print_center") return &this->print_center;
if (opt_key == "notes") return &this->notes;
if (opt_key == "use_relative_e_distances") return &this->use_relative_e_distances;
return NULL;
};
};
@ -239,19 +293,22 @@ static t_optiondef_map _build_optiondef_map () {
Options["notes"].type = coString;
Options["use_relative_e_distances"].type = coBool;
return Options;
}
static FullConfig _build_default_config () {
FullConfig defconf;
defconf.layer_height.value = 0.4;
defconf.first_layer_height.value = 0.35;
defconf.first_layer_height.percent = false;
defconf.perimeters.value = 3;
defconf.extrusion_axis.value = "E";
defconf.print_center.point = Pointf(100,100);
defconf.notes.value = "";
defconf.layer_height.value = 0.4;
defconf.first_layer_height.value = 0.35;
defconf.first_layer_height.percent = false;
defconf.perimeters.value = 3;
defconf.extrusion_axis.value = "E";
defconf.print_center.point = Pointf(100,100);
defconf.notes.value = "";
defconf.use_relative_e_distances.value = false;
return defconf;
}

View File

@ -4,7 +4,7 @@ use strict;
use warnings;
use Slic3r::XS;
use Test::More tests => 16;
use Test::More tests => 18;
{
my $config = Slic3r::Config->new;
@ -40,6 +40,10 @@ use Test::More tests => 16;
is $config->serialize('print_center'), '50,80', 'serialize point';
$config->set_deserialize('print_center', '20,10');
is_deeply $config->get('print_center'), [20,10], 'deserialize point';
$config->set('use_relative_e_distances', 1);
is $config->get('use_relative_e_distances'), 1, 'set/get bool';
is $config->serialize('use_relative_e_distances'), '1', 'serialize bool';
}
__END__