type casts for config manager

master
Eduardo Bart 13 years ago
parent afc79a99e1
commit f72214f090

@ -40,17 +40,12 @@ bool Configs::load(const std::string& fileName)
return false; return false;
try { try {
YAML::Parser parser(fin);
YAML::Node doc; YAML::Node doc;
YAML::Parser parser(fin);
parser.GetNextDocument(doc); parser.GetNextDocument(doc);
for(auto it = doc.begin(); it != doc.end(); ++it) { for(auto it = doc.begin(); it != doc.end(); ++it)
std::string key, value; m_confsMap[yamlRead<std::string>(it.first())] = yamlRead<std::string>(it.second());
it.first() >> key;
it.second() >> value;
m_confsMap[key] = value;
}
} 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;
@ -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<std::string>(value));
}
void Configs::setValue(const std::string &key, float value)
{
setValue(key, convert_cast<std::string>(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<float>(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<int>(it->second);
}

@ -27,6 +27,18 @@
#include <prerequisites.h> #include <prerequisites.h>
class ConfigValueProxy {
public:
ConfigValueProxy(const std::string& value) : value(value) { }
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 class Configs
{ {
public: public:
@ -38,16 +50,10 @@ public:
/// Dump all settings to configuration file /// Dump all settings to configuration file
void save(); void save();
void setValue(const std::string &key, const std::string &value); template<class T>
void setValue(const std::string &key, const char *value); void setValue(const std::string& key, const T& value) { m_confsMap[key] = convert_cast<std::string>(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);
const std::string& getString(const std::string &key) const; ConfigValueProxy get(const std::string& key) { return ConfigValueProxy(m_confsMap[key]); }
float getFloat(const std::string &key) const;
bool getBoolean(const std::string &key) const;
int getInteger(const std::string &key) const;
private: private:
std::string m_fileName; std::string m_fileName;

@ -112,10 +112,10 @@ int main(int argc, const char *argv[])
logInfo("OTClient 0.2.0"); logInfo("OTClient 0.2.0");
// create the window // create the window
Platform::createWindow(g_configs.getInteger("window x"), g_configs.getInteger("window y"), Platform::createWindow(g_configs.get("window x"), g_configs.get("window y"),
g_configs.getInteger("window width"), g_configs.getInteger("window height"), g_configs.get("window width"), g_configs.get("window height"),
550, 450, 550, 450,
g_configs.getBoolean("window maximized")); g_configs.get("window maximized"));
Platform::setWindowTitle("OTClient"); Platform::setWindowTitle("OTClient");
//Platform::setVsync(); //Platform::setVsync();

Loading…
Cancel
Save