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