Merge branch 'xs-config'

Conflicts:
	lib/Slic3r/Config.pm
	xs/MANIFEST
This commit is contained in:
Alessandro Ranellucci 2013-12-22 01:41:55 +01:00
commit ab25cc4940
19 changed files with 2370 additions and 1235 deletions

257
xs/src/Config.cpp Normal file
View file

@ -0,0 +1,257 @@
#include "Config.hpp"
namespace Slic3r {
bool
ConfigBase::has(const t_config_option_key opt_key) {
return (this->option(opt_key, false) != NULL);
}
void
ConfigBase::apply(ConfigBase &other, bool ignore_nonexistent) {
// get list of option keys to apply
t_config_option_keys opt_keys;
other.keys(&opt_keys);
// loop through options and apply them
for (t_config_option_keys::const_iterator it = opt_keys.begin(); it != opt_keys.end(); ++it) {
ConfigOption* my_opt = this->option(*it, true);
if (my_opt == NULL) {
if (ignore_nonexistent == false) throw "Attempt to apply non-existent option";
continue;
}
// not the most efficient way, but easier than casting pointers to subclasses
my_opt->deserialize( other.option(*it)->serialize() );
}
}
std::string
ConfigBase::serialize(const t_config_option_key opt_key) {
ConfigOption* opt = this->option(opt_key);
assert(opt != NULL);
return opt->serialize();
}
void
ConfigBase::set_deserialize(const t_config_option_key opt_key, std::string str) {
ConfigOption* opt = this->option(opt_key);
assert(opt != NULL);
opt->deserialize(str);
}
double
ConfigBase::get_abs_value(const t_config_option_key opt_key) {
// get option definition
assert(this->def->count(opt_key) != 0);
ConfigOptionDef* def = &(*this->def)[opt_key];
assert(def->type == coFloatOrPercent);
// get stored option value
ConfigOptionFloatOrPercent* opt = dynamic_cast<ConfigOptionFloatOrPercent*>(this->option(opt_key));
assert(opt != NULL);
// compute absolute value
if (opt->percent) {
ConfigOptionFloat* optbase = dynamic_cast<ConfigOptionFloat*>(this->option(def->ratio_over));
if (optbase == NULL) throw "ratio_over option not found";
return optbase->value * opt->value / 100;
} else {
return opt->value;
}
}
#ifdef SLIC3RXS
SV*
ConfigBase::as_hash() {
HV* hv = newHV();
t_config_option_keys opt_keys;
this->keys(&opt_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 );
return newRV_noinc((SV*)hv);
}
SV*
ConfigBase::get(t_config_option_key opt_key) {
ConfigOption* opt = this->option(opt_key);
if (opt == NULL) return &PL_sv_undef;
if (ConfigOptionFloat* optv = dynamic_cast<ConfigOptionFloat*>(opt)) {
return newSVnv(optv->value);
} else if (ConfigOptionFloats* optv = dynamic_cast<ConfigOptionFloats*>(opt)) {
AV* av = newAV();
av_fill(av, optv->values.size()-1);
for (std::vector<double>::iterator it = optv->values.begin(); it != optv->values.end(); ++it)
av_store(av, it - optv->values.begin(), newSVnv(*it));
return newRV_noinc((SV*)av);
} else if (ConfigOptionInt* optv = dynamic_cast<ConfigOptionInt*>(opt)) {
return newSViv(optv->value);
} else if (ConfigOptionInts* optv = dynamic_cast<ConfigOptionInts*>(opt)) {
AV* av = newAV();
av_fill(av, optv->values.size()-1);
for (std::vector<int>::iterator it = optv->values.begin(); it != optv->values.end(); ++it)
av_store(av, it - optv->values.begin(), newSViv(*it));
return newRV_noinc((SV*)av);
} else if (ConfigOptionString* optv = dynamic_cast<ConfigOptionString*>(opt)) {
// we don't serialize() because that would escape newlines
return newSVpvn(optv->value.c_str(), optv->value.length());
} else if (ConfigOptionStrings* optv = dynamic_cast<ConfigOptionStrings*>(opt)) {
AV* av = newAV();
av_fill(av, optv->values.size()-1);
for (std::vector<std::string>::iterator it = optv->values.begin(); it != optv->values.end(); ++it)
av_store(av, it - optv->values.begin(), newSVpvn(it->c_str(), it->length()));
return newRV_noinc((SV*)av);
} else if (ConfigOptionPoint* optv = dynamic_cast<ConfigOptionPoint*>(opt)) {
return optv->point.to_SV_pureperl();
} else if (ConfigOptionPoints* optv = dynamic_cast<ConfigOptionPoints*>(opt)) {
AV* av = newAV();
av_fill(av, optv->points.size()-1);
for (Pointfs::iterator it = optv->points.begin(); it != optv->points.end(); ++it)
av_store(av, it - optv->points.begin(), it->to_SV_pureperl());
return newRV_noinc((SV*)av);
} else if (ConfigOptionBool* optv = dynamic_cast<ConfigOptionBool*>(opt)) {
return newSViv(optv->value ? 1 : 0);
} else if (ConfigOptionBools* optv = dynamic_cast<ConfigOptionBools*>(opt)) {
AV* av = newAV();
av_fill(av, optv->values.size()-1);
for (std::vector<bool>::iterator it = optv->values.begin(); it != optv->values.end(); ++it)
av_store(av, it - optv->values.begin(), newSViv(*it ? 1 : 0));
return newRV_noinc((SV*)av);
} else {
std::string serialized = opt->serialize();
return newSVpvn(serialized.c_str(), serialized.length());
}
}
void
ConfigBase::set(t_config_option_key opt_key, SV* value) {
ConfigOption* opt = this->option(opt_key, true);
if (opt == NULL) CONFESS("Trying to set non-existing option");
if (ConfigOptionFloat* optv = dynamic_cast<ConfigOptionFloat*>(opt)) {
optv->value = SvNV(value);
} else if (ConfigOptionFloats* optv = dynamic_cast<ConfigOptionFloats*>(opt)) {
optv->values.clear();
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0);
optv->values.push_back(SvNV(*elem));
}
} else if (ConfigOptionInt* optv = dynamic_cast<ConfigOptionInt*>(opt)) {
optv->value = SvIV(value);
} else if (ConfigOptionInts* optv = dynamic_cast<ConfigOptionInts*>(opt)) {
optv->values.clear();
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0);
optv->values.push_back(SvIV(*elem));
}
} else if (ConfigOptionString* optv = dynamic_cast<ConfigOptionString*>(opt)) {
optv->value = std::string(SvPV_nolen(value), SvCUR(value));
} else if (ConfigOptionStrings* optv = dynamic_cast<ConfigOptionStrings*>(opt)) {
optv->values.clear();
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0);
optv->values.push_back(std::string(SvPV_nolen(*elem), SvCUR(*elem)));
}
} else if (ConfigOptionPoint* optv = dynamic_cast<ConfigOptionPoint*>(opt)) {
optv->point.from_SV(value);
} else if (ConfigOptionPoints* optv = dynamic_cast<ConfigOptionPoints*>(opt)) {
optv->points.clear();
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0);
Pointf point;
point.from_SV(*elem);
optv->points.push_back(point);
}
} else if (ConfigOptionBool* optv = dynamic_cast<ConfigOptionBool*>(opt)) {
optv->value = SvTRUE(value);
} else if (ConfigOptionBools* optv = dynamic_cast<ConfigOptionBools*>(opt)) {
optv->values.clear();
AV* av = (AV*)SvRV(value);
const size_t len = av_len(av)+1;
for (size_t i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0);
optv->values.push_back(SvTRUE(*elem));
}
} else {
opt->deserialize( std::string(SvPV_nolen(value)) );
}
}
#endif
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;
}
}
ConfigOption*
DynamicConfig::option(const t_config_option_key opt_key, bool create) {
if (this->options.count(opt_key) == 0) {
if (create) {
ConfigOptionDef* optdef = &(*this->def)[opt_key];
ConfigOption* opt;
if (optdef->type == coFloat) {
opt = new ConfigOptionFloat ();
} else if (optdef->type == coFloats) {
opt = new ConfigOptionFloats ();
} else if (optdef->type == coInt) {
opt = new ConfigOptionInt ();
} else if (optdef->type == coInts) {
opt = new ConfigOptionInts ();
} else if (optdef->type == coString) {
opt = new ConfigOptionString ();
} else if (optdef->type == coStrings) {
opt = new ConfigOptionStrings ();
} else if (optdef->type == coFloatOrPercent) {
opt = new ConfigOptionFloatOrPercent ();
} else if (optdef->type == coPoint) {
opt = new ConfigOptionPoint ();
} else if (optdef->type == coPoints) {
opt = new ConfigOptionPoints ();
} else if (optdef->type == coBool) {
opt = new ConfigOptionBool ();
} else if (optdef->type == coBools) {
opt = new ConfigOptionBools ();
} else if (optdef->type == coEnum) {
ConfigOptionEnumGeneric* optv = new ConfigOptionEnumGeneric ();
optv->keys_map = &optdef->enum_keys_map;
opt = static_cast<ConfigOption*>(optv);
} else {
throw "Unknown option type";
}
this->options[opt_key] = opt;
return opt;
} else {
return NULL;
}
}
return this->options[opt_key];
}
void
DynamicConfig::keys(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);
}
void
StaticConfig::keys(t_config_option_keys *keys) {
for (t_optiondef_map::const_iterator it = this->def->begin(); it != this->def->end(); ++it) {
ConfigOption* opt = this->option(it->first);
if (opt != NULL) keys->push_back(it->first);
}
}
}

