Reputation: 1420
I have a pure C library whose headers are generated from a custom IDL in Perl. I'd like to move to SWIG (or another generator) to remove the dependency on Perl. I'd also like the free generation of Python and Ruby wrappers that come with SWIG.
I currently generate a C# wrapper which requires the notion of a parent passed to a constructor:
// Pure C API
b = StructB_Create(a);
// not safe to free a until b is freed
// C# wrapper ctor
B(ClassA a)
{
this.parent = a; // ref to prevent gc of a
}
Is it easy to use SWIG with an existing pure C API in this way?
Upvotes: 1
Views: 183
Reputation: 955
Even if SWIG can't automatically create the reference count for you, it has good abilities for tweaking the resultant code. (I did something similar with refs in Python). You can customize the resultant C#, or you can enhance the C side of things all from within the SWIG .i file.
Using the .i file you can add methods to classes, or you can add prefix or suffix code to existing methods in the target language. Pretty much anything you might need is catered for. For example, see http://www.swig.org/Doc2.0/CSharp.html#CSharp_extending_proxy_class
The doco is not always easy to follow, but it's worth it in the long run. Especially if you have multiple target languages.
Upvotes: 1