e1add0eb5aeefa9d43035ebba13f03c8c7c72433
[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 xmltooling/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 #include <xmltooling/exceptions.h>\r
28 \r
29 #include <map>\r
30 #include <string>\r
31 \r
32 #if defined (_MSC_VER)\r
33     #pragma warning( push )\r
34     #pragma warning( disable : 4250 4251 )\r
35 #endif\r
36 \r
37 namespace xmltooling {\r
38 \r
39     /**\r
40      * Template for management/access to plugins constructed based on a string type\r
41      * and arbitrary parameters.\r
42      * \r
43      * @param T         class of plugin to manage\r
44      * @param Params    parameters for plugin construction\r
45      */\r
46     template <class T, typename Params> class PluginManager\r
47     {\r
48     public:\r
49         PluginManager() {}\r
50         ~PluginManager() {}\r
51 \r
52         /** Factory function for plugin. */\r
53         typedef T* Factory(const Params&);\r
54 \r
55         /**\r
56          * Registers the factory for a given type.\r
57          * \r
58          * @param type      the name of the plugin type\r
59          * @param factory   the factory function for the plugin type\r
60          */\r
61         void registerFactory(const char* type, typename PluginManager::Factory* factory) {\r
62             if (type && factory)\r
63                 m_map[type]=factory;\r
64         }\r
65 \r
66         /**\r
67          * Unregisters the factory for a given type.\r
68          * \r
69          * @param type  the name of the plugin type\r
70          */\r
71         void deregisterFactory(const char* type) {\r
72             if (type) {\r
73                 m_map.erase(type);\r
74             }\r
75         }\r
76 \r
77         /**\r
78          * Builds a new instance of a plugin of a given type, configuring it\r
79          * with the supplied parameters.\r
80          * \r
81          * @param type  the name of the plugin type\r
82          * @param p     parameters to configure plugin\r
83          * @return      the constructed plugin  \r
84          */\r
85         T* newPlugin(const char* type, const Params& p) {\r
86             typename std::map<std::string, typename PluginManager::Factory*>::const_iterator i=m_map.find(type);\r
87             if (i==m_map.end())\r
88                 throw UnknownExtensionException("Unable to build plugin of type '$1'",params(1,type));\r
89             return i->second(p);\r
90         }\r
91         \r
92     private:\r
93         std::map<std::string, typename PluginManager::Factory*> m_map;\r
94     };\r
95 \r
96 };\r
97 \r
98 #if defined (_MSC_VER)\r
99     #pragma warning( pop )\r
100 #endif\r
101 \r
102 #endif /* __xmltooling_plugin_h__ */\r