427
xs/src/Config.hpp Normal file
View file

@ -0,0 +1,427 @@
#ifndef slic3r_Config_hpp_
#define slic3r_Config_hpp_
#include <myinit.h>
#include <map>
#include <sstream>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include "Point.hpp"
namespace Slic3r {
typedef std::string t_config_option_key;
typedef std::vector<std::string> t_config_option_keys;
class ConfigOption {
public:
virtual ~ConfigOption() {};
virtual std::string serialize() = 0;
virtual void deserialize(std::string str) = 0;
};
class ConfigOptionFloat : public ConfigOption
{
public:
double value; // use double instead of float for preserving compatibility with values coming from Perl
ConfigOptionFloat() : value(0) {};
operator double() const { return this->value; };
std::string serialize() {
std::ostringstream ss;
ss << this->value;
return ss.str();
};
void deserialize(std::string str) {
this->value = ::atof(str.c_str());
};
};
class ConfigOptionFloats : public ConfigOption
{
public:
std::vector<double> values;
std::string serialize() {
std::ostringstream ss;
for (std::vector<double>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
if (it - this->values.begin() != 0) ss << ",";
ss << *it;
}
return ss.str();
};
void deserialize(std::string str) {
this->values.clear();
std::istringstream is(str);
std::string item_str;
while (std::getline(is, item_str, ',')) {
this->values.push_back(::atof(item_str.c_str()));
}
};
};
class ConfigOptionInt : public ConfigOption
{
public:
int value;
ConfigOptionInt() : value(0) {};
operator int() const { return this->value; };
std::string serialize() {
std::ostringstream ss;
ss << this->value;
return ss.str();
};
void deserialize(std::string str) {
this->value = ::atoi(str.c_str());
};
};
class ConfigOptionInts : public ConfigOption
{
public:
std::vector<int> values;
std::string serialize() {
std::ostringstream ss;
for (std::vector<int>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
if (it - this->values.begin() != 0) ss << ",";
ss << *it;
}
return ss.str();
};
void deserialize(std::string str) {
this->values.clear();
std::istringstream is(str);
std::string item_str;
while (std::getline(is, item_str, ',')) {
this->values.push_back(::atoi(item_str.c_str()));
}
};
};
class ConfigOptionString : public ConfigOption
{
public:
std::string value;
ConfigOptionString() : value("") {};
operator std::string() const { return this->value; };
std::string serialize() {
std::string str = this->value;
// s/\R/\\n/g
size_t pos = 0;
while ((pos = str.find("\n", pos)) != std::string::npos || (pos = str.find("\r", pos)) != std::string::npos) {
str.replace(pos, 1, "\\n");
pos += 2; // length of "\\n"
}
return str;
};
void deserialize(std::string str) {
// s/\\n/\n/g
size_t pos = 0;
while ((pos = str.find("\\n", pos)) != std::string::npos) {
str.replace(pos, 2, "\n");
pos += 1; // length of "\n"
}
this->value = str;
};
};
// semicolon-separated strings
class ConfigOptionStrings : public ConfigOption
{
public:
std::vector<std::string> values;
std::string serialize() {
std::ostringstream ss;
for (std::vector<std::string>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
if (it - this->values.begin() != 0) ss << ";";
ss << *it;
}
return ss.str();
};
void deserialize(std::string str) {
this->values.clear();
std::istringstream is(str);
std::string item_str;
while (std::getline(is, item_str, ';')) {
this->values.push_back(item_str);
}
};
};
class ConfigOptionFloatOrPercent : public ConfigOption
{
public:
double value;
bool percent;
ConfigOptionFloatOrPercent() : value(0), percent(false) {};
std::string serialize() {
std::ostringstream ss;
ss << this->value;
std::string s(ss.str());
if (this->percent) s += "%";
return s;
};
void deserialize(std::string str) {
if (str.find_first_of("%") != std::string::npos) {
sscanf(str.c_str(), "%lf%%", &this->value);
this->percent = true;
} else {
this->value = ::atof(str.c_str());
this->percent = false;
}
};
};
class ConfigOptionPoint : public ConfigOption
{
public:
Pointf point;
ConfigOptionPoint() : point(Pointf(0,0)) {};
operator Pointf() const { return this->point; };
std::string serialize() {
std::ostringstream ss;
ss << this->point.x;
ss << ",";
ss << this->point.y;
return ss.str();
};
void deserialize(std::string str) {
sscanf(str.c_str(), "%lf%*1[,x]%lf", &this->point.x, &this->point.y);
};
};
class ConfigOptionPoints : public ConfigOption
{
public:
Pointfs points;
std::string serialize() {
std::ostringstream ss;
for (Pointfs::const_iterator it = this->points.begin(); it != this->points.end(); ++it) {
if (it - this->points.begin() != 0) ss << ",";
ss << it->x;
ss << "x";
ss << it->y;
}
return ss.str();
};
void deserialize(std::string str) {
this->points.clear();
std::istringstream is(str);
std::string point_str;
while (std::getline(is, point_str, ',')) {
Pointf point;
sscanf(point_str.c_str(), "%lfx%lf", &point.x, &point.y);
this->points.push_back(point);
}
};
};
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);
};
};
class ConfigOptionBools : public ConfigOption
{
public:
std::vector<bool> values;
std::string serialize() {
std::ostringstream ss;
for (std::vector<bool>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
if (it - this->values.begin() != 0) ss << ",";
ss << (*it ? "1" : "0");
}
return ss.str();
};
void deserialize(std::string str) {
this->values.clear();
std::istringstream is(str);
std::string item_str;
while (std::getline(is, item_str, ',')) {
this->values.push_back(item_str.compare("1") == 0);
}
};
};
typedef std::map<std::string,int> t_config_enum_values;
template <class T>
class ConfigOptionEnum : public ConfigOption
{
public:
T value;
operator T() const { return this->value; };
std::string serialize() {
t_config_enum_values enum_keys_map = ConfigOptionEnum<T>::get_enum_values();
for (t_config_enum_values::iterator it = enum_keys_map.begin(); it != enum_keys_map.end(); ++it) {
if (it->second == static_cast<int>(this->value)) return it->first;
}
return "";
};
void deserialize(std::string str) {
t_config_enum_values enum_keys_map = ConfigOptionEnum<T>::get_enum_values();
assert(enum_keys_map.count(str) > 0);
this->value = static_cast<T>(enum_keys_map[str]);
};
static t_config_enum_values get_enum_values();
};
/* We use this one in DynamicConfig objects, otherwise it's better to use
the specialized ConfigOptionEnum<T> containers. */
class ConfigOptionEnumGeneric : public ConfigOption
{
public:
int value;
t_config_enum_values* keys_map;
operator int() const { return this->value; };
std::string serialize() {
for (t_config_enum_values::iterator it = this->keys_map->begin(); it != this->keys_map->end(); ++it) {
if (it->second == this->value) return it->first;
}
return "";
};
void deserialize(std::string str) {
assert(this->keys_map->count(str) != 0);
this->value = (*this->keys_map)[str];
};
};
enum ConfigOptionType {
coFloat,
coFloats,
coInt,
coInts,
coString,
coStrings,
coFloatOrPercent,
coPoint,
coPoints,
coBool,
coBools,
coEnum,
};
class ConfigOptionDef
{
public:
ConfigOptionType type;
std::string label;
std::string category;
std::string tooltip;
std::string sidetext;
std::string cli;
std::string scope;
t_config_option_key ratio_over;
bool multiline;
bool full_label;
bool full_width;
bool readonly;
int height;
int width;
int min;
int max;
std::vector<t_config_option_key> aliases;
std::vector<t_config_option_key> shortcut;
std::vector<std::string> enum_values;
std::vector<std::string> enum_labels;
t_config_enum_values enum_keys_map;
ConfigOptionDef() : multiline(false), full_label(false), full_width(false), readonly(false),
height(-1), width(-1), min(INT_MIN), max(INT_MAX) {};
};
typedef std::map<t_config_option_key,ConfigOptionDef> t_optiondef_map;
class ConfigBase
{
public:
t_optiondef_map* def;
ConfigBase() : def(NULL) {};
bool has(const t_config_option_key opt_key);
virtual ConfigOption* option(const t_config_option_key opt_key, bool create = false) = 0;
virtual void keys(t_config_option_keys *keys) = 0;
void apply(ConfigBase &other, bool ignore_nonexistent = false);
std::string serialize(const t_config_option_key opt_key);
void set_deserialize(const t_config_option_key opt_key, std::string str);
double get_abs_value(const t_config_option_key opt_key);
#ifdef SLIC3RXS
SV* as_hash();
SV* get(t_config_option_key opt_key);
void set(t_config_option_key opt_key, SV* value);
#endif
};
class DynamicConfig : public ConfigBase
{
public:
DynamicConfig() {};
~DynamicConfig();
ConfigOption* option(const t_config_option_key opt_key, bool create = false);
void keys(t_config_option_keys *keys);
private:
DynamicConfig(const DynamicConfig& other); // we disable this by making it private and unimplemented
DynamicConfig& operator= (const DynamicConfig& other); // we disable this by making it private and unimplemented
typedef std::map<t_config_option_key,ConfigOption*> t_options_map;
t_options_map options;
};
class StaticConfig : public ConfigBase
{
public:
void keys(t_config_option_keys *keys);
};
}
#endif

