From 7c46ff1b6e2be624841ac9320a63be0631c7c609 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 6 Dec 2021 04:25:44 +0100 Subject: [PATCH] Add "cxxheader" section to allow injecting extra C++ declarations This can also be used to include extra C++ headers. Document this section as well as the already existing "cheader" one. --- Doc/Manual/C.html | 12 ++++++++++++ Source/Modules/c.cxx | 11 ++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Doc/Manual/C.html b/Doc/Manual/C.html index eb9089d0c..a31329200 100644 --- a/Doc/Manual/C.html +++ b/Doc/Manual/C.html @@ -199,6 +199,15 @@ This will compile the application code (runme.c) and link it against th Wrapping C functions and variables is obviously performed in a straightforward way. There is no need to perform type conversions, and all language constructs can be preserved in their original form. However, SWIG allows you to enhance the code with some additional elements, for instance using check typemap or %extend directive.

+

+It is also possible to output arbitrary additional code into the generated header by using %insert directive with cheader section, e.g. +

+%insert("cheader") %{
+#include "another.h"
+%}
+
+

+

36.3.1 Functions

@@ -668,5 +677,8 @@ Other ones are due to things that could be supported but haven't been implemente

+Note that cxxheader section can be used to output additional +declarations to the C++-only part of the generated header. + diff --git a/Source/Modules/c.cxx b/Source/Modules/c.cxx index 3b6990d32..5898619d5 100644 --- a/Source/Modules/c.cxx +++ b/Source/Modules/c.cxx @@ -251,13 +251,17 @@ struct cxx_wrappers // Default ctor doesn't do anything, use initialize() if C++ wrappers really need to be generated. cxx_wrappers() : except_check_start(NULL), except_check_end(NULL), - sect_types(NULL), sect_decls(NULL), sect_impls(NULL) { + sect_cxx_h(NULL), sect_types(NULL), sect_decls(NULL), sect_impls(NULL) { } void initialize() { + sect_cxx_h = NewStringEmpty(); sect_types = NewStringEmpty(); sect_decls = NewStringEmpty(); sect_impls = NewStringEmpty(); + + // Allow using SWIG directive to inject code here. + Swig_register_filebyname("cxxheader", sect_cxx_h); } // This function must be called after initialize(). The two can't be combined because we don't yet know if we're going to use exceptions or not when we @@ -307,6 +311,10 @@ struct cxx_wrappers // The order of the members here is the same as the order in which they appear in the output file. + // This section doesn't contain anything by default but can be used by typemaps etc. It is the only section outside of the namespace in which all the other + // declaration live. + String* sect_cxx_h; + // This section contains forward declarations of the classes. String* sect_types; @@ -1619,6 +1627,7 @@ public: } Printv(f_wrappers_h, "#ifdef __cplusplus\n\n", NIL); + Dump(cxx_wrappers_.sect_cxx_h, f_wrappers_h); // Generate possibly nested namespace declarations, as unfortunately we can't rely on C++17 nested namespace definitions being always available. scoped_dohptr cxx_ns_end(NewStringEmpty());