Moved DOM methods up the tree, add copy c'tors, KeyInfo sample
[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 /**
78  * Blocks copy c'tor and assignment operator for a class.
79  */
80 #define MAKE_NONCOPYABLE(type) \
81     private: \
82         type(const type&); \
83         type& operator=(const type&);
84
85 /**
86  * Begins the declaration of an XMLObject specialization.
87  * Basic boilerplate includes a protected constructor, empty virtual destructor,
88  * and Unicode constants for the default associated element's name and prefix.
89  * 
90  * @param cname the name of the class to declare
91  * @param base  the base class to derive from using public virtual inheritance
92  */
93 #define BEGIN_XMLOBJECT(cname,base) \
94     class XMLTOOL_API cname : public virtual base, public virtual ValidatingXMLObject { \
95     protected: \
96         cname() {} \
97     public: \
98         virtual ~cname() {} \
99         /##** Type-specific clone method. */ \
100         virtual cname* clone##cname() const=0; \
101         /##** Element prefix */ \
102         static const XMLCh PREFIX[]; \
103         /##** Element local name */ \
104         static const XMLCh LOCAL_NAME[]
105
106 /**
107  * Ends the declaration of an XMLObject specialization.
108  */
109 #define END_XMLOBJECT }
110
111 /**
112  * Declares abstract get/set methods for a named XML attribute.
113  * 
114  * @param proper    the proper name of the attribute
115  * @param upcased   the upcased name of the attribute
116  */
117 #define DECL_XMLOBJECT_ATTRIB(proper,upcased) \
118     /##** proper attribute name */ \
119     static const XMLCh upcased##_ATTRIB_NAME[]; \
120     /##** Returns the proper attribute. */ \
121     virtual const XMLCh* get##proper() const=0; \
122     /##** Sets the proper attribute. */ \
123     virtual void set##proper(const XMLCh* proper)=0
124
125 /**
126  * Implements get/set methods and a private member for a named XML attribute.
127  * 
128  * @param proper    the proper name of the attribute
129  */
130 #define IMPL_XMLOBJECT_ATTRIB(proper) \
131     private: \
132         XMLCh* m_##proper; \
133     public: \
134         const XMLCh* get##proper() const { \
135             return m_##proper; \
136         } \
137         void set##proper(const XMLCh* proper) { \
138             m_##proper = prepareForAssignment(m_##proper,proper); \
139         }
140
141 /**
142  * Implements cloning methods for an XMLObject specialization implementation class.
143  * 
144  * @param cname    the name of the XMLObject specialization
145  */
146 #define IMPL_XMLOBJECT_CLONE(cname) \
147     cname* clone##cname() const { \
148         return clone(); \
149     } \
150     cname* clone() const { \
151         auto_ptr<XMLObject> domClone(AbstractDOMCachingXMLObject::clone()); \
152         cname##Impl* ret=dynamic_cast<cname##Impl*>(domClone.get()); \
153         if (ret) { \
154             domClone.release(); \
155             return ret; \
156         } \
157         return new cname##Impl(*this); \
158     }
159
160 /**
161  * Begins the declaration of an XMLObjectBuilder specialization.
162  * Basic boilerplate includes an empty virtual destructor, and
163  * a default builder.
164  * 
165  * @param cname the name of the XMLObject specialization
166  */
167 #define BEGIN_XMLOBJECTBUILDER(cname) \
168     /##** Builder for cname objects. */ \
169     class XMLTOOL_API cname##Builder : public xmltooling::XMLObjectBuilder { \
170     public: \
171         virtual ~cname##Builder() {} \
172         /##** Default builder. */ \
173         virtual cname* buildObject() const=0
174
175 /**
176  * Ends the declaration of an XMLObjectBuilder specialization.
177  */
178 #define END_XMLOBJECTBUILDER }
179
180 /**
181  * Begins the declaration of an XMLObjectBuilder specialization implementation class.
182  * 
183  * @param cname         the name of the XMLObject specialization
184  * @param namespaceURI  the XML namespace of the default associated element
185  */
186 #define BEGIN_XMLOBJECTBUILDERIMPL(cname,namespaceURI) \
187     class XMLTOOL_DLLLOCAL cname##BuilderImpl : public cname##Builder { \
188     public: \
189         cname* buildObject(const XMLCh* ns, const XMLCh* name, const XMLCh* prefix=NULL) const; \
190         cname* buildObject() const { \
191             return buildObject(namespaceURI,cname::LOCAL_NAME,cname::PREFIX); \
192         }
193
194 /**
195  * Ends the declaration of an XMLObjectBuilder specialization implementation class.
196  */
197 #define END_XMLOBJECTBUILDERIMPL }
198
199 #include <utility>
200
201 namespace xmltooling {
202
203     /**
204      * Template function for cloning a sequence of XMLObjects.
205      * Invokes the clone() member on each element of the input sequence and adds the copy to
206      * the output sequence. Order is preserved.
207      * 
208      * @param in    input sequence to clone
209      * @param out   output sequence to copy cloned pointers into
210      */
211     template<class InputSequence,class OutputSequence> void clone(const InputSequence& in, OutputSequence& out) {
212         for (typename InputSequence::const_iterator i=in.begin(); i!=in.end(); i++) {
213             if (*i)
214                 out.push_back((*i)->clone());
215             else
216                 out.push_back(*i);
217         }
218     }
219
220     /**
221      * Functor for cleaning up heap objects in containers.
222      */
223     template<class T> struct cleanup
224     {
225         /**
226          * Function operator to delete an object.
227          * 
228          * @param ptr   object to delete
229          */
230         void operator()(T* ptr) {delete ptr;}
231         
232         /**
233          * Function operator to delete an object stored as const.
234          * 
235          * @param ptr   object to delete after casting away const
236          */
237         void operator()(const T* ptr) {delete const_cast<T*>(ptr);}
238     };
239
240     /**
241      * Functor for cleaning up heap objects in key/value containers.
242      */
243     template<class A,class B> struct cleanup_pair
244     {
245         /**
246          * Function operator to delete an object.
247          * 
248          * @param p   a pair in which the second component is the object to delete
249          */
250         void operator()(const std::pair<A,B*>& p) {delete p.second;}
251     };
252 };
253
254 #endif /* __xmltooling_base_h__ */