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