Config: Add a few helper functions for repetitive tasks

Getting and setting configuration from the base config layer are common
and repetitive tasks. This commit adds some simpler to use functions to
make the new system easier to work with.

Config::Get and Config::Set are intended to make switching from
SConfig a bit less painful. They always operate on the main system.

Example usage:

    // before
    auto base_layer = Config::GetLayer(Config::LayerType::Base);
    auto core = base_layer->GetOrCreateSection(Config::System::Main, "Core");
    u8 language;
    core->Get("Language", &language, 0);
    SetData("IPL.LNG", language);

    // now
    auto base_layer = Config::GetLayer(Config::LayerType::Base);
    auto core = base_layer->GetOrCreateSection(Config::System::Main, "Core");
    SetData("IPL.LNG", core->Get<u8>("Language", 0));

    // or simply
    SetData("IPL.LNG", Config::Get<u8>("Core", "Language", 0));
This commit is contained in:
Léo Lam 2017-02-16 10:59:22 +01:00
parent 7bd34ac0b5
commit abe6f8766a
1 changed files with 24 additions and 0 deletions

View File

@ -91,6 +91,14 @@ public:
bool Get(const std::string& key, float* value, float defaultValue = 0.0f) const; bool Get(const std::string& key, float* value, float defaultValue = 0.0f) const;
bool Get(const std::string& key, double* value, double defaultValue = 0.0) const; bool Get(const std::string& key, double* value, double defaultValue = 0.0) const;
template <typename T>
T Get(const std::string& key, const T& default_value) const
{
T value;
Get(key, value, default_value);
return value;
}
// Section chunk // Section chunk
void SetLines(const std::vector<std::string>& lines); void SetLines(const std::vector<std::string>& lines);
// XXX: Add to recursive layer // XXX: Add to recursive layer
@ -187,6 +195,22 @@ void AddConfigChangedCallback(ConfigChangedCallback func);
void Load(); void Load();
void Save(); void Save();
// Often used functions for getting or setting configuration on the base layer for the main system
template <typename T>
T Get(const std::string& section_name, const std::string& key, const T& default_value)
{
auto base_layer = GetLayer(Config::LayerType::Base);
return base_layer->GetOrCreateSection(Config::System::Main, section_name)
->Get(key, default_value);
}
template <typename T>
void Set(const std::string& section_name, const std::string& key, const T& value)
{
auto base_layer = GetLayer(Config::LayerType::Base);
base_layer->GetOrCreateSection(Config::System::Main, section_name)->Set(key, value);
}
void Init(); void Init();
void Shutdown(); void Shutdown();