diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-09-22 22:08:46 +1000 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-09-22 22:08:46 +1000 |
| commit | f9fa8390137e93da8c9b93271e7ce85dfed55b7f (patch) | |
| tree | 3de5488479524c86231d0d06a67edfcc583e13e4 | |
| parent | plugin: delete entries after emitting signal (diff) | |
| download | caelestia-shell-f9fa8390137e93da8c9b93271e7ce85dfed55b7f.tar.gz caelestia-shell-f9fa8390137e93da8c9b93271e7ce85dfed55b7f.tar.bz2 caelestia-shell-f9fa8390137e93da8c9b93271e7ce85dfed55b7f.zip | |
plugin/service: ref by object
Use QSet and auto unref on sender destruction
| -rw-r--r-- | plugin/src/Caelestia/Services/audioprovider.cpp | 4 | ||||
| -rw-r--r-- | plugin/src/Caelestia/Services/cavaprovider.cpp | 3 | ||||
| -rw-r--r-- | plugin/src/Caelestia/Services/cavaprovider.hpp | 4 | ||||
| -rw-r--r-- | plugin/src/Caelestia/Services/service.cpp | 26 | ||||
| -rw-r--r-- | plugin/src/Caelestia/Services/service.hpp | 15 | ||||
| -rw-r--r-- | plugin/src/Caelestia/Services/serviceref.cpp | 12 | ||||
| -rw-r--r-- | plugin/src/Caelestia/Services/serviceref.hpp | 1 |
7 files changed, 20 insertions, 45 deletions
diff --git a/plugin/src/Caelestia/Services/audioprovider.cpp b/plugin/src/Caelestia/Services/audioprovider.cpp index c8a1261..efaadfd 100644 --- a/plugin/src/Caelestia/Services/audioprovider.cpp +++ b/plugin/src/Caelestia/Services/audioprovider.cpp @@ -21,7 +21,7 @@ void AudioProcessor::init() { } void AudioProcessor::start() { - QMetaObject::invokeMethod(&AudioCollector::instance(), &AudioCollector::ref); + QMetaObject::invokeMethod(&AudioCollector::instance(), &AudioCollector::ref, Qt::QueuedConnection, this); if (m_timer) { m_timer->start(); } @@ -31,7 +31,7 @@ void AudioProcessor::stop() { if (m_timer) { m_timer->stop(); } - QMetaObject::invokeMethod(&AudioCollector::instance(), &AudioCollector::unref); + QMetaObject::invokeMethod(&AudioCollector::instance(), &AudioCollector::unref, Qt::QueuedConnection, this); } AudioProvider::AudioProvider(QObject* parent) diff --git a/plugin/src/Caelestia/Services/cavaprovider.cpp b/plugin/src/Caelestia/Services/cavaprovider.cpp index 381183d..0fb3a4c 100644 --- a/plugin/src/Caelestia/Services/cavaprovider.cpp +++ b/plugin/src/Caelestia/Services/cavaprovider.cpp @@ -124,7 +124,8 @@ void CavaProvider::setBars(int bars) { emit barsChanged(); emit valuesChanged(); - QMetaObject::invokeMethod(m_processor, "setBars", Qt::QueuedConnection, Q_ARG(int, bars)); + QMetaObject::invokeMethod( + static_cast<CavaProcessor*>(m_processor), &CavaProcessor::setBars, Qt::QueuedConnection, bars); } QVector<double> CavaProvider::values() const { diff --git a/plugin/src/Caelestia/Services/cavaprovider.hpp b/plugin/src/Caelestia/Services/cavaprovider.hpp index ba04406..97d71c5 100644 --- a/plugin/src/Caelestia/Services/cavaprovider.hpp +++ b/plugin/src/Caelestia/Services/cavaprovider.hpp @@ -13,6 +13,8 @@ public: explicit CavaProcessor(QObject* parent = nullptr); ~CavaProcessor(); + void setBars(int bars); + signals: void valuesChanged(QVector<double> values); @@ -27,8 +29,6 @@ private: int m_bars; QVector<double> m_values; - Q_INVOKABLE void setBars(int bars); - void reload(); void initCava(); void cleanup(); diff --git a/plugin/src/Caelestia/Services/service.cpp b/plugin/src/Caelestia/Services/service.cpp index e63b990..4993772 100644 --- a/plugin/src/Caelestia/Services/service.cpp +++ b/plugin/src/Caelestia/Services/service.cpp @@ -6,31 +6,19 @@ namespace caelestia { Service::Service(QObject* parent) - : QObject(parent) - , m_refCount(0) {} + : QObject(parent) {} -int Service::refCount() const { - return m_refCount; -} - -void Service::ref() { - if (m_refCount == 0) { +void Service::ref(QObject* sender) { + if (m_refs.isEmpty()) { start(); } - m_refCount++; - emit refCountChanged(); + QObject::connect(sender, &QObject::destroyed, this, &Service::unref); + m_refs << sender; } -void Service::unref() { - if (m_refCount == 0) { - return; - } - - m_refCount--; - emit refCountChanged(); - - if (m_refCount == 0) { +void Service::unref(QObject* sender) { + if (m_refs.remove(sender) && m_refs.isEmpty()) { stop(); } } diff --git a/plugin/src/Caelestia/Services/service.hpp b/plugin/src/Caelestia/Services/service.hpp index 7980bdd..dac48760 100644 --- a/plugin/src/Caelestia/Services/service.hpp +++ b/plugin/src/Caelestia/Services/service.hpp @@ -1,28 +1,21 @@ #pragma once -#include <qmutex.h> #include <qobject.h> +#include <qset.h> namespace caelestia { class Service : public QObject { Q_OBJECT - Q_PROPERTY(int refCount READ refCount NOTIFY refCountChanged) - public: explicit Service(QObject* parent = nullptr); - [[nodiscard]] int refCount() const; - - void ref(); - void unref(); - -signals: - void refCountChanged(); + void ref(QObject* sender); + void unref(QObject* sender); private: - int m_refCount; + QSet<QObject*> m_refs; virtual void start() = 0; virtual void stop() = 0; diff --git a/plugin/src/Caelestia/Services/serviceref.cpp b/plugin/src/Caelestia/Services/serviceref.cpp index dc82811..91f8982 100644 --- a/plugin/src/Caelestia/Services/serviceref.cpp +++ b/plugin/src/Caelestia/Services/serviceref.cpp @@ -8,13 +8,7 @@ ServiceRef::ServiceRef(Service* service, QObject* parent) : QObject(parent) , m_service(service) { if (m_service) { - m_service->ref(); - } -} - -ServiceRef::~ServiceRef() { - if (m_service) { - m_service->unref(); + m_service->ref(this); } } @@ -28,14 +22,14 @@ void ServiceRef::setService(Service* service) { } if (m_service) { - m_service->unref(); + m_service->unref(this); } m_service = service; emit serviceChanged(); if (m_service) { - m_service->ref(); + m_service->ref(this); } } diff --git a/plugin/src/Caelestia/Services/serviceref.hpp b/plugin/src/Caelestia/Services/serviceref.hpp index 1d2881d..f1bd0f5 100644 --- a/plugin/src/Caelestia/Services/serviceref.hpp +++ b/plugin/src/Caelestia/Services/serviceref.hpp @@ -14,7 +14,6 @@ class ServiceRef : public QObject { public: explicit ServiceRef(Service* service = nullptr, QObject* parent = nullptr); - ~ServiceRef(); [[nodiscard]] Service* service() const; void setService(Service* service); |