texinfo-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

branch master updated: Add C++ code to have an hashmap when Perl is not


From: Patrice Dumas
Subject: branch master updated: Add C++ code to have an hashmap when Perl is not used
Date: Sat, 05 Oct 2024 03:47:31 -0400

This is an automated email from the git hooks/post-receive script.

pertusus pushed a commit to branch master
in repository texinfo.

The following commit(s) were added to refs/heads/master by this push:
     new a45bddf685 Add C++ code to have an hashmap when Perl is not used
a45bddf685 is described below

commit a45bddf685edda86a09ec022b1c9dc47165731bc
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Fri Aug 23 08:43:08 2024 +0200

    Add C++ code to have an hashmap when Perl is not used
    
    * tp/Texinfo/XS/configure.ac (AC_PROG_CXX, HAVE_CXX_HASHMAP): call
    AC_PROG_CXX to find a C++ compiler, test that a code with an
    unordered map compiles, and set HAVE_CXX_HASHMAP if so.
    
    * tp/Texinfo/XS/main/text.c (xvasprintf): use format instead of
    template, template seems to be reserved when compiled as C++, even in
    extern "C".
    
    * tp/Texinfo/XS/Makefile.am (libtexinfo_convert_la_SOURCES)
    (EXTRA_DIST), tp/Texinfo/XS/convert/call_html_cxx_function.cpp
    (init_registered_ids_hashmap, is_hashmap_registered_id)
    (hashmap_register_id, clear_registered_ids_hashmap)
    (free_registered_ids_hashmap), tp/Texinfo/XS/main/converter_types.h
    (enum ids_data_type, CONVERTER): add an implementation of the
    registered id interface using an unordered_map in
    call_html_cxx_function.cpp.  Add converter registered_ids_hashmap
    field and associated ids_data_type.
    
    * tp/Texinfo/XS/convert/converter.c (new_converter),
    tp/Texinfo/XS/convert/html_converter_finish.c (html_reset_converter)
    (html_free_converter), tp/Texinfo/XS/convert/html_prepare_converter.c
    (html_converter_customize, html_id_is_registered, html_register_id):
    call C++ hashmap interface functions if ids_data_type is
    IDT_cxx_hashmap.
    
    * tp/Texinfo/XS/convert/converter.c (converter_converter): set
    CONVF_cxx_hashmap.
---
 ChangeLog                                        | 32 ++++++++++
 tp/Texinfo/XS/Makefile.am                        |  3 +-
 tp/Texinfo/XS/configure.ac                       | 30 ++++++++++
 tp/Texinfo/XS/convert/call_html_cxx_function.cpp | 75 ++++++++++++++++++++++++
 tp/Texinfo/XS/convert/call_html_cxx_function.h   | 34 +++++++++++
 tp/Texinfo/XS/convert/converter.c                | 10 ++++
 tp/Texinfo/XS/convert/html_converter_finish.c    |  9 +++
 tp/Texinfo/XS/convert/html_prepare_converter.c   | 13 ++++
 tp/Texinfo/XS/main/converter_types.h             |  6 ++
 tp/Texinfo/XS/main/text.c                        |  4 +-
 tp/Texinfo/XS/main/text.h                        |  2 +-
 11 files changed, 214 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 2b83aaac7f..b5857bef81 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,35 @@
+2024-08-23  Patrice Dumas  <pertusus@free.fr>
+
+       Add C++ code to have an hashmap when Perl is not used
+
+       * tp/Texinfo/XS/configure.ac (AC_PROG_CXX, HAVE_CXX_HASHMAP): call
+       AC_PROG_CXX to find a C++ compiler, test that a code with an
+       unordered map compiles, and set HAVE_CXX_HASHMAP if so.
+
+       * tp/Texinfo/XS/main/text.c (xvasprintf): use format instead of
+       template, template seems to be reserved when compiled as C++, even in
+       extern "C".
+
+       * tp/Texinfo/XS/Makefile.am (libtexinfo_convert_la_SOURCES)
+       (EXTRA_DIST), tp/Texinfo/XS/convert/call_html_cxx_function.cpp
+       (init_registered_ids_hashmap, is_hashmap_registered_id)
+       (hashmap_register_id, clear_registered_ids_hashmap)
+       (free_registered_ids_hashmap), tp/Texinfo/XS/main/converter_types.h
+       (enum ids_data_type, CONVERTER): add an implementation of the
+       registered id interface using an unordered_map in
+       call_html_cxx_function.cpp.  Add converter registered_ids_hashmap
+       field and associated ids_data_type.
+
+       * tp/Texinfo/XS/convert/converter.c (new_converter),
+       tp/Texinfo/XS/convert/html_converter_finish.c (html_reset_converter)
+       (html_free_converter), tp/Texinfo/XS/convert/html_prepare_converter.c
+       (html_converter_customize, html_id_is_registered, html_register_id):
+       call C++ hashmap interface functions if ids_data_type is
+       IDT_cxx_hashmap.
+
+       * tp/Texinfo/XS/convert/converter.c (converter_converter): set
+       CONVF_cxx_hashmap.
+
 2024-08-22  Patrice Dumas  <pertusus@free.fr>
 
        * tp/Texinfo/XS/convert/call_html_perl_function.c
diff --git a/tp/Texinfo/XS/Makefile.am b/tp/Texinfo/XS/Makefile.am
index abeb53e5e5..b3437369d3 100644
--- a/tp/Texinfo/XS/Makefile.am
+++ b/tp/Texinfo/XS/Makefile.am
@@ -411,7 +411,8 @@ C_libtexinfo_convert_sources = \
                        convert/html_converter_api.c \
                        convert/html_converter_api.h \
                        convert/texinfo.c \
-                       convert/texinfo.h
+                       convert/texinfo.h \
+                       convert/call_html_cxx_function.h
 
 XS_libtexinfo_convert_sources = \
                        convert/build_html_perl_state.h \
diff --git a/tp/Texinfo/XS/configure.ac b/tp/Texinfo/XS/configure.ac
index 395159f407..d990781335 100644
--- a/tp/Texinfo/XS/configure.ac
+++ b/tp/Texinfo/XS/configure.ac
@@ -142,6 +142,8 @@ AC_SUBST([XSUBPPARGS], [$XSUBPPARGS])
 
 AC_PROG_CC
 
+AC_PROG_CXX
+
 gl_EARLY
 
 # i18n support.  We need to use the same GNU gettext version as the main
@@ -168,6 +170,34 @@ LT_INIT([disable-static])
 # Note that the above have to be outside the disable_xs block
 # otherwise it causes an error with configure.
 
+AC_LANG_PUSH([C++])
+
+AC_CACHE_CHECK([if C++ hashmap can be used], [texinfo_cv_cxx_hashmap],
+[
+texinfo_cv_cxx_hashmap=no
+
+AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
+#include <unordered_map>
+
+void
+init_registered_ids_hashmap (void)
+{
+  std::unordered_map<std::string, int> registered_ids_hashmap;
+  registered_ids_hashmap["toto"] = 1;
+}
+]])], [texinfo_cv_cxx_hashmap=yes])
+])
+
+if test $texinfo_cv_cxx_hashmap = yes; then
+  AC_DEFINE([HAVE_CXX_HASHMAP], [1], [Define if C++ hashmap can be used])
+fi
+
+AM_CONDITIONAL([HAVE_CXX_HASHMAP],
+               [test "x$texinfo_cv_cxx_hashmap" = "xyes"])
+
+AC_LANG_POP([C++])
+
+
 # Disable gnulib provision of "Microsoft deprecated alias" functions on
 # MS-Windows to prevent a clash with definitions in Perl header files
 GL_GNULIB_MDA_FDOPEN=0
