type casts for config manager
This commit is contained in:
parent
afc79a99e1
commit
f72214f090
|
@ -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<std::string>(it.first())] = yamlRead<std::string>(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<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>
|
||||
|
||||
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
|
||||
{
|
||||
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<class T>
|
||||
void setValue(const std::string& key, const T& value) { m_confsMap[key] = convert_cast<std::string>(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;
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
Loading…
Reference in New Issue