Albert
Loading...
Searching...
No Matches
extensionwatcher.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 Manuel Schneider
2// SPDX-License-Identifier: MIT
3
4#pragma once
5#include <QObject>
6#include <albert/export.h>
8
9namespace albert
10{
11
22template<class T>
23class ALBERT_EXPORT ExtensionWatcher
24{
25public:
28 explicit ExtensionWatcher(ExtensionRegistry *registry = nullptr)
29 {
30 if (registry)
31 setExtensionRegistry(registry);
32 }
33
37 {
38 QObject::disconnect(conn_add);
39 conn_add = QObject::connect(registry, &ExtensionRegistry::added,
40 [this](Extension *e){ if (T *t = dynamic_cast<T*>(e)) onAdd(t); });
41
42 QObject::disconnect(conn_rem);
43 conn_rem = QObject::connect(registry, &ExtensionRegistry::removed,
44 [this](Extension *e){ if (T *t = dynamic_cast<T*>(e)) onRem(t); });
45 }
46
47protected:
48
50 virtual void onAdd(T *) {}
51
53 virtual void onRem(T *) {}
54
57 {
58 QObject::disconnect(conn_add);
59 QObject::disconnect(conn_rem);
60 }
61
62private:
63 QMetaObject::Connection conn_add;
64 QMetaObject::Connection conn_rem;
65};
66}
The common extension pool.
Definition extensionregistry.h:23
Non-QObject extension registry observer.
Definition extensionwatcher.h:24
ExtensionWatcher(ExtensionRegistry *registry=nullptr)
ExtensionWatcher constructor.
Definition extensionwatcher.h:28
void setExtensionRegistry(ExtensionRegistry *registry)
Sets the extension registry to track.
Definition extensionwatcher.h:36
virtual ~ExtensionWatcher()
Destructor must not be called though this interface.
Definition extensionwatcher.h:56
virtual void onRem(T *)
Called when an extension has been deregistered.
Definition extensionwatcher.h:53
virtual void onAdd(T *)
Called when an extension has been registered.
Definition extensionwatcher.h:50
Abstract extension class.
Definition extension.h:19
Definition action.h:10