mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 19:50:05 +00:00
Inheriting GeneralSettings from SettingsCategory, making generic read/write JSON functions
This commit is contained in:
parent
78739cd627
commit
c93e776137
7 changed files with 184 additions and 96 deletions
|
|
@ -36,42 +36,28 @@ public:
|
|||
gridHeight_.SetMaximum(2);
|
||||
mapboxApiKey_.SetValidator([](const std::string& value)
|
||||
{ return !value.empty(); });
|
||||
|
||||
RegisterVariables({&debugEnabled_,
|
||||
&defaultRadarSite_,
|
||||
&fontSizes_,
|
||||
&gridWidth_,
|
||||
&gridHeight_,
|
||||
&mapboxApiKey_});
|
||||
|
||||
SetDefaults();
|
||||
}
|
||||
|
||||
~GeneralSettingsImpl() {}
|
||||
|
||||
void SetDefaults()
|
||||
{
|
||||
for (auto& variable : variables_)
|
||||
{
|
||||
variable->SetValueToDefault();
|
||||
}
|
||||
}
|
||||
|
||||
SettingsVariable<bool> debugEnabled_ {"debug_enabled"};
|
||||
SettingsVariable<std::string> defaultRadarSite_ {"default_radar_site"};
|
||||
SettingsContainer<std::vector<std::int64_t>> fontSizes_ {"font_sizes"};
|
||||
SettingsVariable<std::int64_t> gridWidth_ {"grid_width"};
|
||||
SettingsVariable<std::int64_t> gridHeight_ {"grid_height"};
|
||||
SettingsVariable<std::string> mapboxApiKey_ {"mapbox_api_key"};
|
||||
|
||||
std::vector<SettingsVariableBase*> variables_;
|
||||
|
||||
void
|
||||
RegisterVariables(std::initializer_list<SettingsVariableBase*> variables);
|
||||
};
|
||||
|
||||
GeneralSettings::GeneralSettings() : p(std::make_unique<GeneralSettingsImpl>())
|
||||
GeneralSettings::GeneralSettings() :
|
||||
SettingsCategory("general"), p(std::make_unique<GeneralSettingsImpl>())
|
||||
{
|
||||
RegisterVariables({&p->debugEnabled_,
|
||||
&p->defaultRadarSite_,
|
||||
&p->fontSizes_,
|
||||
&p->gridWidth_,
|
||||
&p->gridHeight_,
|
||||
&p->mapboxApiKey_});
|
||||
SetDefaults();
|
||||
}
|
||||
GeneralSettings::~GeneralSettings() = default;
|
||||
|
||||
|
|
@ -79,12 +65,6 @@ GeneralSettings::GeneralSettings(GeneralSettings&&) noexcept = default;
|
|||
GeneralSettings&
|
||||
GeneralSettings::operator=(GeneralSettings&&) noexcept = default;
|
||||
|
||||
void GeneralSettingsImpl::RegisterVariables(
|
||||
std::initializer_list<SettingsVariableBase*> variables)
|
||||
{
|
||||
variables_.insert(variables_.end(), variables);
|
||||
}
|
||||
|
||||
bool GeneralSettings::debug_enabled() const
|
||||
{
|
||||
return p->debugEnabled_.GetValue();
|
||||
|
|
@ -115,57 +95,6 @@ std::string GeneralSettings::mapbox_api_key() const
|
|||
return p->mapboxApiKey_.GetValue();
|
||||
}
|
||||
|
||||
boost::json::value GeneralSettings::ToJson() const
|
||||
{
|
||||
boost::json::object json;
|
||||
|
||||
for (auto& variable : p->variables_)
|
||||
{
|
||||
variable->WriteValue(json);
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
std::shared_ptr<GeneralSettings> GeneralSettings::Create()
|
||||
{
|
||||
std::shared_ptr<GeneralSettings> generalSettings =
|
||||
std::make_shared<GeneralSettings>();
|
||||
|
||||
return generalSettings;
|
||||
}
|
||||
|
||||
std::shared_ptr<GeneralSettings>
|
||||
GeneralSettings::Load(const boost::json::value* json, bool& jsonDirty)
|
||||
{
|
||||
std::shared_ptr<GeneralSettings> generalSettings =
|
||||
std::make_shared<GeneralSettings>();
|
||||
|
||||
if (json != nullptr && json->is_object())
|
||||
{
|
||||
for (auto& variable : generalSettings->p->variables_)
|
||||
{
|
||||
jsonDirty |= !variable->ReadValue(json->as_object());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (json == nullptr)
|
||||
{
|
||||
logger_->warn("Key is not present, resetting to defaults");
|
||||
}
|
||||
else if (!json->is_object())
|
||||
{
|
||||
logger_->warn("Invalid json, resetting to defaults");
|
||||
}
|
||||
|
||||
generalSettings->p->SetDefaults();
|
||||
jsonDirty = true;
|
||||
}
|
||||
|
||||
return generalSettings;
|
||||
}
|
||||
|
||||
bool operator==(const GeneralSettings& lhs, const GeneralSettings& rhs)
|
||||
{
|
||||
return (lhs.p->debugEnabled_ == rhs.p->debugEnabled_ &&
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue