ctkServiceTracker.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. If the
  82. * customizer is not <code>null</code>, this <code>ctkServiceTracker</code>
  83. * takes ownership of the customizer.
  84. */
  85. ctkServiceTracker(ctkPluginContext* context,
  86. const ctkServiceReference& reference,
  87. ctkServiceTrackerCustomizer<T>* customizer = 0);
  88. /**
  89. * Create a <code>ctkServiceTracker</code> on the specified class name.
  90. *
  91. * <p>
  92. * Services registered under the specified class name will be tracked by
  93. * this <code>ctkServiceTracker</code>.
  94. *
  95. * @param context The <code>ctkPluginContext</code> against which the tracking
  96. * is done.
  97. * @param clazz The class name of the services to be tracked.
  98. * @param customizer The customizer object to call when services are added,
  99. * modified, or removed in this <code>ctkServiceTracker</code>. If
  100. * customizer is <code>null</code>, then this
  101. * <code>ctkServiceTracker</code> will be used as the
  102. * <code>ctkServiceTrackerCustomizer</code> and this
  103. * <code>ctkServiceTracker</code> will call the
  104. * <code>ctkServiceTrackerCustomizer</code> methods on itself. If the
  105. * customizer is not <code>null</code>, this <code>ctkServiceTracker</code>
  106. * takes ownership of the customizer.
  107. */
  108. ctkServiceTracker(ctkPluginContext* context, const QString& clazz,
  109. ctkServiceTrackerCustomizer<T>* customizer = 0);
  110. /**
  111. * Create a <code>ctkServiceTracker</code> on the specified
  112. * <code>ctkLDAPSearchFilter</code> object.
  113. *
  114. * <p>
  115. * Services which match the specified <code>ctkLDAPSearchFilter</code> object will be
  116. * tracked by this <code>ctkServiceTracker</code>.
  117. *
  118. * @param context The <code>ctkPluginContext</code> against which the tracking
  119. * is done.
  120. * @param filter The <code>ctkLDAPSearchFilter</code> to select the services to be
  121. * tracked.
  122. * @param customizer The customizer object to call when services are added,
  123. * modified, or removed in this <code>ctkServiceTracker</code>. If
  124. * customizer is null, then this <code>ctkServiceTracker</code> will be
  125. * used as the <code>ctkServiceTrackerCustomizer</code> and this
  126. * <code>ctkServiceTracker</code> will call the
  127. * <code>ctkServiceTrackerCustomizer</code> methods on itself. If the
  128. * customizer is not <code>null</code>, this <code>ctkServiceTracker</code>
  129. * takes ownership of the customizer.
  130. */
  131. ctkServiceTracker(ctkPluginContext* context, const ctkLDAPSearchFilter& filter,
  132. ctkServiceTrackerCustomizer<T>* customizer = 0);
  133. /**
  134. * Create a <code>ctkServiceTracker</code> on the class template
  135. * argument S.
  136. *
  137. * <p>
  138. * Services registered under the interface name of the class template
  139. * argument S will be tracked by this <code>ctkServiceTracker</code>.
  140. *
  141. * @param context The <code>ctkPluginContext</code> against which the tracking
  142. * is done.
  143. * @param customizer The customizer object to call when services are added,
  144. * modified, or removed in this <code>ctkServiceTracker</code>. If
  145. * customizer is null, then this <code>ctkServiceTracker</code> will be
  146. * used as the <code>ctkServiceTrackerCustomizer</code> and this
  147. * <code>ctkServiceTracker</code> will call the
  148. * <code>ctkServiceTrackerCustomizer</code> methods on itself. If the
  149. * customizer is not <code>null</code>, this <code>ctkServiceTracker</code>
  150. * takes ownership of the customizer.
  151. */
  152. ctkServiceTracker(ctkPluginContext* context, ctkServiceTrackerCustomizer<T>* customizer = 0);
  153. /**
  154. * Open this <code>ctkServiceTracker</code> and begin tracking services.
  155. *
  156. * <p>
  157. * Services which match the search criteria specified when this
  158. * <code>ctkServiceTracker</code> was created are now tracked by this
  159. * <code>ctkServiceTracker</code>.
  160. *
  161. * @throws std::logic_error If the <code>ctkPluginContext</code>
  162. * with which this <code>ctkServiceTracker</code> was created is no
  163. * longer valid.
  164. */
  165. void open();
  166. /**
  167. * Close this <code>ctkServiceTracker</code>.
  168. *
  169. * <p>
  170. * This method should be called when this <code>ctkServiceTracker</code> should
  171. * end the tracking of services.
  172. *
  173. * <p>
  174. * This implementation calls getServiceReferences() to get the list
  175. * of tracked services to remove.
  176. */
  177. void close();
  178. /**
  179. * Wait for at least one service to be tracked by this
  180. * <code>ctkServiceTracker</code>. This method will also return when this
  181. * <code>ctkServiceTracker</code> is closed.
  182. *
  183. * <p>
  184. * It is strongly recommended that <code>waitForService</code> is not used
  185. * during the calling of the <code>ctkPluginActivator</code> methods.
  186. * <code>ctkPluginActivator</code> methods are expected to complete in a short
  187. * period of time.
  188. *
  189. * <p>
  190. * This implementation calls getService() to determine if a service
  191. * is being tracked.
  192. *
  193. * @param timeout The time interval in milliseconds to wait. If zero, the
  194. * method will wait indefinitely.
  195. * @return Returns the result of getService().
  196. */
  197. T waitForService(unsigned long timeout);
  198. /**
  199. * Return a list of <code>ctkServiceReference</code>s for all services being
  200. * tracked by this <code>ctkServiceTracker</code>.
  201. *
  202. * @return List of <code>ctkServiceReference</code>s.
  203. */
  204. QList<ctkServiceReference> getServiceReferences() const;
  205. /**
  206. * Returns a <code>ctkServiceReference</code> for one of the services being
  207. * tracked by this <code>ctkServiceTracker</code>.
  208. *
  209. * <p>
  210. * If multiple services are being tracked, the service with the highest
  211. * ranking (as specified in its <code>service.ranking</code> property) is
  212. * returned. If there is a tie in ranking, the service with the lowest
  213. * service ID (as specified in its <code>service.id</code> property); that
  214. * is, the service that was registered first is returned. This is the same
  215. * algorithm used by <code>ctkPluginContext::getServiceReference()</code>.
  216. *
  217. * <p>
  218. * This implementation calls getServiceReferences() to get the list
  219. * of references for the tracked services.
  220. *
  221. * @return A <code>ctkServiceReference</code> for a tracked service.
  222. * @throws ctkServiceException if no services are being tracked.
  223. */
  224. ctkServiceReference getServiceReference() const;
  225. /**
  226. * Returns the service object for the specified
  227. * <code>ctkServiceReference</code> if the specified referenced service is
  228. * being tracked by this <code>ctkServiceTracker</code>.
  229. *
  230. * @param reference The reference to the desired service.
  231. * @return A service object or <code>null</code> if the service referenced
  232. * by the specified <code>ctkServiceReference</code> is not being
  233. * tracked.
  234. */
  235. T getService(const ctkServiceReference& reference) const;
  236. /**
  237. * Return a list of service objects for all services being tracked by this
  238. * <code>ctkServiceTracker</code>.
  239. *
  240. * <p>
  241. * This implementation calls getServiceReferences() to get the list
  242. * of references for the tracked services and then calls
  243. * getService(const ctkServiceReference&) for each reference to get the
  244. * tracked service object.
  245. *
  246. * @return A list of service objects or an empty list if no services
  247. * are being tracked.
  248. */
  249. QList<T> getServices() const;
  250. /**
  251. * Returns a service object for one of the services being tracked by this
  252. * <code>ctkServiceTracker</code>.
  253. *
  254. * <p>
  255. * If any services are being tracked, this implementation returns the result
  256. * of calling <code>getService(getServiceReference())</code>.
  257. *
  258. * @return A service object or <code>null</code> if no services are being
  259. * tracked.
  260. */
  261. T getService() const;
  262. /**
  263. * Remove a service from this <code>ctkServiceTracker</code>.
  264. *
  265. * The specified service will be removed from this
  266. * <code>ctkServiceTracker</code>. If the specified service was being tracked
  267. * then the <code>ctkServiceTrackerCustomizer::removedService</code> method will
  268. * be called for that service.
  269. *
  270. * @param reference The reference to the service to be removed.
  271. */
  272. void remove(const ctkServiceReference& reference);
  273. /**
  274. * Return the number of services being tracked by this
  275. * <code>ctkServiceTracker</code>.
  276. *
  277. * @return The number of services being tracked.
  278. */
  279. int size() const;
  280. /**
  281. * Returns the tracking count for this <code>ctkServiceTracker</code>.
  282. *
  283. * The tracking count is initialized to 0 when this
  284. * <code>ctkServiceTracker</code> is opened. Every time a service is added,
  285. * modified or removed from this <code>ctkServiceTracker</code>, the tracking
  286. * count is incremented.
  287. *
  288. * <p>
  289. * The tracking count can be used to determine if this
  290. * <code>ctkServiceTracker</code> has added, modified or removed a service by
  291. * comparing a tracking count value previously collected with the current
  292. * tracking count value. If the value has not changed, then no service has
  293. * been added, modified or removed from this <code>ctkServiceTracker</code>
  294. * since the previous tracking count was collected.
  295. *
  296. * @return The tracking count for this <code>ctkServiceTracker</code> or -1 if
  297. * this <code>ctkServiceTracker</code> is not open.
  298. */
  299. int getTrackingCount() const;
  300. protected:
  301. /**
  302. * Default implementation of the
  303. * <code>ctkServiceTrackerCustomizer::addingService</code> method.
  304. *
  305. * <p>
  306. * This method is only called when this <code>ctkServiceTracker</code> has been
  307. * constructed with a <code>null</code> ctkServiceTrackerCustomizer argument.
  308. *
  309. * <p>
  310. * This implementation returns the result of calling <code>getService</code>
  311. * on the <code>ctkPluginContext</code> with which this
  312. * <code>ctkServiceTracker</code> was created passing the specified
  313. * <code>ctkServiceReference</code>.
  314. * <p>
  315. * This method can be overridden in a subclass to customize the service
  316. * object to be tracked for the service being added. In that case, take care
  317. * not to rely on the default implementation of
  318. * \link removedService(const ctkServiceReference&, QObject*) removedService\endlink
  319. * to unget the service.
  320. *
  321. * @param reference The reference to the service being added to this
  322. * <code>ctkServiceTracker</code>.
  323. * @return The service object to be tracked for the service added to this
  324. * <code>ctlServiceTracker</code>.
  325. * @see ctkServiceTrackerCustomizer::addingService(const ctkServiceReference&)
  326. */
  327. T addingService(const ctkServiceReference& reference);
  328. /**
  329. * Default implementation of the
  330. * <code>ctkServiceTrackerCustomizer::modifiedService</code> method.
  331. *
  332. * <p>
  333. * This method is only called when this <code>ctkServiceTracker</code> has been
  334. * constructed with a <code>null</code> ctkServiceTrackerCustomizer argument.
  335. *
  336. * <p>
  337. * This implementation does nothing.
  338. *
  339. * @param reference The reference to modified service.
  340. * @param service The service object for the modified service.
  341. * @see ctkServiceTrackerCustomizer::modifiedService(const ctkServiceReference&, QObject*)
  342. */
  343. void modifiedService(const ctkServiceReference& reference, T service);
  344. /**
  345. * Default implementation of the
  346. * <code>ctkServiceTrackerCustomizer::removedService</code> method.
  347. *
  348. * <p>
  349. * This method is only called when this <code>ctkServiceTracker</code> has been
  350. * constructed with a <code>null</code> ctkServiceTrackerCustomizer argument.
  351. *
  352. * <p>
  353. * This implementation calls <code>ungetService</code>, on the
  354. * <code>ctkPluginContext</code> with which this <code>ctkServiceTracker</code>
  355. * was created, passing the specified <code>ctkServiceReference</code>.
  356. * <p>
  357. * This method can be overridden in a subclass. If the default
  358. * implementation of \link addingService(const ctkServiceReference&) addingService\endlink
  359. * method was used, this method must unget the service.
  360. *
  361. * @param reference The reference to removed service.
  362. * @param service The service object for the removed service.
  363. * @see ctkServiceTrackerCustomizer::removedService(const ServiceReference&, QObject*)
  364. */
  365. void removedService(const ctkServiceReference& reference, T service);
  366. private:
  367. typedef ctkServiceTracker<S,T> ServiceTracker;
  368. typedef ctkTrackedService<S,T> TrackedService;
  369. typedef ctkServiceTrackerPrivate<S,T> ServiceTrackerPrivate;
  370. typedef ctkServiceTrackerCustomizer<T> ServiceTrackerCustomizer;
  371. friend class ctkTrackedService<S,T>;
  372. friend class ctkServiceTrackerPrivate<S,T>;
  373. inline ServiceTrackerPrivate* d_func()
  374. {
  375. return reinterpret_cast<ServiceTrackerPrivate*>(qGetPtrHelper(d_ptr));
  376. }
  377. inline const ServiceTrackerPrivate* d_func() const
  378. {
  379. return reinterpret_cast<const ServiceTrackerPrivate*>(qGetPtrHelper(d_ptr));
  380. }
  381. const QScopedPointer<ServiceTrackerPrivate> d_ptr;
  382. };
  383. #include "ctkServiceTracker.tpp"
  384. #endif // CTKSERVICETRACKER_H