Browse Source

change service registry semantics

pull/2/head
Skycoder42 7 years ago
parent
commit
d1d6c0b908
No known key found for this signature in database GPG Key ID: 8E01AD9EF0578D2B
  1. 2
      src/imports/mvvmcore/qqmlserviceregistry.cpp
  2. 2
      src/mvvmcore/qtmvvmcore_global.cpp
  3. 8
      src/mvvmcore/serviceregistry.cpp
  4. 58
      src/mvvmcore/serviceregistry.h
  5. 4
      src/mvvmquick/quickpresenter.cpp
  6. 4
      src/mvvmwidgets/widgetspresenter.cpp

2
src/imports/mvvmcore/qqmlserviceregistry.cpp

@ -37,7 +37,7 @@ void QQmlServiceRegistry::registerObject(const QUrl &componentUrl, bool weak)
Q_UNREACHABLE(); Q_UNREACHABLE();
return nullptr; return nullptr;
} }
}, {}, weak, ServiceRegistry::DestroyOnAppQuit); }, {}, ServiceRegistry::DestroyOnAppQuit, weak);
} }
QObject *QQmlServiceRegistry::service(const QString &iid) QObject *QQmlServiceRegistry::service(const QString &iid)

2
src/mvvmcore/qtmvvmcore_global.cpp

@ -12,7 +12,7 @@ void qtMvvmCoreStartup()
using namespace QtMvvm; using namespace QtMvvm;
registerInterfaceConverter<ISettingsSetupLoader>(); registerInterfaceConverter<ISettingsSetupLoader>();
try { try {
ServiceRegistry::instance()->registerInterface<ISettingsSetupLoader, SettingsSetupLoader>(true); ServiceRegistry::instance()->registerInterface<ISettingsSetupLoader, SettingsSetupLoader>(ServiceRegistry::DestroyOnAppDestroy, true);
} catch(ServiceExistsException &e) { } catch(ServiceExistsException &e) {
logDebug() << "Unable to register default ISettingsSetupLoader with error:" << e.what(); logDebug() << "Unable to register default ISettingsSetupLoader with error:" << e.what();
} }

8
src/mvvmcore/serviceregistry.cpp

