5#include <QCoreApplication>
7#include <albert/export.h>
24 inline operator bool()
const {
return dependency_ !=
nullptr; }
60 if (!this->dependency_)
61 throw std::runtime_error(
62 QCoreApplication::translate(
64 "Extension '%1' is available, but it is not of the expected type."
65 ).arg(
id).toStdString());
67 catch (
const std::out_of_range &)
69 throw std::runtime_error(
70 QCoreApplication::translate(
72 "The required extension '%1' is not available."
73 ).arg(
id).toStdString());
98 explicit WeakDependency(
const QString &
id, std::function<
void(
bool)> on_registered = {}):
99 callback(on_registered),
104 if (!this->dependency_)
105 WARN << QString(
"Found '%1' but failed casting to expected type.").arg(
id);
106 }
catch (
const std::out_of_range &) { }
109 [
this](Extension *e){ onRegistered(e);});
112 [
this](Extension *e){ onDeregistered(e);});
117 QObject::disconnect(conn_add_);
118 QObject::disconnect(conn_rem_);
127 if (e->
id() != this->id_)
130 if (!this->dependency_)
132 if (
auto *d =
dynamic_cast<T*
>(e); d)
134 this->dependency_ = d;
139 WARN << QString(
"Failed casting '%1' to expected type.").arg(this->id_);
142 CRIT <<
"WeakDependency already set. Internal logic error?";
148 if (e->
id() != this->id_)
151 if (this->dependency_)
153 if (
auto *d =
dynamic_cast<T*
>(e); d)
157 this->dependency_ =
nullptr;
160 WARN << QString(
"Failed casting '%1' to expected type.").arg(this->id_);
163 CRIT <<
"WeakDependency already unset. Internal logic error?";
166 QMetaObject::Connection conn_add_;
167 QMetaObject::Connection conn_rem_;
Base class for StrongDependency and WeakDependency.
Definition plugindependency.h:20
T * dependency_
Definition plugindependency.h:32
const T * get() const
Definition plugindependency.h:27
const T * operator->() const
Definition plugindependency.h:25
T * get()
Definition plugindependency.h:28
T * operator->()
Definition plugindependency.h:26
const std::map< QString, Extension * > & extensions() const
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:46
StrongDependency(QString id)
Constructs a StrongDependency with id.
Definition plugindependency.h:54
Convenience holder class for plugin soft dependencies.
Definition plugindependency.h:92
~WeakDependency()
Definition plugindependency.h:115
WeakDependency(const QString &id, std::function< void(bool)> on_registered={})
Constructs a WeakDependency with id and callback on_registered.
Definition plugindependency.h:98
std::function< void(bool)> callback
Definition plugindependency.h:121
#define WARN
Creates a log object (level warning) you can use to pipe text into (<<).
Definition logging.h:20
#define CRIT
Creates a log object (level critial) you can use to pipe text into (<<).
Definition logging.h:23
const ExtensionRegistry & extensionRegistry()
The extension registry.