diff --git a/src/corelib/io/qsettings_win.cpp b/src/corelib/io/qsettings_win.cpp index 25cba3ecb4..caa8c84f24 100644 --- a/src/corelib/io/qsettings_win.cpp +++ b/src/corelib/io/qsettings_win.cpp @@ -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 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 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(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(rSubkeyName.utf16()), 0, 0, reinterpret_cast(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(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(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(&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(&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(dataType)); - if (value != 0) - *value = QVariant(); break; } - return true; + return std::nullopt; } HKEY QWinSettingsPrivate::writeHandle() const @@ -751,13 +739,12 @@ std::optional 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; }