Add static mgmt of interfaces.
[shibboleth/cpp-xmltooling.git] / xmltooling / XMLObjectBuilder.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 XMLObjectBuilder.h\r
19  * \r
20  * Factory interface for XMLObjects \r
21  */\r
22 \r
23 #if !defined(__xmltooling_xmlobjbuilder_h__)\r
24 #define __xmltooling_xmlobjbuilder_h__\r
25 \r
26 #include <map>\r
27 #include <xmltooling/QName.h>\r
28 #include <xmltooling/XMLObject.h>\r
29 \r
30 #if defined (_MSC_VER)\r
31     #pragma warning( push )\r
32     #pragma warning( disable : 4250 4251 )\r
33 #endif\r
34 \r
35 namespace xmltooling {\r
36 \r
37     /**\r
38      * A factory interface for obtaining XMLObjects.\r
39      * Subclasses MAY supply additional factory methods.\r
40      */\r
41     class XMLTOOL_API XMLObjectBuilder\r
42     {\r
43     MAKE_NONCOPYABLE(XMLObjectBuilder);\r
44     public:\r
45         virtual ~XMLObjectBuilder() {}\r
46         \r
47         /**\r
48          * Creates an empty XMLObject.\r
49          * \r
50          * @return the empty XMLObject\r
51          */\r
52         virtual XMLObject* buildObject()=0;\r
53 \r
54         /**\r
55          * Resets the state of the builder.\r
56          * \r
57          * This normally means null'ing out any properties that were\r
58          * needed to build an object.\r
59          */\r
60         virtual void resetState()=0;\r
61         \r
62         /**\r
63          * Retrieves an XMLObjectBuilder using the key it was registered with.\r
64          * \r
65          * @param key the key used to register the builder\r
66          * @return the builder\r
67          */\r
68         static XMLObjectBuilder* getBuilder(const QName& key) {\r
69             std::map<QName,XMLObjectBuilder*>::const_iterator i=m_map.find(key);\r
70             return (i==m_map.end()) ? NULL : i->second;\r
71         }\r
72     \r
73         /**\r
74          * Gets an immutable list of all the builders currently registered.\r
75          * \r
76          * @return list of all the builders currently registered\r
77          */\r
78         static const std::map<QName,XMLObjectBuilder*>& getBuilders() {\r
79             return m_map;\r
80         }\r
81     \r
82         /**\r
83          * Registers a new builder for the given key.\r
84          * \r
85          * @param builderKey the key used to retrieve this builder later\r
86          * @param builder the builder\r
87          */\r
88         static void registerBuilder(const QName& builderKey, XMLObjectBuilder* builder) {\r
89             m_map[builderKey]=builder;\r
90         }\r
91     \r
92         /**\r
93          * Deregisters a builder.\r
94          * \r
95          * @param builderKey the key for the builder to be deregistered\r
96          */\r
97         static void deregisterBuilder(const QName& builderKey) {\r
98             delete getBuilder(builderKey);\r
99             m_map.erase(builderKey);\r
100         }\r
101         \r
102         /**\r
103          * Unregisters and destroys all registered builders. \r
104          */\r
105         static void destroyBuilders();\r
106     \r
107     private:\r
108         static std::map<QName,XMLObjectBuilder*> m_map;\r
109     };\r
110 \r
111 };\r
112 \r
113 #if defined (_MSC_VER)\r
114     #pragma warning( pop )\r
115 #endif\r
116 \r
117 #endif /* __xmltooling_xmlobjbuilder_h__ */\r