2 * Copyright 2001-2006 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @file xmltooling/PluginManager.h
20 * Plugin management template
23 #ifndef __xmltooling_plugin_h__
24 #define __xmltooling_plugin_h__
26 #include <xmltooling/base.h>
27 #include <xmltooling/exceptions.h>
32 #if defined (_MSC_VER)
33 #pragma warning( push )
34 #pragma warning( disable : 4250 4251 )
37 namespace xmltooling {
40 * Template for management/access to plugins constructed based on a string type
41 * and arbitrary parameters.
43 * @param T class of plugin to manage
44 * @param Params parameters for plugin construction
46 template <class T, typename Params> class PluginManager
52 /** Factory function for plugin. */
53 typedef T* Factory(const Params&);
56 * Registers the factory for a given type.
58 * @param type the name of the plugin type
59 * @param factory the factory function for the plugin type
61 void registerFactory(const std::string& type, typename PluginManager::Factory* factory) {
62 if (!type.empty() && factory)
67 * Unregisters the factory for a given type.
69 * @param type the name of the plugin type
71 void deregisterFactory(const std::string& type) {
77 * Unregisters all registered factories.
79 void deregisterFactories() {
84 * Builds a new instance of a plugin of a given type, configuring it
85 * with the supplied parameters.
87 * @param type the name of the plugin type
88 * @param p parameters to configure plugin
89 * @return the constructed plugin
91 T* newPlugin(const std::string& type, const Params& p) {
92 typename std::map<std::string, typename PluginManager::Factory*>::const_iterator i=m_map.find(type);
94 throw UnknownExtensionException("Unable to build plugin of type '$1'",params(1,type.c_str()));
99 std::map<std::string, typename PluginManager::Factory*> m_map;
104 #if defined (_MSC_VER)
105 #pragma warning( pop )
108 #endif /* __xmltooling_plugin_h__ */