C++ Binding Generator

Abdulhaq alynch4047 at gmail.com
Tue Feb 18 03:52:23 PST 2014


On Tuesday, 18 February 2014 at 11:05:20 UTC, Moritz Maxeiner 
wrote:
>
> This looks very nice!
> Two questions, though:
>
> 1) Is it possible to use an already existing C API with morsel? 
> The reason I'm asking is, because LLVM classes contain many 
> methods where an automatic conversion is likely to cause memory 
> leaks to appear. This is the main reason why I decided to use 
> the C-API for my llvm-d project, instead of attempting to use 
> automatic tools like SWIG (that and the fact that LLVM did not 
> export C++ symbols properly for a Windows DLL, so you needed to 
> use the C API anyway).
>

At the moment smidgen (morsel is the example C++ library I use 
for testing) only wraps C++ classes, on the TODO is of course to 
wrap C++ top level functions etc. It makes a lot of sense to add 
support for C functions too, but to be totally honest I have no 
personal need for that ATM so it would come after everything 
else. I would class smidgen as at beta stage at the moment in 
terms of the functionality it currently supports. It needs to be 
(ab)used by some other developers testing it out on some small 
APIs, and then once I feel it has settled down others could 
contribute code to support C APIs.

Regarding memory leaks, you're right, to wrap a C++ library there 
has to be manual effort on the part of the developer to tell the 
wrapper where object ownership is transferred etc. This cannot be 
automated. Smidgen is based on the sip format and it's pretty 
easy to annotate the API to indicate where ownership is 
transferred.

> 2) What is the main difference between using morsel and SWIG, 
> other than the language used to specify the interface?

I don't know much about SWIG but the biggest difference would 
have to be SWIG's great maturity and extensive feature set. 
Smidgen has its own plusses too though:

* It's pure D and the code is tested and maintainable - i.e. if 
it doesn't work for someone they could probably extend/change it 
so that it did

* It's a very thin wrapper around the client library, so I 
suspect it's a bit faster at runtime than the more generic SWIG

* Did I mention it's in D :-)

I should stress that this is beta code and needs a bit of a 
hammering on smaller libraries before anyone spends a lot of 
effort with it. However, a smaller library would not take long to 
wrap using smidgen, so little time would be wasted if it didn't 
work out.



More information about the Digitalmars-d mailing list