Add support for "cxxcode" typemap

This allows injecting arbitrary code into the generated C++ classes and
can be useful for defining extra constructors or operators, for example.
This commit is contained in:
Vadim Zeitlin 2021-12-06 17:19:14 +01:00
commit e2d32f33b2
2 changed files with 18 additions and 1 deletions

View file

@ -496,6 +496,11 @@ area: 7.068583
<td>Defines how to transform <tt>ctype</tt> value returned by a function to <tt>cxxouttype</tt>
</td>
</tr>
<tr>
<td><tt>cxxcode</tt></td>
<td>May contain arbitrary code that will be injected in the declaration of the C++ wrapper class corresponding to the given type. Ignored for non-class types.
</td>
</tr>
</table>
<H3>C Typemaps, a Code Generation Walkthrough</H3>

View file

@ -528,10 +528,22 @@ public:
Printv(cxx_wrappers_.sect_decls,
"class ", Getattr(n, "sym:name"), base_classes.get(), " {\n"
"public:\n",
"public:",
NIL
);
// If we have any extra code, inject it. Note that we need a hack with an artificial extra node to use Swig_typemap_lookup(), as it needs a "type" attribute
// which the class node doesn't have.
scoped_dohptr dummy(NewHash());
Setattr(dummy, "type", Getattr(n, "name"));
Setfile(dummy, Getfile(n));
Setline(dummy, Getline(n));
scoped_dohptr cxxcode(Swig_typemap_lookup("cxxcode", dummy, "", NULL));
if (!cxxcode || *Char(cxxcode) != '\n')
Append(cxx_wrappers_.sect_decls, "\n");
if (cxxcode)
Append(cxx_wrappers_.sect_decls, cxxcode);
class_node_ = n;
dtor_wname_ = NULL;
has_copy_ctor_ = false;