@ -40,7 +40,7 @@ bool ServiceRegistry::isRegistered(const QByteArray &iid) const
return d->services.contains(iid); return d->services.contains(iid);
} }
void ServiceRegistry::registerService(const QByteArray &iid, const QMetaObject *metaObject, bool weak, DestructionScope scope) void ServiceRegistry::registerService(const QByteArray &iid, const QMetaObject *metaObject, DestructionScope scope, bool weak)
{ {
QMutexLocker _(&d->serviceMutex); QMutexLocker _(&d->serviceMutex);
if(d->serviceBlocked(iid)) if(d->serviceBlocked(iid))
@ -50,10 +50,10 @@ void ServiceRegistry::registerService(const QByteArray &iid, const QMetaObject *
void ServiceRegistry::registerService(const QByteArray &iid, const QMetaObject *metaObject, bool weak) void ServiceRegistry::registerService(const QByteArray &iid, const QMetaObject *metaObject, bool weak)
{ {
registerService(iid, metaObject, weak, DestroyOnAppDestroy); registerService(iid, metaObject, DestroyOnAppDestroy, weak);
} }
void ServiceRegistry::registerService(const QByteArray &iid, const std::function<QObject *(const QObjectList &)> &fn, QByteArrayList injectables, bool weak, DestructionScope scope) void ServiceRegistry::registerService(const QByteArray &iid, const std::function<QObject *(const QObjectList &)> &fn, QByteArrayList injectables, DestructionScope scope, bool weak)
{ {
QMutexLocker _(&d->serviceMutex); QMutexLocker _(&d->serviceMutex);
if(d->serviceBlocked(iid)) if(d->serviceBlocked(iid))
@ -63,7 +63,7 @@ void ServiceRegistry::registerService(const QByteArray &iid, const std::function
void ServiceRegistry::registerService(const QByteArray &iid, const std::function<QObject*(const QObjectList &)> &fn, QByteArrayList injectables, bool weak) void ServiceRegistry::registerService(const QByteArray &iid, const std::function<QObject*(const QObjectList &)> &fn, QByteArrayList injectables, bool weak)
{ {
registerService(iid, fn, std::move(injectables), weak, DestroyOnAppDestroy); registerService(iid, fn, std::move(injectables), DestroyOnAppDestroy, weak);
} }
QObject *ServiceRegistry::serviceObj(const QByteArray &iid) QObject *ServiceRegistry::serviceObj(const QByteArray &iid)

58
src/mvvmcore/serviceregistry.h

@ -40,41 +40,41 @@ public:
//! Register a service for its interface via the type //! Register a service for its interface via the type
template <typename TInterface, typename TService> template <typename TInterface, typename TService>
void registerInterface(bool weak = false, DestructionScope scope = DestroyOnAppDestroy); void registerInterface(DestructionScope scope = DestroyOnAppDestroy, bool weak = false);
//! Register a service for its interface via a constructor function //! Register a service for its interface via a constructor function
template <typename TInterface, typename TService, typename TFunc> template <typename TInterface, typename TService, typename TFunc>
void registerInterface(TFunc fn, bool weak = false, DestructionScope scope = DestroyOnAppDestroy); void registerInterface(TFunc fn, DestructionScope scope = DestroyOnAppDestroy, bool weak = false);
//! Register a service for its interface via an already existing instance //! Register a service for its interface via an already existing instance
template <typename TInterface, typename TService> template <typename TInterface, typename TService>
void registerInterface(TService *service, bool weak = false, DestructionScope scope = DestroyOnAppDestroy); void registerInterface(TService *service, DestructionScope scope = DestroyOnAppDestroy, bool weak = false);
//! Register a service via its type //! Register a service via its type
template <typename TService> template <typename TService>
void registerObject(bool weak = false, DestructionScope scope = DestroyOnAppDestroy); void registerObject(DestructionScope scope = DestroyOnAppDestroy, bool weak = false);
//! Register a service via a constructor function //! Register a service via a constructor function
template <typename TService, typename TFunc> template <typename TService, typename TFunc>
void registerObject(TFunc fn, bool weak = false, DestructionScope scope = DestroyOnAppDestroy); void registerObject(TFunc fn, DestructionScope scope = DestroyOnAppDestroy, bool weak = false);
//! Register a service via an already existing instance //! Register a service via an already existing instance
template <typename TService> template <typename TService>
void registerObject(TService *service, bool weak = false, DestructionScope scope = DestroyOnAppDestroy); void registerObject(TService *service, DestructionScope scope = DestroyOnAppDestroy, bool weak = false);
//! Register a service by an iid via their metadata //! Register a service by an iid via their metadata
void registerService(const QByteArray &iid, void registerService(const QByteArray &iid,
const QMetaObject *metaObject, const QMetaObject *metaObject,
bool weak, DestructionScope scope = DestroyOnAppDestroy,
DestructionScope scope); bool weak = false);
void registerService(const QByteArray &iid, Q_DECL_DEPRECATED void registerService(const QByteArray &iid,
const QMetaObject *metaObject, const QMetaObject *metaObject,
bool weak = false); //MAJOR merge methods bool weak);
//! Register a service by an iid via a generalized constructor function //! Register a service by an iid via a generalized constructor function
void registerService(const QByteArray &iid, void registerService(const QByteArray &iid,
const std::function<QObject*(const QObjectList &)> &fn, const std::function<QObject*(const QObjectList &)> &fn,
QByteArrayList injectables, QByteArrayList injectables,
bool weak, DestructionScope scope = DestroyOnAppDestroy,
DestructionScope scope); bool weak = false);
void registerService(const QByteArray &iid, Q_DECL_DEPRECATED void registerService(const QByteArray &iid,
const std::function<QObject*(const QObjectList &)> &fn, const std::function<QObject*(const QObjectList &)> &fn,
QByteArrayList injectables, QByteArrayList injectables,
bool weak = false);//MAJOR merge methods bool weak);
//! Returns the service for the given interface //! Returns the service for the given interface
template <typename TInterface> template <typename TInterface>
@ -169,29 +169,29 @@ bool ServiceRegistry::isRegistered() const
Q_ASSERT_X(qobject_interface_iid<TInterface*>(), Q_FUNC_INFO, "TInterface must be registered with Q_DECLARE_INTERFACE"); Q_ASSERT_X(qobject_interface_iid<TInterface*>(), Q_FUNC_INFO, "TInterface must be registered with Q_DECLARE_INTERFACE");
template<typename TInterface, typename TService> template<typename TInterface, typename TService>
void ServiceRegistry::registerInterface(bool weak, DestructionScope scope) void ServiceRegistry::registerInterface(DestructionScope scope, bool weak)
{ {
QTMVVM_SERVICE_ASSERT(TInterface, TService) QTMVVM_SERVICE_ASSERT(TInterface, TService)
registerService(qobject_interface_iid<TInterface*>(), &TService::staticMetaObject, weak, scope); registerService(qobject_interface_iid<TInterface*>(), &TService::staticMetaObject, scope, weak);
} }
template <typename TInterface, typename TService, typename TFunc> template <typename TInterface, typename TService, typename TFunc>
void ServiceRegistry::registerInterface(TFunc fn, bool weak, DestructionScope scope) void ServiceRegistry::registerInterface(TFunc fn, DestructionScope scope, bool weak)
{ {
QTMVVM_SERVICE_ASSERT(TInterface, TService) QTMVVM_SERVICE_ASSERT(TInterface, TService)
QByteArrayList injectables; QByteArrayList injectables;
auto packed_fn = __helpertypes::pack_function(std::move(fn), injectables); auto packed_fn = __helpertypes::pack_function(std::move(fn), injectables);
registerService(qobject_interface_iid<TInterface*>(), packed_fn, injectables, weak, scope); registerService(qobject_interface_iid<TInterface*>(), packed_fn, injectables, scope, weak);
} }
template<typename TInterface, typename TService> template<typename TInterface, typename TService>
void ServiceRegistry::registerInterface(TService *service, bool weak, DestructionScope scope) void ServiceRegistry::registerInterface(TService *service, DestructionScope scope, bool weak)
{ {
QTMVVM_SERVICE_ASSERT(TInterface, TService) QTMVVM_SERVICE_ASSERT(TInterface, TService)
registerService(qobject_interface_iid<TInterface*>(), [service](const QObjectList &params) -> QObject* { registerService(qobject_interface_iid<TInterface*>(), [service](const QObjectList &params) -> QObject* {
Q_UNUSED(params); Q_UNUSED(params);
return service; return service;
}, QByteArrayList(), weak, scope); }, QByteArrayList(), scope, weak);
} }
#undef QTMVVM_SERVICE_ASSERT #undef QTMVVM_SERVICE_ASSERT
@ -199,29 +199,29 @@ void ServiceRegistry::registerInterface(TService *service, bool weak, Destructio
static_assert(__helpertypes::is_qobj<tsvc>::value, "TService must be a qobject class"); static_assert(__helpertypes::is_qobj<tsvc>::value, "TService must be a qobject class");
template<typename TService> template<typename TService>
void ServiceRegistry::registerObject(bool weak, DestructionScope scope) void ServiceRegistry::registerObject(DestructionScope scope, bool weak)
{ {
QTMVVM_SERVICE_ASSERT(TService) QTMVVM_SERVICE_ASSERT(TService)
registerService(__helpertypes::qobject_iid<TService*>(), &TService::staticMetaObject, weak, scope); registerService(__helpertypes::qobject_iid<TService*>(), &TService::staticMetaObject, scope, weak);
} }
template<typename TService, typename TFunc> template<typename TService, typename TFunc>
void ServiceRegistry::registerObject(TFunc fn, bool weak, DestructionScope scope) void ServiceRegistry::registerObject(TFunc fn, DestructionScope scope, bool weak)
{ {
QTMVVM_SERVICE_ASSERT(TService) QTMVVM_SERVICE_ASSERT(TService)
QByteArrayList injectables; QByteArrayList injectables;
auto packed_fn = __helpertypes::pack_function(std::move(fn), injectables); auto packed_fn = __helpertypes::pack_function(std::move(fn), injectables);
registerService(__helpertypes::qobject_iid<TService*>(), packed_fn, injectables, weak, scope); registerService(__helpertypes::qobject_iid<TService*>(), packed_fn, injectables, scope, weak);
} }
template<typename TService> template<typename TService>
void ServiceRegistry::registerObject(TService *service, bool weak, DestructionScope scope) void ServiceRegistry::registerObject(TService *service, DestructionScope scope, bool weak)
{ {
QTMVVM_SERVICE_ASSERT(TService) QTMVVM_SERVICE_ASSERT(TService)
registerService(__helpertypes::qobject_iid<TService*>(), [service](const QObjectList &params) -> QObject* { registerService(__helpertypes::qobject_iid<TService*>(), [service](const QObjectList &params) -> QObject* {
Q_UNUSED(params); Q_UNUSED(params);
return service; return service;
}, QByteArrayList(), weak, scope); }, QByteArrayList(), scope, weak);
} }
#undef QTMVVM_SERVICE_ASSERT #undef QTMVVM_SERVICE_ASSERT

4
src/mvvmquick/quickpresenter.cpp

@ -17,8 +17,8 @@ namespace {
void qtMvvmQuickInit() void qtMvvmQuickInit()
{ {
qmlRegisterType<QUrlValidator>("de.skycoder42.QtMvvm.Quick.Private", 1, 0, "UrlValidator"); qmlRegisterType<QUrlValidator>("de.skycoder42.QtMvvm.Quick.Private", 1, 0, "UrlValidator");
QtMvvm::ServiceRegistry::instance()->registerObject<QtMvvm::InputViewFactory>(true); QtMvvm::ServiceRegistry::instance()->registerObject<QtMvvm::InputViewFactory>(QtMvvm::ServiceRegistry::DestroyOnAppDestroy, true);
QtMvvm::ServiceRegistry::instance()->registerInterface<QtMvvm::IPresenter, QtMvvm::QuickPresenter>(true); QtMvvm::ServiceRegistry::instance()->registerInterface<QtMvvm::IPresenter, QtMvvm::QuickPresenter>(QtMvvm::ServiceRegistry::DestroyOnAppDestroy, true);
} }
void initResources() void initResources()

4
src/mvvmwidgets/widgetspresenter.cpp

@ -28,8 +28,8 @@ namespace {
void qtMvvmWidgetsInit() void qtMvvmWidgetsInit()
{ {
QtMvvm::ServiceRegistry::instance()->registerObject<QtMvvm::InputWidgetFactory>(true); QtMvvm::ServiceRegistry::instance()->registerObject<QtMvvm::InputWidgetFactory>(QtMvvm::ServiceRegistry::DestroyOnAppDestroy, true);
QtMvvm::ServiceRegistry::instance()->registerInterface<QtMvvm::IPresenter, QtMvvm::WidgetsPresenter>(true); QtMvvm::ServiceRegistry::instance()->registerInterface<QtMvvm::IPresenter, QtMvvm::WidgetsPresenter>(QtMvvm::ServiceRegistry::DestroyOnAppDestroy, true);
} }
void initResources() void initResources()

Loading…
Cancel
Save