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: outfoxxed
GPG key ID: 4C88A185FB89301E
6 changed files with 108 additions and 5 deletions

35
src/core/singleton.hpp Normal file
View file

@ -0,0 +1,35 @@
#pragma once
#include <qobject.h>
#include <qqmlintegration.h>
#include <qtclasshelpermacros.h>
#include <qtmetamacros.h>
#include <qurl.h>
#include "reload.hpp"
///! The root component for reloadable singletons.
/// All singletons should inherit from this type.
class Singleton: public ReloadPropagator {
Q_OBJECT;
QML_ELEMENT;
public:
void componentComplete() override;
};
class SingletonRegistry {
public:
SingletonRegistry() = default;
~SingletonRegistry();
Q_DISABLE_COPY_MOVE(SingletonRegistry);
void install(const QUrl& url, Singleton* singleton);
void flip();
static SingletonRegistry* instance();
private:
QMap<QUrl, QObject*>* previousRegistry = nullptr;
QMap<QUrl, QObject*>* currentRegistry = nullptr;
};