Bug fixes and API changes from second unit test.
[shibboleth/cpp-xmltooling.git] / xmltooling / base.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 base.h\r
19  * \r
20  * Base header file definitions\r
21  * Must be included prior to including any other header\r
22  */\r
23 \r
24 #if !defined(__xmltooling_base_h__)\r
25 #define __xmltooling_base_h__\r
26 \r
27 #if defined (_MSC_VER) || defined(__BORLANDC__)\r
28   #include <xmltooling/config_pub_win32.h>\r
29 #else\r
30   #include <xmltooling/config_pub.h>\r
31 #endif\r
32 \r
33 /**\r
34  * @namespace xmltooling\r
35  * Public namespace of XML Tooling library\r
36  */\r
37 \r
38 // Windows and GCC4 Symbol Visibility Macros\r
39 \r
40 #ifdef WIN32\r
41   #define XMLTOOL_IMPORT __declspec(dllimport)\r
42   #define XMLTOOL_EXPORT __declspec(dllexport)\r
43   #define XMLTOOL_DLLLOCAL\r
44   #define XMLTOOL_DLLPUBLIC\r
45 #else\r
46   #define XMLTOOL_IMPORT\r
47   #ifdef GCC_HASCLASSVISIBILITY\r
48     #define XMLTOOL_EXPORT __attribute__ ((visibility("default")))\r
49     #define XMLTOOL_DLLLOCAL __attribute__ ((visibility("hidden")))\r
50     #define XMLTOOL_DLLPUBLIC __attribute__ ((visibility("default")))\r
51   #else\r
52     #define XMLTOOL_EXPORT\r
53     #define XMLTOOL_DLLLOCAL\r
54     #define XMLTOOL_DLLPUBLIC\r
55   #endif\r
56 #endif\r
57 \r
58 // Define XMLTOOL_API for DLL builds\r
59 #ifdef XMLTOOLING_EXPORTS\r
60   #define XMLTOOL_API XMLTOOL_EXPORT\r
61 #else\r
62   #define XMLTOOL_API XMLTOOL_IMPORT\r
63 #endif\r
64 \r
65 // Throwable classes must always be visible on GCC in all binaries\r
66 #ifdef WIN32\r
67   #define XMLTOOL_EXCEPTIONAPI(api) api\r
68 #elif defined(GCC_HASCLASSVISIBILITY)\r
69   #define XMLTOOL_EXCEPTIONAPI(api) XMLTOOL_EXPORT\r
70 #else\r
71   #define XMLTOOL_EXCEPTIONAPI(api)\r
72 #endif\r
73 \r
74 // Macro to block copy c'tor and assignment operator for a class\r
75 #define MAKE_NONCOPYABLE(type) \\r
76     private: \\r
77         type(const type&); \\r
78         type& operator=(const type&);\r
79 \r
80 #ifndef NULL\r
81 #define NULL    0\r
82 #endif\r
83 \r
84 #include <utility>\r
85 \r
86 namespace xmltooling {\r
87 \r
88     /**\r
89      * Template function for cloning a sequence of XMLObjects.\r
90      * Invokes the clone() member on each element of the input sequence and adds the copy to\r
91      * the output sequence. Order is preserved.\r
92      * \r
93      * @param in    input sequence to clone\r
94      * @param out   output sequence to copy cloned pointers into\r
95      */\r
96     template<class InputSequence,class OutputSequence> void clone(const InputSequence& in, OutputSequence& out) {\r
97         for (InputSequence::const_iterator i=in.begin(); i!=in.end(); i++)\r
98             out.push_back((*i)->clone());\r
99     }\r
100 \r
101     /*\r
102      * Functor for cleaning up heap objects in containers.\r
103      */\r
104     template<class T> struct cleanup\r
105     {\r
106         /**\r
107          * Function operator to delete an object.\r
108          * \r
109          * @param ptr   object to delete\r
110          */\r
111         void operator()(T* ptr) {delete ptr;}\r
112         \r
113         /**\r
114          * Function operator to delete an object stored as const.\r
115          * \r
116          * @param ptr   object to delete after casting away const\r
117          */\r
118         void operator()(const T* ptr) {delete const_cast<T*>(ptr);}\r
119     };\r
120 \r
121     /*\r
122      * Functor for cleaning up heap objects in key/value containers.\r
123      */\r
124     template<class A,class B> struct cleanup_pair\r
125     {\r
126         /**\r
127          * Function operator to delete an object.\r
128          * \r
129          * @param p   a pair in which the second component is the object to delete\r
130          */\r
131         void operator()(const std::pair<A,B*>& p) {delete p.second;}\r
132     };\r
133 };\r
134 \r
135 #endif /* __xmltooling_base_h__ */\r