diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-09-13 14:38:44 +1000 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-09-13 14:38:44 +1000 |
| commit | 306cfc06ed38a2f86616c1f2fe64de45321f21a6 (patch) | |
| tree | a27c79d9c4d01c2dadeeae74c844875ab7ab4eed /plugin/src/Caelestia/Services/service.cpp | |
| parent | popouts/tray: better interaction (diff) | |
| download | caelestia-shell-306cfc06ed38a2f86616c1f2fe64de45321f21a6.tar.gz caelestia-shell-306cfc06ed38a2f86616c1f2fe64de45321f21a6.tar.bz2 caelestia-shell-306cfc06ed38a2f86616c1f2fe64de45321f21a6.zip | |
plugin: refactor into modules
Diffstat (limited to 'plugin/src/Caelestia/Services/service.cpp')
| -rw-r--r-- | plugin/src/Caelestia/Services/service.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/plugin/src/Caelestia/Services/service.cpp b/plugin/src/Caelestia/Services/service.cpp new file mode 100644 index 0000000..bc919c9 --- /dev/null +++ b/plugin/src/Caelestia/Services/service.cpp @@ -0,0 +1,70 @@ +#include "service.hpp" + +#include <qdebug.h> +#include <qpointer.h> + +namespace caelestia { + +Service::Service(QObject* parent) + : QObject(parent) + , m_refCount(0) {} + +int Service::refCount() const { + QMutexLocker locker(&m_mutex); + return m_refCount; +} + +void Service::ref() { + bool needsStart = false; + + { + QMutexLocker locker(&m_mutex); + if (m_refCount == 0) { + needsStart = true; + } + m_refCount++; + } + emit refCountChanged(); + + if (needsStart) { + const QPointer<Service> self(this); + QMetaObject::invokeMethod( + this, + [self]() { + if (self) { + self->start(); + } + }, + Qt::QueuedConnection); + } +} + +void Service::unref() { + bool needsStop = false; + + { + QMutexLocker locker(&m_mutex); + if (m_refCount == 0) { + return; + } + m_refCount--; + if (m_refCount == 0) { + needsStop = true; + } + } + emit refCountChanged(); + + if (needsStop) { + const QPointer<Service> self(this); + QMetaObject::invokeMethod( + this, + [self]() { + if (self) { + self->stop(); + } + }, + Qt::QueuedConnection); + } +} + +} // namespace caelestia |