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;
28 template <
typename TInterface>
29 bool isRegistered()
const;
31 bool isRegistered(
const QByteArray &iid)
const;
34 template <
typename TInterface,
typename TService>
35 void registerInterface(
bool weak =
false);
37 template <
typename TInterface,
typename TService,
typename TFunc>
38 void registerInterface(
const TFunc &fn,
bool weak =
false);
40 template <
typename TInterface,
typename TService>
41 void registerInterface(TService *service,
bool weak =
false);
43 template <
typename TService>
44 void registerObject(
bool weak =
false);
46 template <
typename TService,
typename TFunc>
47 void registerObject(
const TFunc &fn,
bool weak =
false);
49 template <
typename TService>
50 void registerObject(TService *service,
bool weak =
false);
58 const std::function<
QObject*(
const QObjectList &)> &fn,
63 template <
typename TInterface>
64 TInterface *service();
69 void injectServices(
QObject *
object);
71 template <
typename TClass>
72 TClass *constructInjected(
QObject *parent =
nullptr);
77 friend class QtMvvm::ServiceRegistryPrivate;
89 const char *what()
const noexcept
override;
92 void raise()
const override;
112 const char *what()
const noexcept
override;
115 void raise()
const override;
134 void raise()
const override;
144 template<
typename TInterface>
147 return isRegistered(__helpertypes::inject_iid<TInterface*>());
150 #define QTMVVM_SERVICE_ASSERT(tint, tsvc) \ 151 static_assert(__helpertypes::is_valid_interface<TInterface, TService>::value, "TService must implement the given TInterface interface and be a qobject class"); \ 152 Q_ASSERT_X(qobject_interface_iid<TInterface*>(), Q_FUNC_INFO, "TInterface must be registered with Q_DECLARE_INTERFACE"); 154 template<
typename TInterface,
typename TService>
157 QTMVVM_SERVICE_ASSERT(TInterface, TService)
158 registerService(qobject_interface_iid<TInterface*>(), &TService::staticMetaObject, weak);
161 template <
typename TInterface,
typename TService,
typename TFunc>
164 QTMVVM_SERVICE_ASSERT(TInterface, TService)
166 auto packed_fn = __helpertypes::pack_function(fn, injectables);
167 registerService(qobject_interface_iid<TInterface*>(), packed_fn, injectables, weak);
170 template<
typename TInterface,
typename TService>
173 QTMVVM_SERVICE_ASSERT(TInterface, TService)
180 #undef QTMVVM_SERVICE_ASSERT 181 #define QTMVVM_SERVICE_ASSERT(tsvc) \ 182 static_assert(__helpertypes::is_qobj<tsvc>::value, "TService must be a qobject class"); 184 template<
typename TService>
187 QTMVVM_SERVICE_ASSERT(TService)
188 registerService(__helpertypes::qobject_iid<TService*>(), &TService::staticMetaObject, weak);
191 template<
typename TService,
typename TFunc>
194 QTMVVM_SERVICE_ASSERT(TService)
196 auto packed_fn = __helpertypes::pack_function(fn, injectables);
197 registerService(__helpertypes::qobject_iid<TService*>(), packed_fn, injectables, weak);
200 template<
typename TService>
203 QTMVVM_SERVICE_ASSERT(TService)
210 #undef QTMVVM_SERVICE_ASSERT 212 template<
typename TInterface>
215 return qobject_cast<TInterface*>(
serviceObj(__helpertypes::inject_iid<TInterface*>()));
218 template<
typename TClass>
221 static_assert(__helpertypes::is_qobj<TClass>::value,
"TClass must be a qobject class");
222 return qobject_cast<TClass*>(
constructInjected(&TClass::staticMetaObject, parent));
227 #endif // QTMVVM_SERVICEREGISTRY_H void registerObject(bool weak=false)
Register a service via its type.
TClass * constructInjected(QObject *parent=nullptr)
Constructs a new instance of TClass with properties injected.
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.
void registerService(const QByteArray &iid, const QMetaObject *metaObject, bool weak=false)
Register a service by an iid via their metadata.
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(bool weak=false)
Register a service for its interface via the type.