mirror of
				https://github.com/SoftFever/OrcaSlicer.git
				synced 2025-10-30 20:21:12 -06:00 
			
		
		
		
	Finished porting Config and option definition to XS
This commit is contained in:
		
							parent
							
								
									9836e963a5
								
							
						
					
					
						commit
						4515d90052
					
				
					 8 changed files with 1301 additions and 1204 deletions
				
			
		|  | @ -133,6 +133,8 @@ sub parallelize { | |||
| sub thread_cleanup { | ||||
|     # prevent destruction of shared objects | ||||
|     no warnings 'redefine'; | ||||
|     *Slic3r::Config::DESTROY                = sub {}; | ||||
|     *Slic3r::Config::Print::DESTROY         = sub {}; | ||||
|     *Slic3r::ExPolygon::DESTROY             = sub {}; | ||||
|     *Slic3r::ExPolygon::Collection::DESTROY = sub {}; | ||||
|     *Slic3r::ExtrusionLoop::DESTROY         = sub {}; | ||||
|  |  | |||
							
								
								
									
										1146
									
								
								lib/Slic3r/Config.pm
									
										
									
									
									
								
							
							
						
						
									
										1146
									
								
								lib/Slic3r/Config.pm
									
										
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							|  | @ -152,7 +152,7 @@ sub usage { | |||
|     my ($exit_code) = @_; | ||||
|      | ||||
|     my $config = Slic3r::Config->new_from_defaults->as_hash; | ||||
|     use Devel::Peek; Dump($config->{layer_height});exit; | ||||
|      | ||||
|     my $j = ''; | ||||
|     if ($Slic3r::have_threads) { | ||||
|         $j = <<"EOF"; | ||||
|  |  | |||
|  | @ -54,7 +54,7 @@ ConfigBase::get_abs_value(const t_config_option_key opt_key) { | |||
|     // compute absolute value
 | ||||
|     if (opt->percent) { | ||||
|         ConfigOptionFloat* optbase = dynamic_cast<ConfigOptionFloat*>(this->option(def->ratio_over)); | ||||
|         assert(optbase != NULL); | ||||
|         if (optbase == NULL) throw "ratio_over option not found"; | ||||
|         return optbase->value * opt->value / 100; | ||||
|     } else { | ||||
|         return opt->value; | ||||
|  | @ -98,6 +98,12 @@ ConfigBase::get(t_config_option_key opt_key) { | |||
|     } 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)) { | ||||
|  | @ -123,7 +129,7 @@ ConfigBase::get(t_config_option_key opt_key) { | |||
| void | ||||
| ConfigBase::set(t_config_option_key opt_key, SV* value) { | ||||
|     ConfigOption* opt = this->option(opt_key, true); | ||||
|     assert(opt != NULL); | ||||
|     if (opt == NULL) CONFESS("Trying to set non-existing option"); | ||||
|      | ||||
|     if (ConfigOptionFloat* optv = dynamic_cast<ConfigOptionFloat*>(opt)) { | ||||
|         optv->value = SvNV(value); | ||||
|  | @ -147,6 +153,14 @@ ConfigBase::set(t_config_option_key opt_key, SV* value) { | |||
|         } | ||||
|     } 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)) { | ||||
|  | @ -198,6 +212,8 @@ DynamicConfig::option(const t_config_option_key opt_key, bool create) { | |||
|                 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) { | ||||
|  |  | |||
|  | @ -4,6 +4,7 @@ | |||
| #include <myinit.h> | ||||
| #include <map> | ||||
| #include <sstream> | ||||
| #include <climits> | ||||
| #include <cstdio> | ||||
| #include <cstdlib> | ||||
| #include <iostream> | ||||
|  | @ -26,10 +27,10 @@ class ConfigOption { | |||
| class ConfigOptionFloat : public ConfigOption | ||||
| { | ||||
|     public: | ||||
|     float value; | ||||
|     double value;  // use double instead of float for preserving compatibility with values coming from Perl
 | ||||
|     ConfigOptionFloat() : value(0) {}; | ||||
|      | ||||
|     operator float() const { return this->value; }; | ||||
|     operator double() const { return this->value; }; | ||||
|      | ||||
|     std::string serialize() { | ||||
|         std::ostringstream ss; | ||||
|  | @ -142,6 +143,31 @@ class ConfigOptionString : public ConfigOption | |||
|     }; | ||||
| }; | ||||
| 
 | ||||
| // 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: | ||||
|  | @ -314,6 +340,7 @@ enum ConfigOptionType { | |||
|     coInt, | ||||
|     coInts, | ||||
|     coString, | ||||
|     coStrings, | ||||
|     coFloatOrPercent, | ||||
|     coPoint, | ||||
|     coPoints, | ||||
|  | @ -348,7 +375,7 @@ class ConfigOptionDef | |||
|     t_config_enum_values enum_keys_map; | ||||
|      | ||||
|     ConfigOptionDef() : multiline(false), full_label(false), full_width(false), readonly(false), | ||||
|                         height(0), width(0), min(0), max(0) {}; | ||||
|                         height(-1), width(-1), min(INT_MIN), max(INT_MAX) {}; | ||||
| }; | ||||
| 
 | ||||
| typedef std::map<t_config_option_key,ConfigOptionDef> t_optiondef_map; | ||||
|  |  | |||
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							|  | @ -4,7 +4,7 @@ use strict; | |||
| use warnings; | ||||
| 
 | ||||
| use Slic3r::XS; | ||||
| use Test::More tests => 70; | ||||
| use Test::More tests => 76; | ||||
| 
 | ||||
| foreach my $config (Slic3r::Config->new, Slic3r::Config::Print->new) { | ||||
|     $config->set('layer_height', 0.3); | ||||
|  | @ -74,6 +74,12 @@ foreach my $config (Slic3r::Config->new, Slic3r::Config::Print->new) { | |||
|     $config->set_deserialize('wipe', '0,1,1'); | ||||
|     is_deeply $config->get('wipe'), [0,1,1], 'deserialize bools'; | ||||
|      | ||||
|     $config->set('post_process', ['foo','bar']); | ||||
|     is_deeply $config->get('post_process'), ['foo','bar'], 'set/get strings'; | ||||
|     is $config->serialize('post_process'), 'foo;bar', 'serialize strings'; | ||||
|     $config->set_deserialize('post_process', 'bar;baz'); | ||||
|     is_deeply $config->get('post_process'), ['bar','baz'], 'deserialize strings'; | ||||
|      | ||||
|     is_deeply [ sort @{$config->get_keys} ], [ sort keys %{$config->as_hash} ], 'get_keys and as_hash'; | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -70,6 +70,8 @@ print_config_def() | |||
|                 opt_type = "i"; | ||||
|             } else if (optdef->type == coString) { | ||||
|                 opt_type = "s"; | ||||
|             } else if (optdef->type == coStrings) { | ||||
|                 opt_type = "s@"; | ||||
|             } else if (optdef->type == coPoint || optdef->type == coPoints) { | ||||
|                 opt_type = "point"; | ||||
|             } else if (optdef->type == coBool || optdef->type == coBools) { | ||||
|  | @ -120,7 +122,7 @@ print_config_def() | |||
|                 av_fill(av, optdef->enum_values.size()-1); | ||||
|                 for (std::vector<std::string>::iterator it = optdef->enum_values.begin(); it != optdef->enum_values.end(); ++it) | ||||
|                     av_store(av, it - optdef->enum_values.begin(), newSVpvn(it->c_str(), it->length())); | ||||
|                 (void)hv_stores( hv, "enum_values", newRV_noinc((SV*)av) ); | ||||
|                 (void)hv_stores( hv, "values", newRV_noinc((SV*)av) ); | ||||
|             } | ||||
|              | ||||
|             // enum_labels | ||||
|  | @ -129,7 +131,7 @@ print_config_def() | |||
|                 av_fill(av, optdef->enum_labels.size()-1); | ||||
|                 for (std::vector<std::string>::iterator it = optdef->enum_labels.begin(); it != optdef->enum_labels.end(); ++it) | ||||
|                     av_store(av, it - optdef->enum_labels.begin(), newSVpvn(it->c_str(), it->length())); | ||||
|                 (void)hv_stores( hv, "enum_labels", newRV_noinc((SV*)av) ); | ||||
|                 (void)hv_stores( hv, "labels", newRV_noinc((SV*)av) ); | ||||
|             } | ||||
|              | ||||
|             (void)hv_stores( hv, "default", config.get(opt_key) ); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Alessandro Ranellucci
						Alessandro Ranellucci