mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2025-12-22 08:27:24 -06:00
Revise SettingsStorage store/load value interface
This commit is contained in:
committed by
sledgehammer999
parent
8608d7b9da
commit
d7afad835e
@@ -29,11 +29,15 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include <QObject>
|
||||
#include <QReadWriteLock>
|
||||
#include <QTimer>
|
||||
#include <QVariantHash>
|
||||
|
||||
#include "utils/string.h"
|
||||
|
||||
class SettingsStorage : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@@ -45,18 +49,48 @@ public:
|
||||
static void freeInstance();
|
||||
static SettingsStorage *instance();
|
||||
|
||||
QVariant loadValue(const QString &key, const QVariant &defaultValue = {}) const;
|
||||
void storeValue(const QString &key, const QVariant &value);
|
||||
template <typename T>
|
||||
T loadValue(const QString &key, const T &defaultValue = {}) const
|
||||
{
|
||||
if constexpr (std::is_enum_v<T>)
|
||||
{
|
||||
const auto value = loadValueImpl(key).toString();
|
||||
return Utils::String::toEnum(value, defaultValue);
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, QVariant>)
|
||||
{
|
||||
return loadValueImpl(key, defaultValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
const QVariant value = loadValueImpl(key);
|
||||
// check if retrieved value is convertible to T
|
||||
return value.template canConvert<T>() ? value.template value<T>() : defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void storeValue(const QString &key, const T &value)
|
||||
{
|
||||
if constexpr (std::is_enum_v<T>)
|
||||
storeValueImpl(key, Utils::String::fromEnum(value));
|
||||
else
|
||||
storeValueImpl(key, value);
|
||||
}
|
||||
|
||||
void removeValue(const QString &key);
|
||||
|
||||
public slots:
|
||||
bool save();
|
||||
|
||||
private:
|
||||
QVariant loadValueImpl(const QString &key, const QVariant &defaultValue = {}) const;
|
||||
void storeValueImpl(const QString &key, const QVariant &value);
|
||||
|
||||
static SettingsStorage *m_instance;
|
||||
|
||||
bool m_dirty = false;
|
||||
QVariantHash m_data;
|
||||
bool m_dirty;
|
||||
QTimer m_timer;
|
||||
mutable QReadWriteLock m_lock;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user