5dd7628d5e31938df9292f76c446ddde78547b2f
[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 Key type
41      * and arbitrary parameters.
42      * 
43      * @param T         class of plugin to manage
44      * @param Key       the key for type lookup
45      * @param Params    parameters for plugin construction
46      */
47     template <class T, class Key, typename Params> class PluginManager
48     {
49     public:
50         PluginManager() {}
51         ~PluginManager() {}
52
53         /** Factory function for plugin. */
54         typedef T* Factory(const Params&);
55
56         /**
57          * Registers the factory for a given type.
58          * 
59          * @param type      the key to the plugin type
60          * @param factory   the factory function for the plugin type
61          */
62         void registerFactory(const Key& type, typename PluginManager::Factory* factory) {
63             if (factory)
64                 m_map[type]=factory;
65         }
66
67         /**
68          * Unregisters the factory for a given type.
69          * 
70          * @param type  the key to the plugin type
71          */
72         void deregisterFactory(const Key& type) {
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 key to the plugin type
88          * @param p     parameters to configure plugin
89          * @return      the constructed plugin  
90          */
91         T* newPlugin(const Key& type, const Params& p) {
92             typename std::map<Key, typename PluginManager::Factory*>::const_iterator i=m_map.find(type);
93             if (i==m_map.end())
94                 throw UnknownExtensionException("Unknown plugin type.");
95             return i->second(p);
96         }
97         
98     private:
99         std::map<Key, 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__ */