Back to index

nagios-plugins  1.4.16
c++defs.h
Go to the documentation of this file.
00001 /* C++ compatible function declaration macros.
00002    Copyright (C) 2010 Free Software Foundation, Inc.
00003 
00004    This program is free software: you can redistribute it and/or modify it
00005    under the terms of the GNU General Public License as published
00006    by the Free Software Foundation; either version 3 of the License, or
00007    (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00016 
00017 #ifndef _GL_CXXDEFS_H
00018 #define _GL_CXXDEFS_H
00019 
00020 /* The three most frequent use cases of these macros are:
00021 
00022    * For providing a substitute for a function that is missing on some
00023      platforms, but is declared and works fine on the platforms on which
00024      it exists:
00025 
00026        #if @GNULIB_FOO@
00027        # if !@HAVE_FOO@
00028        _GL_FUNCDECL_SYS (foo, ...);
00029        # endif
00030        _GL_CXXALIAS_SYS (foo, ...);
00031        _GL_CXXALIASWARN (foo);
00032        #elif defined GNULIB_POSIXCHECK
00033        ...
00034        #endif
00035 
00036    * For providing a replacement for a function that exists on all platforms,
00037      but is broken/insufficient and needs to be replaced on some platforms:
00038 
00039        #if @GNULIB_FOO@
00040        # if @REPLACE_FOO@
00041        #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
00042        #   undef foo
00043        #   define foo rpl_foo
00044        #  endif
00045        _GL_FUNCDECL_RPL (foo, ...);
00046        _GL_CXXALIAS_RPL (foo, ...);
00047        # else
00048        _GL_CXXALIAS_SYS (foo, ...);
00049        # endif
00050        _GL_CXXALIASWARN (foo);
00051        #elif defined GNULIB_POSIXCHECK
00052        ...
00053        #endif
00054 
00055    * For providing a replacement for a function that exists on some platforms
00056      but is broken/insufficient and needs to be replaced on some of them and
00057      is additionally either missing or undeclared on some other platforms:
00058 
00059        #if @GNULIB_FOO@
00060        # if @REPLACE_FOO@
00061        #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
00062        #   undef foo
00063        #   define foo rpl_foo
00064        #  endif
00065        _GL_FUNCDECL_RPL (foo, ...);
00066        _GL_CXXALIAS_RPL (foo, ...);
00067        # else
00068        #  if !@HAVE_FOO@   or   if !@HAVE_DECL_FOO@
00069        _GL_FUNCDECL_SYS (foo, ...);
00070        #  endif
00071        _GL_CXXALIAS_SYS (foo, ...);
00072        # endif
00073        _GL_CXXALIASWARN (foo);
00074        #elif defined GNULIB_POSIXCHECK
00075        ...
00076        #endif
00077 */
00078 
00079 /* _GL_EXTERN_C declaration;
00080    performs the declaration with C linkage.  */
00081 #if defined __cplusplus
00082 # define _GL_EXTERN_C extern "C"
00083 #else
00084 # define _GL_EXTERN_C extern
00085 #endif
00086 
00087 /* _GL_FUNCDECL_RPL (func, rettype, parameters_and_attributes);
00088    declares a replacement function, named rpl_func, with the given prototype,
00089    consisting of return type, parameters, and attributes.
00090    Example:
00091      _GL_FUNCDECL_RPL (open, int, (const char *filename, int flags, ...)
00092                                   _GL_ARG_NONNULL ((1)));
00093  */
00094 #define _GL_FUNCDECL_RPL(func,rettype,parameters_and_attributes) \
00095   _GL_FUNCDECL_RPL_1 (rpl_##func, rettype, parameters_and_attributes)
00096 #define _GL_FUNCDECL_RPL_1(rpl_func,rettype,parameters_and_attributes) \
00097   _GL_EXTERN_C rettype rpl_func parameters_and_attributes
00098 
00099 /* _GL_FUNCDECL_SYS (func, rettype, parameters_and_attributes);
00100    declares the system function, named func, with the given prototype,
00101    consisting of return type, parameters, and attributes.
00102    Example:
00103      _GL_FUNCDECL_SYS (open, int, (const char *filename, int flags, ...)
00104                                   _GL_ARG_NONNULL ((1)));
00105  */
00106 #define _GL_FUNCDECL_SYS(func,rettype,parameters_and_attributes) \
00107   _GL_EXTERN_C rettype func parameters_and_attributes
00108 
00109 /* _GL_CXXALIAS_RPL (func, rettype, parameters);
00110    declares a C++ alias called GNULIB_NAMESPACE::func
00111    that redirects to rpl_func, if GNULIB_NAMESPACE is defined.
00112    Example:
00113      _GL_CXXALIAS_RPL (open, int, (const char *filename, int flags, ...));
00114  */
00115 #define _GL_CXXALIAS_RPL(func,rettype,parameters) \
00116   _GL_CXXALIAS_RPL_1 (func, rpl_##func, rettype, parameters)
00117 #if defined __cplusplus && defined GNULIB_NAMESPACE
00118 # define _GL_CXXALIAS_RPL_1(func,rpl_func,rettype,parameters) \
00119     namespace GNULIB_NAMESPACE                                \
00120     {                                                         \
00121       rettype (*const func) parameters = ::rpl_func;          \
00122     }                                                         \
00123     _GL_EXTERN_C int _gl_cxxalias_dummy
00124 #else
00125 # define _GL_CXXALIAS_RPL_1(func,rpl_func,rettype,parameters) \
00126     _GL_EXTERN_C int _gl_cxxalias_dummy
00127 #endif
00128 
00129 /* _GL_CXXALIAS_RPL_CAST_1 (func, rpl_func, rettype, parameters);
00130    is like  _GL_CXXALIAS_RPL_1 (func, rpl_func, rettype, parameters);
00131    except that the C function rpl_func may have a slightly different
00132    declaration.  A cast is used to silence the "invalid conversion" error
00133    that would otherwise occur.  */
00134 #if defined __cplusplus && defined GNULIB_NAMESPACE
00135 # define _GL_CXXALIAS_RPL_CAST_1(func,rpl_func,rettype,parameters) \
00136     namespace GNULIB_NAMESPACE                                     \
00137     {                                                              \
00138       rettype (*const func) parameters =                           \
00139         reinterpret_cast<rettype(*)parameters>(::rpl_func);        \
00140     }                                                              \
00141     _GL_EXTERN_C int _gl_cxxalias_dummy
00142 #else
00143 # define _GL_CXXALIAS_RPL_CAST_1(func,rpl_func,rettype,parameters) \
00144     _GL_EXTERN_C int _gl_cxxalias_dummy
00145 #endif
00146 
00147 /* _GL_CXXALIAS_SYS (func, rettype, parameters);
00148    declares a C++ alias called GNULIB_NAMESPACE::func
00149    that redirects to the system provided function func, if GNULIB_NAMESPACE
00150    is defined.
00151    Example:
00152      _GL_CXXALIAS_SYS (open, int, (const char *filename, int flags, ...));
00153  */
00154 #if defined __cplusplus && defined GNULIB_NAMESPACE
00155   /* If we were to write
00156        rettype (*const func) parameters = ::func;
00157      like above in _GL_CXXALIAS_RPL_1, the compiler could optimize calls
00158      better (remove an indirection through a 'static' pointer variable),
00159      but then the _GL_CXXALIASWARN macro below would cause a warning not only
00160      for uses of ::func but also for uses of GNULIB_NAMESPACE::func.  */
00161 # define _GL_CXXALIAS_SYS(func,rettype,parameters) \
00162     namespace GNULIB_NAMESPACE                     \
00163     {                                              \
00164       static rettype (*func) parameters = ::func;  \
00165     }                                              \
00166     _GL_EXTERN_C int _gl_cxxalias_dummy
00167 #else
00168 # define _GL_CXXALIAS_SYS(func,rettype,parameters) \
00169     _GL_EXTERN_C int _gl_cxxalias_dummy
00170 #endif
00171 
00172 /* _GL_CXXALIAS_SYS_CAST (func, rettype, parameters);
00173    is like  _GL_CXXALIAS_SYS (func, rettype, parameters);
00174    except that the C function func may have a slightly different declaration.
00175    A cast is used to silence the "invalid conversion" error that would
00176    otherwise occur.  */
00177 #if defined __cplusplus && defined GNULIB_NAMESPACE
00178 # define _GL_CXXALIAS_SYS_CAST(func,rettype,parameters) \
00179     namespace GNULIB_NAMESPACE                          \
00180     {                                                   \
00181       static rettype (*func) parameters =               \
00182         reinterpret_cast<rettype(*)parameters>(::func); \
00183     }                                                   \
00184     _GL_EXTERN_C int _gl_cxxalias_dummy
00185 #else
00186 # define _GL_CXXALIAS_SYS_CAST(func,rettype,parameters) \
00187     _GL_EXTERN_C int _gl_cxxalias_dummy
00188 #endif
00189 
00190 /* _GL_CXXALIAS_SYS_CAST2 (func, rettype, parameters, rettype2, parameters2);
00191    is like  _GL_CXXALIAS_SYS (func, rettype, parameters);
00192    except that the C function is picked among a set of overloaded functions,
00193    namely the one with rettype2 and parameters2.  Two consecutive casts
00194    are used to silence the "cannot find a match" and "invalid conversion"
00195    errors that would otherwise occur.  */
00196 #if defined __cplusplus && defined GNULIB_NAMESPACE
00197   /* The outer cast must be a reinterpret_cast.
00198      The inner cast: When the function is defined as a set of overloaded
00199      functions, it works as a static_cast<>, choosing the designated variant.
00200      When the function is defined as a single variant, it works as a
00201      reinterpret_cast<>. The parenthesized cast syntax works both ways.  */
00202 # define _GL_CXXALIAS_SYS_CAST2(func,rettype,parameters,rettype2,parameters2) \
00203     namespace GNULIB_NAMESPACE                                                \
00204     {                                                                         \
00205       static rettype (*func) parameters =                                     \
00206         reinterpret_cast<rettype(*)parameters>(                               \
00207           (rettype2(*)parameters2)(::func));                                  \
00208     }                                                                         \
00209     _GL_EXTERN_C int _gl_cxxalias_dummy
00210 #else
00211 # define _GL_CXXALIAS_SYS_CAST2(func,rettype,parameters,rettype2,parameters2) \
00212     _GL_EXTERN_C int _gl_cxxalias_dummy
00213 #endif
00214 
00215 /* _GL_CXXALIASWARN (func);
00216    causes a warning to be emitted when ::func is used but not when
00217    GNULIB_NAMESPACE::func is used.  func must be defined without overloaded
00218    variants.  */
00219 #if defined __cplusplus && defined GNULIB_NAMESPACE
00220 # define _GL_CXXALIASWARN(func) \
00221    _GL_CXXALIASWARN_1 (func, GNULIB_NAMESPACE)
00222 # define _GL_CXXALIASWARN_1(func,namespace) \
00223    _GL_CXXALIASWARN_2 (func, namespace)
00224 /* To work around GCC bug <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43881>,
00225    we enable the warning only when not optimizing.  */
00226 # if !__OPTIMIZE__
00227 #  define _GL_CXXALIASWARN_2(func,namespace) \
00228     _GL_WARN_ON_USE (func, \
00229                      "The symbol ::" #func " refers to the system function. " \
00230                      "Use " #namespace "::" #func " instead.")
00231 # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
00232 #  define _GL_CXXALIASWARN_2(func,namespace) \
00233      extern __typeof__ (func) func
00234 # else
00235 #  define _GL_CXXALIASWARN_2(func,namespace) \
00236      _GL_EXTERN_C int _gl_cxxalias_dummy
00237 # endif
00238 #else
00239 # define _GL_CXXALIASWARN(func) \
00240     _GL_EXTERN_C int _gl_cxxalias_dummy
00241 #endif
00242 
00243 /* _GL_CXXALIASWARN1 (func, rettype, parameters_and_attributes);
00244    causes a warning to be emitted when the given overloaded variant of ::func
00245    is used but not when GNULIB_NAMESPACE::func is used.  */
00246 #if defined __cplusplus && defined GNULIB_NAMESPACE
00247 # define _GL_CXXALIASWARN1(func,rettype,parameters_and_attributes) \
00248    _GL_CXXALIASWARN1_1 (func, rettype, parameters_and_attributes, \
00249                         GNULIB_NAMESPACE)
00250 # define _GL_CXXALIASWARN1_1(func,rettype,parameters_and_attributes,namespace) \
00251    _GL_CXXALIASWARN1_2 (func, rettype, parameters_and_attributes, namespace)
00252 /* To work around GCC bug <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43881>,
00253    we enable the warning only when not optimizing.  */
00254 # if !__OPTIMIZE__
00255 #  define _GL_CXXALIASWARN1_2(func,rettype,parameters_and_attributes,namespace) \
00256     _GL_WARN_ON_USE_CXX (func, rettype, parameters_and_attributes, \
00257                          "The symbol ::" #func " refers to the system function. " \
00258                          "Use " #namespace "::" #func " instead.")
00259 # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
00260 #  define _GL_CXXALIASWARN1_2(func,rettype,parameters_and_attributes,namespace) \
00261      extern __typeof__ (func) func
00262 # else
00263 #  define _GL_CXXALIASWARN1_2(func,rettype,parameters_and_attributes,namespace) \
00264      _GL_EXTERN_C int _gl_cxxalias_dummy
00265 # endif
00266 #else
00267 # define _GL_CXXALIASWARN1(func,rettype,parameters_and_attributes) \
00268     _GL_EXTERN_C int _gl_cxxalias_dummy
00269 #endif
00270 
00271 #endif /* _GL_CXXDEFS_H */