More work for porting Config to XS

This commit is contained in:
Alessandro Ranellucci 2013-12-20 16:37:28 +01:00
parent 4d066840ba
commit 008f38cf68
6 changed files with 141 additions and 53 deletions

View File

@ -61,6 +61,7 @@ t/12_extrusionpathcollection.t
t/13_polylinecollection.t
t/14_geometry.t
xsp/Clipper.xsp
xsp/Config.xsp
xsp/ExPolygon.xsp
xsp/ExPolygonCollection.xsp
xsp/ExtrusionEntityCollection.xsp

View File

@ -2,6 +2,63 @@
namespace Slic3r {
t_optiondef_map Options = _build_optiondef_map();
FullConfig DefaultConfig = _build_default_config();
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);
if (my_opt == NULL && ignore_nonexistent == false) throw "Attempt to apply non-existent option";
*my_opt = *(other.option(*it));
}
}
#ifdef SLIC3RXS
SV*
ConfigBase::get(t_config_option_key opt_key) {
ConfigOption* opt = this->option(opt_key);
if (opt == NULL) return &PL_sv_undef;
if (ConfigOptionFloat* v = dynamic_cast<ConfigOptionFloat*>(opt)) {
return newSVnv(v->value);
} else if (ConfigOptionInt* v = dynamic_cast<ConfigOptionInt*>(opt)) {
return newSViv(v->value);
} else {
throw "Unknown option value type";
}
}
#endif
ConfigOption*
DynamicConfig::option(const t_config_option_key opt_key) {
t_options_map::iterator it = this->options.find(opt_key);
if (it == this->options.end()) return NULL;
return it->second;
}
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);
}
bool
DynamicConfig::has(const t_config_option_key opt_key) const {
t_options_map::const_iterator it = this->options.find(opt_key);
return (it != this->options.end());
}
void
StaticConfig::keys(t_config_option_keys *keys) {
for (t_optiondef_map::const_iterator it = Options.begin(); it != Options.end(); ++it) {
ConfigOption* opt = this->option(it->first);
if (opt != NULL) keys->push_back(it->first);
}
}
}

View File

@ -6,29 +6,49 @@
#include <string>
#include <vector>
keyspace Slic3r {
namespace Slic3r {
typedef std::string t_config_option_key;
typedef std::vector<std::string> t_config_option_keys;
class ConfigOption
class ConfigOption {
public:
virtual ~ConfigOption() {};
};
class ConfigOptionFloat : public ConfigOption
{
public:
float float_value;
int int_value;
std::string string_value;
bool percent;
operator float() const { return this->float_value; };
operator int() const { return this->int_value; };
operator std::string() const { return this->string_value; };
float value;
operator float() const { return this->value; };
};
class ConfigOptionInt : public ConfigOption
{
public:
int value;
operator int() const { return this->value; };
};
class ConfigOptionString : public ConfigOption
{
public:
std::string value;
operator std::string() const { return this->value; };
};
class ConfigOptionFloatOrPercent : public ConfigOption
{
public:
float value;
bool percent;
};
enum ConfigOptionType {
coFloat,
coInt,
coFloatOrPercent,
coString,
coFloatOrPercent,
};
class ConfigOptionDef
@ -37,74 +57,45 @@ class ConfigOptionDef
ConfigOptionType type;
std::string label;
std::string tooltip;
ConfigOption default_;
};
typedef std::map<t_config_option_key,ConfigOptionDef> t_optiondef_map;
t_optiondef_map Options;
Options["layer_height"].type = coFloat;
Options["layer_height"].label = "Layer height";
Options["layer_height"].tooltip = "This setting controls the height (and thus the total number) of the slices/layers. Thinner layers give better accuracy but take more time to print.";
Options["layer_height"].default_.float_value = 0.4;
Options["first_layer_height"].type = coFloatOrPercent;
Options["first_layer_height"].default_.percent = false;
Options["first_layer_height"].default_.float_value = 0.35;
class ConfigBase
{
public:
virtual ConfigOption* option(const t_config_option_key opt_key) = 0;
virtual void keys(t_config_option_keys *keys) = 0;
void apply(ConfigBase &other, bool ignore_nonexistent = false);
void apply(const ConfigBase &other, bool ignore_nonexistent = false)
{
// 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);
if (my_opt == NULL && ignore_nonexistent == false) throw "Attempt to apply non-existent option";
*my_opt = *(other.option(*it));
}
};
#ifdef SLIC3RXS
SV* get(t_config_option_key opt_key);
#endif
};
class DynamicConfig : public ConfigBase
{
public:
typedef std::map<t_config_option_key,ConfigOption> t_options_map;
ConfigOption* option(const t_config_option_key opt_key);
void keys(t_config_option_keys *keys);
bool has(const t_config_option_key opt_key) const;
private:
typedef std::map<t_config_option_key,ConfigOption*> t_options_map;
t_options_map options;
ConfigOption* option(const t_config_option_key opt_key) {
t_options_map::iterator it = this->options.find(opt_key);
if (it == this->options.end()) return NULL;
return &it->second;
};
void 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);
};
};
class StaticConfig : public ConfigBase
{
public:
void keys(t_config_option_keys *keys) {
for (t_optiondef_map::const_iterator it = Options.begin(); it != Options.end(); ++it) {
ConfigOption* opt = this->option(it->first);
if (opt != NULL) keys->push_back(it->first);
}
};
void keys(t_config_option_keys *keys);
};
class FullConfig : public StaticConfig
{
public:
ConfigOption layer_height;
ConfigOption first_layer_height;
ConfigOptionFloat layer_height;
ConfigOptionFloatOrPercent first_layer_height;
ConfigOption* option(const t_config_option_key opt_key) {
if (opt_key == "layer_height") return &this->layer_height;
@ -113,6 +104,26 @@ class FullConfig : public StaticConfig
};
};
static t_optiondef_map _build_optiondef_map () {
t_optiondef_map Options;
Options["layer_height"].type = coFloat;
Options["layer_height"].label = "Layer height";
Options["layer_height"].tooltip = "This setting controls the height (and thus the total number) of the slices/layers. Thinner layers give better accuracy but take more time to print.";
Options["first_layer_height"].type = coFloatOrPercent;
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;
return defconf;
}
}
#endif

15
xs/xsp/Config.xsp Normal file
View File

@ -0,0 +1,15 @@
%module{Slic3r::XS};
%{
#include <myinit.h>
#include "Config.hpp"
%}
%name{Slic3r::Config} class DynamicConfig {
DynamicConfig();
~DynamicConfig();
SV* get(t_config_option_key opt_key);
%{
%}
};

View File

@ -1,5 +1,7 @@
std::vector<Points::size_type> T_STD_VECTOR_INT
t_config_option_key T_STD_STRING
DynamicConfig* O_OBJECT
ZTable* O_OBJECT
TriangleMesh* O_OBJECT
Point* O_OBJECT

View File

@ -1,11 +1,13 @@
%typemap{bool}{simple};
%typemap{std::string};
%typemap{t_config_option_key};
%typemap{std::vector<unsigned int>*};
%typemap{std::vector<double>};
%typemap{std::vector<double>*};
%typemap{SV*};
%typemap{AV*};
%typemap{Point*};
%typemap{DynamicConfig*};
%typemap{ExPolygon*};
%typemap{ExPolygonCollection*};
%typemap{Line*};