plugin/service: ref by object
Use QSet and auto unref on sender destruction
This commit is contained in:
parent
1991b4176c
commit
f9fa839013
7 changed files with 20 additions and 45 deletions
|
|
@ -21,7 +21,7 @@ void AudioProcessor::init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioProcessor::start() {
|
void AudioProcessor::start() {
|
||||||
QMetaObject::invokeMethod(&AudioCollector::instance(), &AudioCollector::ref);
|
QMetaObject::invokeMethod(&AudioCollector::instance(), &AudioCollector::ref, Qt::QueuedConnection, this);
|
||||||
if (m_timer) {
|
if (m_timer) {
|
||||||
m_timer->start();
|
m_timer->start();
|
||||||
}
|
}
|
||||||
|
|
@ -31,7 +31,7 @@ void AudioProcessor::stop() {
|
||||||
if (m_timer) {
|
if (m_timer) {
|
||||||
m_timer->stop();
|
m_timer->stop();
|
||||||
}
|
}
|
||||||
QMetaObject::invokeMethod(&AudioCollector::instance(), &AudioCollector::unref);
|
QMetaObject::invokeMethod(&AudioCollector::instance(), &AudioCollector::unref, Qt::QueuedConnection, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
AudioProvider::AudioProvider(QObject* parent)
|
AudioProvider::AudioProvider(QObject* parent)
|
||||||
|
|
|
||||||
|
|
@ -124,7 +124,8 @@ void CavaProvider::setBars(int bars) {
|
||||||
emit barsChanged();
|
emit barsChanged();
|
||||||
emit valuesChanged();
|
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 {
|
QVector<double> CavaProvider::values() const {
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,8 @@ public:
|
||||||
explicit CavaProcessor(QObject* parent = nullptr);
|
explicit CavaProcessor(QObject* parent = nullptr);
|
||||||
~CavaProcessor();
|
~CavaProcessor();
|
||||||
|
|
||||||
|
void setBars(int bars);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void valuesChanged(QVector<double> values);
|
void valuesChanged(QVector<double> values);
|
||||||
|
|
||||||
|
|
@ -27,8 +29,6 @@ private:
|
||||||
int m_bars;
|
int m_bars;
|
||||||
QVector<double> m_values;
|
QVector<double> m_values;
|
||||||
|
|
||||||
Q_INVOKABLE void setBars(int bars);
|
|
||||||
|
|
||||||
void reload();
|
void reload();
|
||||||
void initCava();
|
void initCava();
|
||||||
void cleanup();
|
void cleanup();
|
||||||
|
|
|
||||||
|
|
@ -6,31 +6,19 @@
|
||||||
namespace caelestia {
|
namespace caelestia {
|
||||||
|
|
||||||
Service::Service(QObject* parent)
|
Service::Service(QObject* parent)
|
||||||
: QObject(parent)
|
: QObject(parent) {}
|
||||||
, m_refCount(0) {}
|
|
||||||
|
|
||||||
int Service::refCount() const {
|
void Service::ref(QObject* sender) {
|
||||||
return m_refCount;
|
if (m_refs.isEmpty()) {
|
||||||
}
|
|
||||||
|
|
||||||
void Service::ref() {
|
|
||||||
if (m_refCount == 0) {
|
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
m_refCount++;
|
QObject::connect(sender, &QObject::destroyed, this, &Service::unref);
|
||||||
emit refCountChanged();
|
m_refs << sender;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Service::unref() {
|
void Service::unref(QObject* sender) {
|
||||||
if (m_refCount == 0) {
|
if (m_refs.remove(sender) && m_refs.isEmpty()) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_refCount--;
|
|
||||||
emit refCountChanged();
|
|
||||||
|
|
||||||
if (m_refCount == 0) {
|
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,21 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <qmutex.h>
|
|
||||||
#include <qobject.h>
|
#include <qobject.h>
|
||||||
|
#include <qset.h>
|
||||||
|
|
||||||
namespace caelestia {
|
namespace caelestia {
|
||||||
|
|
||||||
class Service : public QObject {
|
class Service : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
Q_PROPERTY(int refCount READ refCount NOTIFY refCountChanged)
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Service(QObject* parent = nullptr);
|
explicit Service(QObject* parent = nullptr);
|
||||||
|
|
||||||
[[nodiscard]] int refCount() const;
|
void ref(QObject* sender);
|
||||||
|
void unref(QObject* sender);
|
||||||
void ref();
|
|
||||||
void unref();
|
|
||||||
|
|
||||||
signals:
|
|
||||||
void refCountChanged();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_refCount;
|
QSet<QObject*> m_refs;
|
||||||
|
|
||||||
virtual void start() = 0;
|
virtual void start() = 0;
|
||||||
virtual void stop() = 0;
|
virtual void stop() = 0;
|
||||||
|
|
|
||||||
|
|
@ -8,13 +8,7 @@ ServiceRef::ServiceRef(Service* service, QObject* parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
, m_service(service) {
|
, m_service(service) {
|
||||||
if (m_service) {
|
if (m_service) {
|
||||||
m_service->ref();
|
m_service->ref(this);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ServiceRef::~ServiceRef() {
|
|
||||||
if (m_service) {
|
|
||||||
m_service->unref();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -28,14 +22,14 @@ void ServiceRef::setService(Service* service) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_service) {
|
if (m_service) {
|
||||||
m_service->unref();
|
m_service->unref(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_service = service;
|
m_service = service;
|
||||||
emit serviceChanged();
|
emit serviceChanged();
|
||||||
|
|
||||||
if (m_service) {
|
if (m_service) {
|
||||||
m_service->ref();
|
m_service->ref(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,6 @@ class ServiceRef : public QObject {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ServiceRef(Service* service = nullptr, QObject* parent = nullptr);
|
explicit ServiceRef(Service* service = nullptr, QObject* parent = nullptr);
|
||||||
~ServiceRef();
|
|
||||||
|
|
||||||
[[nodiscard]] Service* service() const;
|
[[nodiscard]] Service* service() const;
|
||||||
void setService(Service* service);
|
void setService(Service* service);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue