Blame lib/c++defs.h

Packit 1ac44c
/* C++ compatible function declaration macros.
Packit 1ac44c
   Copyright (C) 2010-2018 Free Software Foundation, Inc.
Packit 1ac44c
Packit 1ac44c
   This program is free software: you can redistribute it and/or modify it
Packit 1ac44c
   under the terms of the GNU General Public License as published
Packit 1ac44c
   by the Free Software Foundation; either version 3 of the License, or
Packit 1ac44c
   (at your option) any later version.
Packit 1ac44c
Packit 1ac44c
   This program is distributed in the hope that it will be useful,
Packit 1ac44c
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 1ac44c
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 1ac44c
   General Public License for more details.
Packit 1ac44c
Packit 1ac44c
   You should have received a copy of the GNU General Public License
Packit 1ac44c
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 1ac44c
Packit 1ac44c
#ifndef _GL_CXXDEFS_H
Packit 1ac44c
#define _GL_CXXDEFS_H
Packit 1ac44c
Packit 1ac44c
/* Begin/end the GNULIB_NAMESPACE namespace.  */
Packit 1ac44c
#if defined __cplusplus && defined GNULIB_NAMESPACE
Packit 1ac44c
# define _GL_BEGIN_NAMESPACE namespace GNULIB_NAMESPACE {
Packit 1ac44c
# define _GL_END_NAMESPACE }
Packit 1ac44c
#else
Packit 1ac44c
# define _GL_BEGIN_NAMESPACE
Packit 1ac44c
# define _GL_END_NAMESPACE
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
/* The three most frequent use cases of these macros are:
Packit 1ac44c
Packit 1ac44c
   * For providing a substitute for a function that is missing on some
Packit 1ac44c
     platforms, but is declared and works fine on the platforms on which
Packit 1ac44c
     it exists:
Packit 1ac44c
Packit 1ac44c
       #if @GNULIB_FOO@
Packit 1ac44c
       # if !@HAVE_FOO@
Packit 1ac44c
       _GL_FUNCDECL_SYS (foo, ...);
Packit 1ac44c
       # endif
Packit 1ac44c
       _GL_CXXALIAS_SYS (foo, ...);
Packit 1ac44c
       _GL_CXXALIASWARN (foo);
Packit 1ac44c
       #elif defined GNULIB_POSIXCHECK
Packit 1ac44c
       ...
Packit 1ac44c
       #endif
Packit 1ac44c
Packit 1ac44c
   * For providing a replacement for a function that exists on all platforms,
Packit 1ac44c
     but is broken/insufficient and needs to be replaced on some platforms:
Packit 1ac44c
Packit 1ac44c
       #if @GNULIB_FOO@
Packit 1ac44c
       # if @REPLACE_FOO@
Packit 1ac44c
       #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
Packit 1ac44c
       #   undef foo
Packit 1ac44c
       #   define foo rpl_foo
Packit 1ac44c
       #  endif
Packit 1ac44c
       _GL_FUNCDECL_RPL (foo, ...);
Packit 1ac44c
       _GL_CXXALIAS_RPL (foo, ...);
Packit 1ac44c
       # else
Packit 1ac44c
       _GL_CXXALIAS_SYS (foo, ...);
Packit 1ac44c
       # endif
Packit 1ac44c
       _GL_CXXALIASWARN (foo);
Packit 1ac44c
       #elif defined GNULIB_POSIXCHECK
Packit 1ac44c
       ...
Packit 1ac44c
       #endif
Packit 1ac44c
Packit 1ac44c
   * For providing a replacement for a function that exists on some platforms
Packit 1ac44c
     but is broken/insufficient and needs to be replaced on some of them and
Packit 1ac44c
     is additionally either missing or undeclared on some other platforms:
Packit 1ac44c
Packit 1ac44c
       #if @GNULIB_FOO@
Packit 1ac44c
       # if @REPLACE_FOO@
Packit 1ac44c
       #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
Packit 1ac44c
       #   undef foo
Packit 1ac44c
       #   define foo rpl_foo
Packit 1ac44c
       #  endif
Packit 1ac44c
       _GL_FUNCDECL_RPL (foo, ...);
Packit 1ac44c
       _GL_CXXALIAS_RPL (foo, ...);
Packit 1ac44c
       # else
Packit 1ac44c
       #  if !@HAVE_FOO@   or   if !@HAVE_DECL_FOO@
Packit 1ac44c
       _GL_FUNCDECL_SYS (foo, ...);
Packit 1ac44c
       #  endif
Packit 1ac44c
       _GL_CXXALIAS_SYS (foo, ...);
Packit 1ac44c
       # endif
Packit 1ac44c
       _GL_CXXALIASWARN (foo);
Packit 1ac44c
       #elif defined GNULIB_POSIXCHECK
Packit 1ac44c
       ...
Packit 1ac44c
       #endif
Packit 1ac44c
*/
Packit 1ac44c
Packit 1ac44c
/* _GL_EXTERN_C declaration;
Packit 1ac44c
   performs the declaration with C linkage.  */
