type casts for config manager

This commit is contained in:
Eduardo Bart 2011-05-19 21:03:15 -03:00
parent f72214f090
commit 0cd4bcd926
2 changed files with 5 additions and 19 deletions

View File

@ -32,9 +32,6 @@ bool Configs::load(const std::string& fileName)
{ {
m_fileName = fileName; m_fileName = fileName;
if(!g_resources.fileExists(fileName))
return false;
std::stringstream fin; std::stringstream fin;
if(!g_resources.loadFile(fileName, fin)) if(!g_resources.loadFile(fileName, fin))
return false; return false;
@ -46,12 +43,12 @@ bool Configs::load(const std::string& fileName)
for(auto it = doc.begin(); it != doc.end(); ++it) for(auto it = doc.begin(); it != doc.end(); ++it)
m_confsMap[yamlRead<std::string>(it.first())] = yamlRead<std::string>(it.second()); m_confsMap[yamlRead<std::string>(it.first())] = yamlRead<std::string>(it.second());
return true;
} catch (YAML::Exception& e) { } catch (YAML::Exception& e) {
flogError("ERROR: Malformed config file: %s", e.what()); flogError("ERROR: Malformed config file: %s", e.what());
return false; return false;
} }
return true;
} }
void Configs::save() void Configs::save()

View File

@ -27,33 +27,22 @@
#include <prerequisites.h> #include <prerequisites.h>
class ConfigValueProxy { struct ConfigValueProxy {
public:
ConfigValueProxy(const std::string& value) : value(value) { }
operator std::string() const { return convert_cast<std::string>(value); } operator std::string() const { return convert_cast<std::string>(value); }
operator float() const { return convert_cast<float>(value); } operator float() const { return convert_cast<float>(value); }
operator int() const { return convert_cast<int>(value); } operator int() const { return convert_cast<int>(value); }
operator bool() const { return convert_cast<bool>(value); } operator bool() const { return convert_cast<bool>(value); }
private:
std::string value; std::string value;
}; };
class Configs class Configs
{ {
public: public:
Configs() { }
/// Read configuration file and parse all settings to memory
bool load(const std::string& fileName); bool load(const std::string& fileName);
/// Dump all settings to configuration file
void save(); void save();
template<class T> template<class T> void setValue(const std::string& key, const T& value) { m_confsMap[key] = convert_cast<std::string>(value); }
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]}; }
ConfigValueProxy get(const std::string& key) { return ConfigValueProxy(m_confsMap[key]); }
private: private:
std::string m_fileName; std::string m_fileName;