From f0ff57555f8fe1ee72b721428636fb3f8caec829 Mon Sep 17 00:00:00 2001 From: yvt Date: Tue, 3 Sep 2013 23:47:28 +0900 Subject: [PATCH] Delayed load of settings (fixes default value bug) --- Sources/Core/Settings.cpp | 41 ++++++++++++++++++++++++++++----------- Sources/Core/Settings.h | 3 +++ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Sources/Core/Settings.cpp b/Sources/Core/Settings.cpp index 3845dda51..90b59f184 100644 --- a/Sources/Core/Settings.cpp +++ b/Sources/Core/Settings.cpp @@ -64,17 +64,7 @@ namespace spades { if(it == items.end()){ Item *item = new Item(); item->name = name; - - char buf[2048]; - buf[2047] = 0; - SPAssert(pref != NULL); - pref->get(name.c_str(), buf, def.c_str(), 2047); - SPAssert(buf); - - item->string = buf; - - item->value = static_cast(atof(item->string.c_str())); - item->intValue = atoi(item->string.c_str()); + item->loaded = false; item->desc = desc; item->defaultValue = def; @@ -85,14 +75,35 @@ namespace spades { return it->second; } + void Settings::Item::Load() { + if(this->loaded){ + return; + } + + char buf[2048]; + buf[2047] = 0; + SPAssert(pref != NULL); + pref->get(name.c_str(), buf, defaultValue.c_str(), 2047); + SPAssert(buf); + + this->string = buf; + + this->value = static_cast(atof(this->string.c_str())); + this->intValue = atoi(this->string.c_str()); + + this->loaded = true; + } + void Settings::Item::Set(const std::string &str) { string = str; value = static_cast(atof(str.c_str())); intValue = atoi(str.c_str()); pref->set(name.c_str(), string.c_str()); + loaded = true; } + void Settings::Item::Set(int v) { char buf[256]; sprintf(buf, "%d", v); @@ -101,6 +112,7 @@ namespace spades { value = (float)v; pref->set(name.c_str(), string.c_str()); + loaded = true; } void Settings::Item::Set(float v){ @@ -111,6 +123,8 @@ namespace spades { value = v; pref->set(name.c_str(), string.c_str()); + + loaded = true; } Settings::ItemHandle::ItemHandle(const std::string& name, @@ -139,18 +153,23 @@ namespace spades { item->Set(value); } Settings::ItemHandle::operator std::string() { + item->Load(); return item->string; } Settings::ItemHandle::operator int() { + item->Load(); return item->intValue; } Settings::ItemHandle::operator float() { + item->Load(); return item->value; } Settings::ItemHandle::operator bool() { + item->Load(); return item->intValue != 0; } const char *Settings::ItemHandle::CString() { + item->Load(); return item->string.c_str(); } std::string Settings::ItemHandle::GetDescription() { diff --git a/Sources/Core/Settings.h b/Sources/Core/Settings.h index 64c1ee5bc..763634dea 100644 --- a/Sources/Core/Settings.h +++ b/Sources/Core/Settings.h @@ -31,10 +31,13 @@ namespace spades { std::string string; float value; int intValue; + + bool loaded; std::string defaultValue; std::string desc; + void Load(); void Set(const std::string&); void Set(int); void Set(float);