Packit 1ac44c
#if defined __cplusplus
Packit 1ac44c
# define _GL_EXTERN_C extern "C"
Packit 1ac44c
#else
Packit 1ac44c
# define _GL_EXTERN_C extern
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
/* _GL_FUNCDECL_RPL (func, rettype, parameters_and_attributes);
Packit 1ac44c
   declares a replacement function, named rpl_func, with the given prototype,
Packit 1ac44c
   consisting of return type, parameters, and attributes.
Packit 1ac44c
   Example:
Packit 1ac44c
     _GL_FUNCDECL_RPL (open, int, (const char *filename, int flags, ...)
Packit 1ac44c
                                  _GL_ARG_NONNULL ((1)));
Packit 1ac44c
 */
Packit 1ac44c
#define _GL_FUNCDECL_RPL(func,rettype,parameters_and_attributes) \
Packit 1ac44c
  _GL_FUNCDECL_RPL_1 (rpl_##func, rettype, parameters_and_attributes)
Packit 1ac44c
#define _GL_FUNCDECL_RPL_1(rpl_func,rettype,parameters_and_attributes) \
Packit 1ac44c
  _GL_EXTERN_C rettype rpl_func parameters_and_attributes
Packit 1ac44c
Packit 1ac44c
/* _GL_FUNCDECL_SYS (func, rettype, parameters_and_attributes);
Packit 1ac44c
   declares the system function, named func, with the given prototype,
Packit 1ac44c
   consisting of return type, parameters, and attributes.
Packit 1ac44c
   Example:
Packit 1ac44c
     _GL_FUNCDECL_SYS (open, int, (const char *filename, int flags, ...)
Packit 1ac44c
                                  _GL_ARG_NONNULL ((1)));
Packit 1ac44c
 */
Packit 1ac44c
#define _GL_FUNCDECL_SYS(func,rettype,parameters_and_attributes) \
Packit 1ac44c
  _GL_EXTERN_C rettype func parameters_and_attributes
Packit 1ac44c
Packit 1ac44c
/* _GL_CXXALIAS_RPL (func, rettype, parameters);
Packit 1ac44c
   declares a C++ alias called GNULIB_NAMESPACE::func
Packit 1ac44c
   that redirects to rpl_func, if GNULIB_NAMESPACE is defined.
Packit 1ac44c
   Example:
Packit 1ac44c
     _GL_CXXALIAS_RPL (open, int, (const char *filename, int flags, ...));
Packit 1ac44c
Packit 1ac44c
   Wrapping rpl_func in an object with an inline conversion operator
Packit 1ac44c
   avoids a reference to rpl_func unless GNULIB_NAMESPACE::func is
Packit 1ac44c
   actually used in the program.  */
Packit 1ac44c
#define _GL_CXXALIAS_RPL(func,rettype,parameters) \
Packit 1ac44c
  _GL_CXXALIAS_RPL_1 (func, rpl_##func, rettype, parameters)
Packit 1ac44c
#if defined __cplusplus && defined GNULIB_NAMESPACE
Packit 1ac44c
# define _GL_CXXALIAS_RPL_1(func,rpl_func,rettype,parameters) \
Packit 1ac44c
    namespace GNULIB_NAMESPACE                                \
Packit 1ac44c
    {                                                         \
Packit 1ac44c
      static const struct _gl_ ## func ## _wrapper            \
Packit 1ac44c
      {                                                       \
Packit 1ac44c
        typedef rettype (*type) parameters;                   \
Packit 1ac44c
                                                              \
Packit 1ac44c
        inline operator type () const                         \
Packit 1ac44c
        {                                                     \
Packit 1ac44c
          return ::rpl_func;                                  \
Packit 1ac44c
        }                                                     \
Packit 1ac44c
      } func = {};                                            \
Packit 1ac44c
    }                                                         \
Packit 1ac44c
    _GL_EXTERN_C int _gl_cxxalias_dummy
Packit 1ac44c
#else
Packit 1ac44c
# define _GL_CXXALIAS_RPL_1(func,rpl_func,rettype,parameters) \
Packit 1ac44c
    _GL_EXTERN_C int _gl_cxxalias_dummy
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
/* _GL_CXXALIAS_RPL_CAST_1 (func, rpl_func, rettype, parameters);
Packit 1ac44c
   is like  _GL_CXXALIAS_RPL_1 (func, rpl_func, rettype, parameters);
Packit 1ac44c
   except that the C function rpl_func may have a slightly different
Packit 1ac44c
   declaration.  A cast is used to silence the "invalid conversion" error
Packit 1ac44c
   that would otherwise occur.  */
Packit 1ac44c
#if defined __cplusplus && defined GNULIB_NAMESPACE
Packit 1ac44c
# define _GL_CXXALIAS_RPL_CAST_1(func,rpl_func,rettype,parameters) \
Packit 1ac44c
    namespace GNULIB_NAMESPACE                                     \
Packit 1ac44c
    {                                                              \
Packit 1ac44c
      static const struct _gl_ ## func ## _wrapper                 \
Packit 1ac44c
      {                                                            \
Packit 1ac44c
        typedef rettype (*type) parameters;                        \
Packit 1ac44c
                                                                   \
Packit 1ac44c
        inline operator type () const                              \
Packit 1ac44c
        {                                                          \
Packit 1ac44c
          return reinterpret_cast<type>(::rpl_func);               \
Packit 1ac44c
        }                                                          \
Packit 1ac44c
      } func = {};                                                 \
Packit 1ac44c
    }                                                              \
Packit 1ac44c
    _GL_EXTERN_C int _gl_cxxalias_dummy
Packit 1ac44c
#else
Packit 1ac44c
# define _GL_CXXALIAS_RPL_CAST_1(func,rpl_func,rettype,parameters) \
Packit 1ac44c
    _GL_EXTERN_C int _gl_cxxalias_dummy
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
/* _GL_CXXALIAS_SYS (func, rettype, parameters);
Packit 1ac44c
   declares a C++ alias called GNULIB_NAMESPACE::func
Packit 1ac44c
   that redirects to the system provided function func, if GNULIB_NAMESPACE
Packit 1ac44c
   is defined.
Packit 1ac44c
   Example:
Packit 1ac44c
     _GL_CXXALIAS_SYS (open, int, (const char *filename, int flags, ...));
Packit 1ac44c
Packit 1ac44c
   Wrapping func in an object with an inline conversion operator
Packit 1ac44c
   avoids a reference to func unless GNULIB_NAMESPACE::func is
Packit 1ac44c
   actually used in the program.  */
Packit 1ac44c
#if defined __cplusplus && defined GNULIB_NAMESPACE
Packit 1ac44c
# define _GL_CXXALIAS_SYS(func,rettype,parameters)            \
Packit 1ac44c
    namespace GNULIB_NAMESPACE                                \
Packit 1ac44c
    {                                                         \
Packit 1ac44c
      static const struct _gl_ ## func ## _wrapper            \
Packit 1ac44c
      {                                                       \
Packit 1ac44c
        typedef rettype (*type) parameters;                   \
Packit 1ac44c
                                                              \
Packit 1ac44c
        inline operator type () const                         \
Packit 1ac44c
        {                                                     \
Packit 1ac44c
          return ::func;                                      \
Packit 1ac44c
        }                                                     \
Packit 1ac44c
      } func = {};                                            \
Packit 1ac44c
    }                                                         \
Packit 1ac44c
    _GL_EXTERN_C int _gl_cxxalias_dummy
Packit 1ac44c
#else
Packit 1ac44c
# define _GL_CXXALIAS_SYS(func,rettype,parameters) \
Packit 1ac44c
    _GL_EXTERN_C int _gl_cxxalias_dummy
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
/* _GL_CXXALIAS_SYS_CAST (func, rettype, parameters);
Packit 1ac44c
   is like  _GL_CXXALIAS_SYS (func, rettype, parameters);
Packit 1ac44c
   except that the C function func may have a slightly different declaration.
Packit 1ac44c
   A cast is used to silence the "invalid conversion" error that would
Packit 1ac44c
   otherwise occur.  */
Packit 1ac44c
#if defined __cplusplus && defined GNULIB_NAMESPACE
Packit 1ac44c
# define _GL_CXXALIAS_SYS_CAST(func,rettype,parameters) \
Packit 1ac44c
    namespace GNULIB_NAMESPACE                          \
Packit 1ac44c
    {                                                   \
Packit 1ac44c
      static const struct _gl_ ## func ## _wrapper      \
Packit 1ac44c
      {                                                 \
Packit 1ac44c
        typedef rettype (*type) parameters;             \
Packit 1ac44c
                                                        \
Packit 1ac44c
        inline operator type () const                   \
Packit 1ac44c
        {                                               \
Packit 1ac44c
          return reinterpret_cast<type>(::func);        \
Packit 1ac44c
        }                                               \
Packit 1ac44c
      } func = {};                                      \
Packit 1ac44c
    }                                                   \
Packit 1ac44c
    _GL_EXTERN_C int _gl_cxxalias_dummy
Packit 1ac44c
#else
Packit 1ac44c
# define _GL_CXXALIAS_SYS_CAST(func,rettype,parameters) \
Packit 1ac44c
    _GL_EXTERN_C int _gl_cxxalias_dummy
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
/* _GL_CXXALIAS_SYS_CAST2 (func, rettype, parameters, rettype2, parameters2);
Packit 1ac44c
   is like  _GL_CXXALIAS_SYS (func, rettype, parameters);
Packit 1ac44c
   except that the C function is picked among a set of overloaded functions,
Packit 1ac44c
   namely the one with rettype2 and parameters2.  Two consecutive casts
Packit 1ac44c
   are used to silence the "cannot find a match" and "invalid conversion"
Packit 1ac44c
   errors that would otherwise occur.  */
Packit 1ac44c
#if defined __cplusplus && defined GNULIB_NAMESPACE
Packit 1ac44c
  /* The outer cast must be a reinterpret_cast.
Packit 1ac44c
     The inner cast: When the function is defined as a set of overloaded
Packit 1ac44c
     functions, it works as a static_cast<>, choosing the designated variant.
Packit 1ac44c
     When the function is defined as a single variant, it works as a
Packit 1ac44c
     reinterpret_cast<>. The parenthesized cast syntax works both ways.  */
Packit 1ac44c
# define _GL_CXXALIAS_SYS_CAST2(func,rettype,parameters,rettype2,parameters2) \
Packit 1ac44c
    namespace GNULIB_NAMESPACE                                                \
Packit 1ac44c
    {                                                                         \
Packit 1ac44c
      static const struct _gl_ ## func ## _wrapper                            \
Packit 1ac44c
      {                                                                       \
Packit 1ac44c
        typedef rettype (*type) parameters;                                   \
Packit 1ac44c
                                                                              \
Packit 1ac44c
        inline operator type () const                                         \
Packit 1ac44c
        {                                                                     \
Packit 1ac44c
          return reinterpret_cast<type>((rettype2 (*) parameters2)(::func));  \
Packit 1ac44c
        }                                                                     \
Packit 1ac44c
      } func = {};                                                            \
Packit 1ac44c
    }                                                                         \
Packit 1ac44c
    _GL_EXTERN_C int _gl_cxxalias_dummy
Packit 1ac44c
#else
Packit 1ac44c
# define _GL_CXXALIAS_SYS_CAST2(func,rettype,parameters,rettype2,parameters2) \
Packit 1ac44c
    _GL_EXTERN_C int _gl_cxxalias_dummy
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
/* _GL_CXXALIASWARN (func);
Packit 1ac44c
   causes a warning to be emitted when ::func is used but not when
Packit 1ac44c
   GNULIB_NAMESPACE::func is used.  func must be defined without overloaded
Packit 1ac44c
   variants.  */
Packit 1ac44c
#if defined __cplusplus && defined GNULIB_NAMESPACE
Packit 1ac44c
# define _GL_CXXALIASWARN(func) \
Packit 1ac44c
   _GL_CXXALIASWARN_1 (func, GNULIB_NAMESPACE)
Packit 1ac44c
# define _GL_CXXALIASWARN_1(func,namespace) \
Packit 1ac44c
   _GL_CXXALIASWARN_2 (func, namespace)
Packit 1ac44c
/* To work around GCC bug <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43881>,
Packit 1ac44c
   we enable the warning only when not optimizing.  */
Packit 1ac44c
# if !__OPTIMIZE__
Packit 1ac44c
#  define _GL_CXXALIASWARN_2(func,namespace) \
Packit 1ac44c
    _GL_WARN_ON_USE (func, \
Packit 1ac44c
                     "The symbol ::" #func " refers to the system function. " \
Packit 1ac44c
                     "Use " #namespace "::" #func " instead.")
Packit 1ac44c
# elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
Packit 1ac44c
#  define _GL_CXXALIASWARN_2(func,namespace) \
Packit 1ac44c
     extern __typeof__ (func) func
Packit 1ac44c
# else
Packit 1ac44c
#  define _GL_CXXALIASWARN_2(func,namespace) \
Packit 1ac44c
     _GL_EXTERN_C int _gl_cxxalias_dummy
Packit 1ac44c
# endif
Packit 1ac44c
#else
Packit 1ac44c
# define _GL_CXXALIASWARN(func) \
Packit 1ac44c
    _GL_EXTERN_C int _gl_cxxalias_dummy
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
/* _GL_CXXALIASWARN1 (func, rettype, parameters_and_attributes);
Packit 1ac44c
   causes a warning to be emitted when the given overloaded variant of ::func
Packit 1ac44c
   is used but not when GNULIB_NAMESPACE::func is used.  */
Packit 1ac44c
#if defined __cplusplus && defined GNULIB_NAMESPACE
Packit 1ac44c
# define _GL_CXXALIASWARN1(func,rettype,parameters_and_attributes) \
Packit 1ac44c
   _GL_CXXALIASWARN1_1 (func, rettype, parameters_and_attributes, \
Packit 1ac44c
                        GNULIB_NAMESPACE)
Packit 1ac44c
# define _GL_CXXALIASWARN1_1(func,rettype,parameters_and_attributes,namespace) \
Packit 1ac44c
   _GL_CXXALIASWARN1_2 (func, rettype, parameters_and_attributes, namespace)
Packit 1ac44c
/* To work around GCC bug <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43881>,
Packit 1ac44c
   we enable the warning only when not optimizing.  */
Packit 1ac44c
# if !__OPTIMIZE__
Packit 1ac44c
#  define _GL_CXXALIASWARN1_2(func,rettype,parameters_and_attributes,namespace) \
Packit 1ac44c
    _GL_WARN_ON_USE_CXX (func, rettype, parameters_and_attributes, \
Packit 1ac44c
                         "The symbol ::" #func " refers to the system function. " \
Packit 1ac44c
                         "Use " #namespace "::" #func " instead.")
Packit 1ac44c
# elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
Packit 1ac44c
#  define _GL_CXXALIASWARN1_2(func,rettype,parameters_and_attributes,namespace) \
Packit 1ac44c
     extern __typeof__ (func) func
Packit 1ac44c
# else
Packit 1ac44c
#  define _GL_CXXALIASWARN1_2(func,rettype,parameters_and_attributes,namespace) \
Packit 1ac44c
     _GL_EXTERN_C int _gl_cxxalias_dummy
Packit 1ac44c
# endif
Packit 1ac44c
#else
Packit 1ac44c
# define _GL_CXXALIASWARN1(func,rettype,parameters_and_attributes) \
Packit 1ac44c
    _GL_EXTERN_C int _gl_cxxalias_dummy
Packit 1ac44c
#endif
Packit 1ac44c
Packit 1ac44c
#endif /* _GL_CXXDEFS_H */