From f9fa8390137e93da8c9b93271e7ce85dfed55b7f Mon Sep 17 00:00:00 2001 From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> Date: Mon, 22 Sep 2025 22:08:46 +1000 Subject: plugin/service: ref by object Use QSet and auto unref on sender destruction --- plugin/src/Caelestia/Services/service.cpp | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) (limited to 'plugin/src/Caelestia/Services/service.cpp') 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(); } } -- cgit v1.2.3-freya