Browse Source

improve register plugin api

pull/2/head
Skycoder42 7 years ago
parent
commit
fd9b3a24c5
No known key found for this signature in database GPG Key ID: 8E01AD9EF0578D2B
  1. 30
      src/imports/mvvmcore/plugins.qmltypes
  2. 9
      src/imports/mvvmcore/qqmlserviceregistry.cpp
  3. 6
      src/imports/mvvmcore/qqmlserviceregistry.h
  4. 2
      src/mvvmcore/mvvmcore.pro
  5. 18
      src/mvvmcore/serviceregistry.cpp
  6. 16
      src/mvvmcore/serviceregistry.h
  7. 6
      src/mvvmcore/serviceregistry_p.h

30
src/imports/mvvmcore/plugins.qmltypes

@ -485,6 +485,36 @@ Module {
name: "registerObject"
Parameter { name: "componentUrl"; type: "QUrl" }
}
Method {
name: "registerPlugin"
Parameter { name: "iid"; type: "string" }
Parameter { name: "pluginType"; type: "string" }
Parameter { name: "pluginKey"; type: "string" }
Parameter { name: "scope"; type: "DestructionScope" }
Parameter { name: "weak"; type: "bool" }
}
Method {
name: "registerPlugin"
Parameter { name: "iid"; type: "string" }
Parameter { name: "pluginType"; type: "string" }
Parameter { name: "pluginKey"; type: "string" }
Parameter { name: "scope"; type: "DestructionScope" }
}
Method {
name: "registerPlugin"
Parameter { name: "iid"; type: "string" }
Parameter { name: "pluginType"; type: "string" }
Parameter { name: "pluginKey"; type: "string" }
}
Method {
name: "registerPlugin"
Parameter { name: "iid"; type: "string" }
Parameter { name: "pluginType"; type: "string" }
}
Method {
name: "registerPlugin"
Parameter { name: "iid"; type: "string" }
}
Method {
name: "service"
type: "QObject*"

9
src/imports/mvvmcore/qqmlserviceregistry.cpp

@ -60,6 +60,15 @@ void QQmlServiceRegistry::registerObject(const QUrl &componentUrl, bool weak)
}, {}, ServiceRegistry::DestroyOnAppQuit, weak);
}
void QQmlServiceRegistry::registerPlugin(const QString &iid, QString pluginType, QString pluginKey, QQmlServiceRegistry::DestructionScope scope, bool weak)
{
ServiceRegistry::instance()->registerPlugin(iid.toUtf8(),
std::move(pluginType),
std::move(pluginKey),
static_cast<ServiceRegistry::DestructionScope>(scope),
weak);
}
QObject *QQmlServiceRegistry::service(const QString &iid)
{
return ServiceRegistry::instance()->serviceObj(iid.toUtf8());

6
src/imports/mvvmcore/qqmlserviceregistry.h

@ -32,6 +32,12 @@ public:
Q_INVOKABLE void registerObject(const QString &iid, const QJSValue &function, bool weak = false);
Q_INVOKABLE void registerObject(const QUrl &componentUrl, bool weak = false);
Q_INVOKABLE void registerPlugin(const QString &iid,
QString pluginType = {},
QString pluginKey = {},
DestructionScope scope = DestroyOnAppDestroy,
bool weak = false);
Q_INVOKABLE QObject *service(const QString &iid);
private:

2
src/mvvmcore/mvvmcore.pro

@ -76,3 +76,5 @@ never_true_for_lupdate {
SOURCES += $$files(../imports/mvvmcore/*.cpp) \
$$files(../imports/mvvmcore/*.qml)
}
mingw: LIBS_PRIVATE += -lQt5Gui -lQt5Core

18
src/mvvmcore/serviceregistry.cpp

@ -68,13 +68,13 @@ void ServiceRegistry::registerService(const QByteArray &iid, const std::function
registerService(iid, fn, std::move(injectables), DestroyOnAppDestroy, weak);
}
void ServiceRegistry::registerService(QByteArray iid, QString pluginKey, QString pluginType, ServiceRegistry::DestructionScope scope, bool weak)
void ServiceRegistry::registerPlugin(QByteArray iid, QString pluginType, QString pluginKey, ServiceRegistry::DestructionScope scope, bool weak)
{
QMutexLocker _(&d->serviceMutex);
if(d->serviceBlocked(iid))
throw ServiceExistsException(iid);
auto info = QSharedPointer<ServiceRegistryPrivate::PluginServiceInfo>::create(std::move(pluginKey),
std::move(pluginType),
auto info = QSharedPointer<ServiceRegistryPrivate::PluginServiceInfo>::create(std::move(pluginType),
std::move(pluginKey),
std::move(iid),
weak,
scope);
@ -276,10 +276,10 @@ QObject *ServiceRegistryPrivate::MetaServiceInfo::construct(ServiceRegistryPriva
ServiceRegistryPrivate::PluginServiceInfo::PluginServiceInfo(QString &&key, QString &&type, QByteArray &&iid, bool weak, ServiceRegistry::DestructionScope scope) :
ServiceRegistryPrivate::PluginServiceInfo::PluginServiceInfo(QString &&type, QString &&key, QByteArray &&iid, bool weak, ServiceRegistry::DestructionScope scope) :
ServiceInfo{weak, scope},
_key{std::move(key)},
_type{std::move(type)},
_key{std::move(key)},
_iid{std::move(iid)}
{}
@ -291,7 +291,13 @@ const QByteArray &ServiceRegistryPrivate::PluginServiceInfo::iid() const
QObject *ServiceRegistryPrivate::PluginServiceInfo::construct(ServiceRegistryPrivate *d) const
{
try {
QPluginFactoryBase factory{_type, _iid};
QFileInfo typeInfo{_type};
QPluginFactoryBase factory{typeInfo.isAbsolute() ? QString{} : _type, _iid};
if(typeInfo.isAbsolute()) {
factory.addSearchDir(typeInfo.absolutePath(), false);
factory.reloadPlugins();
}
QObject *obj = nullptr;
if(_key.isEmpty()){
if(!factory.allKeys().isEmpty())

16
src/mvvmcore/serviceregistry.h

@ -58,7 +58,7 @@ public:
void registerObject(TService *service, DestructionScope scope = DestroyOnAppDestroy, bool weak = false);
template <typename TInterface>
void registerPlugin(QString pluginKey, QString pluginType, DestructionScope scope = DestroyOnAppDestroy, bool weak = false);
void registerPlugin(QString pluginType = {}, QString pluginKey = {}, DestructionScope scope = DestroyOnAppDestroy, bool weak = false);
//! Register a service by an iid via their metadata
void registerService(const QByteArray &iid,
@ -78,11 +78,11 @@ public:
const std::function<QObject*(const QObjectList &)> &fn,
QByteArrayList injectables,
bool weak);
void registerService(QByteArray iid,
QString pluginKey,
QString pluginType,
DestructionScope scope = DestroyOnAppDestroy,
bool weak = false);
void registerPlugin(QByteArray iid,
QString pluginType = {},
QString pluginKey = {},
DestructionScope scope = DestroyOnAppDestroy,
bool weak = false);
//! Returns the service for the given interface
template <typename TInterface>
@ -235,9 +235,9 @@ void ServiceRegistry::registerObject(TService *service, DestructionScope scope,
#undef QTMVVM_SERVICE_ASSERT
template<typename TInterface>
void ServiceRegistry::registerPlugin(QString pluginKey, QString pluginType, DestructionScope scope, bool weak)
void ServiceRegistry::registerPlugin(QString pluginType, QString pluginKey, DestructionScope scope, bool weak)
{
registerService(qobject_interface_iid<TInterface*>(), std::move(pluginKey), std::move(pluginType), scope, weak);
registerPlugin(qobject_interface_iid<TInterface*>(), std::move(pluginType), std::move(pluginKey), scope, weak);
}
template<typename TInterface>

6
src/mvvmcore/serviceregistry_p.h

@ -57,8 +57,8 @@ public:
class PluginServiceInfo : public ServiceInfo {
public:
PluginServiceInfo(QString &&key,
QString &&type,
PluginServiceInfo(QString &&type,
QString &&key,
QByteArray &&iid,
bool weak,
ServiceRegistry::DestructionScope scope);
@ -69,8 +69,8 @@ public:
QObject *construct(ServiceRegistryPrivate *d) const final;
private:
QString _key;
QString _type;
QString _key;
QByteArray _iid;
};

Loading…
Cancel
Save