Config: Flatten structures

Originally, Layer contained a std::map of Sections, which containted a std::map
containing the (key, value) pairs. Here we flattern this structure so that only
one std::map is required, reducing the number of indirections required and
vastly simplifying the code.
This commit is contained in:
MerryMage
2017-10-29 19:11:15 +00:00
parent f3b52c07d7
commit 4c24629b95
13 changed files with 220 additions and 551 deletions

View File

@@ -77,9 +77,9 @@ class BaseConfigLayerLoader final : public Config::ConfigLayerLoader
{
public:
BaseConfigLayerLoader() : ConfigLayerLoader(Config::LayerType::Base) {}
void Load(Config::Layer* config_layer) override
void Load(Config::Layer* layer) override
{
LoadFromSYSCONF(config_layer);
LoadFromSYSCONF(layer);
for (const auto& system : system_to_ini)
{
IniFile ini;
@@ -89,55 +89,62 @@ public:
for (const auto& section : system_sections)
{
const std::string section_name = section.GetName();
Config::Section* config_section =
config_layer->GetOrCreateSection(system.first, section_name);
const IniFile::Section::SectionMap& section_map = section.GetValues();
for (const auto& value : section_map)
config_section->Set(value.first, value.second);
{
const Config::ConfigLocation location{system.first, section_name, value.first};
layer->Set(location, value.second);
}
}
}
}
void Save(Config::Layer* config_layer) override
void Save(Config::Layer* layer) override
{
const Config::LayerMap& sections = config_layer->GetLayerMap();
for (const auto& system : sections)
{
if (system.first == Config::System::SYSCONF)
{
SaveToSYSCONF(config_layer->GetLayer());
continue;
}
SaveToSYSCONF(layer->GetLayer());
auto mapping = system_to_ini.find(system.first);
if (mapping == system_to_ini.end())
std::map<Config::System, IniFile> inis;
for (const auto& system : system_to_ini)
{
inis[system.first].Load(File::GetUserPath(system.second));
}
for (const auto& config : layer->GetLayerMap())
{
const Config::ConfigLocation& location = config.first;
const std::optional<std::string>& value = config.second;
// Done by SaveToSYSCONF
if (location.system == Config::System::SYSCONF)
continue;
auto ini = inis.find(location.system);
if (ini == inis.end())
{
ERROR_LOG(COMMON, "Config can't map system '%s' to an INI file!",
Config::GetSystemName(system.first).c_str());
Config::GetSystemName(location.system).c_str());
continue;
}
IniFile ini;
ini.Load(File::GetUserPath(mapping->second));
if (!IsSettingSaveable(location))
continue;
for (const auto& section : system.second)
if (value)
{
const std::string section_name = section->GetName();
const Config::SectionValueMap& section_values = section->GetValues();
IniFile::Section* ini_section = ini.GetOrCreateSection(section_name);
for (const auto& value : section_values)
{
if (!IsSettingSaveable({system.first, section->GetName(), value.first}))
continue;
ini_section->Set(value.first, value.second);
}
IniFile::Section* ini_section = ini->second.GetOrCreateSection(location.section);
ini_section->Set(location.key, *value);
}
else
{
ini->second.DeleteKey(location.section, location.key);
}
}
ini.Save(File::GetUserPath(mapping->second));
for (const auto& system : system_to_ini)
{
inis[system.first].Save(File::GetUserPath(system.second));
}
}
@@ -153,13 +160,10 @@ private:
std::visit(
[&](auto& info) {
const std::string key = info.location.section + "." + info.location.key;
auto* section =
layer->GetOrCreateSection(Config::System::SYSCONF, info.location.section);
if (setting.type == SysConf::Entry::Type::Long)
section->Set(info.location.key, sysconf.GetData<u32>(key, info.default_value));
layer->Set(info.location, sysconf.GetData<u32>(key, info.default_value));
else if (setting.type == SysConf::Entry::Type::Byte)
section->Set(info.location.key, sysconf.GetData<u8>(key, info.default_value));
layer->Set(info.location, sysconf.GetData<u8>(key, info.default_value));
},
setting.config_info);
}