Linux fixes
[shibboleth/cpp-xmltooling.git] / xmltooling / base.h
1 /*
2  *  Copyright 2001-2006 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 base.h
19  * 
20  * Base header file definitions
21  * Must be included prior to including any other header
22  */
23
24 #ifndef __xmltooling_base_h__
25 #define __xmltooling_base_h__
26
27 #if defined (_MSC_VER) || defined(__BORLANDC__)
28   #include <xmltooling/config_pub_win32.h>
29 #else
30   #include <xmltooling/config_pub.h>
31 #endif
32
33 /**
34  * @namespace xmltooling
35  * Public namespace of XML Tooling library
36  */
37
38 // Windows and GCC4 Symbol Visibility Macros
39 #ifdef WIN32
40   #define XMLTOOL_IMPORT __declspec(dllimport)
41   #define XMLTOOL_EXPORT __declspec(dllexport)
42   #define XMLTOOL_DLLLOCAL
43   #define XMLTOOL_DLLPUBLIC
44 #else
45   #define XMLTOOL_IMPORT
46   #ifdef GCC_HASCLASSVISIBILITY
47     #define XMLTOOL_EXPORT __attribute__ ((visibility("default")))
48     #define XMLTOOL_DLLLOCAL __attribute__ ((visibility("hidden")))
49     #define XMLTOOL_DLLPUBLIC __attribute__ ((visibility("default")))
50   #else
51     #define XMLTOOL_EXPORT
52     #define XMLTOOL_DLLLOCAL
53     #define XMLTOOL_DLLPUBLIC
54   #endif
55 #endif
56
57 // Define XMLTOOL_API for DLL builds
58 #ifdef XMLTOOLING_EXPORTS
59   #define XMLTOOL_API XMLTOOL_EXPORT
60 #else
61   #define XMLTOOL_API XMLTOOL_IMPORT
62 #endif
63
64 // Throwable classes must always be visible on GCC in all binaries
65 #ifdef WIN32
66   #define XMLTOOL_EXCEPTIONAPI(api) api
67 #elif defined(GCC_HASCLASSVISIBILITY)
68   #define XMLTOOL_EXCEPTIONAPI(api) XMLTOOL_EXPORT
69 #else
70   #define XMLTOOL_EXCEPTIONAPI(api)
71 #endif
72
73 #ifndef NULL
74 #define NULL    0
75 #endif
76
77 #ifdef _MSC_VER
78     #define XMLTOOLING_DOXYGEN(desc) /##** desc */
79 #else
80     #define XMLTOOLING_DOXYGEN(desc)
81 #endif
82
83 /**
84  * Blocks copy c'tor and assignment operator for a class.
85  */
86 #define MAKE_NONCOPYABLE(type) \
87     private: \
88         type(const type&); \
89         type& operator=(const type&);
90
91 #define UNICODE_LITERAL_1(a) {chLatin_##a, chNull}
92 #define UNICODE_LITERAL_2(a,b) {chLatin_##a, chLatin_##b, chNull}
93 #define UNICODE_LITERAL_3(a,b,c) {chLatin_##a, chLatin_##b, chLatin_##c, chNull}
94 #define UNICODE_LITERAL_4(a,b,c,d) {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chNull}
95 #define UNICODE_LITERAL_5(a,b,c,d,e) {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chNull}
96 #define UNICODE_LITERAL_6(a,b,c,d,e,f) {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chNull}
97 #define UNICODE_LITERAL_7(a,b,c,d,e,f,g) \
98     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chNull}
99 #define UNICODE_LITERAL_8(a,b,c,d,e,f,g,h) \
100     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chNull}
101 #define UNICODE_LITERAL_9(a,b,c,d,e,f,g,h,i) \
102     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, chNull}
103 #define UNICODE_LITERAL_10(a,b,c,d,e,f,g,h,i,j) \
104     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
105         chLatin_##j, chNull}
106 #define UNICODE_LITERAL_11(a,b,c,d,e,f,g,h,i,j,k) \
107     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
108         chLatin_##j, chLatin_##k, chNull}
109 #define UNICODE_LITERAL_12(a,b,c,d,e,f,g,h,i,j,k,l) \
110     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
111         chLatin_##j, chLatin_##k, chLatin_##l, chNull}
112 #define UNICODE_LITERAL_13(a,b,c,d,e,f,g,h,i,j,k,l,m) \
113     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
114         chLatin_##j, chLatin_##k, chLatin_##l, chLatin_##m, chNull}
115 #define UNICODE_LITERAL_14(a,b,c,d,e,f,g,h,i,j,k,l,m,n) \
116     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
117         chLatin_##j, chLatin_##k, chLatin_##l, chLatin_##m, chLatin_##n, chNull}
118 #define UNICODE_LITERAL_15(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) \
119     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
120         chLatin_##j, chLatin_##k, chLatin_##l, chLatin_##m, chLatin_##n, chLatin_##o, chNull}
121 #define UNICODE_LITERAL_16(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
122     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
123         chLatin_##j, chLatin_##k, chLatin_##l, chLatin_##m, chLatin_##n, chLatin_##o, chLatin_##p, chNull}
124 #define UNICODE_LITERAL_17(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q) \
125     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
126         chLatin_##j, chLatin_##k, chLatin_##l, chLatin_##m, chLatin_##n, chLatin_##o, chLatin_##p, chLatin_##q, chNull}
127 #define UNICODE_LITERAL_18(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r) \
128     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
129         chLatin_##j, chLatin_##k, chLatin_##l, chLatin_##m, chLatin_##n, chLatin_##o, chLatin_##p, chLatin_##q, chLatin_##r, chNull}
130 #define UNICODE_LITERAL_19(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s) \
131     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
132         chLatin_##j, chLatin_##k, chLatin_##l, chLatin_##m, chLatin_##n, chLatin_##o, chLatin_##p, chLatin_##q, chLatin_##r, \
133         chLatin_##s, chNull}
134 #define UNICODE_LITERAL_20(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) \
135     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
136         chLatin_##j, chLatin_##k, chLatin_##l, chLatin_##m, chLatin_##n, chLatin_##o, chLatin_##p, chLatin_##q, chLatin_##r, \
137         chLatin_##s, chLatin_##t, chNull}
138 #define UNICODE_LITERAL_21(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u) \
139     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
140         chLatin_##j, chLatin_##k, chLatin_##l, chLatin_##m, chLatin_##n, chLatin_##o, chLatin_##p, chLatin_##q, chLatin_##r, \
141         chLatin_##s, chLatin_##t, chLatin_##u, chNull}
142 #define UNICODE_LITERAL_22(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v) \
143     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
144         chLatin_##j, chLatin_##k, chLatin_##l, chLatin_##m, chLatin_##n, chLatin_##o, chLatin_##p, chLatin_##q, chLatin_##r, \
145         chLatin_##s, chLatin_##t, chLatin_##u, chLatin_##v, chNull}
146 #define UNICODE_LITERAL_23(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w) \
147     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
148         chLatin_##j, chLatin_##k, chLatin_##l, chLatin_##m, chLatin_##n, chLatin_##o, chLatin_##p, chLatin_##q, chLatin_##r, \
149         chLatin_##s, chLatin_##t, chLatin_##u, chLatin_##v, chLatin_##w, chNull}
150 #define UNICODE_LITERAL_24(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x) \
151     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
152         chLatin_##j, chLatin_##k, chLatin_##l, chLatin_##m, chLatin_##n, chLatin_##o, chLatin_##p, chLatin_##q, chLatin_##r, \
153         chLatin_##s, chLatin_##t, chLatin_##u, chLatin_##v, chLatin_##w, chLatin_##x, chNull}
154 #define UNICODE_LITERAL_25(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y) \
155     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
156         chLatin_##j, chLatin_##k, chLatin_##l, chLatin_##m, chLatin_##n, chLatin_##o, chLatin_##p, chLatin_##q, chLatin_##r, \
157         chLatin_##s, chLatin_##t, chLatin_##u, chLatin_##v, chLatin_##w, chLatin_##x, chLatin_##y, chNull}
158 #define UNICODE_LITERAL_26(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) \
159     {chLatin_##a, chLatin_##b, chLatin_##c, chLatin_##d, chLatin_##e, chLatin_##f, chLatin_##g, chLatin_##h, chLatin_##i, \
160         chLatin_##j, chLatin_##k, chLatin_##l, chLatin_##m, chLatin_##n, chLatin_##o, chLatin_##p, chLatin_##q, chLatin_##r, \
161         chLatin_##s, chLatin_##t, chLatin_##u, chLatin_##v, chLatin_##w, chLatin_##x, chLatin_##y, chLatin_##z, chNull}
162
163 /**
164  * Begins the declaration of an XMLObject specialization.
165  * Basic boilerplate includes a protected constructor, empty virtual destructor,
166  * and Unicode constants for the default associated element's name and prefix.
167  * 
168  * @param linkage   linkage specifier for the class
169  * @param cname     the name of the class to declare
170  * @param base      the base class to derive from using public virtual inheritance
171  */
172 #define BEGIN_XMLOBJECT(linkage,cname,base) \
173     class linkage cname : public virtual base, public virtual ValidatingXMLObject { \
174     protected: \
175         cname() {} \
176     public: \
177         virtual ~cname() {} \
178         XMLTOOLING_DOXYGEN(Type-specific clone method.) \
179         virtual cname* clone##cname() const=0; \
180         XMLTOOLING_DOXYGEN(Element local name) \
181         static const XMLCh LOCAL_NAME[]
182
183 /**
184  * Ends the declaration of an XMLObject specialization.
185  */
186 #define END_XMLOBJECT }
187
188 /**
189  * Declares abstract get/set methods for a named XML attribute.
190  * 
191  * @param proper    the proper name of the attribute
192  * @param upcased   the upcased name of the attribute
193  */
194 #define DECL_XMLOBJECT_ATTRIB(proper,upcased) \
195     public: \
196         XMLTOOLING_DOXYGEN(proper attribute name) \
197         static const XMLCh upcased##_ATTRIB_NAME[]; \
198         XMLTOOLING_DOXYGEN(Returns the proper attribute.) \
199         virtual const XMLCh* get##proper() const=0; \
200         XMLTOOLING_DOXYGEN(Sets the proper attribute.) \
201         virtual void set##proper(const XMLCh* proper)=0
202
203 /**
204  * Implements get/set methods and a private member for a named XML attribute.
205  * 
206  * @param proper    the proper name of the attribute
207  */
208 #define IMPL_XMLOBJECT_ATTRIB(proper) \
209     private: \
210         XMLCh* m_##proper; \
211     public: \
212         const XMLCh* get##proper() const { \
213             return m_##proper; \
214         } \
215         void set##proper(const XMLCh* proper) { \
216             m_##proper = prepareForAssignment(m_##proper,proper); \
217         }
218
219 /**
220  * Declares abstract get/set methods for a typed XML child object.
221  * 
222  * @param proper    the proper name of the child type
223  */
224 #define DECL_XMLOBJECT_CHILD(proper) \
225     public: \
226         XMLTOOLING_DOXYGEN(Returns the proper child.) \
227         virtual proper* get##proper() const=0; \
228         XMLTOOLING_DOXYGEN(Sets the proper child.) \
229         virtual void set##proper(proper* child)=0
230
231 /**
232  * Implements get/set methods and a private list iterator member for a typed XML child object.
233  * 
234  * @param proper    the proper name of the child type
235  */
236 #define IMPL_XMLOBJECT_CHILD(proper) \
237     private: \
238         proper* m_##proper; \
239         std::list<XMLObject*>::iterator m_pos_##proper; \
240     public: \
241         proper* get##proper() const { \
242             return m_##proper; \
243         } \
244         void set##proper(proper* child) { \
245             prepareForAssignment(m_##proper,child); \
246             *m_pos_##proper = m_##proper = child; \
247         }
248
249 /**
250  * Declares abstract get/set methods for a typed XML child collection.
251  * 
252  * @param proper    the proper name of the child type
253  */
254 #define DECL_XMLOBJECT_CHILDREN(proper) \
255     public: \
256         XMLTOOLING_DOXYGEN(Returns modifiable proper collection.) \
257         virtual VectorOf(proper) get##proper##s()=0; \
258         XMLTOOLING_DOXYGEN(Returns reference to immutable proper collection.) \
259         virtual const std::vector<proper*>& get##proper##s() const=0
260
261 /**
262  * Implements get method and a private vector member for a typed XML child collection.
263  * 
264  * @param proper    the proper name of the child type
265  * @param fence     insertion fence for new objects of the child collection in backing list
266  */
267 #define IMPL_XMLOBJECT_CHILDREN(proper,fence) \
268     private: \
269         std::vector<proper*> m_##proper##s; \
270     public: \
271         VectorOf(proper) get##proper##s() { \
272             return VectorOf(proper)(this, m_##proper##s, &m_children, fence); \
273         } \
274         const std::vector<proper*>& get##proper##s() const { \
275             return m_##proper##s; \
276         } 
277
278 /**
279  * Implements unmarshalling process branch for typed child collection element
280  * 
281  * @param proper        the proper name of the child type
282  * @param namespaceURI  the XML namespace of the child element
283  */
284 #define PROC_XMLOBJECT_CHILDREN(proper,namespaceURI) \
285     if (XMLHelper::isNodeNamed(root,namespaceURI,proper::LOCAL_NAME)) { \
286         proper* typesafe=dynamic_cast<proper*>(childXMLObject); \
287         if (typesafe) { \
288             get##proper##s().push_back(typesafe); \
289             return; \
290         } \
291     }
292
293 /**
294  * Implements unmarshalling process branch for typed child singleton element
295  * 
296  * @param proper        the proper name of the child type
297  * @param namespaceURI  the XML namespace of the child element
298  */
299 #define PROC_XMLOBJECT_CHILD(proper,namespaceURI) \
300     if (XMLHelper::isNodeNamed(root,namespaceURI,proper::LOCAL_NAME)) { \
301         proper* typesafe=dynamic_cast<proper*>(childXMLObject); \
302         if (typesafe) { \
303             set##proper(typesafe); \
304             return; \
305         } \
306     }
307
308 /**
309  * Declares abstract get/set methods for named XML element content.
310  * 
311  * @param proper    the proper name to label the element's content
312  */
313 #define DECL_XMLOBJECT_CONTENT(proper) \
314     XMLTOOLING_DOXYGEN(Returns proper.) \
315     virtual const XMLCh* get##proper() const=0; \
316     XMLTOOLING_DOXYGEN(Sets proper.) \
317     virtual void set##proper(const XMLCh* proper)=0
318
319 /**
320  * Implements get/set methods and a private member for named XML element content.
321  * 
322  * @param proper    the proper name to label the element's content
323  */
324 #define IMPL_XMLOBJECT_CONTENT(proper) \
325     private: \
326         XMLCh* m_##proper; \
327     public: \
328         const XMLCh* get##proper() const { \
329             return m_##proper; \
330         } \
331         void set##proper(const XMLCh* proper) { \
332             m_##proper = prepareForAssignment(m_##proper,proper); \
333         } \
334     protected: \
335         void marshallElementContent(DOMElement* domElement) const { \
336             if(get##proper()) { \
337                 domElement->appendChild(domElement->getOwnerDocument()->createTextNode(get##proper())); \
338             } \
339         } \
340         void processElementContent(const XMLCh* elementContent) { \
341             set##proper(elementContent); \
342         }
343
344
345 /**
346  * Implements cloning methods for an XMLObject specialization implementation class.
347  * 
348  * @param cname    the name of the XMLObject specialization
349  */
350 #define IMPL_XMLOBJECT_CLONE(cname) \
351     cname* clone##cname() const { \
352         return clone(); \
353     } \
354     cname* clone() const { \
355         auto_ptr<XMLObject> domClone(AbstractDOMCachingXMLObject::clone()); \
356         cname##Impl* ret=dynamic_cast<cname##Impl*>(domClone.get()); \
357         if (ret) { \
358             domClone.release(); \
359             return ret; \
360         } \
361         return new cname##Impl(*this); \
362     }
363
364 /**
365  * Declares and defines an implementation class for an XMLObject with
366  * a simple content model and type, handling it as string data.
367  * 
368  * @param linkage   linkage specifier for the class
369  * @param cname     the name of the XMLObject specialization
370  * @param proper    the proper name to label the element's content
371  */
372 #define DECL_XMLOBJECTIMPL_SIMPLE(linkage,cname,proper) \
373     class linkage cname##Impl \
374         : public cname, \
375             public AbstractDOMCachingXMLObject, \
376             public AbstractValidatingXMLObject, \
377             public AbstractXMLObjectMarshaller, \
378             public AbstractXMLObjectUnmarshaller \
379     { \
380     public: \
381         virtual ~cname##Impl() {} \
382         cname##Impl(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType) \
383             : AbstractXMLObject(nsURI, localName, prefix, schemaType), m_##proper(NULL) { \
384         } \
385         cname##Impl(const cname##Impl& src) \
386             : AbstractXMLObject(src), \
387                 AbstractDOMCachingXMLObject(src), \
388                 AbstractValidatingXMLObject(src), \
389                 m_##proper(XMLString::replicate(src.m_##proper)) { \
390         } \
391         IMPL_XMLOBJECT_CLONE(cname) \
392         IMPL_XMLOBJECT_CONTENT(proper) \
393     }
394     
395 /**
396  * Begins the declaration of an XMLObjectBuilder specialization.
397  * Basic boilerplate includes an empty virtual destructor, and
398  * a default builder that defaults the element name.
399  * 
400  * @param linkage           linkage specifier for the class
401  * @param cname             the name of the XMLObject specialization
402  * @param namespaceURI      the XML namespace of the default associated element
403  * @param namespacePrefix   the XML namespace prefix of the default associated element
404  */
405 #define BEGIN_XMLOBJECTBUILDER(linkage,cname,namespaceURI,namespacePrefix) \
406     XMLTOOLING_DOXYGEN(Builder for cname objects.) \
407     class linkage cname##Builder : public xmltooling::XMLObjectBuilder { \
408     public: \
409         virtual ~cname##Builder() {} \
410         XMLTOOLING_DOXYGEN(Default builder.) \
411         virtual cname* buildObject() const { \
412             return buildObject(namespaceURI,cname::LOCAL_NAME,namespacePrefix); \
413         } \
414         virtual cname* buildObject( \
415             const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix=NULL, const QName* schemaType=NULL \
416             ) const
417
418 /**
419  * Ends the declaration of an XMLObjectBuilder specialization.
420  */
421 #define END_XMLOBJECTBUILDER }
422
423 /**
424  * Declares a generic XMLObjectBuilder specialization.
425  * 
426  * @param linkage           linkage specifier for the class
427  * @param cname             the name of the XMLObject specialization
428  * @param namespaceURI      the XML namespace of the default associated element
429  * @param namespacePrefix   the XML namespace prefix of the default associated element
430  */
431  #define DECL_XMLOBJECTBUILDER(linkage,cname,namespaceURI,namespacePrefix) \
432     BEGIN_XMLOBJECTBUILDER(linkage,cname,namespaceURI,namespacePrefix); \
433     END_XMLOBJECTBUILDER
434
435 /**
436  * Implements the standard XMLObjectBuilder specialization function. 
437  * 
438  * @param cname the name of the XMLObject specialization
439  */
440 #define IMPL_XMLOBJECTBUILDER(cname) \
441     cname* cname##Builder::buildObject( \
442         const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType \
443         ) const \
444     { \
445         return new cname##Impl(nsURI,localName,prefix,schemaType); \
446     }
447
448 /**
449  * Begins the declaration of a Schema Validator specialization.
450  * 
451  * @param linkage           linkage specifier for the class
452  * @param cname the base name of the Validator specialization
453  */
454  #define BEGIN_XMLOBJECTVALIDATOR(linkage,cname) \
455     class linkage cname##SchemaValidator : public Validator \
456     { \
457     public: \
458         virtual ~cname##SchemaValidator() {} \
459         virtual cname##SchemaValidator* clone() const { \
460             return new cname##SchemaValidator(); \
461         } \
462         virtual void validate(const XMLObject* xmlObject) const { \
463             const cname* ptr=dynamic_cast<const cname*>(xmlObject); \
464             if (!ptr) \
465                 throw ValidationException(#cname"SchemaValidator: unsupported object type ($1).",xmltooling::params(1,typeid(xmlObject).name()))
466
467 /**
468  * Ends the declaration of a Validator specialization.
469  */
470 #define END_XMLOBJECTVALIDATOR } }
471
472 /**
473  * Validator code that checks the object type.
474  * 
475  * @param cname     the name of the XMLObject specialization
476  */
477 #define XMLOBJECTVALIDATOR_CHECKTYPE(cname) \
478     const cname* ptr=dynamic_cast<const cname*>(xmlObject); \
479     if (!ptr) \
480         throw ValidationException(#cname"SchemaValidator: unsupported object type ($1).",xmltooling::params(1,typeid(xmlObject).name()))
481
482 /**
483  * Validator code that checks for a required attribute, content, or singleton.
484  * 
485  * @param cname     the name of the XMLObject specialization
486  * @param proper    the proper name of the attribute, content, or singleton member 
487  */
488 #define XMLOBJECTVALIDATOR_REQUIRE(cname,proper) \
489     if (!ptr->get##proper()) \
490         throw ValidationException(#cname" must have "#proper".")
491
492 /**
493  * Validator code that checks for a non-empty collection.
494  * 
495  * @param cname     the name of the XMLObject specialization
496  * @param proper    the proper name of the collection item 
497  */
498 #define XMLOBJECTVALIDATOR_CHECKEMPTY(cname,proper) \
499     if (ptr->get##proper##s().empty()) \
500         throw ValidationException(#cname" must have at least one "#proper".")
501
502 #include <utility>
503
504 namespace xmltooling {
505
506     /**
507      * Template function for cloning a sequence of XMLObjects.
508      * Invokes the clone() member on each element of the input sequence and adds the copy to
509      * the output sequence. Order is preserved.
510      * 
511      * @param in    input sequence to clone
512      * @param out   output sequence to copy cloned pointers into
513      */
514     template<class InputSequence,class OutputSequence> void clone(const InputSequence& in, OutputSequence& out) {
515         for (typename InputSequence::const_iterator i=in.begin(); i!=in.end(); i++) {
516             if (*i)
517                 out.push_back((*i)->clone());
518             else
519                 out.push_back(*i);
520         }
521     }
522
523     /**
524      * Functor for cleaning up heap objects in containers.
525      */
526     template<class T> struct cleanup
527     {
528         /**
529          * Function operator to delete an object.
530          * 
531          * @param ptr   object to delete
532          */
533         void operator()(T* ptr) {delete ptr;}
534         
535         /**
536          * Function operator to delete an object stored as const.
537          * 
538          * @param ptr   object to delete after casting away const
539          */
540         void operator()(const T* ptr) {delete const_cast<T*>(ptr);}
541     };
542
543     /**
544      * Functor for cleaning up heap objects in key/value containers.
545      */
546     template<class A,class B> struct cleanup_pair
547     {
548         /**
549          * Function operator to delete an object.
550          * 
551          * @param p   a pair in which the second component is the object to delete
552          */
553         void operator()(const std::pair<A,B*>& p) {delete p.second;}
554     };
555 };
556
557 #endif /* __xmltooling_base_h__ */