Albert
Loading...
Searching...
No Matches
plugindependency.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 <QCoreApplication>
6#include <albert/export.h>
8#include <albert/logging.h>
9
10namespace albert
11{
12
17template<class T>
18class ALBERT_EXPORT Dependency
19{
20public:
21
22 inline operator T() const { return dependency; }
23 inline operator bool() const { return dependency != nullptr; }
24 const T* operator->() const { return dependency; }
25 T* operator->() { return dependency; }
26 const T* get() const { return dependency; }
27 T* get() { return dependency; }
28
29protected:
30
31 T *dependency = nullptr;
32
33 explicit Dependency(ExtensionRegistry &registry, const QString &id)
34 {
35 try {
36 auto *e = registry.extensions().at(id);
37 dependency = dynamic_cast<T*>(e);
38 if (!dependency)
39 WARN << QString("Found '%1' but failed casting to expected type.").arg(id);
40 } catch (const std::out_of_range &) {}
41 }
42};
43
53template<class T>
54class ALBERT_EXPORT StrongDependency final : public Dependency<T>
55{
56public:
57
58 explicit StrongDependency(ExtensionRegistry &registry, const QString &id):
59 Dependency<T>(registry, id)
60 {
61 if (!this->dependency)
62 {
63 auto m = QCoreApplication::translate(
64 "Dependency",
65 "Required dependency '%1' not available.").arg(id);
66 throw std::runtime_error(m.toStdString());
67 }
68 }
69};
70
82template<class T>
83class ALBERT_EXPORT WeakDependency final : public Dependency<T>
84{
85public:
86
93 explicit WeakDependency(ExtensionRegistry &registry, const QString &id,
94 std::function<void(bool)> registeredCallback = {}):
95 Dependency<T>(registry, id),
96 callback(registeredCallback)
97 {
98 conn_add_ = QObject::connect(&registry, &ExtensionRegistry::added, [this](Extension *e)
99 {
100 if (e->id() != id_)
101 return;
102
103 if (!this->dependency)
104 {
105 if (auto *d = dynamic_cast<T*>(e); d)
106 {
107 this->dependency = d;
108 if (callback)
109 callback(true);
110 }
111 else
112 WARN << QString("Failed casting '%1' to expected type.").arg(id_);
113 }
114 else
115 WARN << "WeakDependency already set. Internal logic error?";
116
117 });
118
119 conn_rem_ = QObject::connect(&registry, &ExtensionRegistry::removed, [this](Extension *e)
120 {
121 if (e->id() != id_)
122 return;
123
124 if (this->dependency)
125 {
126 if (auto *d = dynamic_cast<T*>(e); d)
127 {
128 if (callback)
129 callback(false); // the dependency should still be usable in the callback
130 this->dependency = nullptr;
131 }
132 else
133 WARN << QString("Failed casting '%1' to expected type.").arg(id_);
134 }
135 else
136 WARN << "WeakDependency already unset. Internal logic error?";
137 });
138 }
139
141 {
142 QObject::disconnect(conn_add_);
143 QObject::disconnect(conn_rem_);
144 }
145
150 std::function<void(bool)> callback;
151
152private:
153
154 QMetaObject::Connection conn_add_;
155 QMetaObject::Connection conn_rem_;
156 const QString id_;
157
158};
159
160}
Base class for StrongDependency and WeakDependency.
Definition plugindependency.h:19
const T * get() const
Definition plugindependency.h:26
const T * operator->() const
Definition plugindependency.h:24
T * get()
Definition plugindependency.h:27
Dependency(ExtensionRegistry &registry, const QString &id)
Definition plugindependency.h:33
T * operator->()
Definition plugindependency.h:25
The common extension pool.
Definition extensionregistry.h:23
const std::map< QString, Extension * > & extensions()
Get map of all registered extensions.
Abstract extension class.
Definition extension.h:19
virtual QString id() const =0
The identifier of this extension.
Convenience holder class for plugin hard dependencies.
Definition plugindependency.h:55
StrongDependency(ExtensionRegistry &registry, const QString &id)
Definition plugindependency.h:58
Convenience holder class for plugin soft dependencies.
Definition plugindependency.h:84
WeakDependency(ExtensionRegistry &registry, const QString &id, std::function< void(bool)> registeredCallback={})
WeakDependency constructor.
Definition plugindependency.h:93
~WeakDependency()
Definition plugindependency.h:140
std::function< void(bool)> callback
(De)Registration callback
Definition plugindependency.h:150
#define WARN
Creates a log object (level warning) you can use to pipe text into (<<).
Definition logging.h:20
Definition action.h:10