From bcbe5178de28447389981b7d66bebe415960a01c Mon Sep 17 00:00:00 2001 From: remi durand Date: Tue, 1 Jun 2021 17:30:32 +0200 Subject: [PATCH] deprecated use of (int) vs int32_t for config storage and add phony to cereal serialization for history storage. --- src/libslic3r/Config.cpp | 18 +++++----- src/libslic3r/Config.hpp | 77 +++++++++++++++++++++------------------- 2 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/libslic3r/Config.cpp b/src/libslic3r/Config.cpp index 9e93783e9..f664c91fd 100644 --- a/src/libslic3r/Config.cpp +++ b/src/libslic3r/Config.cpp @@ -502,7 +502,7 @@ std::string ConfigBase::opt_serialize(const t_config_option_key &opt_key) const return opt->serialize(); } -void ConfigBase::set(const std::string &opt_key, int value, bool create) +void ConfigBase::set(const std::string &opt_key, int32_t value, bool create) { ConfigOption *opt = this->option_throw(opt_key, create); switch (opt->type()) { @@ -930,7 +930,7 @@ bool DynamicConfig::read_cli(int argc, const char* const argv[], t_config_option opts[t] = oit.first; bool parse_options = true; - for (int i = 1; i < argc; ++ i) { + for (size_t i = 1; i < argc; ++ i) { std::string token = argv[i]; // Store non-option arguments in the provided vector. if (! parse_options || ! boost::starts_with(token, "-")) { @@ -1058,14 +1058,14 @@ t_config_option_keys StaticConfig::keys() const #include CEREAL_REGISTER_TYPE(Slic3r::ConfigOption) CEREAL_REGISTER_TYPE(Slic3r::ConfigOptionSingle) -CEREAL_REGISTER_TYPE(Slic3r::ConfigOptionSingle) +CEREAL_REGISTER_TYPE(Slic3r::ConfigOptionSingle) CEREAL_REGISTER_TYPE(Slic3r::ConfigOptionSingle) CEREAL_REGISTER_TYPE(Slic3r::ConfigOptionSingle) CEREAL_REGISTER_TYPE(Slic3r::ConfigOptionSingle) CEREAL_REGISTER_TYPE(Slic3r::ConfigOptionSingle) CEREAL_REGISTER_TYPE(Slic3r::ConfigOptionVectorBase) CEREAL_REGISTER_TYPE(Slic3r::ConfigOptionVector) -CEREAL_REGISTER_TYPE(Slic3r::ConfigOptionVector) +CEREAL_REGISTER_TYPE(Slic3r::ConfigOptionVector) CEREAL_REGISTER_TYPE(Slic3r::ConfigOptionVector) CEREAL_REGISTER_TYPE(Slic3r::ConfigOptionVector) CEREAL_REGISTER_TYPE(Slic3r::ConfigOptionVector) @@ -1094,23 +1094,23 @@ CEREAL_REGISTER_TYPE(Slic3r::ConfigBase) CEREAL_REGISTER_TYPE(Slic3r::DynamicConfig) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOption, Slic3r::ConfigOptionSingle) -CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOption, Slic3r::ConfigOptionSingle) +CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOption, Slic3r::ConfigOptionSingle) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOption, Slic3r::ConfigOptionSingle) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOption, Slic3r::ConfigOptionSingle) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOption, Slic3r::ConfigOptionSingle) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOption, Slic3r::ConfigOptionSingle) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOption, Slic3r::ConfigOptionVectorBase) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionVectorBase, Slic3r::ConfigOptionVector) -CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionVectorBase, Slic3r::ConfigOptionVector) +CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionVectorBase, Slic3r::ConfigOptionVector) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionVectorBase, Slic3r::ConfigOptionVector) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionVectorBase, Slic3r::ConfigOptionVector) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionVectorBase, Slic3r::ConfigOptionVector) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionSingle, Slic3r::ConfigOptionFloat) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionVector, Slic3r::ConfigOptionFloats) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionVector, Slic3r::ConfigOptionFloatsNullable) -CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionSingle, Slic3r::ConfigOptionInt) -CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionVector, Slic3r::ConfigOptionInts) -CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionVector, Slic3r::ConfigOptionIntsNullable) +CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionSingle, Slic3r::ConfigOptionInt) +CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionVector, Slic3r::ConfigOptionInts) +CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionVector, Slic3r::ConfigOptionIntsNullable) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionSingle, Slic3r::ConfigOptionString) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionVector, Slic3r::ConfigOptionStrings) CEREAL_REGISTER_POLYMORPHIC_RELATION(Slic3r::ConfigOptionFloat, Slic3r::ConfigOptionPercent) diff --git a/src/libslic3r/Config.hpp b/src/libslic3r/Config.hpp index 470ea3eb0..35010af25 100644 --- a/src/libslic3r/Config.hpp +++ b/src/libslic3r/Config.hpp @@ -230,10 +230,10 @@ public: virtual ConfigOption* clone() const = 0; // Set a value from a ConfigOption. The two options should be compatible. virtual void set(const ConfigOption *option) = 0; - virtual int getInt() const { throw BadOptionTypeException("Calling ConfigOption::getInt on a non-int ConfigOption"); } + virtual int32_t getInt() const { throw BadOptionTypeException("Calling ConfigOption::getInt on a non-int ConfigOption"); } virtual double getFloat() const { throw BadOptionTypeException("Calling ConfigOption::getFloat on a non-float ConfigOption"); } virtual bool getBool() const { throw BadOptionTypeException("Calling ConfigOption::getBool on a non-boolean ConfigOption"); } - virtual void setInt(int /* val */) { throw BadOptionTypeException("Calling ConfigOption::setInt on a non-int ConfigOption"); } + virtual void setInt(int32_t /* val */) { throw BadOptionTypeException("Calling ConfigOption::setInt on a non-int ConfigOption"); } virtual bool operator==(const ConfigOption &rhs) const = 0; bool operator!=(const ConfigOption &rhs) const { return ! (*this == rhs); } bool is_scalar() const { return (int(this->type()) & int(coVectorType)) == 0; } @@ -257,6 +257,9 @@ public: *this = *rhs; return true; } +private: + friend class cereal::access; + template void serialize(Archive& ar) { ar(this->phony); } }; typedef ConfigOption* ConfigOptionPtr; @@ -293,7 +296,7 @@ public: private: friend class cereal::access; - template void serialize(Archive & ar) { ar(this->value); } + template void serialize(Archive & ar) { ar(this->phony); ar(this->value); } }; // Value of a vector valued option (bools, ints, floats, strings, points) @@ -509,7 +512,7 @@ public: private: friend class cereal::access; - template void serialize(Archive & ar) { ar(this->values); } + template void serialize(Archive & ar) { ar(this->phony); ar(this->values); } }; class ConfigOptionFloat : public ConfigOptionSingle @@ -665,17 +668,17 @@ private: using ConfigOptionFloats = ConfigOptionFloatsTempl; using ConfigOptionFloatsNullable = ConfigOptionFloatsTempl; -class ConfigOptionInt : public ConfigOptionSingle +class ConfigOptionInt : public ConfigOptionSingle { public: - ConfigOptionInt() : ConfigOptionSingle(0) {} - explicit ConfigOptionInt(int value) : ConfigOptionSingle(value) {} - explicit ConfigOptionInt(double _value) : ConfigOptionSingle(int(floor(_value + 0.5))) {} + ConfigOptionInt() : ConfigOptionSingle(0) {} + explicit ConfigOptionInt(int32_t value) : ConfigOptionSingle(value) {} + explicit ConfigOptionInt(double _value) : ConfigOptionSingle(int32_t(floor(_value + 0.5))) {} static ConfigOptionType static_type() { return coInt; } ConfigOptionType type() const override { return static_type(); } - int getInt() const override { return this->value; } - void setInt(int val) override { this->value = val; } + int32_t getInt() const override { return this->value; } + void setInt(int32_t val) override { this->value = val; } ConfigOption* clone() const override { return new ConfigOptionInt(*this); } bool operator==(const ConfigOptionInt &rhs) const { return this->value == rhs.value; } @@ -702,16 +705,16 @@ public: private: friend class cereal::access; - template void serialize(Archive &ar) { ar(cereal::base_class>(this)); } + template void serialize(Archive &ar) { ar(cereal::base_class>(this)); } }; template -class ConfigOptionIntsTempl : public ConfigOptionVector +class ConfigOptionIntsTempl : public ConfigOptionVector { public: - ConfigOptionIntsTempl() : ConfigOptionVector() {} - explicit ConfigOptionIntsTempl(size_t n, int value) : ConfigOptionVector(n, value) {} - explicit ConfigOptionIntsTempl(std::initializer_list il) : ConfigOptionVector(std::move(il)) {} + ConfigOptionIntsTempl() : ConfigOptionVector() {} + explicit ConfigOptionIntsTempl(size_t n, int32_t value) : ConfigOptionVector(n, value) {} + explicit ConfigOptionIntsTempl(std::initializer_list il) : ConfigOptionVector(std::move(il)) {} static ConfigOptionType static_type() { return coInts; } ConfigOptionType type() const override { return static_type(); } @@ -721,7 +724,7 @@ public: // Could a special "nil" value be stored inside the vector, indicating undefined value? bool nullable() const override { return NULLABLE; } // Special "nil" value to be stored into the vector if this->supports_nil(). - static int nil_value() { return std::numeric_limits::max(); } + static int32_t nil_value() { return std::numeric_limits::max(); } // A scalar is nil, or all values of a vector are nil. bool is_nil() const override { for (auto v : this->values) if (v != nil_value()) return false; return true; } bool is_nil(size_t idx) const override { return this->values[idx] == nil_value(); } @@ -729,7 +732,7 @@ public: std::string serialize() const override { std::ostringstream ss; - for (const int &v : this->values) { + for (const int32_t &v : this->values) { if (&v != &this->values.front()) ss << ","; serialize_single_value(ss, v); @@ -741,7 +744,7 @@ public: { std::vector vv; vv.reserve(this->values.size()); - for (const int v : this->values) { + for (const int32_t v : this->values) { std::ostringstream ss; serialize_single_value(ss, v); vv.push_back(ss.str()); @@ -764,7 +767,7 @@ public: throw Slic3r::RuntimeError("Deserializing nil into a non-nullable object"); } else { std::istringstream iss(item_str); - int value; + int32_t value; iss >> value; this->values.push_back(value); } @@ -773,7 +776,7 @@ public: } private: - void serialize_single_value(std::ostringstream &ss, const int v) const { + void serialize_single_value(std::ostringstream &ss, const int32_t v) const { if (v == nil_value()) { if (NULLABLE) ss << "nil"; @@ -784,7 +787,7 @@ private: } friend class cereal::access; - template void serialize(Archive &ar) { ar(cereal::base_class>(this)); } + template void serialize(Archive &ar) { ar(cereal::base_class>(this)); } }; using ConfigOptionInts = ConfigOptionIntsTempl; @@ -1413,7 +1416,7 @@ using ConfigOptionBoolsNullable = ConfigOptionBoolsTempl; // Map from an enum integer value to an enum name. typedef std::vector t_config_enum_names; // Map from an enum name to an enum integer value. -typedef std::map t_config_enum_values; +typedef std::map t_config_enum_values; template class ConfigOptionEnum : public ConfigOptionSingle @@ -1428,7 +1431,7 @@ public: ConfigOption* clone() const override { return new ConfigOptionEnum(*this); } ConfigOptionEnum& operator=(const ConfigOption *opt) { this->set(opt); return *this; } bool operator==(const ConfigOptionEnum &rhs) const { return this->value == rhs.value; } - int getInt() const override { return (int)this->value; } + int32_t getInt() const override { return (int32_t)this->value; } bool operator==(const ConfigOption &rhs) const override { @@ -1450,8 +1453,8 @@ public: { // as names are static-initialized, it's thread safe static t_config_enum_names names = ConfigOptionEnum::create_enum_names(); - assert(static_cast(this->value) < int(names.size())); - return names[static_cast(this->value)]; + assert(static_cast(this->value) < int32_t(names.size())); + return names[static_cast(this->value)]; } bool deserialize(const std::string &str, bool append = false) override @@ -1462,7 +1465,7 @@ public: static bool has(T value) { - for (const std::pair &kvp : ConfigOptionEnum::get_enum_values()) + for (const std::pair &kvp : ConfigOptionEnum::get_enum_values()) if (kvp.second == value) return true; return false; @@ -1475,12 +1478,12 @@ public: if (names.empty()) { // Initialize the map. const t_config_enum_values &enum_keys_map = ConfigOptionEnum::get_enum_values(); - int cnt = 0; - for (const std::pair &kvp : enum_keys_map) + int32_t cnt = 0; + for (const std::pair &kvp : enum_keys_map) cnt = std::max(cnt, kvp.second); cnt += 1; names.assign(cnt, ""); - for (const std::pair &kvp : enum_keys_map) + for (const std::pair &kvp : enum_keys_map) names[kvp.second] = kvp.first; } return names; @@ -1506,7 +1509,7 @@ class ConfigOptionEnumGeneric : public ConfigOptionInt { public: ConfigOptionEnumGeneric(const t_config_enum_values* keys_map = nullptr) : keys_map(keys_map) {} - explicit ConfigOptionEnumGeneric(const t_config_enum_values* keys_map, int value) : ConfigOptionInt(value), keys_map(keys_map) {} + explicit ConfigOptionEnumGeneric(const t_config_enum_values* keys_map, int32_t value) : ConfigOptionInt(value), keys_map(keys_map) {} const t_config_enum_values* keys_map; @@ -1891,7 +1894,7 @@ public: // Conversion to string is always possible. void set(const std::string &opt_key, bool value, bool create = false) { this->option_throw(opt_key, create)->value = value; } - void set(const std::string &opt_key, int value, bool create = false); + void set(const std::string &opt_key, int32_t value, bool create = false); void set(const std::string &opt_key, double value, bool create = false); void set(const std::string &opt_key, const char *value, bool create = false) { this->option_throw(opt_key, create)->value = value; } @@ -1908,8 +1911,8 @@ public: SetDeserializeItem(const std::string &opt_key, const std::string &opt_value, bool append = false) : opt_key(opt_key), opt_value(opt_value), append(append) {} SetDeserializeItem(const char *opt_key, const bool value, bool append = false) : opt_key(opt_key), opt_value(value ? "1" : "0"), append(append) {} SetDeserializeItem(const std::string &opt_key, const bool value, bool append = false) : opt_key(opt_key), opt_value(value ? "1" : "0"), append(append) {} - SetDeserializeItem(const char *opt_key, const int value, bool append = false) : opt_key(opt_key), opt_value(std::to_string(value)), append(append) {} - SetDeserializeItem(const std::string &opt_key, const int value, bool append = false) : opt_key(opt_key), opt_value(std::to_string(value)), append(append) {} + SetDeserializeItem(const char *opt_key, const int32_t value, bool append = false) : opt_key(opt_key), opt_value(std::to_string(value)), append(append) {} + SetDeserializeItem(const std::string &opt_key, const int32_t value, bool append = false) : opt_key(opt_key), opt_value(std::to_string(value)), append(append) {} SetDeserializeItem(const char *opt_key, const float value, bool append = false) : opt_key(opt_key), opt_value(std::to_string(value)), append(append) {} SetDeserializeItem(const std::string &opt_key, const float value, bool append = false) : opt_key(opt_key), opt_value(std::to_string(value)), append(append) {} SetDeserializeItem(const char *opt_key, const double value, bool append = false) : opt_key(opt_key), opt_value(std::to_string(value)), append(append) {} @@ -2075,10 +2078,10 @@ public: double& opt_float(const t_config_option_key &opt_key, unsigned int idx) { return this->option(opt_key)->get_at(idx); } const double& opt_float(const t_config_option_key &opt_key, unsigned int idx) const { return dynamic_cast(this->option(opt_key))->get_at(idx); } - int& opt_int(const t_config_option_key &opt_key) { return this->option(opt_key)->value; } - int opt_int(const t_config_option_key &opt_key) const { return dynamic_cast(this->option(opt_key))->value; } - int& opt_int(const t_config_option_key &opt_key, unsigned int idx) { return this->option(opt_key)->get_at(idx); } - int opt_int(const t_config_option_key &opt_key, unsigned int idx) const { return dynamic_cast(this->option(opt_key))->get_at(idx); } + int32_t& opt_int(const t_config_option_key &opt_key) { return this->option(opt_key)->value; } + int32_t opt_int(const t_config_option_key &opt_key) const { return dynamic_cast(this->option(opt_key))->value; } + int32_t& opt_int(const t_config_option_key &opt_key, unsigned int idx) { return this->option(opt_key)->get_at(idx); } + int32_t opt_int(const t_config_option_key &opt_key, unsigned int idx) const { return dynamic_cast(this->option(opt_key))->get_at(idx); } template ENUM opt_enum(const t_config_option_key &opt_key) const {