View file

@ -174,6 +174,23 @@ Point::from_SV_check(SV* point_sv)
this->from_SV(point_sv);
}
}
SV*
Pointf::to_SV_pureperl() const {
AV* av = newAV();
av_fill(av, 1);
av_store(av, 0, newSVnv(this->x));
av_store(av, 1, newSVnv(this->y));
return newRV_noinc((SV*)av);
}
void
Pointf::from_SV(SV* point_sv)
{
AV* point_av = (AV*)SvRV(point_sv);
this->x = SvNV(*av_fetch(point_av, 0, 0));
this->y = SvNV(*av_fetch(point_av, 1, 0));
}
#endif
}

View file

@ -9,8 +9,10 @@ namespace Slic3r {
class Line;
class Point;
class Pointf;
typedef std::vector<Point> Points;
typedef std::vector<Point*> PointPtrs;
typedef std::vector<Pointf> Pointfs;
class Point
{
@ -42,6 +44,19 @@ class Point
#endif
};
class Pointf
{
public:
double x;
double y;
explicit Pointf(double _x = 0, double _y = 0): x(_x), y(_y) {};
#ifdef SLIC3RXS
void from_SV(SV* point_sv);
SV* to_SV_pureperl() const;
#endif
};
}
#endif

7
xs/src/PrintConfig.cpp Normal file
View file

@ -0,0 +1,7 @@
#include "PrintConfig.hpp"
namespace Slic3r {
t_optiondef_map PrintConfig::PrintConfigDef = PrintConfig::build_def();
}

1307
xs/src/PrintConfig.hpp Normal file

File diff suppressed because it is too large Load diff