mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-07-31 03:50:20 -07:00
Extract enum serialization/parsing functions
This commit is contained in:
parent
b8f1142abe
commit
cd0b6d9a43
2 changed files with 21 additions and 9 deletions
|
@ -34,6 +34,7 @@
|
|||
#include <QString>
|
||||
|
||||
#include "settingsstorage.h"
|
||||
#include "utils/string.h"
|
||||
|
||||
template <typename T>
|
||||
class CachedSettingValue
|
||||
|
@ -94,20 +95,13 @@ private:
|
|||
template <typename U, typename std::enable_if<std::is_enum<U>::value, int>::type = 0>
|
||||
U loadValue(const U &defaultValue)
|
||||
{
|
||||
static_assert(std::is_same<int, typename std::underlying_type<U>::type>::value,
|
||||
"Enumeration underlying type has to be int");
|
||||
|
||||
bool ok = false;
|
||||
const U res = static_cast<U>(QMetaEnum::fromType<U>().keyToValue(
|
||||
SettingsStorage::instance()->loadValue(m_keyName).toString().toLatin1().constData(), &ok));
|
||||
return ok ? res : defaultValue;
|
||||
return Utils::String::parse(SettingsStorage::instance()->loadValue(m_keyName).toString(), defaultValue);
|
||||
}
|
||||
|
||||
template <typename U, typename std::enable_if<std::is_enum<U>::value, int>::type = 0>
|
||||
void storeValue(const U &value)
|
||||
{
|
||||
SettingsStorage::instance()->storeValue(m_keyName,
|
||||
QString::fromLatin1(QMetaEnum::fromType<U>().valueToKey(static_cast<int>(value))));
|
||||
SettingsStorage::instance()->storeValue(m_keyName, Utils::String::serialize(value));
|
||||
}
|
||||
|
||||
const QString m_keyName;
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <QChar>
|
||||
#include <QMetaEnum>
|
||||
#include <QString>
|
||||
#include <Qt>
|
||||
#include <QtContainerFwd>
|
||||
|
@ -71,5 +72,22 @@ namespace Utils
|
|||
TriStateBool parseTriStateBool(const QString &string);
|
||||
|
||||
QString join(const QVector<QStringRef> &strings, const QString &separator);
|
||||
|
||||
template <typename U, typename std::enable_if<std::is_enum<U>::value, int>::type = 0>
|
||||
QString serialize(const U &value)
|
||||
{
|
||||
return QString::fromLatin1(QMetaEnum::fromType<U>().valueToKey(static_cast<int>(value)));
|
||||
}
|
||||
|
||||
template <typename U, typename std::enable_if<std::is_enum<U>::value, int>::type = 0>
|
||||
U parse(const QString &serializedValue, const U &defaultValue)
|
||||
{
|
||||
static_assert(std::is_same<int, typename std::underlying_type<U>::type>::value,
|
||||
"Enumeration underlying type has to be int.");
|
||||
|
||||
bool ok = false;
|
||||
const U value = static_cast<U>(QMetaEnum::fromType<U>().keyToValue(serializedValue.toLatin1().constData(), &ok));
|
||||
return (ok ? value : defaultValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue