I'm trying to find a way to add code to swig generated functions. I have used typemaps to extend classes, but can't find anything in the documentation about extending specific functions.
Given the following swig interface file:
%module Test
%{
#include "example.h"
%}
%typemap(cscode) Example %{
bool 64bit = SizeOf(typeof(System.IntPtr)) == 8;
static string Path = 64bit ? "/...Path to 64 bit dll.../" :
"/...Path to 32 bit dll.../";
%}
%include "example.h"
I get the following C# code:
public class MyClass : global::System.IDisposable {
...
bool 64bit = SizeOf(typeof(System.IntPtr)) == 8;
static string Path = 64bit ? "/...Path to 64 bit dll.../" :
"/...Path to 32 bit dll.../";
...
public static SomeObject Process(...) { // Function defined in example.h
<- I would like to add some code here.
SomeObject ret = new SomeObject(...);
}
...
}
I would like to add some code to the function Process, this code is a call to SetDllDirectory(Path)
which loads the correct dll depending on the platform type. This needs to happen inside the Process()
call.
Any help is greatly appreciated!
SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is used with different types of target languages including common scripting languages such as Javascript, Perl, PHP, Python, Tcl and Ruby.
The most common format of a SWIG interface is as follows: %module mymodule %{ #include "myheader. h" %} // Now list ANSI C/C++ declarations int foo; int bar(int x); ... The module name is supplied using the special %module directive.
In a nutshell, SWIG is a compiler that takes C/C++ declarations and creates a wrapper needed to access those declarations from other languages like Python, Tcl, Ruby etc. It normally required no changes in existing code and create an interface within a minute. Building interpreted interface for existing C programs.
The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other languages like C#, Java, JavaScript, Go, D, OCaml, Octave, Scilab and Scheme.
You can generate the code you're looking for using %typemap(csout)
. It's a bit of a hack though, and you'll need to copy some of the existing typemap for SWIGTYPE (which is a generic place holder) that can be found in csharp.swg
So for example, given a header file example.h:
struct SomeObject {};
struct MyClass {
static SomeObject test();
};
You can then write the following SWIG interface file:
%module Test
%{
#include "example.h"
%}
%typemap(csout,excode=SWIGEXCODE) SomeObject {
// Some extra stuff here
$&csclassname ret = new $&csclassname($imcall, true);$excode
return ret;
}
%include "example.h"
Which produces:
public static SomeObject test() {
// Some extra stuff here
SomeObject ret = new SomeObject(TestPINVOKE.MyClass_test(), true);
return ret;
}
If you want to generate that for all return types, not just things which return SomeObject you'll have a bit more work to do for all the variants of csout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With