From 7e4c6ad74d4fcd68b9bece5c8379ba8e6807378d Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 8 Mar 2010 01:16:01 +0100 Subject: [PATCH] search: Avoid #define replacements in C++ mode. --- ChangeLog | 8 +++ lib/search.in.h | 126 +++++++++++++++++++++++++++++++++++++----------- modules/search | 4 +- 3 files changed, 108 insertions(+), 30 deletions(-) diff --git a/ChangeLog b/ChangeLog index b461cf089f..e48c85ae69 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2010-03-07 Bruno Haible + search: Avoid #define replacements in C++ mode. + * lib/search.in.h: Include c++defs.h. + (_gl_search_compar_fn, _gl_search_action_fn): New types. + (tsearch, tfind, tdelete, twalk): In C++, define a namespaced alias + symbol. + * modules/search (Depends-on): Add c++defs. + (Makefile.am): Update search.h rule. + math: Avoid #define replacements in C++ mode. * lib/math.in.h: Include c++defs.h. (frexp, acosl, asinl, atanl, ceilf, ceill, cosl, expl, floorf, floorl, diff --git a/lib/search.in.h b/lib/search.in.h index cec7ebe90c..d61f9e4cb6 100644 --- a/lib/search.in.h +++ b/lib/search.in.h @@ -29,30 +29,28 @@ #define _GL_SEARCH_H +/* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */ + /* The definition of _GL_ARG_NONNULL is copied here. */ /* The definition of _GL_WARN_ON_USE is copied here. */ -#ifdef __cplusplus -extern "C" { -#endif - - #if @GNULIB_TSEARCH@ # if @REPLACE_TSEARCH@ -# define tsearch rpl_tsearch -# define tfind rpl_tfind -# define tdelete rpl_tdelete -# define twalk rpl_twalk +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# define tsearch rpl_tsearch +# define tfind rpl_tfind +# define tdelete rpl_tdelete +# define twalk rpl_twalk +# endif # endif -# if !@HAVE_TSEARCH@ || @REPLACE_TSEARCH@ /* See , for details. */ -# if !@HAVE_TSEARCH@ +# if !@HAVE_TSEARCH@ typedef enum { preorder, @@ -61,27 +59,89 @@ typedef enum leaf } VISIT; -# endif +# endif + +# ifdef __cplusplus +extern "C" { +# endif +typedef int (*_gl_search_compar_fn) (const void *, const void *); +typedef void (*_gl_search_action_fn) (const void *, VISIT, int); +# ifdef __cplusplus +} +# endif /* Searches an element in the tree *VROOTP that compares equal to KEY. If one is found, it is returned. Otherwise, a new element equal to KEY is inserted in the tree and is returned. */ -extern void * tsearch (const void *key, void **vrootp, - int (*compar) (const void *, const void *)) - _GL_ARG_NONNULL ((1, 2, 3)); +# if @REPLACE_TSEARCH@ +_GL_FUNCDECL_RPL (tsearch, void *, + (const void *key, void **vrootp, + _gl_search_compar_fn compar) + _GL_ARG_NONNULL ((1, 2, 3))); +_GL_CXXALIAS_RPL (tsearch, void *, + (const void *key, void **vrootp, + _gl_search_compar_fn compar)); +# else +# if !@HAVE_TSEARCH@ +_GL_FUNCDECL_SYS (tsearch, void *, + (const void *key, void **vrootp, + _gl_search_compar_fn compar) + _GL_ARG_NONNULL ((1, 2, 3))); +# endif +_GL_CXXALIAS_SYS (tsearch, void *, + (const void *key, void **vrootp, + _gl_search_compar_fn compar)); +# endif +_GL_CXXALIASWARN (tsearch); /* Searches an element in the tree *VROOTP that compares equal to KEY. If one is found, it is returned. Otherwise, NULL is returned. */ -extern void * tfind (const void *key, void *const *vrootp, - int (*compar) (const void *, const void *)) - _GL_ARG_NONNULL ((1, 2, 3)); +# if @REPLACE_TSEARCH@ +_GL_FUNCDECL_RPL (tfind, void *, + (const void *key, void *const *vrootp, + _gl_search_compar_fn compar) + _GL_ARG_NONNULL ((1, 2, 3))); +_GL_CXXALIAS_RPL (tfind, void *, + (const void *key, void *const *vrootp, + _gl_search_compar_fn compar)); +# else +# if !@HAVE_TSEARCH@ +_GL_FUNCDECL_SYS (tfind, void *, + (const void *key, void *const *vrootp, + _gl_search_compar_fn compar) + _GL_ARG_NONNULL ((1, 2, 3))); +# endif +/* Need to cast, because on Cygwin 1.5.x systems, the second parameter is + void **vrootp. */ +_GL_CXXALIAS_SYS_CAST (tfind, void *, + (const void *key, void *const *vrootp, + _gl_search_compar_fn compar)); +# endif +_GL_CXXALIASWARN (tfind); /* Searches an element in the tree *VROOTP that compares equal to KEY. If one is found, it is removed from the tree, and its parent node is returned. Otherwise, NULL is returned. */ -extern void * tdelete (const void *key, void **vrootp, - int (*compar) (const void *, const void *)) - _GL_ARG_NONNULL ((1, 2, 3)); +# if @REPLACE_TSEARCH@ +_GL_FUNCDECL_RPL (tdelete, void *, + (const void *key, void **vrootp, + _gl_search_compar_fn compar) + _GL_ARG_NONNULL ((1, 2, 3))); +_GL_CXXALIAS_RPL (tdelete, void *, + (const void *key, void **vrootp, + _gl_search_compar_fn compar)); +# else +# if !@HAVE_TSEARCH@ +_GL_FUNCDECL_SYS (tdelete, void *, + (const void *key, void **vrootp, + _gl_search_compar_fn compar) + _GL_ARG_NONNULL ((1, 2, 3))); +# endif +_GL_CXXALIAS_SYS (tdelete, void *, + (const void *key, void **vrootp, + _gl_search_compar_fn compar)); +# endif +_GL_CXXALIASWARN (tdelete); /* Perform a depth-first, left-to-right traversal of the tree VROOT. The ACTION function is called: @@ -94,11 +154,23 @@ extern void * tdelete (const void *key, void **vrootp, pointer to the key, 2. an indicator which visit of the node this is, 3. the level of the node in the tree (0 for the root). */ -extern void twalk (const void *vroot, - void (*action) (const void *, VISIT, int)) - _GL_ARG_NONNULL ((2)); - +# if @REPLACE_TSEARCH@ +_GL_FUNCDECL_RPL (twalk, void, + (const void *vroot, _gl_search_action_fn action) + _GL_ARG_NONNULL ((2))); +_GL_CXXALIAS_RPL (twalk, void, + (const void *vroot, _gl_search_action_fn action)); +# else +# if !@HAVE_TSEARCH@ +_GL_FUNCDECL_SYS (twalk, void, + (const void *vroot, _gl_search_action_fn action) + _GL_ARG_NONNULL ((2))); +# endif +_GL_CXXALIAS_SYS (twalk, void, + (const void *vroot, _gl_search_action_fn action)); # endif +_GL_CXXALIASWARN (twalk); + #elif defined GNULIB_POSIXCHECK # undef tsearch # if HAVE_RAW_DECL_TSEARCH @@ -123,9 +195,5 @@ _GL_WARN_ON_USE (twalk, "twalk is unportable - " #endif -#ifdef __cplusplus -} -#endif - #endif /* _GL_SEARCH_H */ #endif /* _GL_SEARCH_H */ diff --git a/modules/search b/modules/search index 898decefb6..dce574756a 100644 --- a/modules/search +++ b/modules/search @@ -7,6 +7,7 @@ m4/search_h.m4 Depends-on: arg-nonnull +c++defs include_next warn-on-use @@ -18,7 +19,7 @@ BUILT_SOURCES += search.h # We need the following in order to create when the system # doesn't have one that works with the given compiler. -search.h: search.in.h $(WARN_ON_USE_H) $(ARG_NONNULL_H) +search.h: search.in.h $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H) $(AM_V_GEN)rm -f $@-t $@ && \ { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \ sed -e 's|@''HAVE_SEARCH_H''@|$(HAVE_SEARCH_H)|g' \ @@ -28,6 +29,7 @@ search.h: search.in.h $(WARN_ON_USE_H) $(ARG_NONNULL_H) -e 's|@''GNULIB_TSEARCH''@|$(GNULIB_TSEARCH)|g' \ -e 's|@''HAVE_TSEARCH''@|$(HAVE_TSEARCH)|g' \ -e 's|@''REPLACE_TSEARCH''@|$(REPLACE_TSEARCH)|g' \ + -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \ -e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \ -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \ < $(srcdir)/search.in.h; \ -- 2.30.2