diff options
author | Jøger Hansegård <joger.hansegard@qt.io> | 2024-01-27 16:23:28 +0100 |
---|---|---|
committer | Jøger Hansegård <joger.hansegard@qt.io> | 2024-01-29 11:51:18 +0100 |
commit | 0180bce8d3d861fe4a1350734f04d9165b3fb407 (patch) | |
tree | a9d85d03e189d2761dfb61f650f6268ab4300665 | |
parent | ebabd4c29e6d752c7e8ed28858fb2e2407ef9eee (diff) |
Make singletons easier to spot using Q_GLOBAL_STATIC
In QtMultimedia we have some singletons that are a bit hard to see in
the code. It would be good if we can easily search for singleton
objects.
This patch makes these singletons easier to identify by using the
Q_GLOBAL_STATIC macro.
Pick-to: 6.7 6.6 6.5
Change-Id: I921230fdcc6e34c4a311dd2f1a22e7741ccf31b6
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Artem Dyomin <artem.dyomin@qt.io>
4 files changed, 54 insertions, 47 deletions
diff --git a/src/multimedia/platform/qplatformmediadevices.cpp b/src/multimedia/platform/qplatformmediadevices.cpp index a9d591ee8..61b6515e6 100644 --- a/src/multimedia/platform/qplatformmediadevices.cpp +++ b/src/multimedia/platform/qplatformmediadevices.cpp @@ -53,32 +53,32 @@ struct DevicesHolder QPlatformMediaDevices *nativeInstance = nullptr; }; -DevicesHolder devicesHolder; +Q_GLOBAL_STATIC(DevicesHolder, devicesHolder); } QPlatformMediaDevices *QPlatformMediaDevices::instance() { - QMutexLocker locker(&devicesHolder.mutex); - if (devicesHolder.instance) - return devicesHolder.instance; + QMutexLocker locker(&devicesHolder->mutex); + if (devicesHolder->instance) + return devicesHolder->instance; #ifdef Q_OS_DARWIN - devicesHolder.nativeInstance = new QDarwinMediaDevices; + devicesHolder->nativeInstance = new QDarwinMediaDevices; #elif defined(Q_OS_WINDOWS) - devicesHolder.nativeInstance = new QWindowsMediaDevices; + devicesHolder->nativeInstance = new QWindowsMediaDevices; #elif defined(Q_OS_ANDROID) - devicesHolder.nativeInstance = new QAndroidMediaDevices; + devicesHolder->nativeInstance = new QAndroidMediaDevices; #elif QT_CONFIG(alsa) - devicesHolder.nativeInstance = new QAlsaMediaDevices; + devicesHolder->nativeInstance = new QAlsaMediaDevices; #elif QT_CONFIG(pulseaudio) - devicesHolder.nativeInstance = new QPulseAudioMediaDevices; + devicesHolder->nativeInstance = new QPulseAudioMediaDevices; #elif defined(Q_OS_QNX) - devicesHolder.nativeInstance = new QQnxMediaDevices; + devicesHolder->nativeInstance = new QQnxMediaDevices; #elif defined(Q_OS_WASM) - devicesHolder.nativeInstance = new QWasmMediaDevices; + devicesHolder->nativeInstance = new QWasmMediaDevices; #else - devicesHolder.nativeInstance = new QPlatformMediaDevices; + devicesHolder->nativeInstance = new QPlatformMediaDevices; #endif if (!QCoreApplication::instance()) { @@ -86,13 +86,13 @@ QPlatformMediaDevices *QPlatformMediaDevices::instance() // application with Windows backend. qWarning("Accessing QMediaDevices without a QCoreApplication"); } else { - connect(qApp, &QObject::destroyed, devicesHolder.nativeInstance, []() { - devicesHolder.reset(); + connect(qApp, &QObject::destroyed, devicesHolder->nativeInstance, []() { + devicesHolder->reset(); }); } - devicesHolder.instance = devicesHolder.nativeInstance; - return devicesHolder.instance; + devicesHolder->instance = devicesHolder->nativeInstance; + return devicesHolder->instance; } @@ -111,7 +111,7 @@ void QPlatformMediaDevices::initVideoDevicesConnection() { void QPlatformMediaDevices::setDevices(QPlatformMediaDevices *devices) { - devicesHolder.instance = devices; + devicesHolder->instance = devices; } QPlatformMediaDevices::~QPlatformMediaDevices() = default; diff --git a/src/multimedia/platform/qplatformmediaintegration.cpp b/src/multimedia/platform/qplatformmediaintegration.cpp index eee36beff..e4258a179 100644 --- a/src/multimedia/platform/qplatformmediaintegration.cpp +++ b/src/multimedia/platform/qplatformmediaintegration.cpp @@ -71,25 +71,31 @@ static QString defaultBackend(const QStringList &backends) return backends[0]; } -QT_BEGIN_NAMESPACE +namespace { -struct QPlatformMediaIntegration::InstanceHolder +struct InstanceHolder { // TODO: replace the mutex with std::once QBasicMutex mutex; std::unique_ptr<QPlatformMediaIntegration> instance; - Factory factory; -} instanceHolder; + std::function<std::unique_ptr<QPlatformMediaIntegration>()> factory; +}; + +Q_GLOBAL_STATIC(InstanceHolder, instanceHolder); + +} // namespace + +QT_BEGIN_NAMESPACE QPlatformMediaIntegration *QPlatformMediaIntegration::instance() { - QMutexLocker locker(&instanceHolder.mutex); - if (instanceHolder.instance) - return instanceHolder.instance.get(); + QMutexLocker locker(&instanceHolder->mutex); + if (instanceHolder->instance) + return instanceHolder->instance.get(); - if (instanceHolder.factory) { - instanceHolder.instance = instanceHolder.factory(); - return instanceHolder.instance.get(); + if (instanceHolder->factory) { + instanceHolder->instance = instanceHolder->factory(); + return instanceHolder->instance.get(); } const auto backends = availableBackends(); @@ -98,25 +104,25 @@ QPlatformMediaIntegration *QPlatformMediaIntegration::instance() backend = defaultBackend(backends); qCDebug(qLcMediaPlugin) << "loading backend" << backend; - instanceHolder.instance.reset( + instanceHolder->instance.reset( qLoadPlugin<QPlatformMediaIntegration, QPlatformMediaPlugin>(loader(), backend)); - if (!instanceHolder.instance) { + if (!instanceHolder->instance) { qWarning() << "could not load multimedia backend" << backend; - instanceHolder.instance = std::make_unique<QDummyIntegration>(); + instanceHolder->instance = std::make_unique<QDummyIntegration>(); } - return instanceHolder.instance.get(); + return instanceHolder->instance.get(); } /* This API is there to be able to test with a mock backend. */ -void QPlatformMediaIntegration::setPlatformFactory(Factory factory) +void QPlatformMediaIntegration::setPlatformFactory(std::function<std::unique_ptr<QPlatformMediaIntegration>()> factory) { - Q_ASSERT((factory == nullptr) ^ (instanceHolder.factory == nullptr)); - instanceHolder.instance.reset(); - instanceHolder.factory = std::move(factory); + Q_ASSERT((factory == nullptr) ^ (instanceHolder->factory == nullptr)); + instanceHolder->instance.reset(); + instanceHolder->factory = std::move(factory); } QList<QCameraDevice> QPlatformMediaIntegration::videoInputs() diff --git a/src/multimedia/platform/qplatformmediaintegration_p.h b/src/multimedia/platform/qplatformmediaintegration_p.h index 21891b866..10845326c 100644 --- a/src/multimedia/platform/qplatformmediaintegration_p.h +++ b/src/multimedia/platform/qplatformmediaintegration_p.h @@ -103,9 +103,7 @@ protected: private: friend class QMockIntegrationFactory; // API to be able to test with a mock backend - using Factory = std::function<std::unique_ptr<QPlatformMediaIntegration>()>; - struct InstanceHolder; - static void setPlatformFactory(Factory factory); + static void setPlatformFactory(std::function<std::unique_ptr<QPlatformMediaIntegration>()> factory); private: std::unique_ptr<QPlatformVideoDevices> m_videoDevices; diff --git a/src/multimedia/windows/qwindowsmediafoundation.cpp b/src/multimedia/windows/qwindowsmediafoundation.cpp index 55b2f5459..9b67e9e63 100644 --- a/src/multimedia/windows/qwindowsmediafoundation.cpp +++ b/src/multimedia/windows/qwindowsmediafoundation.cpp @@ -9,6 +9,7 @@ QT_BEGIN_NAMESPACE namespace { + struct Holder { ~Holder() { @@ -18,9 +19,11 @@ struct Holder { bool loadFailed = false; QBasicMutex mutex; std::unique_ptr<QWindowsMediaFoundation> instance; -} holder; +}; -} +Q_GLOBAL_STATIC(Holder, holder) + +} // namespace QWindowsMediaFoundation::~QWindowsMediaFoundation() = default; @@ -32,11 +35,11 @@ template<typename T> bool setProcAddress(QSystemLibrary &lib, T &f, std::string_ QWindowsMediaFoundation *QWindowsMediaFoundation::instance() { - QMutexLocker locker(&holder.mutex); - if (holder.instance) - return holder.instance.get(); + QMutexLocker locker(&holder->mutex); + if (holder->instance) + return holder->instance.get(); - if (holder.loadFailed) + if (holder->loadFailed) return nullptr; std::unique_ptr<QWindowsMediaFoundation> wmf(new QWindowsMediaFoundation); @@ -45,12 +48,12 @@ QWindowsMediaFoundation *QWindowsMediaFoundation::instance() && setProcAddress(wmf->m_mfplat, wmf->mfCreateMemoryBuffer, "MFCreateMemoryBuffer") && setProcAddress(wmf->m_mfplat, wmf->mfCreateSample, "MFCreateSample")) { - holder.instance = std::move(wmf); - return holder.instance.get(); + holder->instance = std::move(wmf); + return holder->instance.get(); } } - holder.loadFailed = true; + holder->loadFailed = true; return nullptr; } |