Refactor of ConfigDef / ConfigOptionDef / PrintConfigDef /

Field and other UI elements to handle enums in a generic way.
Needs a review by @yusanka
This commit is contained in:
Vojtech Bubnik 2023-02-01 15:31:10 +01:00 committed by YuSanka
parent a1b0188e3a
commit d58c8b1bbd
14 changed files with 627 additions and 586 deletions

View file

@ -268,7 +268,7 @@ ConfigOption* ConfigOptionDef::create_empty_option() const
// case coPoint3s: return new ConfigOptionPoint3s();
case coBool: return new ConfigOptionBool();
case coBools: return new ConfigOptionBools();
case coEnum: return new ConfigOptionEnumGeneric(this->enum_keys_map);
case coEnum: return new ConfigOptionEnumGeneric(this->enum_def->m_enum_keys_map);
default: throw ConfigurationError(std::string("Unknown option type for option ") + this->label);
}
}
@ -279,7 +279,7 @@ ConfigOption* ConfigOptionDef::create_default_option() const
if (this->default_value)
return (this->default_value->type() == coEnum) ?
// Special case: For a DynamicConfig, convert a templated enum to a generic enum.
new ConfigOptionEnumGeneric(this->enum_keys_map, this->default_value->getInt()) :
new ConfigOptionEnumGeneric(this->enum_def->m_enum_keys_map, this->default_value->getInt()) :
this->default_value->clone();
return this->create_empty_option();
}
@ -303,6 +303,31 @@ ConfigOptionDef* ConfigDef::add_nullable(const t_config_option_key &opt_key, Con
return def;
}
void ConfigDef::finalize()
{
// Validate & finalize open & closed enums.
for (std::pair<const t_config_option_key, ConfigOptionDef> &kvp : options) {
ConfigOptionDef& def = kvp.second;
if (def.type == coEnum) {
assert(def.enum_def);
assert(def.enum_def->is_valid_closed_enum());
assert(def.gui_type != ConfigOptionDef::GUIType::i_enum_open &&
def.gui_type != ConfigOptionDef::GUIType::f_enum_open &&
def.gui_type != ConfigOptionDef::GUIType::select_open);
def.enum_def->finalize_closed_enum();
} else if (def.gui_type == ConfigOptionDef::GUIType::i_enum_open || def.gui_type == ConfigOptionDef::GUIType::f_enum_open ||
def.gui_type == ConfigOptionDef::GUIType::select_open) {
assert(def.enum_def);
assert(def.enum_def->is_valid_open_enum());
assert(def.gui_type != ConfigOptionDef::GUIType::i_enum_open || def.type == coInt || def.type == coInts);
assert(def.gui_type != ConfigOptionDef::GUIType::f_enum_open || def.type == coFloat || def.type == coPercent || def.type == coFloatOrPercent);
assert(def.gui_type != ConfigOptionDef::GUIType::select_open || def.type == coString || def.type == coStrings);
} else {
assert(! def.enum_def);
}
}
}
std::ostream& ConfigDef::print_cli_help(std::ostream& out, bool show_defaults, std::function<bool(const ConfigOptionDef &)> filter) const
{
// prepare a function for wrapping text
@ -378,8 +403,8 @@ std::ostream& ConfigDef::print_cli_help(std::ostream& out, bool show_defaults, s
descr += " (";
if (!def.sidetext.empty()) {
descr += def.sidetext + ", ";
} else if (!def.enum_values.empty()) {
descr += boost::algorithm::join(def.enum_values, ", ") + "; ";
} else if (def.enum_def->has_values()) {
descr += boost::algorithm::join(def.enum_def->values(), ", ") + "; ";
}
descr += "default: " + def.default_value->serialize() + ")";
}
@ -1142,7 +1167,7 @@ bool DynamicConfig::read_cli(int argc, const char* const argv[], t_config_option
}
const t_config_option_key &opt_key = it->second;
const ConfigOptionDef &optdef = this->def()->options.at(opt_key);
const ConfigOptionDef &optdef = *this->option_def(opt_key);
// If the option type expects a value and it was not already provided,
// look for it in the next token.