perglue.cpp - use static_cast instead of dynamic_cast if possible,

use switch instead of plenty of ifs,
export clone<DynamicPrintConfig> to Perl XS.
This commit is contained in:
bubnikv 2017-10-17 19:19:41 +02:00
parent 746afbd790
commit b9d57483d8
4 changed files with 150 additions and 166 deletions

View file

@ -38,7 +38,7 @@ Fill* Fill::new_from_type(const InfillPattern type)
Fill* Fill::new_from_type(const std::string &type) Fill* Fill::new_from_type(const std::string &type)
{ {
static t_config_enum_values enum_keys_map = ConfigOptionEnum<InfillPattern>::get_enum_values(); const t_config_enum_values &enum_keys_map = ConfigOptionEnum<InfillPattern>::get_enum_values();
t_config_enum_values::const_iterator it = enum_keys_map.find(type); t_config_enum_values::const_iterator it = enum_keys_map.find(type);
return (it == enum_keys_map.end()) ? nullptr : new_from_type(InfillPattern(it->second)); return (it == enum_keys_map.end()) ? nullptr : new_from_type(InfillPattern(it->second));
} }

View file

@ -61,135 +61,132 @@ REGISTER_CLASS(Preset, "GUI::Preset");
REGISTER_CLASS(PresetCollection, "GUI::PresetCollection"); REGISTER_CLASS(PresetCollection, "GUI::PresetCollection");
REGISTER_CLASS(PresetBundle, "GUI::PresetBundle"); REGISTER_CLASS(PresetBundle, "GUI::PresetBundle");
SV* SV* ConfigBase__as_hash(ConfigBase* THIS)
ConfigBase__as_hash(ConfigBase* THIS) { {
HV* hv = newHV(); HV* hv = newHV();
for (auto &key : THIS->keys())
t_config_option_keys opt_keys = THIS->keys(); (void)hv_store(hv, key.c_str(), key.length(), ConfigBase__get(THIS, *it), 0);
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(), ConfigBase__get(THIS, *it), 0 );
return newRV_noinc((SV*)hv); return newRV_noinc((SV*)hv);
} }
SV* SV* ConfigBase__get(ConfigBase* THIS, const t_config_option_key &opt_key)
ConfigBase__get(ConfigBase* THIS, const t_config_option_key &opt_key) { {
ConfigOption* opt = THIS->option(opt_key); ConfigOption* opt = THIS->option(opt_key);
if (opt == NULL) return &PL_sv_undef; return (opt == nullptr) ?
&PL_sv_undef :
const ConfigOptionDef* def = THIS->def()->get(opt_key); ConfigOption_to_SV(*opt, *THIS->def->get(opt_key));
return ConfigOption_to_SV(*opt, *def);
} }
SV* SV* ConfigOption_to_SV(const ConfigOption &opt, const ConfigOptionDef &def)
ConfigOption_to_SV(const ConfigOption &opt, const ConfigOptionDef &def) { {
if (def.type == coFloat) { switch (def.type) {
const ConfigOptionFloat* optv = dynamic_cast<const ConfigOptionFloat*>(&opt); case coFloat:
return newSVnv(optv->value); case coPercent:
} else if (def.type == coFloats) { return newSVnv(static_cast<const ConfigOptionFloat*>(&opt)->value);
const ConfigOptionFloats* optv = dynamic_cast<const ConfigOptionFloats*>(&opt); case coFloats:
AV* av = newAV(); case coPercents:
av_fill(av, optv->values.size()-1); {
for (std::vector<double>::const_iterator it = optv->values.begin(); it != optv->values.end(); ++it) auto optv = static_cast<const ConfigOptionFloats*>(&opt);
av_store(av, it - optv->values.begin(), newSVnv(*it));
return newRV_noinc((SV*)av);
} else if (def.type == coPercent) {
const ConfigOptionPercent* optv = dynamic_cast<const ConfigOptionPercent*>(&opt);
return newSVnv(optv->value);
} else if (def.type == coPercents) {
const ConfigOptionPercents* optv = dynamic_cast<const ConfigOptionPercents*>(&opt);
AV* av = newAV(); AV* av = newAV();
av_fill(av, optv->values.size()-1); av_fill(av, optv->values.size()-1);
for (const double &v : optv->values) for (const double &v : optv->values)
av_store(av, &v - &optv->values.front(), newSVnv(v)); av_store(av, &v - optv->values.data(), newSVnv(v));
return newRV_noinc((SV*)av); return newRV_noinc((SV*)av);
} else if (def.type == coInt) { }
const ConfigOptionInt* optv = dynamic_cast<const ConfigOptionInt*>(&opt); case coInt:
return newSViv(optv->value); return newSViv(static_cast<const ConfigOptionInt*>(&opt)->value);
} else if (def.type == coInts) { case coInts:
const ConfigOptionInts* optv = dynamic_cast<const ConfigOptionInts*>(&opt); {
auto optv = static_cast<const ConfigOptionInts*>(&opt);
AV* av = newAV(); AV* av = newAV();
av_fill(av, optv->values.size()-1); av_fill(av, optv->values.size()-1);
for (std::vector<int>::const_iterator it = optv->values.begin(); it != optv->values.end(); ++it) for (const int &v : optv->values)
av_store(av, it - optv->values.begin(), newSViv(*it)); av_store(av, &v - optv->values.data(), newSViv(v));
return newRV_noinc((SV*)av); return newRV_noinc((SV*)av);
} else if (def.type == coString) { }
const ConfigOptionString* optv = dynamic_cast<const ConfigOptionString*>(&opt); case coString:
{
auto optv = static_cast<const ConfigOptionString*>(&opt);
// we don't serialize() because that would escape newlines // we don't serialize() because that would escape newlines
return newSVpvn_utf8(optv->value.c_str(), optv->value.length(), true); return newSVpvn_utf8(optv->value.c_str(), optv->value.length(), true);
} else if (def.type == coStrings) { }
const ConfigOptionStrings* optv = dynamic_cast<const ConfigOptionStrings*>(&opt); case coStrings:
{
auto optv = static_cast<const ConfigOptionStrings*>(&opt);
AV* av = newAV(); AV* av = newAV();
av_fill(av, optv->values.size()-1); av_fill(av, optv->values.size()-1);
for (std::vector<std::string>::const_iterator it = optv->values.begin(); it != optv->values.end(); ++it) for (const std::string &v : optv->values)
av_store(av, it - optv->values.begin(), newSVpvn_utf8(it->c_str(), it->length(), true)); av_store(av, &v - optv->values.data(), newSVpvn_utf8(v.c_str(), v.length(), true));
return newRV_noinc((SV*)av); return newRV_noinc((SV*)av);
} else if (def.type == coPoint) { }
const ConfigOptionPoint* optv = dynamic_cast<const ConfigOptionPoint*>(&opt); case coPoint:
return perl_to_SV_clone_ref(optv->value); return perl_to_SV_clone_ref(static_cast<const ConfigOptionPoint*>(&opt)->value);
} else if (def.type == coPoints) { case coPoints:
const ConfigOptionPoints* optv = dynamic_cast<const ConfigOptionPoints*>(&opt); {
auto optv = static_cast<const ConfigOptionPoints*>(&opt);
AV* av = newAV(); AV* av = newAV();
av_fill(av, optv->values.size()-1); av_fill(av, optv->values.size()-1);
for (Pointfs::const_iterator it = optv->values.begin(); it != optv->values.end(); ++it) for (const Pointf &v : optv->values)
av_store(av, it - optv->values.begin(), perl_to_SV_clone_ref(*it)); av_store(av, &v - optv->values.data(), perl_to_SV_clone_ref(v));
return newRV_noinc((SV*)av); return newRV_noinc((SV*)av);
} else if (def.type == coBool) { }
const ConfigOptionBool* optv = dynamic_cast<const ConfigOptionBool*>(&opt); case coBool:
return newSViv(optv->value ? 1 : 0); return newSViv(static_cast<const ConfigOptionBool*>(&opt)->value ? 1 : 0);
} else if (def.type == coBools) { case coBools:
const ConfigOptionBools* optv = dynamic_cast<const ConfigOptionBools*>(&opt); {
auto optv = static_cast<const ConfigOptionBools*>(&opt);
AV* av = newAV(); AV* av = newAV();
av_fill(av, optv->values.size()-1); av_fill(av, optv->values.size()-1);
for (size_t i = 0; i < optv->values.size(); ++ i) for (size_t i = 0; i < optv->values.size(); ++ i)
av_store(av, i, newSViv(optv->values[i] ? 1 : 0)); av_store(av, i, newSViv(optv->values[i] ? 1 : 0));
return newRV_noinc((SV*)av); return newRV_noinc((SV*)av);
} else { }
default:
std::string serialized = opt.serialize(); std::string serialized = opt.serialize();
return newSVpvn_utf8(serialized.c_str(), serialized.length(), true); return newSVpvn_utf8(serialized.c_str(), serialized.length(), true);
} }
} }
SV* SV* ConfigBase__get_at(ConfigBase* THIS, const t_config_option_key &opt_key, size_t i)
ConfigBase__get_at(ConfigBase* THIS, const t_config_option_key &opt_key, size_t i) { {
ConfigOption* opt = THIS->option(opt_key); ConfigOption* opt = THIS->option(opt_key);
if (opt == NULL) return &PL_sv_undef; if (opt == NULL) return &PL_sv_undef;
const ConfigOptionDef* def = THIS->def()->get(opt_key); const ConfigOptionDef* def = THIS->def->get(opt_key);
if (def->type == coFloats || def->type == coPercents) { switch (def->type) {
ConfigOptionFloats* optv = dynamic_cast<ConfigOptionFloats*>(opt); case coFloats:
return newSVnv(optv->get_at(i)); case coPercents:
} else if (def->type == coInts) { return newSVnv(static_cast<ConfigOptionFloats*>(opt)->get_at(i));
ConfigOptionInts* optv = dynamic_cast<ConfigOptionInts*>(opt); case coInts:
return newSViv(optv->get_at(i)); return newSViv(static_cast<ConfigOptionInts*>(opt)->get_at(i));
} else if (def->type == coStrings) { case coStrings:
ConfigOptionStrings* optv = dynamic_cast<ConfigOptionStrings*>(opt); {
// we don't serialize() because that would escape newlines // we don't serialize() because that would escape newlines
std::string val = optv->get_at(i); const std::string &val = static_cast<ConfigOptionStrings*>(opt)->get_at(i);
return newSVpvn_utf8(val.c_str(), val.length(), true); return newSVpvn_utf8(val.c_str(), val.length(), true);
} else if (def->type == coPoints) { }
ConfigOptionPoints* optv = dynamic_cast<ConfigOptionPoints*>(opt); case coPoints:
return perl_to_SV_clone_ref(optv->get_at(i)); return perl_to_SV_clone_ref(static_cast<ConfigOptionPoints*>(opt)->get_at(i));
} else if (def->type == coBools) { case coBools:
ConfigOptionBools* optv = dynamic_cast<ConfigOptionBools*>(opt); return newSViv(static_cast<ConfigOptionBools*>(opt)->get_at(i) ? 1 : 0);
return newSViv(optv->get_at(i) ? 1 : 0); default:
} else {
return &PL_sv_undef; return &PL_sv_undef;
} }
} }
bool bool ConfigBase__set(ConfigBase* THIS, const t_config_option_key &opt_key, SV* value)
ConfigBase__set(ConfigBase* THIS, const t_config_option_key &opt_key, SV* value) { {
ConfigOption* opt = THIS->option(opt_key, true); ConfigOption* opt = THIS->option(opt_key, true);
if (opt == NULL) CONFESS("Trying to set non-existing option"); if (opt == NULL) CONFESS("Trying to set non-existing option");
const ConfigOptionDef* def = THIS->def()->get(opt_key); const ConfigOptionDef* def = THIS->def->get(opt_key);
if (def->type == coFloat) { case coFloat:
if (!looks_like_number(value)) return false; if (!looks_like_number(value))
ConfigOptionFloat* optv = dynamic_cast<ConfigOptionFloat*>(opt); return false;
optv->value = SvNV(value); static_cast<ConfigOptionFloat*>(opt)->value = SvNV(value);
} else if (def->type == coFloats) { break;
ConfigOptionFloats* optv = dynamic_cast<ConfigOptionFloats*>(opt); case coFloats:
{
std::vector<double> values; std::vector<double> values;
AV* av = (AV*)SvRV(value); AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1; const size_t len = av_len(av)+1;
@ -198,9 +195,11 @@ ConfigBase__set(ConfigBase* THIS, const t_config_option_key &opt_key, SV* value)
if (elem == NULL || !looks_like_number(*elem)) return false; if (elem == NULL || !looks_like_number(*elem)) return false;
values.push_back(SvNV(*elem)); values.push_back(SvNV(*elem));
} }
optv->values = values; static_cast<ConfigOptionFloats*>(opt)->values = std::move(values);
} else if (def->type == coPercents) { break;
ConfigOptionPercents* optv = dynamic_cast<ConfigOptionPercents*>(opt); }
case coPercents:
{
std::vector<double> values; std::vector<double> values;
AV* av = (AV*)SvRV(value); AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1; const size_t len = av_len(av)+1;
@ -209,13 +208,15 @@ ConfigBase__set(ConfigBase* THIS, const t_config_option_key &opt_key, SV* value)
if (elem == NULL || !looks_like_number(*elem)) return false; if (elem == NULL || !looks_like_number(*elem)) return false;
values.push_back(SvNV(*elem)); values.push_back(SvNV(*elem));
} }
optv->values = values; static_cast<ConfigOptionPercents*>(opt)->values = std::move(values);
} else if (def->type == coInt) { break;
}
case coInt:
if (!looks_like_number(value)) return false; if (!looks_like_number(value)) return false;
ConfigOptionInt* optv = dynamic_cast<ConfigOptionInt*>(opt); static_cast<ConfigOptionInt*>(opt)->value = SvIV(value);
optv->value = SvIV(value); break;
} else if (def->type == coInts) { case coInts:
ConfigOptionInts* optv = dynamic_cast<ConfigOptionInts*>(opt); {
std::vector<int> values; std::vector<int> values;
AV* av = (AV*)SvRV(value); AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1; const size_t len = av_len(av)+1;
@ -224,25 +225,30 @@ ConfigBase__set(ConfigBase* THIS, const t_config_option_key &opt_key, SV* value)
if (elem == NULL || !looks_like_number(*elem)) return false; if (elem == NULL || !looks_like_number(*elem)) return false;
values.push_back(SvIV(*elem)); values.push_back(SvIV(*elem));
} }
optv->values = values; static_cast<ConfigOptionInts*>(opt)->values = std::move(values);
} else if (def->type == coString) { break;
ConfigOptionString* optv = dynamic_cast<ConfigOptionString*>(opt); }
optv->value = std::string(SvPV_nolen(value), SvCUR(value)); case coString:
} else if (def->type == coStrings) { static_cast<ConfigOptionString*>(opt)->value = std::string(SvPV_nolen(value), SvCUR(value));
ConfigOptionStrings* optv = dynamic_cast<ConfigOptionStrings*>(opt); break;
case coStrings:
{
auto optv = static_cast<ConfigOptionStrings*>(opt);
optv->values.clear(); optv->values.clear();
AV* av = (AV*)SvRV(value); AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1; const size_t len = av_len(av)+1;
optv->values.reserve(len);
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0); SV** elem = av_fetch(av, i, 0);
if (elem == NULL) return false; if (elem == NULL) return false;
optv->values.push_back(std::string(SvPV_nolen(*elem), SvCUR(*elem))); optv->values.emplace_back(std::string(SvPV_nolen(*elem), SvCUR(*elem)));
} }
} else if (def->type == coPoint) { break;
ConfigOptionPoint* optv = dynamic_cast<ConfigOptionPoint*>(opt); }
return from_SV_check(value, &optv->value); case coPoint:
} else if (def->type == coPoints) { return from_SV_check(value, &static_cast<ConfigOptionPoint*>(opt)->value);
ConfigOptionPoints* optv = dynamic_cast<ConfigOptionPoints*>(opt); case coPoints:
{
std::vector<Pointf> values; std::vector<Pointf> values;
AV* av = (AV*)SvRV(value); AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1; const size_t len = av_len(av)+1;
@ -252,60 +258,62 @@ ConfigBase__set(ConfigBase* THIS, const t_config_option_key &opt_key, SV* value)
if (elem == NULL || !from_SV_check(*elem, &point)) return false; if (elem == NULL || !from_SV_check(*elem, &point)) return false;
values.push_back(point); values.push_back(point);
} }
optv->values = values; static_cast<ConfigOptionPoints*>(opt)->values = std::move(values);
} else if (def->type == coBool) { break;
ConfigOptionBool* optv = dynamic_cast<ConfigOptionBool*>(opt); }
optv->value = SvTRUE(value); case coBool:
} else if (def->type == coBools) { static_cast<ConfigOptionBool*>(opt)->value = SvTRUE(value);
ConfigOptionBools* optv = dynamic_cast<ConfigOptionBools*>(opt); break;
case coBools:
{
auto optv = static_cast<ConfigOptionBools*>(opt);
optv->values.clear(); optv->values.clear();
AV* av = (AV*)SvRV(value); AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1; const size_t len = av_len(av)+1;
optv->values.reserve(len);
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0); SV** elem = av_fetch(av, i, 0);
if (elem == NULL) return false; if (elem == NULL) return false;
optv->values.push_back(SvTRUE(*elem)); optv->values.emplace_back(SvTRUE(*elem));
} }
} else { break;
if (!opt->deserialize( std::string(SvPV_nolen(value)) )) return false; }
default:
if (! opt->deserialize(std::string(SvPV_nolen(value))))
return false;
} }
return true; return true;
} }
/* This method is implemented as a workaround for this typemap bug: /* This method is implemented as a workaround for this typemap bug:
https://rt.cpan.org/Public/Bug/Display.html?id=94110 */ https://rt.cpan.org/Public/Bug/Display.html?id=94110 */
bool bool ConfigBase__set_deserialize(ConfigBase* THIS, const t_config_option_key &opt_key, SV* str)
ConfigBase__set_deserialize(ConfigBase* THIS, const t_config_option_key &opt_key, SV* str) { {
size_t len; size_t len;
const char * c = SvPV(str, len); const char * c = SvPV(str, len);
std::string value(c, len); std::string value(c, len);
return THIS->set_deserialize(opt_key, value); return THIS->set_deserialize(opt_key, value);
} }
void void ConfigBase__set_ifndef(ConfigBase* THIS, const t_config_option_key &opt_key, SV* value, bool deserialize)
ConfigBase__set_ifndef(ConfigBase* THIS, const t_config_option_key &opt_key, SV* value, bool deserialize)
{ {
if (!THIS->has(opt_key)) { if (THIS->has(opt_key))
if (deserialize) { return;
ConfigBase__set_deserialize(THIS, opt_key, value); if (deserialize)
} else { ConfigBase__set_deserialize(THIS, opt_key, value);
ConfigBase__set(THIS, opt_key, value); else
} ConfigBase__set(THIS, opt_key, value);
}
} }
bool bool StaticConfig__set(StaticConfig* THIS, const t_config_option_key &opt_key, SV* value)
StaticConfig__set(StaticConfig* THIS, const t_config_option_key &opt_key, SV* value) { {
const ConfigOptionDef* optdef = THIS->def()->get(opt_key); const ConfigOptionDef* optdef = THIS->def->get(opt_key);
if (!optdef->shortcut.empty()) { if (optdef->shortcut.empty())
for (std::vector<t_config_option_key>::const_iterator it = optdef->shortcut.begin(); it != optdef->shortcut.end(); ++it) { return ConfigBase__set(THIS, opt_key, value);
if (!StaticConfig__set(THIS, *it, value)) return false; for (const t_config_option_key &key : optdef->shortcut)
} if (! StaticConfig__set(THIS, key, value))
return true; return false;
} return true;
return ConfigBase__set(THIS, opt_key, value);
} }
SV* to_AV(ExPolygon* expolygon) SV* to_AV(ExPolygon* expolygon)
@ -535,31 +543,5 @@ SV* to_SV(TriangleMesh* THIS)
return sv; return sv;
} }
SV*
polynode_children_2_perl(const ClipperLib::PolyNode& node)
{
AV* av = newAV();
const int len = node.ChildCount();
if (len > 0) av_extend(av, len-1);
for (int i = 0; i < len; ++i) {
av_store(av, i, polynode2perl(*node.Childs[i]));
}
return (SV*)newRV_noinc((SV*)av);
}
SV*
polynode2perl(const ClipperLib::PolyNode& node)
{
HV* hv = newHV();
Slic3r::Polygon p = ClipperPath_to_Slic3rPolygon(node.Contour);
if (node.IsHole()) {
(void)hv_stores( hv, "hole", Slic3r::perl_to_SV_clone_ref(p) );
} else {
(void)hv_stores( hv, "outer", Slic3r::perl_to_SV_clone_ref(p) );
}
(void)hv_stores( hv, "children", polynode_children_2_perl(node) );
return (SV*)newRV_noinc((SV*)hv);
}
} }
#endif #endif

