From 3fb8ff281fbef0a374ae3f891e7fd2acf30765f5 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 14 May 2017 15:21:43 +0800 Subject: [PATCH] Tweak CachedSettingValue * Add another constructor to save a call to proxyFunc when its not needed note that this call is a virtual function call * Pass in proxyFunc by const reference * Move get methods together --- src/base/settingvalue.h | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/base/settingvalue.h b/src/base/settingvalue.h index d04f1ad8e..80ab0b19a 100644 --- a/src/base/settingvalue.h +++ b/src/base/settingvalue.h @@ -40,8 +40,15 @@ class CachedSettingValue using ProxyFunc = std::function; public: - explicit CachedSettingValue(const char *keyName, const T &defaultValue = T() - , ProxyFunc proxyFunc = [](const T &value) { return value; }) + explicit CachedSettingValue(const char *keyName, const T &defaultValue = T()) + : m_keyName(QLatin1String(keyName)) + , m_value(SettingsStorage::instance()->loadValue( + m_keyName, defaultValue).template value()) + { + } + + explicit CachedSettingValue(const char *keyName, const T &defaultValue + , const ProxyFunc &proxyFunc) : m_keyName(QLatin1String(keyName)) , m_value(proxyFunc(SettingsStorage::instance()->loadValue( m_keyName, defaultValue).template value())) @@ -53,6 +60,11 @@ public: return m_value; } + operator T() const + { + return value(); + } + CachedSettingValue &operator=(const T &newValue) { m_value = newValue; @@ -60,11 +72,6 @@ public: return *this; } - operator T() const - { - return value(); - } - private: const QString m_keyName; T m_value;