More sample objects and macros.
[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 #ifdef _MSC_VER
74     #define XMLTOOLING_DOXYGEN(desc) /##** desc */
75 #else
76     #define XMLTOOLING_DOXYGEN(desc)
77 #endif
78
79 /**
80  * Blocks copy c'tor and assignment operator for a class.
81  */
82 #define MAKE_NONCOPYABLE(type) \
83     private: \
84         type(const type&); \
85         type& operator=(const type&);
86
87 #ifndef DOXYGEN_SKIP
88 #ifndef NULL
89 #define NULL    0
90 #endif
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 #endif /* DOXYGEN_SKIP */
163
164 /**
165  * Begins the declaration of an XMLObject specialization.
166  * Basic boilerplate includes a protected constructor, empty virtual destructor,
167  * and Unicode constants for the default associated element's name and prefix.
168  * 
169  * @param linkage   linkage specifier for the class
170  * @param cname     the name of the class to declare
171  * @param base      the base class to derive from using public virtual inheritance
172  */
173 #define BEGIN_XMLOBJECT(linkage,cname,base) \
174     class linkage cname : public virtual base, public virtual ValidatingXMLObject { \
175     protected: \
176         cname() {} \
177     public: \
178         virtual ~cname() {} \
179         XMLTOOLING_DOXYGEN(Type-specific clone method.) \
180         virtual cname* clone##cname() const=0; \
181         XMLTOOLING_DOXYGEN(Element local name) \
182         static const XMLCh LOCAL_NAME[]
183
184 /**
185  * Ends the declaration of an XMLObject specialization.
186  */
187 #define END_XMLOBJECT }
188
189 /**
190  * Declares abstract get/set methods for a named XML attribute.
191  * 
192  * @param proper    the proper name of the attribute
193  * @param upcased   the upcased name of the attribute
194  */
195 #define DECL_XMLOBJECT_ATTRIB(proper,upcased) \
196     public: \
197         XMLTOOLING_DOXYGEN(proper attribute name) \
198         static const XMLCh upcased##_ATTRIB_NAME[]; \
199         XMLTOOLING_DOXYGEN(Returns the proper attribute.) \
200         virtual const XMLCh* get##proper() const=0; \
201         XMLTOOLING_DOXYGEN(Sets the proper attribute.) \
202         virtual void set##proper(const XMLCh* proper)=0
203
204 /**
205  * Implements get/set methods and a private member for a named XML attribute.
206  * 
207  * @param proper    the proper name of the attribute
208  */
209 #define IMPL_XMLOBJECT_ATTRIB(proper) \
210     private: \
211         XMLCh* m_##proper; \
212     public: \
213         const XMLCh* get##proper() const { \
214             return m_##proper; \
215         } \
216         void set##proper(const XMLCh* proper) { \
217             m_##proper = prepareForAssignment(m_##proper,proper); \
218         }
219
220 /**
221  * Declares abstract get/set methods for a typed XML child object.
222  * 
223  * @param proper    the proper name of the child type
224  */
225 #define DECL_XMLOBJECT_CHILD(proper) \
226     public: \
227         XMLTOOLING_DOXYGEN(Returns the proper child.) \
228         virtual proper* get##proper() const=0; \
229         XMLTOOLING_DOXYGEN(Sets the proper child.) \
230         virtual void set##proper(proper* child)=0
231
232 /**
233  * Implements get/set methods and a private list iterator member for a typed XML child object.
234  * 
235  * @param proper    the proper name of the child type
236  */
237 #define IMPL_XMLOBJECT_CHILD(proper) \
238     private: \
239         proper* m_##proper; \
240         std::list<XMLObject*>::iterator m_pos_##proper; \
241     public: \
242         proper* get##proper() const { \
243             return m_##proper; \
244         } \
245         void set##proper(proper* child) { \
246             prepareForAssignment(m_##proper,child); \
247             *m_pos_##proper = m_##proper = child; \
248         }
249
250 /**
251  * Declares abstract get/set methods for a typed XML child collection.
252  * 
253  * @param proper    the proper name of the child type
254  */
255 #define DECL_XMLOBJECT_CHILDREN(proper) \
256     public: \
257         XMLTOOLING_DOXYGEN(Returns modifiable proper collection.) \
258         virtual VectorOf(proper) get##proper##s()=0; \
259         XMLTOOLING_DOXYGEN(Returns reference to immutable proper collection.) \
260         virtual const std::vector<proper*>& get##proper##s() const=0
261
262 /**
263  * Implements get method and a private vector member for a typed XML child collection.
264  * 
265  * @param proper    the proper name of the child type
266  * @param fence     insertion fence for new objects of the child collection in backing list
267  */
268 #define IMPL_XMLOBJECT_CHILDREN(proper,fence) \
269     private: \
270         std::vector<proper*> m_##proper##s; \
271     public: \
272         VectorOf(proper) get##proper##s() { \
273             return VectorOf(proper)(this, m_##proper##s, &m_children, fence); \
274         } \
275         const std::vector<proper*>& get##proper##s() const { \
276             return m_##proper##s; \
277         } 
278
279 /**
280  * Implements unmarshalling process branch for typed child collection element
281  * 
282  * @param proper        the proper name of the child type
283  * @param namespaceURI  the XML namespace of the child element
284  */
285 #define PROC_XMLOBJECT_CHILDREN(proper,namespaceURI) \
286     if (XMLHelper::isNodeNamed(root,namespaceURI,proper::LOCAL_NAME)) { \
287         proper* typesafe=dynamic_cast<proper*>(childXMLObject); \
288         if (typesafe) { \
289             get##proper##s().push_back(typesafe); \
290             return; \
291         } \
292     }
293
294 /**
295  * Implements unmarshalling process branch for typed child singleton element
296  * 
297  * @param proper        the proper name of the child type
298  * @param namespaceURI  the XML namespace of the child element
299  */
300 #define PROC_XMLOBJECT_CHILD(proper,namespaceURI) \
301     if (XMLHelper::isNodeNamed(root,namespaceURI,proper::LOCAL_NAME)) { \
302         proper* typesafe=dynamic_cast<proper*>(childXMLObject); \
303         if (typesafe) { \
304             set##proper(typesafe); \
305             return; \
306         } \
307     }
308
309 /**
310  * Declares abstract get/set methods for named XML element content.
311  * 
312  * @param proper    the proper name to label the element's content
313  */
314 #define DECL_XMLOBJECT_CONTENT(proper) \
315     XMLTOOLING_DOXYGEN(Returns proper.) \
316     virtual const XMLCh* get##proper() const=0; \
317     XMLTOOLING_DOXYGEN(Sets proper.) \
318     virtual void set##proper(const XMLCh* proper)=0
319
320 /**
321  * Implements get/set methods and a private member for named XML element content.
322  * 
323  * @param proper    the proper name to label the element's content
324  */
325 #define IMPL_XMLOBJECT_CONTENT(proper) \
326     private: \
327         XMLCh* m_##proper; \
328     public: \
329         const XMLCh* get##proper() const { \
330             return m_##proper; \
331         } \
332         void set##proper(const XMLCh* proper) { \
333             m_##proper = prepareForAssignment(m_##proper,proper); \
334         } \
335     protected: \
336         void marshallElementContent(DOMElement* domElement) const { \
337             if(get##proper()) { \
338                 domElement->appendChild(domElement->getOwnerDocument()->createTextNode(get##proper())); \
339             } \
340         } \
341         void processElementContent(const XMLCh* elementContent) { \
342             set##proper(elementContent); \
343         }
344
345
346 /**
347  * Implements cloning methods for an XMLObject specialization implementation class.
348  * 
349  * @param cname    the name of the XMLObject specialization
350  */
351 #define IMPL_XMLOBJECT_CLONE(cname) \
352     cname* clone##cname() const { \
353         return clone(); \
354     } \
355     cname* clone() const { \
356         auto_ptr<XMLObject> domClone(AbstractDOMCachingXMLObject::clone()); \
357         cname##Impl* ret=dynamic_cast<cname##Impl*>(domClone.get()); \
358         if (ret) { \
359             domClone.release(); \
360             return ret; \
361         } \
362         return new cname##Impl(*this); \
363     }
364
365 /**
366  * Declares an XMLObject specialization with a simple content model and type,
367  * handling it as string data.
368  * 
369  * @param linkage   linkage specifier for the class
370  * @param cname     the name of the XMLObject specialization
371  * @param proper    the proper name to label the element's content
372  */
373 #define DECL_XMLOBJECT_SIMPLE(linkage,cname,proper,desc) \
374     XMLTOOLING_DOXYGEN(desc) \
375     BEGIN_XMLOBJECT(linkage,cname,XMLObject); \
376         DECL_XMLOBJECT_CONTENT(proper); \
377     END_XMLOBJECT
378
379 /**
380  * Declares and defines an implementation class for an XMLObject with
381  * a simple content model and type, handling it as string data.
382  * 
383  * @param linkage   linkage specifier for the class
384  * @param cname     the name of the XMLObject specialization
385  * @param proper    the proper name to label the element's content
386  */
387 #define DECL_XMLOBJECTIMPL_SIMPLE(linkage,cname,proper) \
388     class linkage cname##Impl \
389         : public cname, \
390             public AbstractDOMCachingXMLObject, \
391             public AbstractValidatingXMLObject, \
392             public AbstractXMLObjectMarshaller, \
393             public AbstractXMLObjectUnmarshaller \
394     { \
395     public: \
396         virtual ~cname##Impl() {} \
397         cname##Impl(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType) \
398             : AbstractXMLObject(nsURI, localName, prefix, schemaType), m_##proper(NULL) { \
399         } \
400         cname##Impl(const cname##Impl& src) \
401             : AbstractXMLObject(src), \
402                 AbstractDOMCachingXMLObject(src), \
403                 AbstractValidatingXMLObject(src), \
404                 m_##proper(XMLString::replicate(src.m_##proper)) { \
405         } \
406         IMPL_XMLOBJECT_CLONE(cname) \
407         IMPL_XMLOBJECT_CONTENT(proper) \
408     }
409     
410 /**
411  * Begins the declaration of an XMLObjectBuilder specialization.
412  * Basic boilerplate includes an empty virtual destructor, and
413  * a default builder that defaults the element name.
414  * 
415  * @param linkage           linkage specifier for the class
416  * @param cname             the name of the XMLObject specialization
417  * @param namespaceURI      the XML namespace of the default associated element
418  * @param namespacePrefix   the XML namespace prefix of the default associated element
419  */
420 #define BEGIN_XMLOBJECTBUILDER(linkage,cname,namespaceURI,namespacePrefix) \
421     XMLTOOLING_DOXYGEN(Builder for cname objects.) \
422     class linkage cname##Builder : public xmltooling::XMLObjectBuilder { \
423     public: \
424         virtual ~cname##Builder() {} \
425         XMLTOOLING_DOXYGEN(Default builder.) \
426         virtual cname* buildObject() const { \
427             return buildObject(namespaceURI,cname::LOCAL_NAME,namespacePrefix); \
428         } \
429         virtual cname* buildObject( \
430             const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix=NULL, const QName* schemaType=NULL \
431             ) const
432
433 /**
434  * Ends the declaration of an XMLObjectBuilder specialization.
435  */
436 #define END_XMLOBJECTBUILDER }
437
438 /**
439  * Declares a generic XMLObjectBuilder specialization.
440  * 
441  * @param linkage           linkage specifier for the class
442  * @param cname             the name of the XMLObject specialization
443  * @param namespaceURI      the XML namespace of the default associated element
444  * @param namespacePrefix   the XML namespace prefix of the default associated element
445  */
446  #define DECL_XMLOBJECTBUILDER(linkage,cname,namespaceURI,namespacePrefix) \
447     BEGIN_XMLOBJECTBUILDER(linkage,cname,namespaceURI,namespacePrefix); \
448     END_XMLOBJECTBUILDER
449
450 /**
451  * Implements the standard XMLObjectBuilder specialization function. 
452  * 
453  * @param cname the name of the XMLObject specialization
454  */
455 #define IMPL_XMLOBJECTBUILDER(cname) \
456     cname* cname##Builder::buildObject( \
457         const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType \
458         ) const \
459     { \
460         return new cname##Impl(nsURI,localName,prefix,schemaType); \
461     }
462
463 /**
464  * Begins the declaration of a Schema Validator specialization.
465  * 
466  * @param linkage           linkage specifier for the class
467  * @param cname the base name of the Validator specialization
468  */
469  #define BEGIN_XMLOBJECTVALIDATOR(linkage,cname) \
470     class linkage cname##SchemaValidator : public Validator \
471     { \
472     public: \
473         virtual ~cname##SchemaValidator() {} \
474         virtual cname##SchemaValidator* clone() const { \
475             return new cname##SchemaValidator(); \
476         } \
477         virtual void validate(const XMLObject* xmlObject) const { \
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  * Ends the declaration of a Validator specialization.
484  */
485 #define END_XMLOBJECTVALIDATOR } }
486
487 /**
488  * Validator code that checks the object type.
489  * 
490  * @param cname     the name of the XMLObject specialization
491  */
492 #define XMLOBJECTVALIDATOR_CHECKTYPE(cname) \
493     const cname* ptr=dynamic_cast<const cname*>(xmlObject); \
494     if (!ptr) \
495         throw ValidationException(#cname"SchemaValidator: unsupported object type ($1).",xmltooling::params(1,typeid(xmlObject).name()))
496
497 /**
498  * Validator code that checks for a required attribute, content, or singleton.
499  * 
500  * @param cname     the name of the XMLObject specialization
501  * @param proper    the proper name of the attribute, content, or singleton member 
502  */
503 #define XMLOBJECTVALIDATOR_REQUIRE(cname,proper) \
504     if (!ptr->get##proper()) \
505         throw ValidationException(#cname" must have "#proper".")
506
507 /**
508  * Validator code that checks for one of a pair of
509  * required attributes, content, or singletons.
510  * 
511  * @param cname     the name of the XMLObject specialization
512  * @param proper1   the proper name of the first attribute, content, or singleton member 
513  * @param proper2   the proper name of the second attribute, content, or singleton member 
514  */
515 #define XMLOBJECTVALIDATOR_ONEOF(cname,proper1,proper2) \
516     if (!ptr->get##proper1() && !ptr->get##proper2()) \
517         throw ValidationException(#cname" must have "#proper1" or "#proper2".")
518
519 /**
520  * Validator code that checks for one of a set of three
521  * required attributes, content, or singletons.
522  * 
523  * @param cname     the name of the XMLObject specialization
524  * @param proper1   the proper name of the first attribute, content, or singleton member
525  * @param proper2   the proper name of the second attribute, content, or singleton member
526  * @param proper3   the proper name of the third attribute, content, or singleton member
527  */
528 #define XMLOBJECTVALIDATOR_ONEOF3(cname,proper1,proper2,proper3) \
529     if (!ptr->get##proper1() && !ptr->get##proper2() && !ptr->get##proper3()) \
530         throw ValidationException(#cname" must have "#proper1", "#proper2", or "#proper3".")
531
532 /**
533  * Validator code that checks a co-constraint (if one present, the other must be)
534  * between a pair of attributes, content, or singletons.
535  * 
536  * @param cname     the name of the XMLObject specialization
537  * @param proper1   the proper name of the first attribute, content, or singleton member 
538  * @param proper2   the proper name of the second attribute, content, or singleton member 
539  */
540 #define XMLOBJECTVALIDATOR_NONEORBOTH(cname,proper1,proper2) \
541     if ((ptr->get##proper1() && !ptr->get##proper2()) || (!ptr->get##proper1() && ptr->get##proper2())) \
542         throw ValidationException(#cname" cannot have "#proper1" without "#proper2".")
543
544 /**
545  * Validator code that checks for a non-empty collection.
546  * 
547  * @param cname     the name of the XMLObject specialization
548  * @param proper    the proper name of the collection item 
549  */
550 #define XMLOBJECTVALIDATOR_NONEMPTY(cname,proper) \
551     if (ptr->get##proper##s().empty()) \
552         throw ValidationException(#cname" must have at least one "#proper".")
553
554 /**
555  * Declares/defines a Validator specialization that checks object type and
556  * a non-empty simple content model.
557  * 
558  * @param linkage   linkage specifier for the class
559  * @param cname     the name of the XMLObject specialization
560  * @param proper    the proper name to label the element's content
561  */
562 #define XMLOBJECTVALIDATOR_SIMPLE(linkage,cname,proper) \
563     BEGIN_XMLOBJECTVALIDATOR(linkage,cname); \
564         XMLOBJECTVALIDATOR_REQUIRE(cname,proper); \
565     END_XMLOBJECTVALIDATOR
566
567 #include <utility>
568
569 namespace xmltooling {
570
571     /**
572      * Template function for cloning a sequence of XMLObjects.
573      * Invokes the clone() member on each element of the input sequence and adds the copy to
574      * the output sequence. Order is preserved.
575      * 
576      * @param in    input sequence to clone
577      * @param out   output sequence to copy cloned pointers into
578      */
579     template<class InputSequence,class OutputSequence> void clone(const InputSequence& in, OutputSequence& out) {
580         for (typename InputSequence::const_iterator i=in.begin(); i!=in.end(); i++) {
581             if (*i)
582                 out.push_back((*i)->clone());
583             else
584                 out.push_back(*i);
585         }
586     }
587
588     /**
589      * Functor for cleaning up heap objects in containers.
590      */
591     template<class T> struct cleanup
592     {
593         /**
594          * Function operator to delete an object.
595          * 
596          * @param ptr   object to delete
597          */
598         void operator()(T* ptr) {delete ptr;}
599         
600         /**
601          * Function operator to delete an object stored as const.
602          * 
603          * @param ptr   object to delete after casting away const
604          */
605         void operator()(const T* ptr) {delete const_cast<T*>(ptr);}
606     };
607
608     /**
609      * Functor for cleaning up heap objects in key/value containers.
610      */
611     template<class A,class B> struct cleanup_pair
612     {
613         /**
614          * Function operator to delete an object.
615          * 
616          * @param p   a pair in which the second component is the object to delete
617          */
618         void operator()(const std::pair<A,B*>& p) {delete p.second;}
619     };
620 };
621
622 #endif /* __xmltooling_base_h__ */