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 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
30 #include <xercesc/dom/DOM.hpp>
\r
32 using namespace xercesc;
\r
34 namespace xmltooling {
\r
37 * Template for management/access to plugins constructed based on a string type
\r
38 * and arbitrary parameters.
\r
40 * @param T class of plugin to manage
\r
41 * @param Params parameters for plugin construction
\r
43 template <class T, typename Params> class XMLTOOL_API PluginManager
\r
49 /** Factory function for plugin. */
\r
50 typedef T* Factory(typename Params&);
\r
53 * Registers the factory for a given type.
\r
55 * @param type the name of the plugin type
\r
56 * @param factory the factory function for the plugin type
\r
58 void registerFactory(const char* type, typename Factory* factory) {
\r
59 if (type && factory)
\r
60 m_map[type]=factory;
\r
64 * Unregisters the factory for a given type.
\r
66 * @param type the name of the plugin type
\r
68 void deregisterFactory(const char* type) {
\r
75 * Builds a new instance of a plugin of a given type, configuring it
\r
76 * with the supplied element, if any.
\r
78 * @param type the name of the plugin type
\r
79 * @param p parameters to configure plugin
\r
80 * @return the constructed plugin
\r
82 T* newPlugin(const char* type, typename Params& p) {
\r
83 std::map<std::string, typename Factory*>::const_iterator i=m_map.find(type);
\r
85 throw UnknownExtensionException("Unable to build plugin of type '$1'",params(1,type));
\r
86 return i->second(p);
\r
90 std::map<std::string, typename Factory*> m_map;
\r
95 #endif /* __xmltooling_plugin_h__ */
\r