QWinSettings: port readKey() helper to std::optional
Less boilerplate code and no more impedance mismatch with recently-ported QSettingsPrivate::get(). Change-Id: I5e113a44a48274aecb20fc9cb5d8db02c6cd0a35 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>bb10
parent
c587ebf54c
commit
f3c9200b5c
|
|
@ -394,7 +394,7 @@ public:
|
|||
void flush() override;
|
||||
bool isWritable() const override;
|
||||
HKEY writeHandle() const;
|
||||
bool readKey(HKEY parentHandle, const QString &rSubKey, QVariant *value) const;
|
||||
std::optional<QVariant> readKey(HKEY parentHandle, const QString &rSubKey) const;
|
||||
QString fileName() const override;
|
||||
|
||||
private:
|
||||
|
|
@ -478,7 +478,7 @@ QWinSettingsPrivate::QWinSettingsPrivate(QString rPath, REGSAM access)
|
|||
regList.append(RegistryKey(keyName, rPath.mid(keyLength+1), false, access));
|
||||
}
|
||||
|
||||
bool QWinSettingsPrivate::readKey(HKEY parentHandle, const QString &rSubKey, QVariant *value) const
|
||||
std::optional<QVariant> QWinSettingsPrivate::readKey(HKEY parentHandle, const QString &rSubKey) const
|
||||
{
|
||||
QString rSubkeyName = keyName(rSubKey);
|
||||
QString rSubkeyPath = keyPath(rSubKey);
|
||||
|
|
@ -486,7 +486,7 @@ bool QWinSettingsPrivate::readKey(HKEY parentHandle, const QString &rSubKey, QVa
|
|||
// open a handle on the subkey
|
||||
HKEY handle = openKey(parentHandle, KEY_READ, rSubkeyPath, access);
|
||||
if (handle == 0)
|
||||
return false;
|
||||
return std::nullopt;
|
||||
|
||||
const auto closeKey = qScopeGuard([handle] { RegCloseKey(handle); });
|
||||
|
||||
|
|
@ -495,7 +495,7 @@ bool QWinSettingsPrivate::readKey(HKEY parentHandle, const QString &rSubKey, QVa
|
|||
DWORD dataSize;
|
||||
LONG res = RegQueryValueEx(handle, reinterpret_cast<const wchar_t *>(rSubkeyName.utf16()), 0, &dataType, 0, &dataSize);
|
||||
if (res != ERROR_SUCCESS)
|
||||
return false;
|
||||
return std::nullopt;
|
||||
|
||||
// workaround for rare cases where trailing '\0' are missing in registry
|
||||
if (dataType == REG_SZ || dataType == REG_EXPAND_SZ)
|
||||
|
|
@ -508,7 +508,7 @@ bool QWinSettingsPrivate::readKey(HKEY parentHandle, const QString &rSubKey, QVa
|
|||
res = RegQueryValueEx(handle, reinterpret_cast<const wchar_t *>(rSubkeyName.utf16()), 0, 0,
|
||||
reinterpret_cast<unsigned char*>(data.data()), &dataSize);
|
||||
if (res != ERROR_SUCCESS)
|
||||
return false;
|
||||
return std::nullopt;
|
||||
|
||||
switch (dataType) {
|
||||
case REG_EXPAND_SZ:
|
||||
|
|
@ -517,9 +517,7 @@ bool QWinSettingsPrivate::readKey(HKEY parentHandle, const QString &rSubKey, QVa
|
|||
if (dataSize) {
|
||||
s = QString::fromWCharArray(reinterpret_cast<const wchar_t *>(data.constData()));
|
||||
}
|
||||
if (value != 0)
|
||||
*value = stringToVariant(s);
|
||||
break;
|
||||
return stringToVariant(s);
|
||||
}
|
||||
|
||||
case REG_MULTI_SZ: {
|
||||
|
|
@ -535,9 +533,7 @@ bool QWinSettingsPrivate::readKey(HKEY parentHandle, const QString &rSubKey, QVa
|
|||
l.append(s);
|
||||
}
|
||||
}
|
||||
if (value != 0)
|
||||
*value = stringListToVariantList(l);
|
||||
break;
|
||||
return stringListToVariantList(l);
|
||||
}
|
||||
|
||||
case REG_NONE:
|
||||
|
|
@ -546,9 +542,7 @@ bool QWinSettingsPrivate::readKey(HKEY parentHandle, const QString &rSubKey, QVa
|
|||
if (dataSize) {
|
||||
s = QString::fromWCharArray(reinterpret_cast<const wchar_t *>(data.constData()), data.size() / 2);
|
||||
}
|
||||
if (value != 0)
|
||||
*value = stringToVariant(s);
|
||||
break;
|
||||
return stringToVariant(s);
|
||||
}
|
||||
|
||||
case REG_DWORD_BIG_ENDIAN:
|
||||
|
|
@ -556,28 +550,22 @@ bool QWinSettingsPrivate::readKey(HKEY parentHandle, const QString &rSubKey, QVa
|
|||
Q_ASSERT(data.size() == sizeof(int));
|
||||
int i;
|
||||
memcpy(reinterpret_cast<char*>(&i), data.constData(), sizeof(int));
|
||||
if (value != 0)
|
||||
*value = i;
|
||||
break;
|
||||
return i;
|
||||
}
|
||||
|
||||
case REG_QWORD: {
|
||||
Q_ASSERT(data.size() == sizeof(qint64));
|
||||
qint64 i;
|
||||
memcpy(reinterpret_cast<char*>(&i), data.constData(), sizeof(qint64));
|
||||
if (value != 0)
|
||||
*value = i;
|
||||
break;
|
||||
return i;
|
||||
}
|
||||
|
||||
default:
|
||||
qWarning("QSettings: Unknown data %d type in Windows registry", static_cast<int>(dataType));
|
||||
if (value != 0)
|
||||
*value = QVariant();
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
HKEY QWinSettingsPrivate::writeHandle() const
|
||||
|
|
@ -751,13 +739,12 @@ std::optional<QVariant> QWinSettingsPrivate::get(const QString &uKey) const
|
|||
{
|
||||
QString rKey = escapedKey(uKey);
|
||||
|
||||
QVariant value;
|
||||
|
||||
for (const RegistryKey &r : regList) {
|
||||
HKEY handle = r.handle();
|
||||
if (handle != 0 && readKey(handle, rKey, &value))
|
||||
return value;
|
||||
|
||||
if (handle != 0) {
|
||||
if (auto result = readKey(handle, rKey))
|
||||
return result;
|
||||
}
|
||||
if (!fallbacks)
|
||||
return std::nullopt;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue