5#include <QCoreApplication>
7#include <albert/export.h>
20 inline operator bool()
const {
return dependency_ !=
nullptr; }
59 this->dependency_ =
dynamic_cast<T*
>(App::instance().extensionRegistry().extensions().at(
id));
61 if (!this->dependency_)
62 throw std::runtime_error(
63 QCoreApplication::translate(
65 "Extension '%1' is available, but it is not of the expected type."
66 ).arg(
id).toStdString());
68 catch (
const std::out_of_range &)
70 throw std::runtime_error(
71 QCoreApplication::translate(
73 "The required extension '%1' is not available."
74 ).arg(
id).toStdString());
101 explicit WeakDependency(
const QString &
id, std::function<
void(
bool)> on_registered = {}):
102 callback(on_registered),
106 this->dependency_ =
dynamic_cast<T*
>(App::instance().extensionRegistry().extensions().at(
id));
107 if (!this->dependency_)
108 WARN << QStringLiteral(
"Found '%1' but failed casting to expected type.").arg(
id);
109 }
catch (
const std::out_of_range &) { }
111 conn_add_ = QObject::connect(&App::instance().extensionRegistry(), &ExtensionRegistry::added,
112 [
this](Extension *e){ onRegistered(e);});
114 conn_rem_ = QObject::connect(&App::instance().extensionRegistry(), &ExtensionRegistry::removed,
115 [
this](Extension *e){ onDeregistered(e);});
120 QObject::disconnect(conn_add_);
121 QObject::disconnect(conn_rem_);
130 if (e->
id() != this->id_)
133 if (!this->dependency_)
135 if (
auto *d =
dynamic_cast<T*
>(e); d)
137 this->dependency_ = d;
142 WARN << QStringLiteral(
"Failed casting '%1' to expected type.").arg(this->id_);
145 CRIT <<
"WeakDependency already set. Internal logic error?";
151 if (e->
id() != this->id_)
154 if (this->dependency_)
156 if (
auto *d =
dynamic_cast<T*
>(e); d)
160 this->dependency_ =
nullptr;
163 WARN << QStringLiteral(
"Failed casting '%1' to expected type.").arg(this->id_);
166 CRIT <<
"WeakDependency already unset. Internal logic error?";
169 QMetaObject::Connection conn_add_;
170 QMetaObject::Connection conn_rem_;
Definition plugindependency.h:16
T * dependency_
Definition plugindependency.h:31
const T * get() const
Definition plugindependency.h:23
const T * operator->() const
Definition plugindependency.h:21
T * get()
Definition plugindependency.h:24
T * operator->()
Definition plugindependency.h:22
Abstract extension class.
Definition extension.h:19
virtual QString id() const =0
Returns the extension identifier.
Convenience holder class for hard plugin dependencies.
Definition plugindependency.h:47
StrongDependency(QString id)
Constructs a StrongDependency with id.
Definition plugindependency.h:55
Convenience holder class for soft plugin dependencies.
Definition plugindependency.h:95
~WeakDependency()
Definition plugindependency.h:118
WeakDependency(const QString &id, std::function< void(bool)> on_registered={})
Constructs a WeakDependency with id and callback on_registered.
Definition plugindependency.h:101
std::function< void(bool)> callback
Definition plugindependency.h:124
#define WARN
Creates a log object (level warning) you can use to pipe text into (<<).
Definition logging.h:27
#define CRIT
Creates a log object (level critial) you can use to pipe text into (<<).
Definition logging.h:32