ctkPluginTracker.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 CTKPLUGINTRACKER_H
  16. #define CTKPLUGINTRACKER_H
  17. #include <QScopedPointer>
  18. #include "ctkPluginFrameworkExport.h"
  19. #include "ctkPlugin.h"
  20. #include "ctkPluginTrackerCustomizer.h"
  21. template<class T> class ctkTrackedPlugin;
  22. template<class T> class ctkPluginTrackerPrivate;
  23. /**
  24. * \ingroup PluginFramework
  25. *
  26. * The <code>ctkPluginTracker</code> class simplifies tracking plugins much like
  27. * the <code>ctkServiceTracker</code> simplifies tracking services.
  28. * <p>
  29. * A <code>ctkPluginTracker</code> is constructed with state criteria and a
  30. * <code>ctkPluginTrackerCustomizer</code> object. A <code>ctkPluginTracker</code> can
  31. * use the <code>ctkPluginTrackerCustomizer</code> to select which plugins are
  32. * tracked and to create a customized object to be tracked with the plugin. The
  33. * <code>ctkPluginTracker</code> can then be opened to begin tracking all plugins
  34. * whose state matches the specified state criteria.
  35. * <p>
  36. * The <code>getPlugins</code> method can be called to get the
  37. * <code>ctkPlugin</code> objects of the plugins being tracked. The
  38. * <code>getObject</code> method can be called to get the customized object for
  39. * a tracked plugin.
  40. * <p>
  41. * The <code>ctkPluginTracker</code> class is thread-safe. It does not call a
  42. * <code>ctkPluginTrackerCustomizer</code> while holding any locks.
  43. * <code>ctkPluginTrackerCustomizer</code> implementations must also be
  44. * thread-safe.
  45. *
  46. * \tparam T The type of the tracked object. The type must be an assignable
  47. * datatype, provide a boolean conversion function, and provide
  48. * a constructor and an assignment operator which can handle 0 as an argument.
  49. * \threadsafe
  50. */
  51. template<class T = QSharedPointer<ctkPlugin> >
  52. class ctkPluginTracker : protected ctkPluginTrackerCustomizer<T>
  53. {
  54. public:
  55. ~ctkPluginTracker();
  56. /**
  57. * Create a <code>ctkPluginTracker</code> for plugins whose state is present in
  58. * the specified state mask.
  59. *
  60. * <p>
  61. * Plugins whose state is present on the specified state mask will be
  62. * tracked by this <code>ctkPluginTracker</code>.
  63. *
  64. * @param context The <code>ctkPluginContext</code> against which the tracking
  65. * is done.
  66. * @param stateMask The bit mask of the <code>OR</code>ing of the plugin
  67. * states to be tracked.
  68. * @param customizer The customizer object to call when plugins are added,
  69. * modified, or removed in this <code>ctkPluginTracker</code>. If
  70. * customizer is <code>null</code>, then this
  71. * <code>ctkPluginTracker</code> will be used as the
  72. * <code>ctkPluginTrackerCustomizer</code> and this
  73. * <code>ctkPluginTracker</code> will call the
  74. * <code>ctkPluginTrackerCustomizer</code> methods on itself. If the
  75. * customizer is not <code>null</code>, this <code>ctkPluginTracker</code>
  76. * takes ownership of the customizer.
  77. * @see ctkPlugin#getState()
  78. */
  79. ctkPluginTracker(ctkPluginContext* context, ctkPlugin::States stateMask,
  80. ctkPluginTrackerCustomizer<T>* customizer = 0);
  81. /**
  82. * Open this <code>ctkPluginTracker</code> and begin tracking plugins.
  83. *
  84. * <p>
  85. * ctkPlugin's which match the state criteria specified when this
  86. * <code>ctkPluginTracker</code> was created are now tracked by this
  87. * <code>ctkPluginTracker</code>.
  88. *
  89. * @throws std::logic_error If the <code>ctkPluginContext</code>
  90. * with which this <code>ctkPluginTracker</code> was created is no
  91. * longer valid.
  92. */
  93. virtual void open();
  94. /**
  95. * Close this <code>ctkPluginTracker</code>.
  96. *
  97. * <p>
  98. * This method should be called when this <code>ctkPluginTracker</code> should
  99. * end the tracking of plugins.
  100. *
  101. * <p>
  102. * This implementation calls getPlugins() to get the list of
  103. * tracked plugins to remove.
  104. */
  105. virtual void close();
  106. /**
  107. * Return a list of <code>ctkPlugin</code>s for all plugins being tracked by
  108. * this <code>ctkPluginTracker</code>.
  109. *
  110. * @return A list of <code>ctkPlugin</code>s.
  111. */
  112. virtual QList<QSharedPointer<ctkPlugin> > getPlugins() const;
  113. /**
  114. * Returns the customized object for the specified <code>ctkPlugin</code> if
  115. * the specified plugin is being tracked by this <code>ctkPluginTracker</code>.
  116. *
  117. * @param plugin The <code>ctkPlugin</code> being tracked.
  118. * @return The customized object for the specified <code>ctkPlugin</code> or
  119. * <code>null</code> if the specified <code>ctkPlugin</code> is not
  120. * being tracked.
  121. */
  122. virtual T getObject(QSharedPointer<ctkPlugin> plugin) const;
  123. /**
  124. * Remove a plugin from this <code>ctkPluginTracker</code>.
  125. *
  126. * The specified plugin will be removed from this <code>ctkPluginTracker</code>.
  127. * If the specified plugin was being tracked then the
  128. * <code>ctkPluginTrackerCustomizer::removedPlugin</code> method will be called
  129. * for that plugin.
  130. *
  131. * @param plugin The <code>ctkPlugin</code> to be removed.
  132. */
  133. virtual void remove(QSharedPointer<ctkPlugin> plugin);
  134. /**
  135. * Return the number of plugins being tracked by this
  136. * <code>ctkPluginTracker</code>.
  137. *
  138. * @return The number of plugins being tracked.
  139. */
  140. virtual int size() const;
  141. /**
  142. * Returns the tracking count for this <code>ctkPluginTracker</code>.
  143. *
  144. * The tracking count is initialized to 0 when this
  145. * <code>ctkPluginTracker</code> is opened. Every time a plugin is added,
  146. * modified or removed from this <code>ctkPluginTracker</code> the tracking
  147. * count is incremented.
  148. *
  149. * <p>
  150. * The tracking count can be used to determine if this
  151. * <code>ctkPluginTracker</code> has added, modified or removed a plugin by
  152. * comparing a tracking count value previously collected with the current
  153. * tracking count value. If the value has not changed, then no plugin has
  154. * been added, modified or removed from this <code>ctkPluginTracker</code>
  155. * since the previous tracking count was collected.
  156. *
  157. * @return The tracking count for this <code>ctkPluginTracker</code> or -1 if
  158. * this <code>ctkPluginTracker</code> is not open.
  159. */
  160. virtual int getTrackingCount() const;
  161. /**
  162. * Return a <code>QMap</code> with the <code>ctkPlugin</code>s and customized
  163. * objects for all plugins being tracked by this <code>ctkPluginTracker</code>.
  164. *
  165. * @return A <code>QMap</code> with the <code>ctkPlugin</code>s and customized
  166. * objects for all services being tracked by this
  167. * <code>ctkPluginTracker</code>. If no plugins are being tracked, then
  168. * the returned map is empty.
  169. */
  170. virtual QMap<QSharedPointer<ctkPlugin>, T> getTracked() const;
  171. /**
  172. * Return if this <code>ctkPluginTracker</code> is empty.
  173. *
  174. * @return <code>true</code> if this <code>ctkPluginTracker</code> is not tracking any
  175. * plugins.
  176. */
  177. virtual bool isEmpty() const;
  178. protected:
  179. /**
  180. * Default implementation of the
  181. * <code>ctkPluginTrackerCustomizer::addingPlugin</code> method.
  182. *
  183. * <p>
  184. * This method is only called when this <code>ctkPluginTracker</code> has been
  185. * constructed with a <code>null</code> ctkPluginTrackerCustomizer argument.
  186. *
  187. * <p>
  188. * This implementation simply returns the specified <code>ctkPlugin*</code> in
  189. * a QVariant.
  190. *
  191. * <p>
  192. * This method can be overridden in a subclass to customize the object to be
  193. * tracked for the plugin being added.
  194. *
  195. * @param plugin The <code>ctkPlugin</code> being added to this
  196. * <code>ctkPluginTracker</code> object.
  197. * @param event The plugin event which caused this customizer method to be
  198. * called or an invalid event if there is no plugin event associated
  199. * with the call to this method.
  200. * @return The specified plugin.
  201. * @see ctkPluginTrackerCustomizer::addingPlugin(ctkPlugin*, const ctkPluginEvent&)
  202. */
  203. T addingPlugin(QSharedPointer<ctkPlugin> plugin, const ctkPluginEvent& event);
  204. /**
  205. * Default implementation of the
  206. * <code>ctkPluginTrackerCustomizer::modifiedPlugin</code> method.
  207. *
  208. * <p>
  209. * This method is only called when this <code>ctkPluginTracker</code> has been
  210. * constructed with a <code>null</code> ctkPluginTrackerCustomizer argument.
  211. *
  212. * <p>
  213. * This implementation does nothing.
  214. *
  215. * @param plugin The <code>ctkPlugin</code> whose state has been modified.
  216. * @param event The plugin event which caused this customizer method to be
  217. * called or an invalid event if there is no plugin event associated
  218. * with the call to this method.
  219. * @param object The customized object for the specified ctkPlugin.
  220. * @see ctkPluginTrackerCustomizer::modifiedPlugin(ctkPlugin*, const ctkPluginEvent&, QVariant)
  221. */
  222. void modifiedPlugin(QSharedPointer<ctkPlugin> plugin, const ctkPluginEvent& event, T object);
  223. /**
  224. * Default implementation of the
  225. * <code>ctkPluginTrackerCustomizer::removedPlugin</code> method.
  226. *
  227. * <p>
  228. * This method is only called when this <code>ctkPluginTracker</code> has been
  229. * constructed with a <code>null</code> ctkPluginTrackerCustomizer argument.
  230. *
  231. * <p>
  232. * This implementation does nothing.
  233. *
  234. * @param plugin The <code>ctkPlugin</code> being removed.
  235. * @param event The plugin event which caused this customizer method to be
  236. * called or an invalid event if there is no plugin event associated
  237. * with the call to this method.
  238. * @param object The customized object for the specified plugin.
  239. * @see ctkPluginTrackerCustomizer::removedPlugin(ctkPlugin*, const ctkPluginEvent&, QVariant)
  240. */
  241. void removedPlugin(QSharedPointer<ctkPlugin> plugin, const ctkPluginEvent& event, T object);
  242. private:
  243. typedef ctkPluginTracker<T> PluginTracker;
  244. typedef ctkTrackedPlugin<T> TrackedPlugin;
  245. typedef ctkPluginTrackerPrivate<T> PluginTrackerPrivate;
  246. typedef ctkPluginTrackerCustomizer<T> PluginTrackerCustomizer;
  247. friend class ctkTrackedPlugin<T>;
  248. friend class ctkPluginTrackerPrivate<T>;
  249. inline PluginTrackerPrivate* d_func()
  250. {
  251. return reinterpret_cast<PluginTrackerPrivate*>(qGetPtrHelper(d_ptr));
  252. }
  253. inline const PluginTrackerPrivate* d_func() const
  254. {
  255. return reinterpret_cast<const PluginTrackerPrivate*>(qGetPtrHelper(d_ptr));
  256. }
  257. const QScopedPointer<PluginTrackerPrivate> d_ptr;
  258. };
  259. #include "ctkPluginTracker.tpp"
  260. #endif // CTKPLUGINTRACKER_H