1 /* C++ compatible function declaration macros.
2 Copyright (C) 2010 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* The three most frequent use cases of these macros are:
22 * For providing a substitute for a function that is missing on some
23 platforms, but is declared and works fine on the platforms on which
28 _GL_FUNCDECL_SYS (foo, ...);
30 _GL_CXXALIAS_SYS (foo, ...);
31 _GL_CXXALIASWARN (foo);
32 #elif defined GNULIB_POSIXCHECK
36 * For providing a replacement for a function that exists on all platforms,
37 but is broken/insufficient and needs to be replaced on some platforms:
41 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
45 _GL_FUNCDECL_RPL (foo, ...);
46 _GL_CXXALIAS_RPL (foo, ...);
48 _GL_CXXALIAS_SYS (foo, ...);
50 _GL_CXXALIASWARN (foo);
51 #elif defined GNULIB_POSIXCHECK
55 * For providing a replacement for a function that exists on some platforms
56 but is broken/insufficient and needs to be replaced on some of them and
57 is additionally either missing or undeclared on some other platforms:
61 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
65 _GL_FUNCDECL_RPL (foo, ...);
66 _GL_CXXALIAS_RPL (foo, ...);
68 # if !@HAVE_FOO@ or if !@HAVE_DECL_FOO@
69 _GL_FUNCDECL_SYS (foo, ...);
71 _GL_CXXALIAS_SYS (foo, ...);
73 _GL_CXXALIASWARN (foo);
74 #elif defined GNULIB_POSIXCHECK
79 /* _GL_EXTERN_C declaration;
80 performs the declaration with C linkage. */
81 #if defined __cplusplus
82 # define _GL_EXTERN_C extern "C"
84 # define _GL_EXTERN_C extern
87 /* _GL_FUNCDECL_RPL (func, rettype, parameters_and_attributes);
88 declares a replacement function, named rpl_func, with the given prototype,
89 consisting of return type, parameters, and attributes.
91 _GL_FUNCDECL_RPL (open, int, (const char *filename, int flags, ...)
92 _GL_ARG_NONNULL ((1)));
94 #define _GL_FUNCDECL_RPL(func,rettype,parameters_and_attributes) \
95 _GL_FUNCDECL_RPL_1 (rpl_##func, rettype, parameters_and_attributes)
96 #define _GL_FUNCDECL_RPL_1(rpl_func,rettype,parameters_and_attributes) \
97 _GL_EXTERN_C rettype rpl_func parameters_and_attributes
99 /* _GL_FUNCDECL_SYS (func, rettype, parameters_and_attributes);
100 declares the system function, named func, with the given prototype,
101 consisting of return type, parameters, and attributes.
103 _GL_FUNCDECL_SYS (open, int, (const char *filename, int flags, ...)
104 _GL_ARG_NONNULL ((1)));
106 #define _GL_FUNCDECL_SYS(func,rettype,parameters_and_attributes) \
107 _GL_EXTERN_C rettype func parameters_and_attributes
109 /* _GL_CXXALIAS_RPL (func, rettype, parameters);
110 declares a C++ alias called GNULIB_NAMESPACE::func
111 that redirects to rpl_func, if GNULIB_NAMESPACE is defined.
113 _GL_CXXALIAS_RPL (open, int, (const char *filename, int flags, ...));
115 #define _GL_CXXALIAS_RPL(func,rettype,parameters) \
116 _GL_CXXALIAS_RPL_1 (func, rpl_##func, rettype, parameters)
117 #if defined __cplusplus && defined GNULIB_NAMESPACE
118 # define _GL_CXXALIAS_RPL_1(func,rpl_func,rettype,parameters) \
119 namespace GNULIB_NAMESPACE \
121 rettype (*const func) parameters = ::rpl_func; \
123 _GL_EXTERN_C int _gl_cxxalias_dummy
125 # define _GL_CXXALIAS_RPL_1(func,rpl_func,rettype,parameters) \
126 _GL_EXTERN_C int _gl_cxxalias_dummy
129 /* _GL_CXXALIAS_SYS (func, rettype, parameters);
130 declares a C++ alias called GNULIB_NAMESPACE::func
131 that redirects to the system provided function func, if GNULIB_NAMESPACE
134 _GL_CXXALIAS_SYS (open, int, (const char *filename, int flags, ...));
136 #if defined __cplusplus && defined GNULIB_NAMESPACE
137 /* If we were to write
138 rettype (*const func) parameters = ::func;
139 like above in _GL_CXXALIAS_RPL_1, the compiler could optimize calls
140 better (remove an indirection through a 'static' pointer variable),
141 but then the _GL_CXXALIASWARN macro below would cause a warning not only
142 for uses of ::func but also for uses of GNULIB_NAMESPACE::func. */
143 # define _GL_CXXALIAS_SYS(func,rettype,parameters) \
144 namespace GNULIB_NAMESPACE \
146 static rettype (*func) parameters = ::func; \
148 _GL_EXTERN_C int _gl_cxxalias_dummy
150 # define _GL_CXXALIAS_SYS(func,rettype,parameters) \
151 _GL_EXTERN_C int _gl_cxxalias_dummy
154 /* _GL_CXXALIAS_SYS_CAST (func, rettype, parameters);
155 is like _GL_CXXALIAS_SYS (func, rettype, parameters);
156 except that the C function func may have a slightly different declaration.
157 A cast is used to silence the "invalid conversion" error that would
159 #if defined __cplusplus && defined GNULIB_NAMESPACE
160 # define _GL_CXXALIAS_SYS_CAST(func,rettype,parameters) \
161 namespace GNULIB_NAMESPACE \
163 static rettype (*func) parameters = \
164 reinterpret_cast<rettype(*)parameters>(::func); \
166 _GL_EXTERN_C int _gl_cxxalias_dummy
168 # define _GL_CXXALIAS_SYS_CAST(func,rettype,parameters) \
169 _GL_EXTERN_C int _gl_cxxalias_dummy
172 /* _GL_CXXALIAS_SYS_CAST2 (func, rettype, parameters, rettype2, parameters2);
173 is like _GL_CXXALIAS_SYS (func, rettype, parameters);
174 except that the C function is picked among a set of overloaded functions,
175 namely the one with rettype2 and parameters2. Two consecutive casts
176 are used to silence the "cannot find a match" and "invalid conversion"
177 errors that would otherwise occur. */
178 #if defined __cplusplus && defined GNULIB_NAMESPACE
179 /* The outer cast must be a reinterpret_cast.
180 The inner cast: When the function is defined as a set of overloaded
181 functions, it works as a static_cast<>, choosing the designated variant.
182 When the function is defined as a single variant, it works as a
183 reinterpret_cast<>. The parenthesized cast syntax works both ways. */
184 # define _GL_CXXALIAS_SYS_CAST2(func,rettype,parameters,rettype2,parameters2) \
185 namespace GNULIB_NAMESPACE \
187 static rettype (*func) parameters = \
188 reinterpret_cast<rettype(*)parameters>( \
189 (rettype2(*)parameters2)(::func)); \
191 _GL_EXTERN_C int _gl_cxxalias_dummy
193 # define _GL_CXXALIAS_SYS_CAST2(func,rettype,parameters,rettype2,parameters2) \
194 _GL_EXTERN_C int _gl_cxxalias_dummy
197 /* _GL_CXXALIASWARN (func);
198 causes a warning to be emitted when ::func is used but not when
199 GNULIB_NAMESPACE::func is used. func must be defined without overloaded
201 #if defined __cplusplus && defined GNULIB_NAMESPACE
202 # define _GL_CXXALIASWARN(func) \
203 _GL_CXXALIASWARN_1 (func, GNULIB_NAMESPACE)
204 # define _GL_CXXALIASWARN_1(func,namespace) \
205 _GL_CXXALIASWARN_2 (func, namespace)
206 # define _GL_CXXALIASWARN_2(func,namespace) \
207 _GL_WARN_ON_USE (func, \
208 "The symbol ::" #func " refers to the system function. " \
209 "Use " #namespace "::" #func " instead.")
211 # define _GL_CXXALIASWARN(func) \
212 _GL_EXTERN_C int _gl_cxxalias_dummy
215 /* _GL_CXXALIASWARN1 (func, rettype, parameters_and_attributes);
216 causes a warning to be emitted when the given overloaded variant of ::func
217 is used but not when GNULIB_NAMESPACE::func is used. */
218 #if defined __cplusplus && defined GNULIB_NAMESPACE
219 # define _GL_CXXALIASWARN1(func,rettype,parameters_and_attributes) \
220 # _GL_CXXALIASWARN1_1 (func, rettype, parameters_and_attributes, \
222 # define _GL_CXXALIASWARN1_1(func,rettype,parameters_and_attributes,namespace) \
223 _GL_CXXALIASWARN1_2 (func, rettype, parameters_and_attributes, namespace)
224 # define _GL_CXXALIASWARN1_2(func,rettype,parameters_and_attributes,namespace) \
225 _GL_WARN_ON_USE_CXX (func, rettype, parameters_and_attributes, \
226 "The symbol ::" #func " refers to the system function. " \
227 "Use " #namespace "::" #func " instead.")
229 # define _GL_CXXALIASWARN1(func,rettype,parameters_and_attributes) \
230 _GL_EXTERN_C int _gl_cxxalias_dummy
233 #endif /* _GL_CXXDEFS_H */