singleton: add reloadable Singleton type

This commit is contained in:
outfoxxed 2024-03-13 00:57:03 -07:00
parent 463f9a297f
commit 211f454de9
Signed by untrusted user: outfoxxed
GPG key ID: 4C88A185FB89301E
6 changed files with 108 additions and 5 deletions

64
src/core/singleton.cpp Normal file
View file

@ -0,0 +1,64 @@
#include "singleton.hpp"
#include <qlogging.h>
#include <qmap.h>
#include <qobject.h>
#include <qqmlcontext.h>
#include <qqmlengine.h>
#include <qurl.h>
#include "reload.hpp"
void Singleton::componentComplete() {
auto* context = QQmlEngine::contextForObject(this);
if (context == nullptr) {
qWarning() << "not registering singleton not created in the qml context:" << this;
return;
}
auto url = context->baseUrl();
if (this->parent() != nullptr || context->contextObject() != this) {
qWarning() << "tried to register singleton" << this
<< "which is not the root component of its file" << url;
return;
}
SingletonRegistry::instance()->install(url, this);
this->ReloadPropagator::componentComplete();
}
SingletonRegistry::~SingletonRegistry() {
delete this->previousRegistry;
delete this->currentRegistry;
}
void SingletonRegistry::install(const QUrl& url, Singleton* singleton) {
QObject* old = nullptr;
if (this->previousRegistry != nullptr) {
old = this->previousRegistry->value(url);
}
if (this->currentRegistry == nullptr) {
this->currentRegistry = new QMap<QUrl, QObject*>();
}
this->currentRegistry->insert(url, singleton);
singleton->onReload(old);
}
void SingletonRegistry::flip() {
delete this->previousRegistry;
this->previousRegistry = this->currentRegistry;
this->currentRegistry = nullptr;
}
SingletonRegistry* SingletonRegistry::instance() {
static SingletonRegistry* instance = nullptr; // NOLINT
if (instance == nullptr) {
instance = new SingletonRegistry();
}
return instance;
}