From bf541a1fed2632cf66de8dfda314ffb14c9d7ba7 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sat, 2 May 2015 21:43:22 +0200 Subject: [PATCH] Refactoring in PlaceholderParser --- xs/src/libslic3r/Config.hpp | 62 +++++++++++-- xs/src/libslic3r/PlaceholderParser.cpp | 119 ++++++++----------------- xs/src/libslic3r/PlaceholderParser.hpp | 9 +- xs/src/libslic3r/Point.cpp | 7 +- xs/src/libslic3r/Point.hpp | 3 + 5 files changed, 105 insertions(+), 95 deletions(-) diff --git a/xs/src/libslic3r/Config.hpp b/xs/src/libslic3r/Config.hpp index 313b524fa..49e999bc0 100644 --- a/xs/src/libslic3r/Config.hpp +++ b/xs/src/libslic3r/Config.hpp @@ -26,8 +26,14 @@ class ConfigOption { virtual void setInt(int val) {}; }; +class ConfigOptionVectorBase : public ConfigOption { + public: + virtual ~ConfigOptionVectorBase() {}; + virtual std::vector vserialize() const = 0; +}; + template -class ConfigOptionVector +class ConfigOptionVector : public ConfigOptionVectorBase { public: virtual ~ConfigOptionVector() {}; @@ -63,7 +69,7 @@ class ConfigOptionFloat : public ConfigOption }; }; -class ConfigOptionFloats : public ConfigOption, public ConfigOptionVector +class ConfigOptionFloats : public ConfigOptionVector { public: @@ -76,6 +82,16 @@ class ConfigOptionFloats : public ConfigOption, public ConfigOptionVector vserialize() const { + std::vector vv; + for (std::vector::const_iterator it = this->values.begin(); it != this->values.end(); ++it) { + std::ostringstream ss; + ss << *it; + vv.push_back(ss.str()); + } + return vv; + }; + bool deserialize(std::string str) { this->values.clear(); std::istringstream is(str); @@ -112,7 +128,7 @@ class ConfigOptionInt : public ConfigOption }; }; -class ConfigOptionInts : public ConfigOption, public ConfigOptionVector +class ConfigOptionInts : public ConfigOptionVector { public: @@ -125,6 +141,16 @@ class ConfigOptionInts : public ConfigOption, public ConfigOptionVector return ss.str(); }; + std::vector vserialize() const { + std::vector vv; + for (std::vector::const_iterator it = this->values.begin(); it != this->values.end(); ++it) { + std::ostringstream ss; + ss << *it; + vv.push_back(ss.str()); + } + return vv; + }; + bool deserialize(std::string str) { this->values.clear(); std::istringstream is(str); @@ -174,7 +200,7 @@ class ConfigOptionString : public ConfigOption }; // semicolon-separated strings -class ConfigOptionStrings : public ConfigOption, public ConfigOptionVector +class ConfigOptionStrings : public ConfigOptionVector { public: @@ -187,6 +213,10 @@ class ConfigOptionStrings : public ConfigOption, public ConfigOptionVector vserialize() const { + return this->values; + }; + bool deserialize(std::string str) { this->values.clear(); std::istringstream is(str); @@ -279,7 +309,7 @@ class ConfigOptionPoint : public ConfigOption }; }; -class ConfigOptionPoints : public ConfigOption, public ConfigOptionVector +class ConfigOptionPoints : public ConfigOptionVector { public: @@ -294,6 +324,16 @@ class ConfigOptionPoints : public ConfigOption, public ConfigOptionVector vserialize() const { + std::vector vv; + for (Pointfs::const_iterator it = this->values.begin(); it != this->values.end(); ++it) { + std::ostringstream ss; + ss << *it; + vv.push_back(ss.str()); + } + return vv; + }; + bool deserialize(std::string str) { this->values.clear(); std::istringstream is(str); @@ -332,7 +372,7 @@ class ConfigOptionBool : public ConfigOption }; }; -class ConfigOptionBools : public ConfigOption, public ConfigOptionVector +class ConfigOptionBools : public ConfigOptionVector { public: @@ -345,6 +385,16 @@ class ConfigOptionBools : public ConfigOption, public ConfigOptionVector return ss.str(); }; + std::vector vserialize() const { + std::vector vv; + for (std::vector::const_iterator it = this->values.begin(); it != this->values.end(); ++it) { + std::ostringstream ss; + ss << (*it ? "1" : "0"); + vv.push_back(ss.str()); + } + return vv; + }; + bool deserialize(std::string str) { this->values.clear(); std::istringstream is(str); diff --git a/xs/src/libslic3r/PlaceholderParser.cpp b/xs/src/libslic3r/PlaceholderParser.cpp index 31056dd2c..583cf51e6 100644 --- a/xs/src/libslic3r/PlaceholderParser.cpp +++ b/xs/src/libslic3r/PlaceholderParser.cpp @@ -29,22 +29,14 @@ PlaceholderParser::update_timestamp() ss << std::setw(2) << std::setfill('0') << timeinfo->tm_hour; ss << std::setw(2) << std::setfill('0') << timeinfo->tm_min; ss << std::setw(2) << std::setfill('0') << timeinfo->tm_sec; - this->_single["timestamp"] = ss.str(); + this->set("timestamp", ss.str()); } - this->_single["year"] = this->_int_to_string(1900 + timeinfo->tm_year); - this->_single["month"] = this->_int_to_string(1 + timeinfo->tm_mon); - this->_single["day"] = this->_int_to_string(timeinfo->tm_mday); - this->_single["hour"] = this->_int_to_string(timeinfo->tm_hour); - this->_single["minute"] = this->_int_to_string(timeinfo->tm_min); - this->_single["second"] = this->_int_to_string(timeinfo->tm_sec); -} - -std::string -PlaceholderParser::_int_to_string(int value) const -{ - std::ostringstream ss; - ss << value; - return ss.str(); + this->set("year", 1900 + timeinfo->tm_year); + this->set("month", 1 + timeinfo->tm_mon); + this->set("day", timeinfo->tm_mday); + this->set("hour", timeinfo->tm_hour); + this->set("minute", timeinfo->tm_min); + this->set("second", timeinfo->tm_sec); } void PlaceholderParser::apply_config(DynamicPrintConfig &config) @@ -66,53 +58,20 @@ void PlaceholderParser::apply_config(DynamicPrintConfig &config) i != opt_keys.end(); ++i) { const t_config_option_key &key = *i; - - // set placeholders for options with multiple values - const ConfigOptionDef &def = (*config.def)[key]; - switch (def.type) { - case coFloats: - this->set_multiple_from_vector(key, - *(ConfigOptionFloats*)config.option(key)); - break; - - case coInts: - this->set_multiple_from_vector(key, - *(ConfigOptionInts*)config.option(key)); - break; - - case coStrings: - this->set_multiple_from_vector(key, - *(ConfigOptionStrings*)config.option(key)); - break; - - case coPoints: - this->set_multiple_from_vector(key, - *(ConfigOptionPoints*)config.option(key)); - break; - - case coBools: - this->set_multiple_from_vector(key, - *(ConfigOptionBools*)config.option(key)); - break; - - case coPoint: - { - const ConfigOptionPoint &opt = - *(ConfigOptionPoint*)config.option(key); - - this->_single[key] = opt.serialize(); - - Pointf val = opt; - this->_multiple[key + "_X"] = val.x; - this->_multiple[key + "_Y"] = val.y; - } - - break; - - default: + const ConfigOption* opt = config.option(key); + + if (const ConfigOptionVectorBase* optv = dynamic_cast(opt)) { + // set placeholders for options with multiple values + this->set(key, optv->vserialize()); + } else if (const ConfigOptionPoint* optp = dynamic_cast(opt)) { + this->_single[key] = optp->serialize(); + + Pointf val = *optp; + this->_multiple[key + "_X"] = val.x; + this->_multiple[key + "_Y"] = val.y; + } else { // set single-value placeholders - this->_single[key] = config.serialize(key); - break; + this->_single[key] = opt->serialize(); } } } @@ -121,34 +80,30 @@ void PlaceholderParser::set(const std::string &key, const std::string &value) { this->_single[key] = value; + this->_multiple.erase(key); } -std::ostream& operator<<(std::ostream &stm, const Pointf &pointf) +void +PlaceholderParser::set(const std::string &key, int value) { - return stm << pointf.x << "," << pointf.y; + std::ostringstream ss; + ss << value; + this->set(key, ss.str()); } -template -void PlaceholderParser::set_multiple_from_vector(const std::string &key, - ConfigOptionVector &opt) +void +PlaceholderParser::set(const std::string &key, const std::vector &values) { - const std::vector &vals = opt.values; - - for (size_t i = 0; i < vals.size(); ++i) { - std::stringstream multikey_stm; - multikey_stm << key << "_" << i; - - std::stringstream val_stm; - val_stm << vals[i]; - - this->_multiple[multikey_stm.str()] = val_stm.str(); - } - - if (vals.size() > 0) { - std::stringstream val_stm; - val_stm << vals[0]; - this->_multiple[key] = val_stm.str(); + for (std::vector::const_iterator v = values.begin(); v != values.end(); ++v) { + std::stringstream ss; + ss << key << "_" << (v - values.begin()); + + this->_multiple[ ss.str() ] = *v; + if (v == values.begin()) { + this->_multiple[key] = *v; + } } + this->_single.erase(key); } #ifdef SLIC3RXS diff --git a/xs/src/libslic3r/PlaceholderParser.hpp b/xs/src/libslic3r/PlaceholderParser.hpp index eb061fab2..25d1bcdc3 100644 --- a/xs/src/libslic3r/PlaceholderParser.hpp +++ b/xs/src/libslic3r/PlaceholderParser.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "PrintConfig.hpp" @@ -20,12 +21,8 @@ class PlaceholderParser void update_timestamp(); void apply_config(DynamicPrintConfig &config); void set(const std::string &key, const std::string &value); - - private: - template - void set_multiple_from_vector( - const std::string &key, ConfigOptionVector &opt); - std::string _int_to_string(int value) const; + void set(const std::string &key, int value); + void set(const std::string &key, const std::vector &values); }; } diff --git a/xs/src/libslic3r/Point.cpp b/xs/src/libslic3r/Point.cpp index 9a564f879..9707cd931 100644 --- a/xs/src/libslic3r/Point.cpp +++ b/xs/src/libslic3r/Point.cpp @@ -3,7 +3,6 @@ #include "MultiPoint.hpp" #include #include -#include namespace Slic3r { @@ -340,6 +339,12 @@ REGISTER_CLASS(Point3, "Point3"); #endif +std::ostream& +operator<<(std::ostream &stm, const Pointf &pointf) +{ + return stm << pointf.x << "," << pointf.y; +} + void Pointf::scale(double factor) { diff --git a/xs/src/libslic3r/Point.hpp b/xs/src/libslic3r/Point.hpp index 327199690..f9850c774 100644 --- a/xs/src/libslic3r/Point.hpp +++ b/xs/src/libslic3r/Point.hpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace Slic3r { @@ -77,6 +78,8 @@ class Point3 : public Point explicit Point3(coord_t _x = 0, coord_t _y = 0, coord_t _z = 0): Point(_x, _y), z(_z) {}; }; +std::ostream& operator<<(std::ostream &stm, const Pointf &pointf); + class Pointf { public: