1 #ifndef QTMVVM_SERVICEREGISTRY_H     2 #define QTMVVM_SERVICEREGISTRY_H     6 #include <QtCore/qscopedpointer.h>     7 #include <QtCore/qvariant.h>     8 #include <QtCore/qexception.h>    10 #include "QtMvvmCore/qtmvvmcore_global.h"    11 #include "QtMvvmCore/injection.h"    15 class ServiceRegistryPrivate;
    23         DestroyOnAppDestroy = 2, 
    24         DestroyOnRegistryDestroy = 3, 
    37     template <
typename TInterface>
    38     bool isRegistered() 
const;
    40     bool isRegistered(
const QByteArray &iid) 
const;
    43     template <
typename TInterface, 
typename TService>
    44     void registerInterface(DestructionScope scope = DestroyOnAppDestroy, 
bool weak = 
false);
    46     template <
typename TInterface, 
typename TService, 
typename TFunc>
    47     void registerInterface(TFunc fn, DestructionScope scope = DestroyOnAppDestroy, 
bool weak = 
false);
    49     template <
typename TInterface, 
typename TService>
    50     void registerInterface(TService *service, DestructionScope scope = DestroyOnAppDestroy, 
bool weak = 
false);
    53     template <
typename TService>
    54     void registerObject(DestructionScope scope = DestroyOnAppDestroy, 
bool weak = 
false);
    56     template <
typename TService, 
typename TFunc>
    57     void registerObject(TFunc fn, DestructionScope scope = DestroyOnAppDestroy, 
bool weak = 
false);
    59     template <
typename TService>
    60     void registerObject(TService *service, DestructionScope scope = DestroyOnAppDestroy, 
bool weak = 
false);
    63     template <
typename TInterface>
    64     void registerPlugin(
QString pluginType = {}, 
QString pluginKey = {}, DestructionScope scope = DestroyOnAppDestroy, 
bool weak = 
false);
    69                          DestructionScope scope = DestroyOnAppDestroy,
    71     Q_DECL_DEPRECATED 
void registerService(
const QByteArray &iid,
    76                          const std::function<
QObject*(
const QObjectList &)> &fn,
    78                          DestructionScope scope = DestroyOnAppDestroy,
    80     Q_DECL_DEPRECATED 
void registerService(
const QByteArray &iid,
    81                                            const std::function<
QObject*(
const QObjectList &)> &fn,
    88                          DestructionScope scope = DestroyOnAppDestroy,
    92     template <
typename TInterface>
    93     TInterface *service();
    98     void injectServices(
QObject *
object);
   100     template <
typename TClass>
   101     TClass *constructInjected(
QObject *parent = 
nullptr);
   106     friend class QtMvvm::ServiceRegistryPrivate;
   118     const char *what() 
const noexcept 
override;
   121     void raise() 
const override;
   141     const char *what() 
const noexcept 
override;
   144     void raise() 
const override;
   163     void raise() 
const override;
   173 template<
typename TInterface>
   176     return isRegistered(__helpertypes::inject_iid<TInterface*>());
   179 #define QTMVVM_SERVICE_ASSERT(tint, tsvc) \   180     static_assert(__helpertypes::is_valid_interface<TInterface, TService>::value, "TService must implement the given TInterface interface and be a qobject class"); \   181     Q_ASSERT_X(qobject_interface_iid<TInterface*>(), Q_FUNC_INFO, "TInterface must be registered with Q_DECLARE_INTERFACE");   183 template<
typename TInterface, 
typename TService>
   186     QTMVVM_SERVICE_ASSERT(TInterface, TService)
   187     registerService(qobject_interface_iid<TInterface*>(), &TService::staticMetaObject, scope, weak);
   190 template <
typename TInterface, 
typename TService, 
typename TFunc>
   193     QTMVVM_SERVICE_ASSERT(TInterface, TService)
   195     auto packed_fn = __helpertypes::pack_function(std::move(fn), injectables);
   196     registerService(qobject_interface_iid<TInterface*>(), packed_fn, injectables, scope, weak);
   199 template<
typename TInterface, 
typename TService>
   202     QTMVVM_SERVICE_ASSERT(TInterface, TService)
   209 #undef QTMVVM_SERVICE_ASSERT   210 #define QTMVVM_SERVICE_ASSERT(tsvc) \   211     static_assert(__helpertypes::is_qobj<tsvc>::value, "TService must be a qobject class");   213 template<
typename TService>
   216     QTMVVM_SERVICE_ASSERT(TService)
   217     registerService(__helpertypes::qobject_iid<TService*>(), &TService::staticMetaObject, scope, weak);
   220 template<
typename TService, 
typename TFunc>
   223     QTMVVM_SERVICE_ASSERT(TService)
   225     auto packed_fn = __helpertypes::pack_function(std::move(fn), injectables);
   226     registerService(__helpertypes::qobject_iid<TService*>(), packed_fn, injectables, scope, weak);
   229 template<
typename TService>
   232     QTMVVM_SERVICE_ASSERT(TService)
   239 #undef QTMVVM_SERVICE_ASSERT   241 template<
typename TInterface>
   244     registerService(qobject_interface_iid<TInterface*>(), std::move(pluginType), std::move(pluginKey), scope, weak);
   247 template<
typename TInterface>
   250     return qobject_cast<TInterface*>(
serviceObj(__helpertypes::inject_iid<TInterface*>()));
   253 template<
typename TClass>
   256     static_assert(__helpertypes::is_qobj<TClass>::value, 
"TClass must be a qobject class");
   257     return qobject_cast<TClass*>(
constructInjected(&TClass::staticMetaObject, parent));
   262 #endif // QTMVVM_SERVICEREGISTRY_H void registerService(const QByteArray &iid, const QMetaObject *metaObject, DestructionScope scope=DestroyOnAppDestroy, bool weak=false)
Register a service by an iid via their metadata. 
 
TClass * constructInjected(QObject *parent=nullptr)
Constructs a new instance of TClass with properties injected. 
 
void registerObject(DestructionScope scope=DestroyOnAppDestroy, bool weak=false)
Register a service via its type. 
 
void registerPlugin(QString pluginType={}, QString pluginKey={}, DestructionScope scope=DestroyOnAppDestroy, bool weak=false)
Register a service via specifing a plugin to be loaded. 
 
Is thrown if a service could not be created because of a missing dependency to be injected...
 
bool isRegistered() const
Checks if a given interface or service is already registered. 
 
Is thrown if a service is beeing registered that is already registered. 
 
A singleton to prepare services for dependency injection and to access them. 
 
DestructionScope
A scope to indicate when a service should be deleted. 
 
TInterface * service()
Returns the service for the given interface. 
 
Is thrown in case the construction of a service has failed. 
 
QObject * serviceObj(const QByteArray &iid)
Returns the service for the given iid. 
 
The primary namespace of the QtMvvm library. 
 
void registerInterface(DestructionScope scope=DestroyOnAppDestroy, bool weak=false)
Register a service for its interface via the type.