ctkPluginTracker.h 10 KB

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