ConfigBase with option template to do the type conversion

of resolved ConfigOption.
This commit is contained in:
bubnikv 2017-11-09 10:05:37 +01:00
parent 71b58e24a9
commit e4614f301c

View file

@ -245,7 +245,8 @@ public:
ConfigOptionFloat() : ConfigOptionSingle<double>(0) {} ConfigOptionFloat() : ConfigOptionSingle<double>(0) {}
explicit ConfigOptionFloat(double _value) : ConfigOptionSingle<double>(_value) {} explicit ConfigOptionFloat(double _value) : ConfigOptionSingle<double>(_value) {}
ConfigOptionType type() const override { return coFloat; } static ConfigOptionType static_type() { return coFloat; }
ConfigOptionType type() const override { return static_type(); }
double getFloat() const override { return this->value; } double getFloat() const override { return this->value; }
ConfigOption* clone() const override { return new ConfigOptionFloat(*this); } ConfigOption* clone() const override { return new ConfigOptionFloat(*this); }
bool operator==(const ConfigOptionFloat &rhs) const { return this->value == rhs.value; } bool operator==(const ConfigOptionFloat &rhs) const { return this->value == rhs.value; }
@ -279,7 +280,8 @@ public:
explicit ConfigOptionFloats(size_t n, double value) : ConfigOptionVector<double>(n, value) {} explicit ConfigOptionFloats(size_t n, double value) : ConfigOptionVector<double>(n, value) {}
explicit ConfigOptionFloats(std::initializer_list<double> il) : ConfigOptionVector<double>(std::move(il)) {} explicit ConfigOptionFloats(std::initializer_list<double> il) : ConfigOptionVector<double>(std::move(il)) {}
ConfigOptionType type() const override { return coFloats; } static ConfigOptionType static_type() { return coFloats; }
ConfigOptionType type() const override { return static_type(); }
ConfigOption* clone() const override { return new ConfigOptionFloats(*this); } ConfigOption* clone() const override { return new ConfigOptionFloats(*this); }
bool operator==(const ConfigOptionFloats &rhs) const { return this->values == rhs.values; } bool operator==(const ConfigOptionFloats &rhs) const { return this->values == rhs.values; }
@ -334,7 +336,8 @@ public:
explicit ConfigOptionInt(int value) : ConfigOptionSingle<int>(value) {} explicit ConfigOptionInt(int value) : ConfigOptionSingle<int>(value) {}
explicit ConfigOptionInt(double _value) : ConfigOptionSingle<int>(int(floor(_value + 0.5))) {} explicit ConfigOptionInt(double _value) : ConfigOptionSingle<int>(int(floor(_value + 0.5))) {}
ConfigOptionType type() const override { return coInt; } static ConfigOptionType static_type() { return coInt; }
ConfigOptionType type() const override { return static_type(); }
int getInt() const override { return this->value; } int getInt() const override { return this->value; }
void setInt(int val) { this->value = val; } void setInt(int val) { this->value = val; }
ConfigOption* clone() const override { return new ConfigOptionInt(*this); } ConfigOption* clone() const override { return new ConfigOptionInt(*this); }
@ -369,7 +372,8 @@ public:
explicit ConfigOptionInts(size_t n, int value) : ConfigOptionVector<int>(n, value) {} explicit ConfigOptionInts(size_t n, int value) : ConfigOptionVector<int>(n, value) {}
explicit ConfigOptionInts(std::initializer_list<int> il) : ConfigOptionVector<int>(std::move(il)) {} explicit ConfigOptionInts(std::initializer_list<int> il) : ConfigOptionVector<int>(std::move(il)) {}
ConfigOptionType type() const override { return coInts; } static ConfigOptionType static_type() { return coInts; }
ConfigOptionType type() const override { return static_type(); }
ConfigOption* clone() const override { return new ConfigOptionInts(*this); } ConfigOption* clone() const override { return new ConfigOptionInts(*this); }
ConfigOptionInts& operator=(const ConfigOption *opt) { this->set(opt); return *this; } ConfigOptionInts& operator=(const ConfigOption *opt) { this->set(opt); return *this; }
bool operator==(const ConfigOptionInts &rhs) const { return this->values == rhs.values; } bool operator==(const ConfigOptionInts &rhs) const { return this->values == rhs.values; }
@ -417,7 +421,8 @@ public:
ConfigOptionString() : ConfigOptionSingle<std::string>("") {} ConfigOptionString() : ConfigOptionSingle<std::string>("") {}
explicit ConfigOptionString(const std::string &value) : ConfigOptionSingle<std::string>(value) {} explicit ConfigOptionString(const std::string &value) : ConfigOptionSingle<std::string>(value) {}
ConfigOptionType type() const override { return coString; } static ConfigOptionType static_type() { return coString; }
ConfigOptionType type() const override { return static_type(); }
ConfigOption* clone() const override { return new ConfigOptionString(*this); } ConfigOption* clone() const override { return new ConfigOptionString(*this); }
ConfigOptionString& operator=(const ConfigOption *opt) { this->set(opt); return *this; } ConfigOptionString& operator=(const ConfigOption *opt) { this->set(opt); return *this; }
bool operator==(const ConfigOptionString &rhs) const { return this->value == rhs.value; } bool operator==(const ConfigOptionString &rhs) const { return this->value == rhs.value; }
@ -442,7 +447,8 @@ public:
explicit ConfigOptionStrings(size_t n, const std::string &value) : ConfigOptionVector<std::string>(n, value) {} explicit ConfigOptionStrings(size_t n, const std::string &value) : ConfigOptionVector<std::string>(n, value) {}
explicit ConfigOptionStrings(std::initializer_list<std::string> il) : ConfigOptionVector<std::string>(std::move(il)) {} explicit ConfigOptionStrings(std::initializer_list<std::string> il) : ConfigOptionVector<std::string>(std::move(il)) {}
ConfigOptionType type() const override { return coStrings; } static ConfigOptionType static_type() { return coStrings; }
ConfigOptionType type() const override { return static_type(); }
ConfigOption* clone() const override { return new ConfigOptionStrings(*this); } ConfigOption* clone() const override { return new ConfigOptionStrings(*this); }
ConfigOptionStrings& operator=(const ConfigOption *opt) { this->set(opt); return *this; } ConfigOptionStrings& operator=(const ConfigOption *opt) { this->set(opt); return *this; }
bool operator==(const ConfigOptionStrings &rhs) const { return this->values == rhs.values; } bool operator==(const ConfigOptionStrings &rhs) const { return this->values == rhs.values; }
@ -471,7 +477,8 @@ public:
ConfigOptionPercent() : ConfigOptionFloat(0) {} ConfigOptionPercent() : ConfigOptionFloat(0) {}
explicit ConfigOptionPercent(double _value) : ConfigOptionFloat(_value) {} explicit ConfigOptionPercent(double _value) : ConfigOptionFloat(_value) {}
ConfigOptionType type() const override { return coPercent; } static ConfigOptionType static_type() { return coPercent; }
ConfigOptionType type() const override { return static_type(); }
ConfigOption* clone() const override { return new ConfigOptionPercent(*this); } ConfigOption* clone() const override { return new ConfigOptionPercent(*this); }
ConfigOptionPercent& operator=(const ConfigOption *opt) { this->set(opt); return *this; } ConfigOptionPercent& operator=(const ConfigOption *opt) { this->set(opt); return *this; }
bool operator==(const ConfigOptionPercent &rhs) const { return this->value == rhs.value; } bool operator==(const ConfigOptionPercent &rhs) const { return this->value == rhs.value; }
@ -503,7 +510,8 @@ public:
explicit ConfigOptionPercents(size_t n, double value) : ConfigOptionFloats(n, value) {} explicit ConfigOptionPercents(size_t n, double value) : ConfigOptionFloats(n, value) {}
explicit ConfigOptionPercents(std::initializer_list<double> il) : ConfigOptionFloats(std::move(il)) {} explicit ConfigOptionPercents(std::initializer_list<double> il) : ConfigOptionFloats(std::move(il)) {}
ConfigOptionType type() const override { return coPercents; } static ConfigOptionType static_type() { return coPercents; }
ConfigOptionType type() const override { return static_type(); }
ConfigOption* clone() const override { return new ConfigOptionPercents(*this); } ConfigOption* clone() const override { return new ConfigOptionPercents(*this); }
ConfigOptionPercents& operator=(const ConfigOption *opt) { this->set(opt); return *this; } ConfigOptionPercents& operator=(const ConfigOption *opt) { this->set(opt); return *this; }
bool operator==(const ConfigOptionPercents &rhs) const { return this->values == rhs.values; } bool operator==(const ConfigOptionPercents &rhs) const { return this->values == rhs.values; }
@ -556,7 +564,8 @@ public:
ConfigOptionFloatOrPercent() : ConfigOptionPercent(0), percent(false) {} ConfigOptionFloatOrPercent() : ConfigOptionPercent(0), percent(false) {}
explicit ConfigOptionFloatOrPercent(double _value, bool _percent) : ConfigOptionPercent(_value), percent(_percent) {} explicit ConfigOptionFloatOrPercent(double _value, bool _percent) : ConfigOptionPercent(_value), percent(_percent) {}
ConfigOptionType type() const override { return coFloatOrPercent; } static ConfigOptionType static_type() { return coFloatOrPercent; }
ConfigOptionType type() const override { return static_type(); }
ConfigOption* clone() const override { return new ConfigOptionFloatOrPercent(*this); } ConfigOption* clone() const override { return new ConfigOptionFloatOrPercent(*this); }
ConfigOptionFloatOrPercent& operator=(const ConfigOption *opt) { this->set(opt); return *this; } ConfigOptionFloatOrPercent& operator=(const ConfigOption *opt) { this->set(opt); return *this; }
bool operator==(const ConfigOptionFloatOrPercent &rhs) const bool operator==(const ConfigOptionFloatOrPercent &rhs) const
@ -596,7 +605,8 @@ public:
ConfigOptionPoint() : ConfigOptionSingle<Pointf>(Pointf(0,0)) {} ConfigOptionPoint() : ConfigOptionSingle<Pointf>(Pointf(0,0)) {}
explicit ConfigOptionPoint(const Pointf &value) : ConfigOptionSingle<Pointf>(value) {} explicit ConfigOptionPoint(const Pointf &value) : ConfigOptionSingle<Pointf>(value) {}
ConfigOptionType type() const override { return coPoint; } static ConfigOptionType static_type() { return coPoint; }
ConfigOptionType type() const override { return static_type(); }
ConfigOption* clone() const override { return new ConfigOptionPoint(*this); } ConfigOption* clone() const override { return new ConfigOptionPoint(*this); }
ConfigOptionPoint& operator=(const ConfigOption *opt) { this->set(opt); return *this; } ConfigOptionPoint& operator=(const ConfigOption *opt) { this->set(opt); return *this; }
bool operator==(const ConfigOptionPoint &rhs) const { return this->value == rhs.value; } bool operator==(const ConfigOptionPoint &rhs) const { return this->value == rhs.value; }
@ -629,7 +639,8 @@ public:
explicit ConfigOptionPoints(size_t n, const Pointf &value) : ConfigOptionVector<Pointf>(n, value) {} explicit ConfigOptionPoints(size_t n, const Pointf &value) : ConfigOptionVector<Pointf>(n, value) {}
explicit ConfigOptionPoints(std::initializer_list<Pointf> il) : ConfigOptionVector<Pointf>(std::move(il)) {} explicit ConfigOptionPoints(std::initializer_list<Pointf> il) : ConfigOptionVector<Pointf>(std::move(il)) {}
ConfigOptionType type() const override { return coPoints; } static ConfigOptionType static_type() { return coPoints; }
ConfigOptionType type() const override { return static_type(); }
ConfigOption* clone() const override { return new ConfigOptionPoints(*this); } ConfigOption* clone() const override { return new ConfigOptionPoints(*this); }
ConfigOptionPoints& operator=(const ConfigOption *opt) { this->set(opt); return *this; } ConfigOptionPoints& operator=(const ConfigOption *opt) { this->set(opt); return *this; }
bool operator==(const ConfigOptionPoints &rhs) const { return this->values == rhs.values; } bool operator==(const ConfigOptionPoints &rhs) const { return this->values == rhs.values; }
@ -685,7 +696,8 @@ public:
ConfigOptionBool() : ConfigOptionSingle<bool>(false) {} ConfigOptionBool() : ConfigOptionSingle<bool>(false) {}
explicit ConfigOptionBool(bool _value) : ConfigOptionSingle<bool>(_value) {} explicit ConfigOptionBool(bool _value) : ConfigOptionSingle<bool>(_value) {}
ConfigOptionType type() const override { return coBool; } static ConfigOptionType static_type() { return coBool; }
ConfigOptionType type() const override { return static_type(); }
bool getBool() const override { return this->value; } bool getBool() const override { return this->value; }
ConfigOption* clone() const override { return new ConfigOptionBool(*this); } ConfigOption* clone() const override { return new ConfigOptionBool(*this); }
ConfigOptionBool& operator=(const ConfigOption *opt) { this->set(opt); return *this; } ConfigOptionBool& operator=(const ConfigOption *opt) { this->set(opt); return *this; }
@ -711,7 +723,8 @@ public:
explicit ConfigOptionBools(size_t n, bool value) : ConfigOptionVector<unsigned char>(n, (unsigned char)value) {} explicit ConfigOptionBools(size_t n, bool value) : ConfigOptionVector<unsigned char>(n, (unsigned char)value) {}
explicit ConfigOptionBools(std::initializer_list<bool> il) { values.reserve(il.size()); for (bool b : il) values.emplace_back((unsigned char)b); } explicit ConfigOptionBools(std::initializer_list<bool> il) { values.reserve(il.size()); for (bool b : il) values.emplace_back((unsigned char)b); }
ConfigOptionType type() const override { return coBools; } static ConfigOptionType static_type() { return coBools; }
ConfigOptionType type() const override { return static_type(); }
ConfigOption* clone() const override { return new ConfigOptionBools(*this); } ConfigOption* clone() const override { return new ConfigOptionBools(*this); }
ConfigOptionBools& operator=(const ConfigOption *opt) { this->set(opt); return *this; } ConfigOptionBools& operator=(const ConfigOption *opt) { this->set(opt); return *this; }
bool operator==(const ConfigOptionBools &rhs) const { return this->values == rhs.values; } bool operator==(const ConfigOptionBools &rhs) const { return this->values == rhs.values; }
@ -771,7 +784,8 @@ public:
ConfigOptionEnum() : ConfigOptionSingle<T>(static_cast<T>(0)) {} ConfigOptionEnum() : ConfigOptionSingle<T>(static_cast<T>(0)) {}
explicit ConfigOptionEnum(T _value) : ConfigOptionSingle<T>(_value) {} explicit ConfigOptionEnum(T _value) : ConfigOptionSingle<T>(_value) {}
ConfigOptionType type() const override { return coEnum; } static ConfigOptionType static_type() { return coEnum; }
ConfigOptionType type() const override { return static_type(); }
ConfigOption* clone() const override { return new ConfigOptionEnum<T>(*this); } ConfigOption* clone() const override { return new ConfigOptionEnum<T>(*this); }
ConfigOptionEnum<T>& operator=(const ConfigOption *opt) { this->set(opt); return *this; } ConfigOptionEnum<T>& operator=(const ConfigOption *opt) { this->set(opt); return *this; }
bool operator==(const ConfigOptionEnum<T> &rhs) const { return this->value == rhs.value; } bool operator==(const ConfigOptionEnum<T> &rhs) const { return this->value == rhs.value; }
@ -833,7 +847,8 @@ public:
const t_config_enum_values* keys_map; const t_config_enum_values* keys_map;
ConfigOptionType type() const override { return coEnum; } static ConfigOptionType static_type() { return coEnum; }
ConfigOptionType type() const override { return static_type(); }
ConfigOption* clone() const override { return new ConfigOptionEnumGeneric(*this); } ConfigOption* clone() const override { return new ConfigOptionEnumGeneric(*this); }
ConfigOptionEnumGeneric& operator=(const ConfigOption *opt) { this->set(opt); return *this; } ConfigOptionEnumGeneric& operator=(const ConfigOption *opt) { this->set(opt); return *this; }
bool operator==(const ConfigOptionEnumGeneric &rhs) const { return this->value == rhs.value; } bool operator==(const ConfigOptionEnumGeneric &rhs) const { return this->value == rhs.value; }
@ -990,6 +1005,16 @@ public:
{ return const_cast<ConfigBase*>(this)->option(opt_key, false); } { return const_cast<ConfigBase*>(this)->option(opt_key, false); }
ConfigOption* option(const t_config_option_key &opt_key, bool create = false) ConfigOption* option(const t_config_option_key &opt_key, bool create = false)
{ return this->optptr(opt_key, create); } { return this->optptr(opt_key, create); }
template<typename TYPE>
TYPE* option(const t_config_option_key &opt_key, bool create = false)
{
ConfigOption *opt = this->optptr(opt_key, create);
assert(opt == nullptr || opt->type() == typename TYPE::static_type());
return (opt == nullptr || opt->type() == typename TYPE::static_type()) ? nullptr : static_cast<TYPE*>(opt);
}
template<typename TYPE>
const TYPE* option(const t_config_option_key &opt_key) const
{ return const_cast<ConfigBase*>(this)->option<TYPE>(opt_key, false); }
// Apply all keys of other ConfigBase defined by this->def() to this ConfigBase. // Apply all keys of other ConfigBase defined by this->def() to this ConfigBase.
// An UnknownOptionException is thrown in case some option keys of other are not defined by this->def(), // An UnknownOptionException is thrown in case some option keys of other are not defined by this->def(),
// or this ConfigBase is of a StaticConfig type and it does not support some of the keys, and ignore_nonexistent is not set. // or this ConfigBase is of a StaticConfig type and it does not support some of the keys, and ignore_nonexistent is not set.
@ -1091,6 +1116,9 @@ public:
int& opt_int(const t_config_option_key &opt_key, unsigned int idx) { return dynamic_cast<ConfigOptionInts*>(this->option(opt_key))->get_at(idx); } int& opt_int(const t_config_option_key &opt_key, unsigned int idx) { return dynamic_cast<ConfigOptionInts*>(this->option(opt_key))->get_at(idx); }
const int opt_int(const t_config_option_key &opt_key, unsigned int idx) const { return dynamic_cast<const ConfigOptionInts*>(this->option(opt_key))->get_at(idx); } const int opt_int(const t_config_option_key &opt_key, unsigned int idx) const { return dynamic_cast<const ConfigOptionInts*>(this->option(opt_key))->get_at(idx); }
bool opt_bool(const t_config_option_key &opt_key) const { return dynamic_cast<const ConfigOptionBool*>(this->option(opt_key))->value != 0; }
bool opt_bool(const t_config_option_key &opt_key, unsigned int idx) const { return dynamic_cast<const ConfigOptionBools*>(this->option(opt_key))->get_at(idx) != 0; }
protected: protected:
DynamicConfig() {} DynamicConfig() {}