From 008f38cf68f24e2f2041eb63e7c6f3412492ef9f Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Fri, 20 Dec 2013 16:37:28 +0100 Subject: [PATCH] More work for porting Config to XS --- xs/MANIFEST | 1 + xs/src/Config.cpp | 57 +++++++++++++++++++++ xs/src/Config.hpp | 117 ++++++++++++++++++++++++-------------------- xs/xsp/Config.xsp | 15 ++++++ xs/xsp/my.map | 2 + xs/xsp/typemap.xspt | 2 + 6 files changed, 141 insertions(+), 53 deletions(-) create mode 100644 xs/xsp/Config.xsp diff --git a/xs/MANIFEST b/xs/MANIFEST index 16914823c..b3f0d64f8 100644 --- a/xs/MANIFEST +++ b/xs/MANIFEST @@ -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 diff --git a/xs/src/Config.cpp b/xs/src/Config.cpp index 07688bd41..764644012 100644 --- a/xs/src/Config.cpp +++ b/xs/src/Config.cpp @@ -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(opt)) { + return newSVnv(v->value); + } else if (ConfigOptionInt* v = dynamic_cast(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); + } +} } diff --git a/xs/src/Config.hpp b/xs/src/Config.hpp index 9ada129bf..275a33a3e 100644 --- a/xs/src/Config.hpp +++ b/xs/src/Config.hpp @@ -6,29 +6,49 @@ #include #include -keyspace Slic3r { +namespace Slic3r { typedef std::string t_config_option_key; typedef std::vector 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_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_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_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 diff --git a/xs/xsp/Config.xsp b/xs/xsp/Config.xsp new file mode 100644 index 000000000..1bd9d9db6 --- /dev/null +++ b/xs/xsp/Config.xsp @@ -0,0 +1,15 @@ +%module{Slic3r::XS}; + +%{ +#include +#include "Config.hpp" +%} + +%name{Slic3r::Config} class DynamicConfig { + DynamicConfig(); + ~DynamicConfig(); + SV* get(t_config_option_key opt_key); +%{ + +%} +}; diff --git a/xs/xsp/my.map b/xs/xsp/my.map index dcd0293f8..7543ce82d 100644 --- a/xs/xsp/my.map +++ b/xs/xsp/my.map @@ -1,5 +1,7 @@ std::vector T_STD_VECTOR_INT +t_config_option_key T_STD_STRING +DynamicConfig* O_OBJECT ZTable* O_OBJECT TriangleMesh* O_OBJECT Point* O_OBJECT diff --git a/xs/xsp/typemap.xspt b/xs/xsp/typemap.xspt index 559dba843..a4e26fcc1 100644 --- a/xs/xsp/typemap.xspt +++ b/xs/xsp/typemap.xspt @@ -1,11 +1,13 @@ %typemap{bool}{simple}; %typemap{std::string}; +%typemap{t_config_option_key}; %typemap{std::vector*}; %typemap{std::vector}; %typemap{std::vector*}; %typemap{SV*}; %typemap{AV*}; %typemap{Point*}; +%typemap{DynamicConfig*}; %typemap{ExPolygon*}; %typemap{ExPolygonCollection*}; %typemap{Line*};