diff --git a/src/test/libslic3r/test_config.cpp b/src/test/libslic3r/test_config.cpp index 52389378f..6ed157d47 100644 --- a/src/test/libslic3r/test_config.cpp +++ b/src/test/libslic3r/test_config.cpp @@ -48,6 +48,24 @@ SCENARIO("Generic config validation performs as expected.") { SCENARIO("Config accessor functions perform as expected.") { GIVEN("A config generated from default options") { auto config {Slic3r::Config::new_from_defaults()}; + WHEN("A boolean option is set through the bool interface") { + config->set("gcode_comments", true); + THEN("The underlying value is set correctly.") { + REQUIRE(config->get("gcode_comments").getBool() == true); + } + } + WHEN("A boolean option is set through the string interface") { + config->set("gcode_comments", "1"); + THEN("The underlying value is set correctly.") { + REQUIRE(config->get("gcode_comments").getBool() == true); + } + } + WHEN("A boolean option is set through the int interface") { + config->set("gcode_comments", 1); + THEN("The underlying value is set correctly.") { + REQUIRE(config->get("gcode_comments").getBool() == true); + } + } WHEN("A numeric option is set through the string interface") { config->set("bed_temperature", "100"); THEN("The underlying value is set correctly.") { diff --git a/xs/src/libslic3r/Config.cpp b/xs/src/libslic3r/Config.cpp index 96b3a7f2e..3b3742cfd 100644 --- a/xs/src/libslic3r/Config.cpp +++ b/xs/src/libslic3r/Config.cpp @@ -96,7 +96,7 @@ Config::set(const t_config_option_key& opt_key, const std::string& value) case coInt: { auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; - ptr->setInt(std::stoi(value)); + ptr->value = std::stoi(value); } break; case coInts: { @@ -137,6 +137,11 @@ Config::set(const t_config_option_key& opt_key, const std::string& value) throw InvalidOptionValue(std::string(opt_key) + std::string(" set with invalid value.")); } } break; + case coBool: + { + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; + ptr->deserialize(value); + } break; default: Slic3r::Log::warn("Config::set", "Unknown set type."); } @@ -147,12 +152,70 @@ Config::set(const t_config_option_key& opt_key, const std::string& value) } } +void +Config::set(const t_config_option_key& opt_key, const bool value) +{ + try { + const auto& def {print_config_def.options.at(opt_key)}; + switch (def.type) { + case coBool: + { + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; + ptr->value = value; + } break; + case coInt: + { + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; + ptr->setInt(value); + } break; + case coInts: + { + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; + ptr->deserialize(std::to_string(value), true); + } break; + case coFloat: + { + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; + ptr->setFloat(value); + } break; + case coFloatOrPercent: + { + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; + ptr->setFloat(value); + ptr->percent = false; + } break; + case coFloats: + { + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; + ptr->deserialize(std::to_string(value), true); + } break; + case coString: + { + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; + if (!ptr->deserialize(std::to_string(value)) ) { + throw InvalidOptionValue(std::string(opt_key) + std::string(" set with invalid value.")); + } + } break; + default: + Slic3r::Log::warn("Config::set", "Unknown set type."); + } + + } catch (std::out_of_range &e) { + throw InvalidOptionType(std::string(opt_key) + std::string(" is an invalid Slic3r option.")); + } + +} void Config::set(const t_config_option_key& opt_key, const int value) { try { const auto& def {print_config_def.options.at(opt_key)}; switch (def.type) { + case coBool: + { + auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; + ptr->value = (value != 0); + } break; case coInt: { auto* ptr {dynamic_cast(this->_config.optptr(opt_key, true))}; diff --git a/xs/src/libslic3r/Config.hpp b/xs/src/libslic3r/Config.hpp index 8f741a1ab..67a9667fb 100644 --- a/xs/src/libslic3r/Config.hpp +++ b/xs/src/libslic3r/Config.hpp @@ -95,11 +95,18 @@ public: /// Function to parse value from a string to whatever opt_key is. void set(const t_config_option_key& opt_key, const std::string& value); + + void set(const t_config_option_key& opt_key, const char* value) { this->set(opt_key, std::string(value));} /// Function to parse value from an integer to whatever opt_key is, if /// opt_key is a numeric type. This will throw an exception and do /// nothing if called with an incorrect type. void set(const t_config_option_key& opt_key, const int value); + + /// Function to parse value from an boolean to whatever opt_key is, if + /// opt_key is a numeric type. This will throw an exception and do + /// nothing if called with an incorrect type. + void set(const t_config_option_key& opt_key, const bool value); /// Function to parse value from an integer to whatever opt_key is, if /// opt_key is a numeric type. This will throw an exception and do