5#include <QCoreApplication>
7#include <albert/export.h>
20 inline operator bool()
const {
return dependency_ !=
nullptr; }
59 if (!this->dependency_)
60 throw std::runtime_error(
61 QCoreApplication::translate(
63 "Extension '%1' is available, but it is not of the expected type."
64 ).arg(
id).toStdString());
66 catch (
const std::out_of_range &)
68 throw std::runtime_error(
69 QCoreApplication::translate(
71 "The required extension '%1' is not available."
72 ).arg(
id).toStdString());
97 explicit WeakDependency(
const QString &
id, std::function<
void(
bool)> on_registered = {}):
98 callback(on_registered),
103 if (!this->dependency_)
104 WARN << QStringLiteral(
"Found '%1' but failed casting to expected type.").arg(
id);
105 }
catch (
const std::out_of_range &) { }
108 [
this](Extension *e){ onRegistered(e);});
111 [
this](Extension *e){ onDeregistered(e);});
116 QObject::disconnect(conn_add_);
117 QObject::disconnect(conn_rem_);
126 if (e->
id() != this->id_)
129 if (!this->dependency_)
131 if (
auto *d =
dynamic_cast<T*
>(e); d)
133 this->dependency_ = d;
138 WARN << QStringLiteral(
"Failed casting '%1' to expected type.").arg(this->id_);
141 CRIT <<
"WeakDependency already set. Internal logic error?";
147 if (e->
id() != this->id_)
150 if (this->dependency_)
152 if (
auto *d =
dynamic_cast<T*
>(e); d)
156 this->dependency_ =
nullptr;
159 WARN << QStringLiteral(
"Failed casting '%1' to expected type.").arg(this->id_);
162 CRIT <<
"WeakDependency already unset. Internal logic error?";
165 QMetaObject::Connection conn_add_;
166 QMetaObject::Connection conn_rem_;
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.
Definition plugindependency.h:16
const T * operator->() const
Definition plugindependency.h:21
T * get()
Definition plugindependency.h:24
T * operator->()
Definition plugindependency.h:22
const T * get() const
Definition plugindependency.h:23
T * dependency_
Definition plugindependency.h:31
Convenience holder class for plugin hard dependencies.
Definition plugindependency.h:45
StrongDependency(QString id)
Constructs a StrongDependency with id.
Definition plugindependency.h:53
Convenience holder class for plugin soft dependencies.
Definition plugindependency.h:91
WeakDependency(const QString &id, std::function< void(bool)> on_registered={})
Constructs a WeakDependency with id and callback on_registered.
Definition plugindependency.h:97
~WeakDependency()
Definition plugindependency.h:114
std::function< void(bool)> callback
Definition plugindependency.h:120
#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
Albert utility namespace.
Definition albert.h:14
const ExtensionRegistry & extensionRegistry()
Returns a const reference to the central ExtensionRegistry.