Blame src/gl/c++defs.h

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