ctkServiceTracker.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #ifndef CTKSERVICETRACKER_H
  16. #define CTKSERVICETRACKER_H
  17. #include <QScopedPointer>
  18. #include "ctkPluginFrameworkExport.h"
  19. #include "ctkServiceReference.h"
  20. #include "ctkServiceTrackerCustomizer.h"
  21. #include "ctkLDAPSearchFilter.h"
  22. template<class S, class T> class ctkTrackedService;
  23. template<class S, class T> class ctkServiceTrackerPrivate;
  24. class ctkPluginContext;
  25. /**
  26. * The <code>ctkServiceTracker</code> class simplifies using services from the
  27. * Framework's service registry.
  28. * <p>
  29. * A <code>ctkServiceTracker</code> object is constructed with search criteria and
  30. * a <code>ctkServiceTrackerCustomizer</code> object. A <code>ctkServiceTracker</code>
  31. * can use a <code>ctkServiceTrackerCustomizer</code> to customize the service
  32. * objects to be tracked. The <code>ctkServiceTracker</code> can then be opened to
  33. * begin tracking all services in the Framework's service registry that match
  34. * the specified search criteria. The <code>ctkServiceTracker</code> correctly
  35. * handles all of the details of listening to <code>ctkServiceEvent</code>s and
  36. * getting and ungetting services.
  37. * <p>
  38. * The <code>getServiceReferences</code> method can be called to get references
  39. * to the services being tracked. The <code>getService</code> and
  40. * <code>getServices</code> methods can be called to get the service objects for
  41. * the tracked service.
  42. * <p>
  43. * The <code>ctkServiceTracker</code> class is thread-safe. It does not call a
  44. * <code>ctkServiceTrackerCustomizer</code> while holding any locks.
  45. * <code>ctkServiceTrackerCustomizer</code> implementations must also be
  46. * thread-safe.
  47. *
  48. * \tparam S The type of the service being tracked. The type must be an
  49. * assignable datatype. Further, if the
  50. * <code>ctkServiceTracker(ctkPluginContext*, ctkServiceTrackerCustomizer<T>*)</code>
  51. * constructor is used, the type must have an associated interface id via
  52. * Q_DECLARE_INTERFACE.
  53. * \tparam T The type of the tracked object. The type must be an assignable
  54. * datatype, provide a boolean conversion function, and provide
  55. * a constructor and an assignment operator which can handle 0 as an argument.
  56. * \threadsafe
  57. */
  58. template<class S = QObject*, class T = S>
  59. class ctkServiceTracker : protected ctkServiceTrackerCustomizer<T>
  60. {
  61. public:
  62. ~ctkServiceTracker();
  63. /**
  64. * Create a <code>ctkServiceTracker</code> on the specified
  65. * <code>ctkServiceReference</code>.
  66. *
  67. * <p>
  68. * The service referenced by the specified <code>ctkServiceReference</code>
  69. * will be tracked by this <code>ctkServiceTracker</code>.
  70. *
  71. * @param context The <code>ctkPluginContext</code> against which the tracking
  72. * is done.
  73. * @param reference The <code>ctkServiceReference</code> for the service to be
  74. * tracked.
  75. * @param customizer The customizer object to call when services are added,
  76. * modified, or removed in this <code>ctkServiceTracker</code>. If
  77. * customizer is <code>null</code>, then this
  78. * <code>ctkServiceTracker</code> will be used as the
  79. * <code>ctkServiceTrackerCustomizer</code> and this
  80. * <code>ctkServiceTracker</code> will call the
  81. * <code>ctkServiceTrackerCustomizer</code> methods on itself.
  82. */
  83. ctkServiceTracker(ctkPluginContext* context,
  84. const ctkServiceReference& reference,
  85. ctkServiceTrackerCustomizer<T>* customizer = 0);
  86. /**
  87. * Create a <code>ctkServiceTracker</code> on the specified class name.
  88. *
  89. * <p>
  90. * Services registered under the specified class name will be tracked by
  91. * this <code>ctkServiceTracker</code>.
  92. *
  93. * @param context The <code>ctkPluginContext</code> against which the tracking
  94. * is done.
  95. * @param clazz The class name of the services to be tracked.
  96. * @param customizer The customizer object to call when services are added,
  97. * modified, or removed in this <code>ctkServiceTracker</code>. If
  98. * customizer is <code>null</code>, then this
  99. * <code>ctkServiceTracker</code> will be used as the
  100. * <code>ctkServiceTrackerCustomizer</code> and this
  101. * <code>ctkServiceTracker</code> will call the
  102. * <code>ctkServiceTrackerCustomizer</code> methods on itself.
  103. */
  104. ctkServiceTracker(ctkPluginContext* context, const QString& clazz,
  105. ctkServiceTrackerCustomizer<T>* customizer = 0);
  106. /**
  107. * Create a <code>ctkServiceTracker</code> on the specified
  108. * <code>ctkLDAPSearchFilter</code> object.
  109. *
  110. * <p>
  111. * Services which match the specified <code>ctkLDAPSearchFilter</code> object will be
  112. * tracked by this <code>ctkServiceTracker</code>.
  113. *
  114. * @param context The <code>ctkPluginContext</code> against which the tracking
  115. * is done.
  116. * @param filter The <code>ctkLDAPSearchFilter</code> to select the services to be
  117. * tracked.
  118. * @param customizer The customizer object to call when services are added,
  119. * modified, or removed in this <code>ctkServiceTracker</code>. If
  120. * customizer is null, then this <code>ctkServiceTracker</code> will be
  121. * used as the <code>ctkServiceTrackerCustomizer</code> and this
  122. * <code>ctkServiceTracker</code> will call the
  123. * <code>ctkServiceTrackerCustomizer</code> methods on itself.
  124. */
  125. ctkServiceTracker(ctkPluginContext* context, const ctkLDAPSearchFilter& filter,
  126. ctkServiceTrackerCustomizer<T>* customizer = 0);
  127. /**
  128. * Create a <code>ctkServiceTracker</code> on the class template
  129. * argument S.
  130. *
  131. * <p>
  132. * Services registered under the interface name of the class template
  133. * argument S will be tracked by this <code>ctkServiceTracker</code>.
  134. *
  135. * @param context The <code>ctkPluginContext</code> against which the tracking
  136. * is done.
  137. * @param customizer The customizer object to call when services are added,
  138. * modified, or removed in this <code>ctkServiceTracker</code>. If
  139. * customizer is null, then this <code>ctkServiceTracker</code> will be
  140. * used as the <code>ctkServiceTrackerCustomizer</code> and this
  141. * <code>ctkServiceTracker</code> will call the
  142. * <code>ctkServiceTrackerCustomizer</code> methods on itself.
  143. */
  144. ctkServiceTracker(ctkPluginContext* context, ctkServiceTrackerCustomizer<T>* customizer = 0);
  145. /**
  146. * Open this <code>ctkServiceTracker</code> and begin tracking services.
  147. *
  148. * <p>
  149. * Services which match the search criteria specified when this
  150. * <code>ctkServiceTracker</code> was created are now tracked by this
  151. * <code>ctkServiceTracker</code>.
  152. *
  153. * @throws std::logic_error If the <code>ctkPluginContext</code>
  154. * with which this <code>ctkServiceTracker</code> was created is no
  155. * longer valid.
  156. */
  157. void open();
  158. /**
  159. * Close this <code>ctkServiceTracker</code>.
  160. *
  161. * <p>
  162. * This method should be called when this <code>ctkServiceTracker</code> should
  163. * end the tracking of services.
  164. *
  165. * <p>
  166. * This implementation calls getServiceReferences() to get the list
  167. * of tracked services to remove.
  168. */
  169. void close();
  170. /**
  171. * Wait for at least one service to be tracked by this
  172. * <code>ctkServiceTracker</code>. This method will also return when this
  173. * <code>ctkServiceTracker</code> is closed.
  174. *
  175. * <p>
  176. * It is strongly recommended that <code>waitForService</code> is not used
  177. * during the calling of the <code>ctkPluginActivator</code> methods.
  178. * <code>ctkPluginActivator</code> methods are expected to complete in a short
  179. * period of time.
  180. *
  181. * <p>
  182. * This implementation calls getService() to determine if a service
  183. * is being tracked.
  184. *
  185. * @param timeout The time interval in milliseconds to wait. If zero, the
  186. * method will wait indefinitely.
  187. * @return Returns the result of getService().
  188. */
  189. T waitForService(unsigned long timeout);
  190. /**
  191. * Return a list of <code>ctkServiceReference</code>s for all services being
  192. * tracked by this <code>ctkServiceTracker</code>.
  193. *
  194. * @return List of <code>ctkServiceReference</code>s.
  195. */
  196. QList<ctkServiceReference> getServiceReferences() const;
  197. /**
  198. * Returns a <code>ctkServiceReference</code> for one of the services being
  199. * tracked by this <code>ctkServiceTracker</code>.
  200. *
  201. * <p>
  202. * If multiple services are being tracked, the service with the highest
  203. * ranking (as specified in its <code>service.ranking</code> property) is
  204. * returned. If there is a tie in ranking, the service with the lowest
  205. * service ID (as specified in its <code>service.id</code> property); that
  206. * is, the service that was registered first is returned. This is the same
  207. * algorithm used by <code>ctkPluginContext::getServiceReference()</code>.
  208. *
  209. * <p>
  210. * This implementation calls getServiceReferences() to get the list
  211. * of references for the tracked services.
  212. *
  213. * @return A <code>ctkServiceReference</code> for a tracked service.
  214. * @throws ctkServiceException if no services are being tracked.
  215. */
  216. ctkServiceReference getServiceReference() const;
  217. /**
  218. * Returns the service object for the specified
  219. * <code>ctkServiceReference</code> if the specified referenced service is
  220. * being tracked by this <code>ctkServiceTracker</code>.
  221. *
  222. * @param reference The reference to the desired service.
  223. * @return A service object or <code>null</code> if the service referenced
  224. * by the specified <code>ctkServiceReference</code> is not being
  225. * tracked.
  226. */
  227. T getService(const ctkServiceReference& reference) const;
  228. /**
  229. * Return a list of service objects for all services being tracked by this
  230. * <code>ctkServiceTracker</code>.
  231. *
  232. * <p>
  233. * This implementation calls getServiceReferences() to get the list
  234. * of references for the tracked services and then calls
  235. * getService(const ctkServiceReference&) for each reference to get the
  236. * tracked service object.
  237. *
  238. * @return A list of service objects or an empty list if no services
  239. * are being tracked.
  240. */
  241. QList<T> getServices() const;
  242. /**
  243. * Returns a service object for one of the services being tracked by this
  244. * <code>ctkServiceTracker</code>.
  245. *
  246. * <p>
  247. * If any services are being tracked, this implementation returns the result
  248. * of calling <code>getService(getServiceReference())</code>.
  249. *
  250. * @return A service object or <code>null</code> if no services are being
  251. * tracked.
  252. */
  253. T getService() const;
  254. /**
  255. * Remove a service from this <code>ctkServiceTracker</code>.
  256. *
  257. * The specified service will be removed from this
  258. * <code>ctkServiceTracker</code>. If the specified service was being tracked
  259. * then the <code>ctkServiceTrackerCustomizer::removedService</code> method will
  260. * be called for that service.
  261. *
  262. * @param reference The reference to the service to be removed.
  263. */
  264. void remove(const ctkServiceReference& reference);
  265. /**
  266. * Return the number of services being tracked by this
  267. * <code>ctkServiceTracker</code>.
  268. *
  269. * @return The number of services being tracked.
  270. */
  271. int size() const;
  272. /**
  273. * Returns the tracking count for this <code>ctkServiceTracker</code>.
  274. *
  275. * The tracking count is initialized to 0 when this
  276. * <code>ctkServiceTracker</code> is opened. Every time a service is added,
  277. * modified or removed from this <code>ctkServiceTracker</code>, the tracking
  278. * count is incremented.
  279. *
  280. * <p>
  281. * The tracking count can be used to determine if this
  282. * <code>ctkServiceTracker</code> has added, modified or removed a service by
  283. * comparing a tracking count value previously collected with the current
  284. * tracking count value. If the value has not changed, then no service has
  285. * been added, modified or removed from this <code>ctkServiceTracker</code>
  286. * since the previous tracking count was collected.
  287. *
  288. * @return The tracking count for this <code>ctkServiceTracker</code> or -1 if
  289. * this <code>ctkServiceTracker</code> is not open.
  290. */
  291. int getTrackingCount() const;
  292. protected:
  293. /**
  294. * Default implementation of the
  295. * <code>ctkServiceTrackerCustomizer::addingService</code> method.
  296. *
  297. * <p>
  298. * This method is only called when this <code>ctkServiceTracker</code> has been
  299. * constructed with a <code>null</code> ctkServiceTrackerCustomizer argument.
  300. *
  301. * <p>
  302. * This implementation returns the result of calling <code>getService</code>
  303. * on the <code>ctkPluginContext</code> with which this
  304. * <code>ctkServiceTracker</code> was created passing the specified
  305. * <code>ctkServiceReference</code>.
  306. * <p>
  307. * This method can be overridden in a subclass to customize the service
  308. * object to be tracked for the service being added. In that case, take care
  309. * not to rely on the default implementation of
  310. * \link removedService(const ctkServiceReference&, QObject*) removedService\endlink
  311. * to unget the service.
  312. *
  313. * @param reference The reference to the service being added to this
  314. * <code>ctkServiceTracker</code>.
  315. * @return The service object to be tracked for the service added to this
  316. * <code>ctlServiceTracker</code>.
  317. * @see ctkServiceTrackerCustomizer::addingService(const ctkServiceReference&)
  318. */
  319. T addingService(const ctkServiceReference& reference);
  320. /**
  321. * Default implementation of the
  322. * <code>ctkServiceTrackerCustomizer::modifiedService</code> method.
  323. *
  324. * <p>
  325. * This method is only called when this <code>ctkServiceTracker</code> has been
  326. * constructed with a <code>null</code> ctkServiceTrackerCustomizer argument.
  327. *
  328. * <p>
  329. * This implementation does nothing.
  330. *
  331. * @param reference The reference to modified service.
  332. * @param service The service object for the modified service.
  333. * @see ctkServiceTrackerCustomizer::modifiedService(const ctkServiceReference&, QObject*)
  334. */
  335. void modifiedService(const ctkServiceReference& reference, T service);
  336. /**
  337. * Default implementation of the
  338. * <code>ctkServiceTrackerCustomizer::removedService</code> method.
  339. *
  340. * <p>
  341. * This method is only called when this <code>ctkServiceTracker</code> has been
  342. * constructed with a <code>null</code> ctkServiceTrackerCustomizer argument.
  343. *
  344. * <p>
  345. * This implementation calls <code>ungetService</code>, on the
  346. * <code>ctkPluginContext</code> with which this <code>ctkServiceTracker</code>
  347. * was created, passing the specified <code>ctkServiceReference</code>.
  348. * <p>
  349. * This method can be overridden in a subclass. If the default
  350. * implementation of \link addingService(const ctkServiceReference&) addingService\endlink
  351. * method was used, this method must unget the service.
  352. *
  353. * @param reference The reference to removed service.
  354. * @param service The service object for the removed service.
  355. * @see ctkServiceTrackerCustomizer::removedService(const ServiceReference&, QObject*)
  356. */
  357. void removedService(const ctkServiceReference& reference, T service);
  358. private:
  359. typedef ctkServiceTracker<S,T> ServiceTracker;
  360. typedef ctkTrackedService<S,T> TrackedService;
  361. typedef ctkServiceTrackerPrivate<S,T> ServiceTrackerPrivate;
  362. typedef ctkServiceTrackerCustomizer<T> ServiceTrackerCustomizer;
  363. friend class ctkTrackedService<S,T>;
  364. friend class ctkServiceTrackerPrivate<S,T>;
  365. inline ServiceTrackerPrivate* d_func()
  366. {
  367. return reinterpret_cast<ServiceTrackerPrivate*>(qGetPtrHelper(d_ptr));
  368. }
  369. inline const ServiceTrackerPrivate* d_func() const
  370. {
  371. return reinterpret_cast<const ServiceTrackerPrivate*>(qGetPtrHelper(d_ptr));
  372. }
  373. const QScopedPointer<ServiceTrackerPrivate> d_ptr;
  374. };
  375. #include "ctkServiceTracker.tpp"
  376. #endif // CTKSERVICETRACKER_H