QLibraryInfo: port from manual memory management to unique_ptr

It's safer and clearer.

Also standardize on unique_ptr instead of QScopedPointer, because the
latter is not supposed to be movable, so the assignment in
QLibrarySettings::load() would not compile.

As a drive-by, scope a variable tighter (in an if condition).

Task-number: QTBUG-132213
Pick-to: 6.8
Change-Id: Iecdd910af4d06dbd03d2daf3ccf99dd4822f04ac
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit aa1f2064801b39a26a718703817e197a3e31447e)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-12-28 11:08:58 +01:00 committed by Qt Cherry-pick Bot
parent e23933e868
commit b79b2edde8

View File

@ -10,7 +10,6 @@
#endif
#include "qlibraryinfo.h"
#include "qlibraryinfo_p.h"
#include "qscopedpointer.h"
#include "qcoreapplication.h"
@ -30,6 +29,8 @@
# include <qt_windows.h>
#endif
#include <memory>
QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
@ -38,7 +39,7 @@ extern void qDumpCPUFeatures(); // in qsimd.cpp
#if QT_CONFIG(settings)
static QSettings *findConfiguration();
static std::unique_ptr<QSettings> findConfiguration();
struct QLibrarySettings
{
@ -47,7 +48,7 @@ struct QLibrarySettings
bool havePaths();
QSettings *configuration();
QScopedPointer<QSettings> settings;
std::unique_ptr<QSettings> settings;
bool paths;
bool reloadOnQAppAvailable;
};
@ -62,7 +63,7 @@ QSettings *QLibrarySettings::configuration()
{
if (reloadOnQAppAvailable && QCoreApplication::instance() != nullptr)
load();
return settings.data();
return settings.get();
}
bool QLibrarySettings::havePaths()
@ -75,8 +76,8 @@ bool QLibrarySettings::havePaths()
void QLibrarySettings::load()
{
// If we get any settings here, those won't change when the application shows up.
settings.reset(findConfiguration());
reloadOnQAppAvailable = (settings.data() == nullptr && QCoreApplication::instance() == nullptr);
settings = findConfiguration();
reloadOnQAppAvailable = !settings && !QCoreApplication::instance();
if (settings) {
// This code needs to be in the regular library, as otherwise a qt.conf that
@ -96,14 +97,14 @@ void QLibraryInfoPrivate::setQtconfManualPath(const QString *path)
qtconfManualPath = path;
}
static QSettings *findConfiguration()
static std::unique_ptr<QSettings> findConfiguration()
{
if (qtconfManualPath)
return new QSettings(*qtconfManualPath, QSettings::IniFormat);
return std::make_unique<QSettings>(*qtconfManualPath, QSettings::IniFormat);
QString qtconfig = QStringLiteral(":/qt/etc/qt.conf");
if (QFile::exists(qtconfig))
return new QSettings(qtconfig, QSettings::IniFormat);
return std::make_unique<QSettings>(qtconfig, QSettings::IniFormat);
#ifdef Q_OS_DARWIN
CFBundleRef bundleRef = CFBundleGetMainBundle();
if (bundleRef) {
@ -115,7 +116,7 @@ static QSettings *findConfiguration()
QCFString path = CFURLCopyFileSystemPath(urlRef, kCFURLPOSIXPathStyle);
qtconfig = QDir::cleanPath(path);
if (QFile::exists(qtconfig))
return new QSettings(qtconfig, QSettings::IniFormat);
return std::make_unique<QSettings>(qtconfig, QSettings::IniFormat);
}
}
#endif
@ -123,10 +124,10 @@ static QSettings *findConfiguration()
QDir pwd(QCoreApplication::applicationDirPath());
qtconfig = pwd.filePath(u"qt" QT_STRINGIFY(QT_VERSION_MAJOR) ".conf"_s);
if (QFile::exists(qtconfig))
return new QSettings(qtconfig, QSettings::IniFormat);
return std::make_unique<QSettings>(qtconfig, QSettings::IniFormat);
qtconfig = pwd.filePath("qt.conf"_L1);
if (QFile::exists(qtconfig))
return new QSettings(qtconfig, QSettings::IniFormat);
return std::make_unique<QSettings>(qtconfig, QSettings::IniFormat);
}
return nullptr; //no luck
}
@ -688,8 +689,7 @@ QString QLibraryInfoPrivate::path(QLibraryInfo::LibraryPath p, UsageMode usageMo
QStringList QLibraryInfo::platformPluginArguments(const QString &platformName)
{
#if QT_CONFIG(settings)
QScopedPointer<const QSettings> settings(findConfiguration());
if (!settings.isNull()) {
if (const auto settings = findConfiguration()) {
const QString key = "Platforms/"_L1
+ platformName
+ "Arguments"_L1;