From ebe090e53d226484dae479cf8cfcb9c95cc65c46 Mon Sep 17 00:00:00 2001 From: cxl Date: Fri, 13 Jul 2012 11:34:27 +0000 Subject: [PATCH] Core: INI_ fixed reloading of config git-svn-id: svn://ultimatepp.org/upp/trunk@5185 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/Ini.cpp | 62 +++++++++++++++++++++++++++++---------------- uppsrc/Core/Util.h | 38 ++++++++++++++------------- 2 files changed, 60 insertions(+), 40 deletions(-) diff --git a/uppsrc/Core/Ini.cpp b/uppsrc/Core/Ini.cpp index a29389683..5047c1ecd 100644 --- a/uppsrc/Core/Ini.cpp +++ b/uppsrc/Core/Ini.cpp @@ -50,11 +50,12 @@ VectorMap LoadIniFile(const char *filename) { static StaticMutex sMtx; static char sIniFile[256]; -static bool s_ini_loaded; +static int64 s_ini_version = 1; void ReloadIniFile() { - s_ini_loaded = false; + Mutex::Lock __(sMtx); + s_ini_version++; } void SetIniFile(const char *name) { @@ -63,11 +64,21 @@ void SetIniFile(const char *name) { ReloadIniFile(); } + +bool sIniChanged(int64& version) +{ + if(version != s_ini_version) { + version = s_ini_version; + return true; + } + return false; +} + String GetIniKey(const char *id, const String& def) { Mutex::Lock __(sMtx); static VectorMap key; - if(!s_ini_loaded) { - s_ini_loaded = true; + static int64 version; + if(sIniChanged(version)) { key = LoadIniFile(*sIniFile ? sIniFile : ~ConfigFile("q.ini")); #ifdef PLATFORM_WIN32 if(key.GetCount() == 0) @@ -90,21 +101,24 @@ String GetIniKey(const char *id) IniString::operator String() { - ONCELOCK_(loaded) { - static Array ss; - String& x = ss.Add(); - x = TrimBoth(GetIniKey(id)); - if(IsNull(x)) - x = (*def)(); - value = &x; + String x; + { + Mutex::Lock __(sMtx); + String& s = (*ref_fn)(); + if(sIniChanged(version)) { + s = TrimBoth(GetIniKey(id)); + if(IsNull(s)) + s = (*def)(); + } + x = s; } - return *value; + return x; } String IniString::operator=(const String& s) { - operator String(); - *value = s; + Mutex::Lock __(sMtx); + (*ref_fn)() = s; return s; } @@ -146,7 +160,8 @@ int64 ReadIniInt(const char *id) } IniInt::operator int() { - ONCELOCK_(loaded) { + Mutex::Lock __(sMtx); + if(sIniChanged(version)) { value = (int)ReadIniInt(id); if(IsNull(value)) value = (*def)(); @@ -155,7 +170,7 @@ IniInt::operator int() { } int IniInt::operator=(int b) { - ONCELOCK_(loaded) {} + Mutex::Lock __(sMtx); return value = b; } @@ -166,7 +181,8 @@ String IniInt::ToString() const IniInt64::operator int64() { - ONCELOCK_(loaded) { + Mutex::Lock __(sMtx); + if(sIniChanged(version)) { value = ReadIniInt(id); if(IsNull(value)) value = (*def)(); @@ -176,7 +192,7 @@ IniInt64::operator int64() int64 IniInt64::operator=(int64 b) { - ONCELOCK_(loaded) {} + Mutex::Lock __(sMtx); return value = b; } @@ -187,7 +203,8 @@ String IniInt64::ToString() const IniDouble::operator double() { - ONCELOCK_(loaded) { + Mutex::Lock __(sMtx); + if(sIniChanged(version)) { value = ScanDouble(TrimBoth(ToLower(GetIniKey(id)))); if(IsNull(value)) value = (*def)(); @@ -197,7 +214,7 @@ IniDouble::operator double() double IniDouble::operator=(double b) { - ONCELOCK_(loaded) {} + Mutex::Lock __(sMtx); return value = b; } @@ -207,7 +224,8 @@ String IniDouble::ToString() const } IniBool::operator bool() { - ONCELOCK_(loaded) { + Mutex::Lock __(sMtx); + if(sIniChanged(version)) { String h = TrimBoth(ToLower(GetIniKey(id))); if(h.GetCount()) value = h == "1" || h == "yes" || h == "true" || h == "y"; @@ -218,7 +236,7 @@ IniBool::operator bool() { } bool IniBool::operator=(bool b) { - ONCELOCK_(loaded) {} + Mutex::Lock __(sMtx); return value = b; } diff --git a/uppsrc/Core/Util.h b/uppsrc/Core/Util.h index 5bf594633..b89f4ce09 100644 --- a/uppsrc/Core/Util.h +++ b/uppsrc/Core/Util.h @@ -32,9 +32,9 @@ String GetIniKey(const char *id); struct IniString { // "private": const char *id; - String (*def)(); - bool loaded; - String *value; + String (*def)(); + String& (*ref_fn)(); + int64 version; // "public:" operator String(); String operator=(const String& s); @@ -45,48 +45,48 @@ struct IniInt { // "private": const char *id; int (*def)(); - bool loaded; + int64 version; int value; // "public:" operator int(); int operator=(int b); - String ToString() const; + String ToString() const; }; struct IniInt64 { // "private": const char *id; int64 (*def)(); - bool loaded; + int64 version; int64 value; // "public:" operator int64(); int64 operator=(int64 b); - String ToString() const; + String ToString() const; }; struct IniDouble { // "private": const char *id; double (*def)(); - bool loaded; + int64 version; double value; // "public:" operator double(); double operator=(double b); - String ToString() const; + String ToString() const; }; struct IniBool { // "private": const char *id; bool (*def)(); - bool loaded; + int64 version; bool value; // "public:" operator bool(); bool operator=(bool b); - String ToString() const; + String ToString() const; }; void AddIniInfo(const char *id, String (*current)(), String (*def)(), const char *info); @@ -101,18 +101,20 @@ struct IniInfo { const Array GetIniInfo(); String GetIniInfoFormatted(); -#define INI_TYPE(var, def, info, type, decl)\ +#define INI_TYPE(var, def, info, type, decl, ref)\ type DefIni_##var() { return def; }\ -decl var = { #var, DefIni_##var };\ +decl var = { #var, DefIni_##var, ref };\ String AsStringIniCurrent_##var() { return AsString(var); } \ String AsStringIniDefault_##var() { return AsString(DefIni_##var()); } \ INITBLOCK { AddIniInfo(#var, AsStringIniCurrent_##var, AsStringIniDefault_##var, info); } -#define INI_BOOL(var, def, info) INI_TYPE(var, def, info, bool, IniBool); -#define INI_STRING(var, def, info) INI_TYPE(var, def, info, String, IniString); -#define INI_INT(var, def, info) INI_TYPE(var, def, info, int, IniInt); -#define INI_INT64(var, def, info) INI_TYPE(var, def, info, int64, IniInt64); -#define INI_DOUBLE(var, def, info) INI_TYPE(var, def, info, double, IniDouble); +#define INI_BOOL(var, def, info) INI_TYPE(var, def, info, bool, IniBool, 0); +#define INI_INT(var, def, info) INI_TYPE(var, def, info, int, IniInt, 0); +#define INI_INT64(var, def, info) INI_TYPE(var, def, info, int64, IniInt64, 0); +#define INI_DOUBLE(var, def, info) INI_TYPE(var, def, info, double, IniDouble, 0); + +#define INI_STRING(var, def, info) String& DefRef##_var() { static String x; return x; }\ + INI_TYPE(var, def, info, String, IniString, DefRef##_var); /* #define INI_BOOL(var, def, info)\