1
#ifndef slic3r_Config_hpp_
2
#define slic3r_Config_hpp_
17
typedef std::string t_config_option_key;
18
typedef std::vector<std::string> t_config_option_keys;
22
virtual ~ConfigOption() {};
23
virtual std::string serialize() const = 0;
24
virtual bool deserialize(std::string str) = 0;
25
virtual int getInt() const { return 0; };
26
virtual void setInt(int val) {};
30
class ConfigOptionVector
33
virtual ~ConfigOptionVector() {};
34
std::vector<T> values;
36
T get_at(size_t i) const {
38
return this->values.at(i);
39
} catch (const std::out_of_range& oor) {
40
return this->values.front();
45
class ConfigOptionFloat : public ConfigOption
48
double value; // use double instead of float for preserving compatibility with values coming from Perl
49
ConfigOptionFloat() : value(0) {};
51
operator double() const { return this->value; };
53
std::string serialize() const {
54
std::ostringstream ss;
59
bool deserialize(std::string str) {
60
this->value = ::atof(str.c_str());
65
class ConfigOptionFloats : public ConfigOption, public ConfigOptionVector<double>
69
std::string serialize() const {
70
std::ostringstream ss;
71
for (std::vector<double>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
72
if (it - this->values.begin() != 0) ss << ",";
78
bool deserialize(std::string str) {
80
std::istringstream is(str);
82
while (std::getline(is, item_str, ',')) {
83
this->values.push_back(::atof(item_str.c_str()));
89
class ConfigOptionInt : public ConfigOption
93
ConfigOptionInt() : value(0) {};
95
operator int() const { return this->value; };
96
int getInt() const { return this->value; };
97
void setInt(int val) { this->value = val; };
99
std::string serialize() const {
100
std::ostringstream ss;
105
bool deserialize(std::string str) {
106
this->value = ::atoi(str.c_str());
111
class ConfigOptionInts : public ConfigOption, public ConfigOptionVector<int>
115
std::string serialize() const {
116
std::ostringstream ss;
117
for (std::vector<int>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
118
if (it - this->values.begin() != 0) ss << ",";
124
bool deserialize(std::string str) {
125
this->values.clear();
126
std::istringstream is(str);
127
std::string item_str;
128
while (std::getline(is, item_str, ',')) {
129
this->values.push_back(::atoi(item_str.c_str()));
135
class ConfigOptionString : public ConfigOption
139
ConfigOptionString() : value("") {};
141
operator std::string() const { return this->value; };
143
std::string serialize() const {
144
std::string str = this->value;
148
while ((pos = str.find("\n", pos)) != std::string::npos || (pos = str.find("\r", pos)) != std::string::npos) {
149
str.replace(pos, 1, "\\n");
150
pos += 2; // length of "\\n"
156
bool deserialize(std::string str) {
159
while ((pos = str.find("\\n", pos)) != std::string::npos) {
160
str.replace(pos, 2, "\n");
161
pos += 1; // length of "\n"
169
// semicolon-separated strings
170
class ConfigOptionStrings : public ConfigOption, public ConfigOptionVector<std::string>
174
std::string serialize() const {
175
std::ostringstream ss;
176
for (std::vector<std::string>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
177
if (it - this->values.begin() != 0) ss << ";";
183
bool deserialize(std::string str) {
184
this->values.clear();
185
std::istringstream is(str);
186
std::string item_str;
187
while (std::getline(is, item_str, ';')) {
188
this->values.push_back(item_str);
194
class ConfigOptionPercent : public ConfigOption
198
ConfigOptionPercent() : value(0) {};
200
double get_abs_value(double ratio_over) const {
201
return ratio_over * this->value / 100;
204
std::string serialize() const {
205
std::ostringstream ss;
207
std::string s(ss.str());
212
bool deserialize(std::string str) {
213
// don't try to parse the trailing % since it's optional
214
int res = sscanf(str.c_str(), "%lf", &this->value);
219
class ConfigOptionFloatOrPercent : public ConfigOption
224
ConfigOptionFloatOrPercent() : value(0), percent(false) {};
226
double get_abs_value(double ratio_over) const {
228
return ratio_over * this->value / 100;
234
std::string serialize() const {
235
std::ostringstream ss;
237
std::string s(ss.str());
238
if (this->percent) s += "%";
242
bool deserialize(std::string str) {
243
if (str.find_first_of("%") != std::string::npos) {
244
int res = sscanf(str.c_str(), "%lf%%", &this->value);
245
if (res == 0) return false;
246
this->percent = true;
248
this->value = ::atof(str.c_str());
249
this->percent = false;
255
class ConfigOptionPoint : public ConfigOption
259
ConfigOptionPoint() : point(Pointf(0,0)) {};
261
operator Pointf() const { return this->point; };
263
std::string serialize() const {
264
std::ostringstream ss;
271
bool deserialize(std::string str) {
272
if (strncmp(str.c_str(), "0x", 2) == 0) {
274
int res = sscanf(str.c_str()+2, "%lf", &this->point.y);
277
int res = sscanf(str.c_str(), "%lf%*1[,x]%lf", &this->point.x, &this->point.y);
283
class ConfigOptionPoints : public ConfigOption, public ConfigOptionVector<Pointf>
287
std::string serialize() const {
288
std::ostringstream ss;
289
for (Pointfs::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
290
if (it - this->values.begin() != 0) ss << ",";
298
bool deserialize(std::string str) {
299
std::vector<Pointf> values;
300
std::istringstream is(str);
301
std::string point_str;
302
while (std::getline(is, point_str, ',')) {
304
if (strncmp(point_str.c_str(), "0x", 2) == 0) {
305
// if string starts with "0x", only apply sscanf() to the second coordinate
306
// otherwise it would parse the string as a hex number
308
int res = sscanf(point_str.c_str()+2, "%lf", &point.y);
309
if (res != 1) return false;
311
int res = sscanf(point_str.c_str(), "%lfx%lf", &point.x, &point.y);
312
if (res != 2) return false;
314
values.push_back(point);
316
this->values = values;
321
class ConfigOptionBool : public ConfigOption
325
ConfigOptionBool() : value(false) {};
327
operator bool() const { return this->value; };
329
std::string serialize() const {
330
return std::string(this->value ? "1" : "0");
333
bool deserialize(std::string str) {
334
this->value = (str.compare("1") == 0);
339
class ConfigOptionBools : public ConfigOption, public ConfigOptionVector<bool>
343
std::string serialize() const {
344
std::ostringstream ss;
345
for (std::vector<bool>::const_iterator it = this->values.begin(); it != this->values.end(); ++it) {
346
if (it - this->values.begin() != 0) ss << ",";
347
ss << (*it ? "1" : "0");
352
bool deserialize(std::string str) {
353
this->values.clear();
354
std::istringstream is(str);
355
std::string item_str;
356
while (std::getline(is, item_str, ',')) {
357
this->values.push_back(item_str.compare("1") == 0);
363
typedef std::map<std::string,int> t_config_enum_values;
366
class ConfigOptionEnum : public ConfigOption
371
operator T() const { return this->value; };
373
std::string serialize() const {
374
t_config_enum_values enum_keys_map = ConfigOptionEnum<T>::get_enum_values();
375
for (t_config_enum_values::iterator it = enum_keys_map.begin(); it != enum_keys_map.end(); ++it) {
376
if (it->second == static_cast<int>(this->value)) return it->first;
381
bool deserialize(std::string str) {
382
t_config_enum_values enum_keys_map = ConfigOptionEnum<T>::get_enum_values();
383
if (enum_keys_map.count(str) == 0) return false;
384
this->value = static_cast<T>(enum_keys_map[str]);
388
static t_config_enum_values get_enum_values();
391
/* We use this one in DynamicConfig objects, otherwise it's better to use
392
the specialized ConfigOptionEnum<T> containers. */
393
class ConfigOptionEnumGeneric : public ConfigOption
397
t_config_enum_values* keys_map;
399
operator int() const { return this->value; };
401
std::string serialize() const {
402
for (t_config_enum_values::iterator it = this->keys_map->begin(); it != this->keys_map->end(); ++it) {
403
if (it->second == this->value) return it->first;
408
bool deserialize(std::string str) {
409
if (this->keys_map->count(str) == 0) return false;
410
this->value = (*this->keys_map)[str];
415
enum ConfigOptionType {
431
class ConfigOptionDef
434
ConfigOptionType type;
436
std::string full_label;
437
std::string category;
439
std::string sidetext;
441
t_config_option_key ratio_over;
449
std::vector<t_config_option_key> aliases;
450
std::vector<t_config_option_key> shortcut;
451
std::vector<std::string> enum_values;
452
std::vector<std::string> enum_labels;
453
t_config_enum_values enum_keys_map;
455
ConfigOptionDef() : multiline(false), full_width(false), readonly(false),
456
height(-1), width(-1), min(INT_MIN), max(INT_MAX) {};
459
typedef std::map<t_config_option_key,ConfigOptionDef> t_optiondef_map;
464
t_optiondef_map* def;
466
ConfigBase() : def(NULL) {};
467
bool has(const t_config_option_key opt_key);
468
virtual ConfigOption* option(const t_config_option_key opt_key, bool create = false) = 0;
469
virtual const ConfigOption* option(const t_config_option_key opt_key) const = 0;
470
virtual void keys(t_config_option_keys *keys) const = 0;
471
void apply(const ConfigBase &other, bool ignore_nonexistent = false);
472
std::string serialize(const t_config_option_key opt_key);
473
bool set_deserialize(const t_config_option_key opt_key, std::string str);
474
double get_abs_value(const t_config_option_key opt_key);
475
double get_abs_value(const t_config_option_key opt_key, double ratio_over);
479
SV* get(t_config_option_key opt_key);
480
SV* get_at(t_config_option_key opt_key, size_t i);
481
bool set(t_config_option_key opt_key, SV* value);
482
bool set_deserialize(const t_config_option_key opt_key, SV* str);
486
class DynamicConfig : public ConfigBase
490
DynamicConfig(const DynamicConfig& other);
491
DynamicConfig& operator= (DynamicConfig other);
492
void swap(DynamicConfig &other);
494
template<class T> T* opt(const t_config_option_key opt_key, bool create = false);
495
ConfigOption* option(const t_config_option_key opt_key, bool create = false);
496
const ConfigOption* option(const t_config_option_key opt_key) const;
497
void keys(t_config_option_keys *keys) const;
498
void erase(const t_config_option_key opt_key);
501
typedef std::map<t_config_option_key,ConfigOption*> t_options_map;
502
t_options_map options;
505
class StaticConfig : public ConfigBase
508
void keys(t_config_option_keys *keys) const;
509
virtual ConfigOption* option(const t_config_option_key opt_key, bool create = false) = 0;
510
const ConfigOption* option(const t_config_option_key opt_key) const;
513
bool set(t_config_option_key opt_key, SV* value);