[Base/Core] Bring cvar/config code in line with the style guide.

This commit is contained in:
gibbed 2019-08-04 02:17:22 -05:00
parent 7ba460552c
commit f360053198
3 changed files with 61 additions and 63 deletions

View File

@ -37,13 +37,13 @@ void ParseLaunchArguments(int argc, char** argv) {
}
for (auto& it : *CmdVars) {
auto cmdVar = static_cast<ICommandVar*>(it.second);
if (result.count(cmdVar->GetName())) {
if (result.count(cmdVar->name())) {
cmdVar->LoadFromLaunchOptions(&result);
}
}
for (auto& it : *ConfigVars) {
auto configVar = static_cast<IConfigVar*>(it.second);
if (result.count(configVar->GetName())) {
if (result.count(configVar->name())) {
configVar->LoadFromLaunchOptions(&result);
}
}

View File

@ -10,8 +10,8 @@ namespace cvar {
class ICommandVar {
public:
virtual ~ICommandVar() = default;
virtual std::string GetName() = 0;
virtual std::string GetDescription() = 0;
virtual const std::string& name() const = 0;
virtual const std::string& description() const = 0;
virtual void UpdateValue() = 0;
virtual void AddToLaunchOptions(cxxopts::Options* options) = 0;
virtual void LoadFromLaunchOptions(cxxopts::ParseResult* result) = 0;
@ -19,9 +19,9 @@ class ICommandVar {
class IConfigVar : virtual public ICommandVar {
public:
virtual std::string GetCategory() = 0;
virtual bool GetIsTransient() = 0;
virtual std::string GetConfigValue() = 0;
virtual const std::string& category() const = 0;
virtual bool is_transient() const = 0;
virtual std::string config_value() const = 0;
virtual void LoadConfigValue(std::shared_ptr<cpptoml::base> result) = 0;
virtual void LoadGameConfigValue(std::shared_ptr<cpptoml::base> result) = 0;
};
@ -29,18 +29,18 @@ class IConfigVar : virtual public ICommandVar {
template <class T>
class CommandVar : virtual public ICommandVar {
public:
CommandVar<T>(const char* name, T* defaultValue, const char* description);
std::string GetName() override;
std::string GetDescription() override;
CommandVar<T>(const char* name, T* default_value, const char* description);
const std::string& name() const override;
const std::string& description() const override;
void AddToLaunchOptions(cxxopts::Options* options) override;
void LoadFromLaunchOptions(cxxopts::ParseResult* result) override;
T* GetCurrentValue() { return currentValue_; }
T* current_value() { return current_value_; }
protected:
std::string name_;
T defaultValue_;
T* currentValue_;
std::unique_ptr<T> commandLineValue_;
T default_value_;
T* current_value_;
std::unique_ptr<T> commandline_value_;
std::string description_;
T Convert(std::string val);
static std::string ToString(T val);
@ -53,11 +53,11 @@ class CommandVar : virtual public ICommandVar {
template <class T>
class ConfigVar : public CommandVar<T>, virtual public IConfigVar {
public:
ConfigVar<T>(const char* name, T* defaultValue, const char* description,
ConfigVar<T>(const char* name, T* default_value, const char* description,
const char* category, bool is_transient);
std::string GetConfigValue() override;
std::string GetCategory() override;
bool GetIsTransient() override;
std::string config_value() const override;
const std::string& category() const override;
bool is_transient() const override;
void AddToLaunchOptions(cxxopts::Options* options) override;
void LoadConfigValue(std::shared_ptr<cpptoml::base> result) override;
void LoadGameConfigValue(std::shared_ptr<cpptoml::base> result) override;
@ -67,32 +67,31 @@ class ConfigVar : public CommandVar<T>, virtual public IConfigVar {
private:
std::string category_;
bool is_transient_;
std::unique_ptr<T> configValue_ = nullptr;
std::unique_ptr<T> gameConfigValue_ = nullptr;
std::unique_ptr<T> config_value_ = nullptr;
std::unique_ptr<T> game_config_value_ = nullptr;
void UpdateValue() override;
};
#pragma warning(pop)
template <class T>
std::string CommandVar<T>::GetName() {
const std::string& CommandVar<T>::name() const {
return name_;
}
template <class T>
std::string CommandVar<T>::GetDescription() {
const std::string& CommandVar<T>::description() const {
return description_;
}
template <class T>
void CommandVar<T>::AddToLaunchOptions(cxxopts::Options* options) {
options->add_options()(this->name_, this->description_, cxxopts::value<T>());
options->add_options()(name_, description_, cxxopts::value<T>());
}
template <class T>
void ConfigVar<T>::AddToLaunchOptions(cxxopts::Options* options) {
options->add_options(category_)(this->name_, this->description_,
cxxopts::value<T>());
options->add_options(category_)(name_, description_, cxxopts::value<T>());
}
template <class T>
void CommandVar<T>::LoadFromLaunchOptions(cxxopts::ParseResult* result) {
T value = (*result)[this->name_].template as<T>();
T value = (*result)[name_].template as<T>();
SetCommandLineValue(value);
}
template <class T>
@ -104,32 +103,32 @@ void ConfigVar<T>::LoadGameConfigValue(std::shared_ptr<cpptoml::base> result) {
SetGameConfigValue(*cpptoml::get_impl<T>(result));
}
template <class T>
CommandVar<T>::CommandVar(const char* name, T* defaultValue,
CommandVar<T>::CommandVar(const char* name, T* default_value,
const char* description)
: name_(name),
defaultValue_(*defaultValue),
default_value_(*default_value),
description_(description),
currentValue_(defaultValue) {}
current_value_(default_value) {}
template <class T>
ConfigVar<T>::ConfigVar(const char* name, T* defaultValue,
ConfigVar<T>::ConfigVar(const char* name, T* default_value,
const char* description, const char* category,
bool is_transient)
: CommandVar<T>(name, defaultValue, description),
: CommandVar<T>(name, default_value, description),
category_(category),
is_transient_(is_transient) {}
template <class T>
void CommandVar<T>::UpdateValue() {
if (this->commandLineValue_) return this->SetValue(*this->commandLineValue_);
return this->SetValue(defaultValue_);
if (commandline_value_) return SetValue(*commandline_value_);
return SetValue(default_value_);
}
template <class T>
void ConfigVar<T>::UpdateValue() {
if (this->commandLineValue_) return this->SetValue(*this->commandLineValue_);
if (this->gameConfigValue_) return this->SetValue(*this->gameConfigValue_);
if (this->configValue_) return this->SetValue(*this->configValue_);
return this->SetValue(this->defaultValue_);
if (commandline_value_) return SetValue(*commandline_value_);
if (game_config_value_) return SetValue(*game_config_value_);
if (config_value_) return SetValue(*config_value_);
return SetValue(default_value_);
}
template <class T>
T CommandVar<T>::Convert(std::string val) {
@ -156,35 +155,35 @@ std::string CommandVar<T>::ToString(T val) {
template <class T>
void CommandVar<T>::SetValue(T val) {
*currentValue_ = val;
*current_value_ = val;
}
template <class T>
std::string ConfigVar<T>::GetCategory() {
const std::string& ConfigVar<T>::category() const {
return category_;
}
template <class T>
bool ConfigVar<T>::GetIsTransient() {
bool ConfigVar<T>::is_transient() const {
return is_transient_;
}
template <class T>
std::string ConfigVar<T>::GetConfigValue() {
if (this->configValue_) return this->ToString(*this->configValue_);
return this->ToString(this->defaultValue_);
std::string ConfigVar<T>::config_value() const {
if (config_value_) return ToString(*config_value_);
return ToString(default_value_);
}
template <class T>
void CommandVar<T>::SetCommandLineValue(const T val) {
this->commandLineValue_ = std::make_unique<T>(val);
this->UpdateValue();
commandline_value_ = std::make_unique<T>(val);
UpdateValue();
}
template <class T>
void ConfigVar<T>::SetConfigValue(T val) {
this->configValue_ = std::make_unique<T>(val);
this->UpdateValue();
config_value_ = std::make_unique<T>(val);
UpdateValue();
}
template <class T>
void ConfigVar<T>::SetGameConfigValue(T val) {
this->gameConfigValue_ = std::make_unique<T>(val);
this->UpdateValue();
game_config_value_ = std::make_unique<T>(val);
UpdateValue();
}
extern std::map<std::string, ICommandVar*>* CmdVars;
@ -192,11 +191,11 @@ extern std::map<std::string, IConfigVar*>* ConfigVars;
inline void AddConfigVar(IConfigVar* cv) {
if (!ConfigVars) ConfigVars = new std::map<std::string, IConfigVar*>();
ConfigVars->insert(std::pair<std::string, IConfigVar*>(cv->GetName(), cv));
ConfigVars->insert(std::pair<std::string, IConfigVar*>(cv->name(), cv));
}
inline void AddCommandVar(ICommandVar* cv) {
if (!CmdVars) CmdVars = new std::map<std::string, ICommandVar*>();
CmdVars->insert(std::pair<std::string, ICommandVar*>(cv->GetName(), cv));
CmdVars->insert(std::pair<std::string, ICommandVar*>(cv->name(), cv));
}
void ParseLaunchArguments(int argc, char** argv);

View File

@ -12,9 +12,9 @@ std::wstring config_folder;
std::wstring config_path;
bool sortCvar(cvar::IConfigVar* a, cvar::IConfigVar* b) {
if (a->GetCategory() < b->GetCategory()) return true;
if (a->GetCategory() > b->GetCategory()) return false;
if (a->GetName() < b->GetName()) return true;
if (a->category() < b->category()) return true;
if (a->category() > b->category()) return false;
if (a->name() < b->name()) return true;
return false;
}
@ -32,7 +32,7 @@ void ReadConfig(const std::wstring& file_path) {
const auto config = ParseConfig(xe::to_string(file_path));
for (auto& it : *cvar::ConfigVars) {
auto configVar = static_cast<cvar::IConfigVar*>(it.second);
auto configKey = configVar->GetCategory() + "." + configVar->GetName();
auto configKey = configVar->category() + "." + configVar->name();
if (config->contains_qualified(configKey)) {
configVar->LoadConfigValue(config->get_qualified(configKey));
}
@ -44,7 +44,7 @@ void ReadGameConfig(std::wstring file_path) {
const auto config = ParseConfig(xe::to_string(file_path));
for (auto& it : *cvar::ConfigVars) {
auto configVar = static_cast<cvar::IConfigVar*>(it.second);
auto configKey = configVar->GetCategory() + "." + configVar->GetName();
auto configKey = configVar->category() + "." + configVar->name();
if (config->contains_qualified(configKey)) {
configVar->LoadGameConfigValue(config->get_qualified(configKey));
}
@ -61,21 +61,20 @@ void SaveConfig() {
std::ostringstream output;
std::string lastCategory;
for (auto configVar : vars) {
if (configVar->GetIsTransient()) {
if (configVar->is_transient()) {
continue;
}
if (lastCategory != configVar->GetCategory()) {
if (lastCategory != configVar->category()) {
if (!lastCategory.empty()) {
output << std::endl;
}
lastCategory = configVar->GetCategory();
lastCategory = configVar->category();
output << xe::format_string("[%s]\n", lastCategory.c_str());
}
output << std::left << std::setw(40) << std::setfill(' ')
<< xe::format_string("%s = %s", configVar->GetName().c_str(),
configVar->GetConfigValue().c_str());
output << xe::format_string("\t# %s\n",
configVar->GetDescription().c_str());
<< xe::format_string("%s = %s", configVar->name().c_str(),
configVar->config_value().c_str());
output << xe::format_string("\t# %s\n", configVar->description().c_str());
}
if (xe::filesystem::PathExists(config_path)) {