5#include <QCoreApplication>
6#include <albert/export.h>
22 inline operator T()
const {
return dependency; }
23 inline operator bool()
const {
return dependency !=
nullptr; }
26 const T*
get()
const {
return dependency; }
27 T*
get() {
return dependency; }
31 T *dependency =
nullptr;
37 dependency =
dynamic_cast<T*
>(e);
39 WARN << QString(
"Found '%1' but failed casting to expected type.").arg(
id);
40 }
catch (
const std::out_of_range &) {}
61 if (!this->dependency)
63 auto m = QCoreApplication::translate(
65 "Required dependency '%1' not available.").arg(
id);
66 throw std::runtime_error(m.toStdString());
94 std::function<
void(
bool)> registeredCallback = {}):
96 callback(registeredCallback)
98 conn_add_ = QObject::connect(®istry, &ExtensionRegistry::added, [
this](
Extension *e)
103 if (!this->dependency)
105 if (
auto *d =
dynamic_cast<T*
>(e); d)
107 this->dependency = d;
112 WARN << QString(
"Failed casting '%1' to expected type.").arg(id_);
115 WARN <<
"WeakDependency already set. Internal logic error?";
119 conn_rem_ = QObject::connect(®istry, &ExtensionRegistry::removed, [
this](Extension *e)
124 if (this->dependency)
126 if (
auto *d =
dynamic_cast<T*
>(e); d)
130 this->dependency =
nullptr;
133 WARN << QString(
"Failed casting '%1' to expected type.").arg(id_);
136 WARN <<
"WeakDependency already unset. Internal logic error?";
142 QObject::disconnect(conn_add_);
143 QObject::disconnect(conn_rem_);
154 QMetaObject::Connection conn_add_;
155 QMetaObject::Connection conn_rem_;
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 ®istry, 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 ®istry, const QString &id)
Definition plugindependency.h:58
Convenience holder class for plugin soft dependencies.
Definition plugindependency.h:84
WeakDependency(ExtensionRegistry ®istry, 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