type casts for config manager
This commit is contained in:
parent
f72214f090
commit
0cd4bcd926
|
@ -32,9 +32,6 @@ bool Configs::load(const std::string& fileName)
|
|||
{
|
||||
m_fileName = fileName;
|
||||
|
||||
if(!g_resources.fileExists(fileName))
|
||||
return false;
|
||||
|
||||
std::stringstream fin;
|
||||
if(!g_resources.loadFile(fileName, fin))
|
||||
return false;
|
||||
|
@ -46,12 +43,12 @@ bool Configs::load(const std::string& fileName)
|
|||
|
||||
for(auto it = doc.begin(); it != doc.end(); ++it)
|
||||
m_confsMap[yamlRead<std::string>(it.first())] = yamlRead<std::string>(it.second());
|
||||
|
||||
return true;
|
||||
} catch (YAML::Exception& e) {
|
||||
flogError("ERROR: Malformed config file: %s", e.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Configs::save()
|
||||
|
|
|
@ -27,33 +27,22 @@
|
|||
|
||||
#include <prerequisites.h>
|
||||
|
||||
class ConfigValueProxy {
|
||||
public:
|
||||
ConfigValueProxy(const std::string& value) : value(value) { }
|
||||
struct ConfigValueProxy {
|
||||
operator std::string() const { return convert_cast<std::string>(value); }
|
||||
operator float() const { return convert_cast<float>(value); }
|
||||
operator int() const { return convert_cast<int>(value); }
|
||||
operator bool() const { return convert_cast<bool>(value); }
|
||||
|
||||
private:
|
||||
std::string value;
|
||||
};
|
||||
|
||||
class Configs
|
||||
{
|
||||
public:
|
||||
Configs() { }
|
||||
|
||||
/// Read configuration file and parse all settings to memory
|
||||
bool load(const std::string& fileName);
|
||||
|
||||
/// Dump all settings to configuration file
|
||||
void save();
|
||||
|
||||
template<class T>
|
||||
void setValue(const std::string& key, const T& value) { m_confsMap[key] = convert_cast<std::string>(value); }
|
||||
|
||||
ConfigValueProxy get(const std::string& key) { return ConfigValueProxy(m_confsMap[key]); }
|
||||
template<class T> void setValue(const std::string& key, const T& value) { m_confsMap[key] = convert_cast<std::string>(value); }
|
||||
ConfigValueProxy get(const std::string& key) { return ConfigValueProxy{m_confsMap[key]}; }
|
||||
|
||||
private:
|
||||
std::string m_fileName;
|
||||
|
|
Loading…
Reference in New Issue