From f72214f0906d52c568106c5881a1634df8f9a6eb Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Thu, 19 May 2011 20:56:27 -0300 Subject: [PATCH] type casts for config manager --- src/framework/core/configs.cpp | 79 ++-------------------------------- src/framework/core/configs.h | 24 +++++++---- src/main.cpp | 6 +-- 3 files changed, 21 insertions(+), 88 deletions(-) diff --git a/src/framework/core/configs.cpp b/src/framework/core/configs.cpp index fa9ad26a..81d9afba 100644 --- a/src/framework/core/configs.cpp +++ b/src/framework/core/configs.cpp @@ -40,17 +40,12 @@ bool Configs::load(const std::string& fileName) return false; try { - YAML::Parser parser(fin); - YAML::Node doc; + YAML::Parser parser(fin); parser.GetNextDocument(doc); - for(auto it = doc.begin(); it != doc.end(); ++it) { - std::string key, value; - it.first() >> key; - it.second() >> value; - m_confsMap[key] = value; - } + for(auto it = doc.begin(); it != doc.end(); ++it) + m_confsMap[yamlRead(it.first())] = yamlRead(it.second()); } catch (YAML::Exception& e) { flogError("ERROR: Malformed config file: %s", e.what()); return false; @@ -68,71 +63,3 @@ void Configs::save() } } -void Configs::setValue(const std::string &key, const std::string &value) -{ - m_confsMap[key] = value; -} - -void Configs::setValue(const std::string &key, const char *value) -{ - m_confsMap[key] = value; -} - -void Configs::setValue(const std::string &key, int value) -{ - setValue(key, convert_cast(value)); -} - -void Configs::setValue(const std::string &key, float value) -{ - setValue(key, convert_cast(value)); -} - -void Configs::setValue(const std::string &key, bool value) -{ - if(value) - setValue(key,"true"); - else - setValue(key,"false"); -} - -const std::string &Configs::getString(const std::string &key) const -{ - auto it = m_confsMap.find(key); - if(it == m_confsMap.end()) { - flogWarning("WARNING: Config value %s not found", key.c_str()); - static std::string emptystr; - return emptystr; - } - return it->second; -} - -float Configs::getFloat(const std::string &key) const -{ - auto it = m_confsMap.find(key); - if(it == m_confsMap.end()) { - flogWarning("WARNING: Config value %s not found", key.c_str()); - return 0; - } - return convert_cast(it->second); -} - -bool Configs::getBoolean(const std::string &key) const -{ - auto it = m_confsMap.find(key); - if(it == m_confsMap.end()) { - flogWarning("WARNING: Config value %s not found", key.c_str()); - return 0; - } - return (it->second == "true"); -} - -int Configs::getInteger(const std::string &key) const -{ - auto it = m_confsMap.find(key); - if(it == m_confsMap.end()) { - flogWarning("WARNING: Config value %s not found", key.c_str()); - return 0; - } - return convert_cast(it->second); -} diff --git a/src/framework/core/configs.h b/src/framework/core/configs.h index ea27b3c3..c58cb43f 100644 --- a/src/framework/core/configs.h +++ b/src/framework/core/configs.h @@ -27,6 +27,18 @@ #include +class ConfigValueProxy { +public: + ConfigValueProxy(const std::string& value) : value(value) { } + operator std::string() const { return convert_cast(value); } + operator float() const { return convert_cast(value); } + operator int() const { return convert_cast(value); } + operator bool() const { return convert_cast(value); } + +private: + std::string value; +}; + class Configs { public: @@ -38,16 +50,10 @@ public: /// Dump all settings to configuration file void save(); - void setValue(const std::string &key, const std::string &value); - void setValue(const std::string &key, const char *value); - void setValue(const std::string &key, float value); - void setValue(const std::string &key, bool value); - void setValue(const std::string &key, int value); + template + void setValue(const std::string& key, const T& value) { m_confsMap[key] = convert_cast(value); } - const std::string& getString(const std::string &key) const; - float getFloat(const std::string &key) const; - bool getBoolean(const std::string &key) const; - int getInteger(const std::string &key) const; + ConfigValueProxy get(const std::string& key) { return ConfigValueProxy(m_confsMap[key]); } private: std::string m_fileName; diff --git a/src/main.cpp b/src/main.cpp index 253d5510..7736ea88 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -112,10 +112,10 @@ int main(int argc, const char *argv[]) logInfo("OTClient 0.2.0"); // create the window - Platform::createWindow(g_configs.getInteger("window x"), g_configs.getInteger("window y"), - g_configs.getInteger("window width"), g_configs.getInteger("window height"), + Platform::createWindow(g_configs.get("window x"), g_configs.get("window y"), + g_configs.get("window width"), g_configs.get("window height"), 550, 450, - g_configs.getBoolean("window maximized")); + g_configs.get("window maximized")); Platform::setWindowTitle("OTClient"); //Platform::setVsync();