/*============================================================================= Library: CTK Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =============================================================================*/ #ifndef CTKPLUGINTRACKER_H #define CTKPLUGINTRACKER_H #include #include "ctkPluginFrameworkExport.h" #include "ctkPlugin.h" #include "ctkPluginTrackerCustomizer.h" template class ctkTrackedPlugin; template class ctkPluginTrackerPrivate; /** * \ingroup PluginFramework * * The ctkPluginTracker class simplifies tracking plugins much like * the ctkServiceTracker simplifies tracking services. *

* A ctkPluginTracker is constructed with state criteria and a * ctkPluginTrackerCustomizer object. A ctkPluginTracker can * use the ctkPluginTrackerCustomizer to select which plugins are * tracked and to create a customized object to be tracked with the plugin. The * ctkPluginTracker can then be opened to begin tracking all plugins * whose state matches the specified state criteria. *

* The getPlugins method can be called to get the * ctkPlugin objects of the plugins being tracked. The * getObject method can be called to get the customized object for * a tracked plugin. *

* The ctkPluginTracker class is thread-safe. It does not call a * ctkPluginTrackerCustomizer while holding any locks. * ctkPluginTrackerCustomizer implementations must also be * thread-safe. * * \tparam T The type of the tracked object. The type must be an assignable * datatype, provide a boolean conversion function, and provide * a constructor and an assignment operator which can handle 0 as an argument. * \remarks This class is thread safe. */ template > class ctkPluginTracker : protected ctkPluginTrackerCustomizer { public: ~ctkPluginTracker(); /** * Create a ctkPluginTracker for plugins whose state is present in * the specified state mask. * *

* Plugins whose state is present on the specified state mask will be * tracked by this ctkPluginTracker. * * @param context The ctkPluginContext against which the tracking * is done. * @param stateMask The bit mask of the ORing of the plugin * states to be tracked. * @param customizer The customizer object to call when plugins are added, * modified, or removed in this ctkPluginTracker. If * customizer is null, then this * ctkPluginTracker will be used as the * ctkPluginTrackerCustomizer and this * ctkPluginTracker will call the * ctkPluginTrackerCustomizer methods on itself. If the * customizer is not null, this ctkPluginTracker * takes ownership of the customizer. * @see ctkPlugin#getState() */ ctkPluginTracker(ctkPluginContext* context, ctkPlugin::States stateMask, ctkPluginTrackerCustomizer* customizer = 0); /** * Open this ctkPluginTracker and begin tracking plugins. * *

* ctkPlugin's which match the state criteria specified when this * ctkPluginTracker was created are now tracked by this * ctkPluginTracker. * * @throws std::logic_error If the ctkPluginContext * with which this ctkPluginTracker was created is no * longer valid. */ virtual void open(); /** * Close this ctkPluginTracker. * *

* This method should be called when this ctkPluginTracker should * end the tracking of plugins. * *

* This implementation calls getPlugins() to get the list of * tracked plugins to remove. */ virtual void close(); /** * Return a list of ctkPlugins for all plugins being tracked by * this ctkPluginTracker. * * @return A list of ctkPlugins. */ virtual QList > getPlugins() const; /** * Returns the customized object for the specified ctkPlugin if * the specified plugin is being tracked by this ctkPluginTracker. * * @param plugin The ctkPlugin being tracked. * @return The customized object for the specified ctkPlugin or * null if the specified ctkPlugin is not * being tracked. */ virtual T getObject(QSharedPointer plugin) const; /** * Remove a plugin from this ctkPluginTracker. * * The specified plugin will be removed from this ctkPluginTracker. * If the specified plugin was being tracked then the * ctkPluginTrackerCustomizer::removedPlugin method will be called * for that plugin. * * @param plugin The ctkPlugin to be removed. */ virtual void remove(QSharedPointer plugin); /** * Return the number of plugins being tracked by this * ctkPluginTracker. * * @return The number of plugins being tracked. */ virtual int size() const; /** * Returns the tracking count for this ctkPluginTracker. * * The tracking count is initialized to 0 when this * ctkPluginTracker is opened. Every time a plugin is added, * modified or removed from this ctkPluginTracker the tracking * count is incremented. * *

* The tracking count can be used to determine if this * ctkPluginTracker has added, modified or removed a plugin by * comparing a tracking count value previously collected with the current * tracking count value. If the value has not changed, then no plugin has * been added, modified or removed from this ctkPluginTracker * since the previous tracking count was collected. * * @return The tracking count for this ctkPluginTracker or -1 if * this ctkPluginTracker is not open. */ virtual int getTrackingCount() const; /** * Return a QMap with the ctkPlugins and customized * objects for all plugins being tracked by this ctkPluginTracker. * * @return A QMap with the ctkPlugins and customized * objects for all services being tracked by this * ctkPluginTracker. If no plugins are being tracked, then * the returned map is empty. */ virtual QMap, T> getTracked() const; /** * Return if this ctkPluginTracker is empty. * * @return true if this ctkPluginTracker is not tracking any * plugins. */ virtual bool isEmpty() const; protected: /** * Default implementation of the * ctkPluginTrackerCustomizer::addingPlugin method. * *

* This method is only called when this ctkPluginTracker has been * constructed with a null ctkPluginTrackerCustomizer argument. * *

* This implementation simply returns the specified ctkPlugin* in * a QVariant. * *

* This method can be overridden in a subclass to customize the object to be * tracked for the plugin being added. * * @param plugin The ctkPlugin being added to this * ctkPluginTracker object. * @param event The plugin event which caused this customizer method to be * called or an invalid event if there is no plugin event associated * with the call to this method. * @return The specified plugin. * @see ctkPluginTrackerCustomizer::addingPlugin(ctkPlugin*, const ctkPluginEvent&) */ T addingPlugin(QSharedPointer plugin, const ctkPluginEvent& event); /** * Default implementation of the * ctkPluginTrackerCustomizer::modifiedPlugin method. * *

* This method is only called when this ctkPluginTracker has been * constructed with a null ctkPluginTrackerCustomizer argument. * *

* This implementation does nothing. * * @param plugin The ctkPlugin whose state has been modified. * @param event The plugin event which caused this customizer method to be * called or an invalid event if there is no plugin event associated * with the call to this method. * @param object The customized object for the specified ctkPlugin. * @see ctkPluginTrackerCustomizer::modifiedPlugin(ctkPlugin*, const ctkPluginEvent&, QVariant) */ void modifiedPlugin(QSharedPointer plugin, const ctkPluginEvent& event, T object); /** * Default implementation of the * ctkPluginTrackerCustomizer::removedPlugin method. * *

* This method is only called when this ctkPluginTracker has been * constructed with a null ctkPluginTrackerCustomizer argument. * *

* This implementation does nothing. * * @param plugin The ctkPlugin being removed. * @param event The plugin event which caused this customizer method to be * called or an invalid event if there is no plugin event associated * with the call to this method. * @param object The customized object for the specified plugin. * @see ctkPluginTrackerCustomizer::removedPlugin(ctkPlugin*, const ctkPluginEvent&, QVariant) */ void removedPlugin(QSharedPointer plugin, const ctkPluginEvent& event, T object); private: typedef ctkPluginTracker PluginTracker; typedef ctkTrackedPlugin TrackedPlugin; typedef ctkPluginTrackerPrivate PluginTrackerPrivate; typedef ctkPluginTrackerCustomizer PluginTrackerCustomizer; friend class ctkTrackedPlugin; friend class ctkPluginTrackerPrivate; inline PluginTrackerPrivate* d_func() { return reinterpret_cast(qGetPtrHelper(d_ptr)); } inline const PluginTrackerPrivate* d_func() const { return reinterpret_cast(qGetPtrHelper(d_ptr)); } const QScopedPointer d_ptr; }; #include "ctkPluginTracker.tpp" #endif // CTKPLUGINTRACKER_H