Wrapping multiple extern (C) declarations in a template

Gary Willoughby via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Dec 13 12:43:45 PST 2014


On Saturday, 13 December 2014 at 19:03:42 UTC, aldanor wrote:
> Let's say there's a "foo.d" that contains raw bindings to 
> "foo.h" (enums, structs, extern variables, function 
> declarations, a whole load of stuff) -- everything but the 
> functions is already fine but all functions need to be wrapped.
>
> If I now want to have exactly the same module but with all 
> function declarations wrapped as described above, I have to 
> public import it in another module and then do some template 
> magic. However, it wouldn't be possible to retain the same 
> function names since they've been imported to the namespace 
> (it's then also not possible to extern them as private 
> initially since then you won't be able to import/wrap them in a 
> different module). Hence the idea of mixing the wrapping 
> template in the initial .d header.
>
> I guess another way would be to split each header into 
> functions / non-functions part, and private import + wrap the 
> first part and public import the second part into a D-ready 
> module. It just sounds like a whole lot of boilerplate so I was 
> hoping there would be some magic automated way of doing 
> something similar (maybe creating a custom "import" template 
> that "imports" non-functions via aliasing and wraps 
> functions)...

I don't think there is an easy way to do this. foo.d contains all 
the converted C code. Then bar.d *privately* imports foo.d and 
adds exceptions. I would use bar.d to improve the interface to 
the library not mearly wrapping functions (even though that's 
essentially what is happening). foo.d is the C interface. bar.d 
is your new OOP API complete with exceptions.


More information about the Digitalmars-d-learn mailing list