Add in plugin manager template.
[shibboleth/cpp-xmltooling.git] / xmltooling / PluginManager.h
1 /*\r
2  *  Copyright 2001-2006 Internet2\r
3  * \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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 /**\r
18  * @file PluginManager.h\r
19  * \r
20  * Plugin management template\r
21  */\r
22 \r
23 #ifndef __xmltooling_plugin_h__\r
24 #define __xmltooling_plugin_h__\r
25 \r
26 #include <xmltooling/base.h>\r
27 \r
28 #include <map>\r
29 #include <string>\r
30 #include <xercesc/dom/DOM.hpp>\r
31 \r
32 using namespace xercesc;\r
33 \r
34 namespace xmltooling {\r
35 \r
36     /**\r
37      * Template for management/access to plugins constructed based on a string type\r
38      * and arbitrary parameters.\r
39      * \r
40      * @param T         class of plugin to manage\r
41      * @param Params    parameters for plugin construction\r
42      */\r
43     template <class T, typename Params> class XMLTOOL_API PluginManager\r
44     {\r
45     public:\r
46         PluginManager() {}\r
47         ~PluginManager() {}\r
48 \r
49         /** Factory function for plugin. */\r
50         typedef T* Factory(typename Params&);\r
51 \r
52         /**\r
53          * Registers the factory for a given type.\r
54          * \r
55          * @param type      the name of the plugin type\r
56          * @param factory   the factory function for the plugin type\r
57          */\r
58         void registerFactory(const char* type, typename Factory* factory) {\r
59             if (type && factory)\r
60                 m_map[type]=factory;\r
61         }\r
62 \r
63         /**\r
64          * Unregisters the factory for a given type.\r
65          * \r
66          * @param type  the name of the plugin type\r
67          */\r
68         void deregisterFactory(const char* type) {\r
69             if (type) {\r
70                 m_map.erase(type);\r
71             }\r
72         }\r
73 \r
74         /**\r
75          * Builds a new instance of a plugin of a given type, configuring it\r
76          * with the supplied element, if any.\r
77          * \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
81          */\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
84             if (i==m_map.end())\r
85                 throw UnknownExtensionException("Unable to build plugin of type '$1'",params(1,type));\r
86             return i->second(p);\r
87         }\r
88         \r
89     private:\r
90         std::map<std::string, typename Factory*> m_map;\r
91     };\r
92 \r
93 };\r
94 \r
95 #endif /* __xmltooling_plugin_h__ */\r