mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2025-12-21 16:07:23 -06:00
Add a thin layer around SettingsStorage class
This new layer would be handy for saving GUI widget states as they don't need the value cached and they store/load rarely.
This commit is contained in:
@@ -30,80 +30,110 @@
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include <QMetaEnum>
|
||||
#include <QString>
|
||||
|
||||
#include "settingsstorage.h"
|
||||
#include "utils/string.h"
|
||||
|
||||
// This is a thin/handy wrapper over `SettingsStorage`. Use it when store/load value
|
||||
// rarely occurs, otherwise use `CachedSettingValue`.
|
||||
template <typename T>
|
||||
class SettingValue
|
||||
{
|
||||
public:
|
||||
explicit SettingValue(const char *keyName)
|
||||
: m_keyName {QLatin1String {keyName}}
|
||||
{
|
||||
}
|
||||
|
||||
T get(const T &defaultValue = {}) const
|
||||
{
|
||||
return load(defaultValue);
|
||||
}
|
||||
|
||||
operator T() const
|
||||
{
|
||||
return get();
|
||||
}
|
||||
|
||||
SettingValue<T> &operator=(const T &value)
|
||||
{
|
||||
store(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
// regular load/store pair
|
||||
template <typename U, typename std::enable_if_t<!std::is_enum<U>::value, int> = 0>
|
||||
U load(const U &defaultValue) const
|
||||
{
|
||||
return SettingsStorage::instance()->loadValue(m_keyName, defaultValue)
|
||||
.template value<U>();
|
||||
}
|
||||
|
||||
template <typename U, typename std::enable_if_t<!std::is_enum<U>::value, int> = 0>
|
||||
void store(const U &value)
|
||||
{
|
||||
SettingsStorage::instance()->storeValue(m_keyName, value);
|
||||
}
|
||||
|
||||
// load/store pair for enum type, saves literal value of the enum constant
|
||||
// TODO: merge the load/store functions with `if constexpr` in C++17
|
||||
template <typename U, typename std::enable_if_t<std::is_enum<U>::value, int> = 0>
|
||||
U load(const U defaultValue) const
|
||||
{
|
||||
return Utils::String::toEnum(load(QString {}), defaultValue);
|
||||
}
|
||||
|
||||
template <typename U, typename std::enable_if_t<std::is_enum<U>::value, int> = 0>
|
||||
void store(const U value)
|
||||
{
|
||||
store(Utils::String::fromEnum(value));
|
||||
}
|
||||
|
||||
const QString m_keyName;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class CachedSettingValue
|
||||
{
|
||||
public:
|
||||
explicit CachedSettingValue(const char *keyName, const T &defaultValue = T())
|
||||
: m_keyName(QLatin1String(keyName))
|
||||
, m_value(loadValue(defaultValue))
|
||||
explicit CachedSettingValue(const char *keyName, const T &defaultValue = {})
|
||||
: m_setting {keyName}
|
||||
, m_cache {m_setting.get(defaultValue)}
|
||||
{
|
||||
}
|
||||
|
||||
// The signature of the ProxyFunc should be equivalent to the following:
|
||||
// T proxyFunc(const T &a);
|
||||
template <typename ProxyFunc>
|
||||
explicit CachedSettingValue(const char *keyName, const T &defaultValue
|
||||
, ProxyFunc &&proxyFunc)
|
||||
: m_keyName(QLatin1String(keyName))
|
||||
, m_value(proxyFunc(loadValue(defaultValue)))
|
||||
explicit CachedSettingValue(const char *keyName, const T &defaultValue, ProxyFunc &&proxyFunc)
|
||||
: m_setting {keyName}
|
||||
, m_cache {proxyFunc(m_setting.get(defaultValue))}
|
||||
{
|
||||
}
|
||||
|
||||
T value() const
|
||||
T get() const
|
||||
{
|
||||
return m_value;
|
||||
return m_cache;
|
||||
}
|
||||
|
||||
operator T() const
|
||||
{
|
||||
return value();
|
||||
return get();
|
||||
}
|
||||
|
||||
CachedSettingValue<T> &operator=(const T &newValue)
|
||||
CachedSettingValue<T> &operator=(const T &value)
|
||||
{
|
||||
if (m_value == newValue)
|
||||
if (m_cache == value)
|
||||
return *this;
|
||||
|
||||
m_value = newValue;
|
||||
storeValue(m_value);
|
||||
m_setting = value;
|
||||
m_cache = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
// regular load/save pair
|
||||
template <typename U, typename std::enable_if_t<!std::is_enum<U>::value, int> = 0>
|
||||
U loadValue(const U &defaultValue)
|
||||
{
|
||||
return SettingsStorage::instance()->loadValue(m_keyName, defaultValue).template value<T>();
|
||||
}
|
||||
|
||||
template <typename U, typename std::enable_if_t<!std::is_enum<U>::value, int> = 0>
|
||||
void storeValue(const U &value)
|
||||
{
|
||||
SettingsStorage::instance()->storeValue(m_keyName, value);
|
||||
}
|
||||
|
||||
// load/save pair for an enum
|
||||
// saves literal value of the enum constant, obtained from QMetaEnum
|
||||
template <typename U, typename std::enable_if_t<std::is_enum<U>::value, int> = 0>
|
||||
U loadValue(const U &defaultValue)
|
||||
{
|
||||
return Utils::String::toEnum(SettingsStorage::instance()->loadValue(m_keyName).toString(), defaultValue);
|
||||
}
|
||||
|
||||
template <typename U, typename std::enable_if_t<std::is_enum<U>::value, int> = 0>
|
||||
void storeValue(const U &value)
|
||||
{
|
||||
SettingsStorage::instance()->storeValue(m_keyName, Utils::String::fromEnum(value));
|
||||
}
|
||||
|
||||
const QString m_keyName;
|
||||
T m_value;
|
||||
SettingValue<T> m_setting;
|
||||
T m_cache;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user