[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) { for (auto& it : *CmdVars) {
auto cmdVar = static_cast<ICommandVar*>(it.second); auto cmdVar = static_cast<ICommandVar*>(it.second);
if (result.count(cmdVar->GetName())) { if (result.count(cmdVar->name())) {
cmdVar->LoadFromLaunchOptions(&result); cmdVar->LoadFromLaunchOptions(&result);
} }
} }
for (auto& it : *ConfigVars) { for (auto& it : *ConfigVars) {
auto configVar = static_cast<IConfigVar*>(it.second); auto configVar = static_cast<IConfigVar*>(it.second);
if (result.count(configVar->GetName())) { if (result.count(configVar->name())) {
configVar->LoadFromLaunchOptions(&result); configVar->LoadFromLaunchOptions(&result);
} }
} }

View File

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

View File

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