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