From ce1a64bc19ffc21e115bdf2587bb053d7a417f3e Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Mon, 19 Jun 2023 01:38:40 +0200 Subject: Move to automatic type registration --- src/timeline/PresenceEmitter.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/timeline/PresenceEmitter.h') diff --git a/src/timeline/PresenceEmitter.h b/src/timeline/PresenceEmitter.h index e89fb316..09ad1301 100644 --- a/src/timeline/PresenceEmitter.h +++ b/src/timeline/PresenceEmitter.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include @@ -15,10 +16,33 @@ class PresenceEmitter final : public QObject { Q_OBJECT + QML_NAMED_ELEMENT(Presence) + QML_SINGLETON + public: PresenceEmitter(QObject *p = nullptr) : QObject(p) { + instance_ = this; + } + + static PresenceEmitter *create(QQmlEngine *qmlEngine, QJSEngine *) + { + // The instance has to exist before it is used. We cannot replace it. + Q_ASSERT(instance_); + + // The engine has to have the same thread affinity as the singleton. + Q_ASSERT(qmlEngine->thread() == instance_->thread()); + + // There can only be one engine accessing the singleton. + static QJSEngine *s_engine = nullptr; + if (s_engine) + Q_ASSERT(qmlEngine == s_engine); + else + s_engine = qmlEngine; + + QJSEngine::setObjectOwnership(instance_, QJSEngine::CppOwnership); + return instance_; } void sync(const std::vector> &presences); @@ -28,4 +52,7 @@ public: signals: void presenceChanged(QString userid); + +private: + inline static PresenceEmitter *instance_ = nullptr; }; -- cgit 1.5.1