Update copyright.
[shibboleth/cpp-xmltooling.git] / xmltooling / PluginManager.h
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file xmltooling/PluginManager.h
19  * 
20  * Plugin management template
21  */
22
23 #ifndef __xmltooling_plugin_h__
24 #define __xmltooling_plugin_h__
25
26 #include <xmltooling/base.h>
27 #include <xmltooling/exceptions.h>
28
29 #include <map>
30 #include <string>
31
32 #if defined (_MSC_VER)
33     #pragma warning( push )
34     #pragma warning( disable : 4250 4251 )
35 #endif
36
37 namespace xmltooling {
38
39     /**
40      * Template for management/access to plugins constructed based on a string type
41      * and arbitrary parameters.
42      * 
43      * @param T         class of plugin to manage
44      * @param Params    parameters for plugin construction
45      */
46     template <class T, typename Params> class PluginManager
47     {
48     public:
49         PluginManager() {}
50         ~PluginManager() {}
51
52         /** Factory function for plugin. */
53         typedef T* Factory(const Params&);
54
55         /**
56          * Registers the factory for a given type.
57          * 
58          * @param type      the name of the plugin type
59          * @param factory   the factory function for the plugin type
60          */
61         void registerFactory(const std::string& type, typename PluginManager::Factory* factory) {
62             if (!type.empty() && factory)
63                 m_map[type]=factory;
64         }
65
66         /**
67          * Unregisters the factory for a given type.
68          * 
69          * @param type  the name of the plugin type
70          */
71         void deregisterFactory(const std::string& type) {
72             if (!type.empty())
73                 m_map.erase(type);
74         }
75
76         /**
77          * Unregisters all registered factories.
78          */
79         void deregisterFactories() {
80             m_map.clear();
81         }
82
83         /**
84          * Builds a new instance of a plugin of a given type, configuring it
85          * with the supplied parameters.
86          * 
87          * @param type  the name of the plugin type
88          * @param p     parameters to configure plugin
89          * @return      the constructed plugin  
90          */
91         T* newPlugin(const std::string& type, const Params& p) {
92             typename std::map<std::string, typename PluginManager::Factory*>::const_iterator i=m_map.find(type);
93             if (i==m_map.end())
94                 throw UnknownExtensionException("Unable to build plugin of type '$1'",params(1,type.c_str()));
95             return i->second(p);
96         }
97         
98     private:
99         std::map<std::string, typename PluginManager::Factory*> m_map;
100     };
101
102 };
103
104 #if defined (_MSC_VER)
105     #pragma warning( pop )
106 #endif
107
108 #endif /* __xmltooling_plugin_h__ */