QtMvvm  1.1.0
A mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel
serviceregistry.h
1 #ifndef QTMVVM_SERVICEREGISTRY_H
2 #define QTMVVM_SERVICEREGISTRY_H
3 
4 #include <functional>
5 
6 #include <QtCore/qscopedpointer.h>
7 #include <QtCore/qvariant.h>
8 #include <QtCore/qexception.h>
9 
10 #include "QtMvvmCore/qtmvvmcore_global.h"
11 #include "QtMvvmCore/injection.h"
12 
13 namespace QtMvvm {
14 
15 class ServiceRegistryPrivate;
17 class Q_MVVMCORE_EXPORT ServiceRegistry //MAJOR make a QObject for invokable methods and Q_ENUM
18 {
19 public:
22  DestroyOnAppQuit = 1,
23  DestroyOnAppDestroy = 2,
24  DestroyOnRegistryDestroy = 3,
25 
26  DestroyNever = 127
27  };
28 
30  ServiceRegistry(ServiceRegistryPrivate *d_ptr);
31  ~ServiceRegistry();
32 
34  static ServiceRegistry* instance();
35 
37  template <typename TInterface>
38  bool isRegistered() const;
40  bool isRegistered(const QByteArray &iid) const;
41 
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);
51 
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);
61 
63  template <typename TInterface>
64  void registerPlugin(QString pluginType = {}, QString pluginKey = {}, DestructionScope scope = DestroyOnAppDestroy, bool weak = false);
65 
67  void registerService(const QByteArray &iid,
68  const QMetaObject *metaObject,
69  DestructionScope scope = DestroyOnAppDestroy,
70  bool weak = false);
71  Q_DECL_DEPRECATED void registerService(const QByteArray &iid,
72  const QMetaObject *metaObject,
73  bool weak);
75  void registerService(const QByteArray &iid,
76  const std::function<QObject*(const QObjectList &)> &fn,
77  QByteArrayList injectables,
78  DestructionScope scope = DestroyOnAppDestroy,
79  bool weak = false);
80  Q_DECL_DEPRECATED void registerService(const QByteArray &iid,
81  const std::function<QObject*(const QObjectList &)> &fn,
82  QByteArrayList injectables,
83  bool weak);
85  void registerService(QByteArray iid,
86  QString pluginType = {},
87  QString pluginKey = {},
88  DestructionScope scope = DestroyOnAppDestroy,
89  bool weak = false);
90 
92  template <typename TInterface>
93  TInterface *service();
95  QObject *serviceObj(const QByteArray &iid);
96 
98  void injectServices(QObject *object);
100  template <typename TClass>
101  TClass *constructInjected(QObject *parent = nullptr);
103  QObject *constructInjected(const QMetaObject *metaObject, QObject *parent = nullptr);
104 
105 private:
106  friend class QtMvvm::ServiceRegistryPrivate;
108 };
109 
111 class Q_MVVMCORE_EXPORT ServiceExistsException : public QException
112 {
113 public:
116 
118  const char *what() const noexcept override;
119 
121  void raise() const override;
123  QException *clone() const override;
124 
125 protected:
127  ServiceExistsException(const ServiceExistsException * const other);
128 
130  const QByteArray _msg;
131 };
132 
134 class Q_MVVMCORE_EXPORT ServiceConstructionException : public QException
135 {
136 public:
139 
141  const char *what() const noexcept override;
142 
144  void raise() const override;
146  QException *clone() const override;
147 
148 protected:
151 
153  const QByteArray _msg;
154 };
155 
158 {
159 public:
162 
163  void raise() const override;
164  QException *clone() const override;
165 
166 protected:
169 };
170 
171 // ------------- Generic Implementation -------------
172 
173 template<typename TInterface>
175 {
176  return isRegistered(__helpertypes::inject_iid<TInterface*>());
177 }
178 
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");
182 
183 template<typename TInterface, typename TService>
185 {
186  QTMVVM_SERVICE_ASSERT(TInterface, TService)
187  registerService(qobject_interface_iid<TInterface*>(), &TService::staticMetaObject, scope, weak);
188 }
189 
190 template <typename TInterface, typename TService, typename TFunc>
191 void ServiceRegistry::registerInterface(TFunc fn, DestructionScope scope, bool weak)
192 {
193  QTMVVM_SERVICE_ASSERT(TInterface, TService)
194  QByteArrayList injectables;
195  auto packed_fn = __helpertypes::pack_function(std::move(fn), injectables);
196  registerService(qobject_interface_iid<TInterface*>(), packed_fn, injectables, scope, weak);
197 }
198 
199 template<typename TInterface, typename TService>
200 void ServiceRegistry::registerInterface(TService *service, DestructionScope scope, bool weak)
201 {
202  QTMVVM_SERVICE_ASSERT(TInterface, TService)
203  registerService(qobject_interface_iid<TInterface*>(), [service](const QObjectList &params) -> QObject* {
204  Q_UNUSED(params);
205  return service;
206  }, QByteArrayList(), scope, weak);
207 }
208 
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");
212 
213 template<typename TService>
215 {
216  QTMVVM_SERVICE_ASSERT(TService)
217  registerService(__helpertypes::qobject_iid<TService*>(), &TService::staticMetaObject, scope, weak);
218 }
219 
220 template<typename TService, typename TFunc>
221 void ServiceRegistry::registerObject(TFunc fn, DestructionScope scope, bool weak)
222 {
223  QTMVVM_SERVICE_ASSERT(TService)
224  QByteArrayList injectables;
225  auto packed_fn = __helpertypes::pack_function(std::move(fn), injectables);
226  registerService(__helpertypes::qobject_iid<TService*>(), packed_fn, injectables, scope, weak);
227 }
228 
229 template<typename TService>
230 void ServiceRegistry::registerObject(TService *service, DestructionScope scope, bool weak)
231 {
232  QTMVVM_SERVICE_ASSERT(TService)
233  registerService(__helpertypes::qobject_iid<TService*>(), [service](const QObjectList &params) -> QObject* {
234  Q_UNUSED(params);
235  return service;
236  }, QByteArrayList(), scope, weak);
237 }
238 
239 #undef QTMVVM_SERVICE_ASSERT
240 
241 template<typename TInterface>
242 void ServiceRegistry::registerPlugin(QString pluginType, QString pluginKey, DestructionScope scope, bool weak)
243 {
244  registerService(qobject_interface_iid<TInterface*>(), std::move(pluginType), std::move(pluginKey), scope, weak);
245 }
246 
247 template<typename TInterface>
249 {
250  return qobject_cast<TInterface*>(serviceObj(__helpertypes::inject_iid<TInterface*>()));
251 }
252 
253 template<typename TClass>
255 {
256  static_assert(__helpertypes::is_qobj<TClass>::value, "TClass must be a qobject class");
257  return qobject_cast<TClass*>(constructInjected(&TClass::staticMetaObject, parent));
258 }
259 
260 }
261 
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.