diff options
| author | Alan Conway <aconway@apache.org> | 2008-06-26 21:39:28 +0000 |
|---|---|---|
| committer | Alan Conway <aconway@apache.org> | 2008-06-26 21:39:28 +0000 |
| commit | 9e115470654bf67fbb4720ab3bfb3768b9331e55 (patch) | |
| tree | b4ba4b4829e955de74caf6c05c9874e52a1fa262 /cpp/src/qpid/Plugin.h | |
| parent | eabcd5c760c8d3b3e8f6f45a29c19148560f91cd (diff) | |
| download | qpid-python-9e115470654bf67fbb4720ab3bfb3768b9331e55.tar.gz | |
Plugin framework change: single PluginFactory creates per-target Plugin instances.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@672032 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/qpid/Plugin.h')
| -rw-r--r-- | cpp/src/qpid/Plugin.h | 137 |
1 files changed, 91 insertions, 46 deletions
diff --git a/cpp/src/qpid/Plugin.h b/cpp/src/qpid/Plugin.h index 3ead770129..5b0bb0c2c1 100644 --- a/cpp/src/qpid/Plugin.h +++ b/cpp/src/qpid/Plugin.h @@ -21,78 +21,123 @@ * */ -#include "qpid/shared_ptr.h" -#include <boost/noncopyable.hpp> +#include <boost/shared_ptr.hpp> #include <vector> -#include <boost/function.hpp> - /**@file Generic plug-in framework. */ namespace qpid { + class Options; /** * Plug-in base class. + * + * A Plugin is created by a Plugin::Factory and attached to a Plugin::Target. */ -class Plugin : boost::noncopyable -{ +class Plugin { public: /** - * Base interface for targets that receive plug-ins. - * - * The Broker is a plug-in target, there might be others - * in future. + * Base class for target objects that receive plug-ins. */ - struct Target { virtual ~Target() {} }; + class Target { + public: + virtual ~Target(); - typedef std::vector<Plugin*> Plugins; - - /** - * Construct registers the plug-in to appear in getPlugins(). - * - * A concrete Plugin is instantiated as a global or static - * member variable in a library so it is registered during static - * initialization when the library is loaded. - */ - Plugin(); - - virtual ~Plugin(); + protected: + /** For each Factory create a plugin attached to this */ + void createPlugins(); - /** - * Configuration options for the plugin. - * Then will be updated during option parsing by the host program. - * - * @return An options group or 0 for no options. Default returns 0. - * Plugin retains ownership of return value. - */ - virtual Options* getOptions(); + /** Initialize all attached plugins */ + void initializePlugins(); + + /** Release the attached plugins. Done automatically in destructor. */ + void releasePlugins(); + + private: + std::vector<boost::shared_ptr<Plugin> > plugins; + }; + + /** Base class for a factory to create Plugins. */ + class Factory { + public: + /** + * Constructor registers the factory so it will be included in getList(). + * + * Derive your Plugin Factory class from Factory and create a + * single global instance in your plug-in library. Loading the + * library will automatically register your factory. + */ + Factory(); + + virtual ~Factory(); + + /** Get the list of Factories */ + static const std::vector<Factory*>& getList(); + + /** For each Factory in Factory::getList() add options to opts. */ + static void addOptions(Options& opts); + + /** + * Configuration options for this factory. + * Then will be updated during option parsing by the host program. + * + * @return An options group or 0 for no options. + */ + virtual Options* getOptions() = 0; + + /** + * Create a Plugin for target. + * Uses option values set by getOptions(). + * Target may not be fully initialized at this point. + * Actions that require a fully-initialized target should be + * done in initialize(). + * + * @returns 0 if the target type is not compatible with this Factory. + */ + virtual boost::shared_ptr<Plugin> create(Target& target) = 0; + }; /** - * Initialize Plugin functionality on a Target. - * Plugins should ignore targets they don't recognize. - * - * Called before the target itself is initialized. + * Template factory implementation, checks target type is correct. */ - virtual void earlyInitialize(Target&) = 0; + template <class TargetType> class FactoryT : public Factory { + Options* getOptions() { return 0; } + + virtual boost::shared_ptr<Plugin> createT(TargetType& target) = 0; + + boost::shared_ptr<Plugin> create(Target& target) { + TargetType* tt = dynamic_cast<TargetType*>(&target); + if (tt) + return createT(*tt); + else + return boost::shared_ptr<Plugin>(); + } + }; + + // Plugin functions. + virtual ~Plugin(); + /** - * Initialize Plugin functionality on a Target. - * Plugins should ignore targets they don't recognize. - * + * Initialize the Plugin. * Called after the target is fully initialized. */ virtual void initialize(Target&) = 0; +}; - /** List of registered Plugin objects. - * Caller must not delete plugin pointers. - */ - static const Plugins& getPlugins(); +/** Template plugin factory */ +template <class TargetType> class PluginT : public Plugin { + + virtual void initializeT(TargetType&) = 0; - /** For each registered plugin, add plugin.getOptions() to opts. */ - static void addOptions(Options& opts); + void initialize(Plugin::Target& target) { + TargetType* tt = dynamic_cast<TargetType*>(&target); + assert(tt); + initializeT(*tt); + } }; - + } // namespace qpid #endif /*!QPID_PLUGIN_H*/ |