diff --git a/tp/Texinfo/XS/convert/call_html_cxx_function.cpp 
b/tp/Texinfo/XS/convert/call_html_cxx_function.cpp
new file mode 100644
index 0000000000..870265abbf
--- /dev/null
+++ b/tp/Texinfo/XS/convert/call_html_cxx_function.cpp
@@ -0,0 +1,75 @@
+/* Copyright 2023-2024 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include <unordered_map>
+#include <iostream>
+
+extern "C"
+{
+#include "converter_types.h"
+}
+
+#include "call_html_cxx_function.h"
+
+// Using const char * did not work, there were spurious matches, so we use
+// a std::string.
+//typedef std::unordered_map<const char *, int> CXX_HASHMAP;
+typedef std::unordered_map<std::string, int> CXX_HASHMAP;
+
+void
+init_registered_ids_hashmap (CONVERTER *self)
+{
+  self->registered_ids_hashmap = new (CXX_HASHMAP);
+}
+
+int
+is_hashmap_registered_id (CONVERTER *self, const char *in_string)
+{
+  CXX_HASHMAP *registered_id = (CXX_HASHMAP *)self->registered_ids_hashmap;
+  std::string string (in_string);
+  int result = registered_id->count(string);
+  return result;
+}
+
+void
+hashmap_register_id (CONVERTER *self, const char *in_string)
+{
+  CXX_HASHMAP *registered_id = (CXX_HASHMAP *)self->registered_ids_hashmap;
+  std::string string (in_string);
+  (*registered_id)[string] = 1;
+}
+
+void
+clear_registered_ids_hashmap (CONVERTER *self)
+{
+  if (self->registered_ids_hashmap)
+    {
+      CXX_HASHMAP *registered_id = (CXX_HASHMAP *)self->registered_ids_hashmap;
+      registered_id->clear ();
+    }
+}
+
+void
+free_registered_ids_hashmap (CONVERTER *self)
+{
+  if (self->registered_ids_hashmap)
+    {
+      CXX_HASHMAP *registered_id = (CXX_HASHMAP *)self->registered_ids_hashmap;
+      delete (registered_id);
+    }
+}
+
diff --git a/tp/Texinfo/XS/convert/call_html_cxx_function.h 
b/tp/Texinfo/XS/convert/call_html_cxx_function.h
new file mode 100644
index 0000000000..eeb00b641c
--- /dev/null
+++ b/tp/Texinfo/XS/convert/call_html_cxx_function.h
@@ -0,0 +1,34 @@
+/* call_html_cxx_function.h - declarations for call_html_cxx_function.cpp */
+#ifndef CALL_HTML_CXX_FUNCTION_H
+#define CALL_HTML_CXX_FUNCTION_H
+
+/* Copyright 2023-2024 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void init_registered_ids_hashmap (CONVERTER *self);
+int is_hashmap_registered_id (CONVERTER *self, const char *in_string);
+void hashmap_register_id (CONVERTER *self, const char *in_string);
+void clear_registered_ids_hashmap (CONVERTER *self);
+void free_registered_ids_hashmap (CONVERTER *self);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/tp/Texinfo/XS/convert/converter.c 
b/tp/Texinfo/XS/convert/converter.c
index 0d515b1c30..302cb2b8c6 100644
--- a/tp/Texinfo/XS/convert/converter.c
+++ b/tp/Texinfo/XS/convert/converter.c
@@ -280,6 +280,10 @@ new_converter (enum converter_format format, unsigned long 
flags)
   /* set low level data representations options */
   if (flags & CONVF_string_list)
     converter->ids_data_type = IDT_string_list;
+#ifdef HAVE_CXX_HASHMAP
+  else if (flags & CONVF_cxx_hashmap)
+    converter->ids_data_type = IDT_cxx_hashmap;
+#endif
   else
     converter->ids_data_type = IDT_perl_hashmap;
 