View file

@ -31,6 +31,7 @@ Clone<BoundingBoxf3> O_OBJECT_SLIC3R_T
DynamicPrintConfig* O_OBJECT_SLIC3R DynamicPrintConfig* O_OBJECT_SLIC3R
Ref<DynamicPrintConfig> O_OBJECT_SLIC3R_T Ref<DynamicPrintConfig> O_OBJECT_SLIC3R_T
Clone<DynamicPrintConfig> O_OBJECT_SLIC3R_T
StaticPrintConfig* O_OBJECT_SLIC3R StaticPrintConfig* O_OBJECT_SLIC3R
Ref<StaticPrintConfig> O_OBJECT_SLIC3R_T Ref<StaticPrintConfig> O_OBJECT_SLIC3R_T

View file

@ -39,6 +39,7 @@
%typemap{Clone<BoundingBoxf3>}{simple}; %typemap{Clone<BoundingBoxf3>}{simple};
%typemap{DynamicPrintConfig*}; %typemap{DynamicPrintConfig*};
%typemap{Ref<DynamicPrintConfig>}{simple}; %typemap{Ref<DynamicPrintConfig>}{simple};
%typemap{Clone<DynamicPrintConfig>}{simple};
%typemap{StaticPrintConfig*}; %typemap{StaticPrintConfig*};
%typemap{Ref<StaticPrintConfig>}{simple}; %typemap{Ref<StaticPrintConfig>}{simple};
%typemap{PrintObjectConfig*}; %typemap{PrintObjectConfig*};