[Core] Implement ability to find ConfigVar based on a reference to its wrapped value

This commit is contained in:
Satori 2020-08-30 18:00:36 +01:00
parent 838870caa4
commit e990505609
2 changed files with 21 additions and 2 deletions

View File

@ -185,7 +185,7 @@ void Config::LoadGameConfig(const std::string_view title_id) {
}
}
cvar::IConfigVar* Config::FindConfigVar(const std::string& name) {
cvar::IConfigVar* Config::FindConfigVarByName(const std::string& name) {
const auto it = config_vars_.find(name);
if (it != config_vars_.end()) {
return it->second.get();

View File

@ -59,7 +59,13 @@ class Config {
/**
* Find a config variable for a given config field
*/
cvar::IConfigVar* FindConfigVar(const std::string& name);
cvar::IConfigVar* FindConfigVarByName(const std::string& name);
/**
* Find a config variable that contains a pointer to a provided reference
*/
template <typename T>
cvar::ConfigVar<T>* FindConfigVar(const T& value);
/**
* Find a command variable for a given name
@ -104,6 +110,19 @@ class Config {
cxxopts::Options options_;
};
template <typename T>
cvar::ConfigVar<T>* Config::FindConfigVar(const T& value) {
for (const auto& [name, var] : config_vars_) {
auto typed_var = dynamic_cast<cvar::ConfigVar<T>*>(var.get());
if (typed_var) {
if (std::addressof(value) == typed_var->current_value()) {
return typed_var;
}
}
}
return nullptr;
}
template <typename T>
cvar::ConfigVar<T>* Config::RegisterConfigVar(cvar::ConfigVar<T>* var) {
const std::string& name = var->name();