@@ -478,7 +482,13 @@ converter_converter (enum converter_format format,
                      const CONVERTER_INITIALIZATION_INFO *input_user_conf)
 {
   CONVERTER_INITIALIZATION_INFO *format_defaults;
+  /* NOTE if HAVE_CXX_HASHMAP is not defined, even with CONVF_cxx_hashmap
+     string lists will be used */
+  size_t converter_descriptor = new_converter (format, CONVF_cxx_hashmap);
+   /*
+   To use a string list.  Slower.
   size_t converter_descriptor = new_converter (format, CONVF_string_list);
+    */
   CONVERTER *converter = retrieve_converter (converter_descriptor);
 
   CONVERTER_INITIALIZATION_INFO *user_conf
diff --git a/tp/Texinfo/XS/convert/html_converter_finish.c 
b/tp/Texinfo/XS/convert/html_converter_finish.c
index d4c22dfb5c..c2b9c6bb45 100644
--- a/tp/Texinfo/XS/convert/html_converter_finish.c
+++ b/tp/Texinfo/XS/convert/html_converter_finish.c
@@ -30,6 +30,7 @@
 #include "converter.h"
 #include "api_to_perl.h"
 #include "call_html_perl_function.h"
+#include "call_html_cxx_function.h"
 /* html_reset_translated_special_unit_info_tree
    html_clear_direction_string_type */
 #include "convert_html.h"
@@ -157,6 +158,10 @@ html_reset_converter (CONVERTER *self)
 
   if (self->ids_data_type == IDT_perl_hashmap)
     clear_registered_ids_hv (self);
+#ifdef HAVE_CXX_HASHMAP
+  else if (self->ids_data_type == IDT_cxx_hashmap)
+    clear_registered_ids_hashmap (self);
+#endif
   else if (self->registered_ids)
     clear_strings_list (self->registered_ids);
 
@@ -334,6 +339,10 @@ html_free_converter (CONVERTER *self)
 
   if (self->ids_data_type == IDT_perl_hashmap)
     free_registered_ids_hv (self);
+#ifdef HAVE_CXX_HASHMAP
+  else if (self->ids_data_type == IDT_cxx_hashmap)
+    free_registered_ids_hashmap (self);
+#endif
   else
     {
       if (self->registered_ids)
diff --git a/tp/Texinfo/XS/convert/html_prepare_converter.c 
b/tp/Texinfo/XS/convert/html_prepare_converter.c
index 887702abda..99b1be007f 100644
--- a/tp/Texinfo/XS/convert/html_prepare_converter.c
+++ b/tp/Texinfo/XS/convert/html_prepare_converter.c
@@ -54,6 +54,7 @@
    xml_text_entity_no_arg_commands_formatting */
 #include "converter.h"
 #include "call_html_perl_function.h"
+#include "call_html_cxx_function.h"
 #include "format_html.h"
 /* html_complete_no_arg_commands_formatting html_run_stage_handlers
    html_add_to_files_source_info html_find_file_source_info
@@ -1696,6 +1697,10 @@ html_converter_customize (CONVERTER *self)
 
   if (self->ids_data_type == IDT_perl_hashmap)
     init_registered_ids_hv (self);
+#ifdef HAVE_CXX_HASHMAP
+  else if (self->ids_data_type == IDT_cxx_hashmap)
+    init_registered_ids_hashmap (self);
+#endif
   else
     self->registered_ids = new_string_list ();
 
@@ -3753,6 +3758,10 @@ html_id_is_registered (CONVERTER *self, const char 
*string)
 {
   if (self->ids_data_type == IDT_perl_hashmap)
     return is_hv_registered_id (self, string);
+#ifdef HAVE_CXX_HASHMAP
+  else if (self->ids_data_type == IDT_cxx_hashmap)
+    return is_hashmap_registered_id (self, string);
+#endif
   else
     return find_string (self->registered_ids, string);
 }
@@ -3762,6 +3771,10 @@ html_register_id (CONVERTER *self, const char *string)
 {
   if (self->ids_data_type == IDT_perl_hashmap)
     hv_register_id (self, string);
+#ifdef HAVE_CXX_HASHMAP
+  else if (self->ids_data_type == IDT_cxx_hashmap)
+    hashmap_register_id (self, string);
+#endif
   else
     add_string (string, self->registered_ids);
 }
diff --git a/tp/Texinfo/XS/main/converter_types.h 
b/tp/Texinfo/XS/main/converter_types.h
index e93949d7d2..e727ba831b 100644
--- a/tp/Texinfo/XS/main/converter_types.h
+++ b/tp/Texinfo/XS/main/converter_types.h
@@ -40,12 +40,14 @@ enum converter_format {
 
 enum ids_data_type {
    IDT_perl_hashmap,
+   IDT_cxx_hashmap,
    IDT_string_list,
 };
 
 /* converter low level customization */
 #define CONVF_perl_hashmap        0x0001
 #define CONVF_string_list         0x0002
+#define CONVF_cxx_hashmap         0x0004
 
 /* for string information passing to/from perl */
 enum sv_string_type {
@@ -886,6 +888,10 @@ typedef struct CONVERTER {
     STRING_LIST *registered_ids;
     /* actually HV * but we do not want to drag in Perl headers */
     void *registered_ids_hv;
+#ifdef HAVE_CXX_HASHMAP
+    /* a pointer on C++ data */
+    void *registered_ids_hashmap;
+#endif
     /* potentially one target list per command (only for some actually) */
     HTML_TARGET_LIST html_targets[BUILTIN_CMD_NUMBER];
     HTML_TARGET_LIST html_special_targets[ST_footnote_location+1];
diff --git a/tp/Texinfo/XS/main/text.c b/tp/Texinfo/XS/main/text.c
index 7ec4667bea..9acfe4ec0b 100644
--- a/tp/Texinfo/XS/main/text.c
+++ b/tp/Texinfo/XS/main/text.c
@@ -23,10 +23,10 @@
 
 /* wrapper for vasprintf */
 int
-xvasprintf (char **ptr, const char *template, va_list ap)
+xvasprintf (char **ptr, const char *format, va_list ap)
 {
   int ret;
-  ret = vasprintf (ptr, template, ap);
+  ret = vasprintf (ptr, format, ap);
   if (ret < 0)
     abort (); /* out of memory */
   return ret;
diff --git a/tp/Texinfo/XS/main/text.h b/tp/Texinfo/XS/main/text.h
index 9c511436d2..ed269407a6 100644
--- a/tp/Texinfo/XS/main/text.h
+++ b/tp/Texinfo/XS/main/text.h
@@ -19,7 +19,7 @@
 #include <stddef.h>
 #include <stdarg.h>
 
-int xvasprintf (char **ptr, const char *template, va_list ap);
+int xvasprintf (char **ptr, const char *format, va_list ap);
 
 typedef struct TEXT {
     char *text;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]