Wrapping multiple extern (C) declarations in a template

aldanor via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Dec 13 11:03:40 PST 2014


> Personally i wouldn't go this route. I would create foo.d as a 
> C to D translation only so it can be imported and used like in 
> C. Then i would create another module which imports this to 
> create your new OOP API adding features and excepions, etc.
>
> This allows the best of both worlds, keep the C api intact for 
> use in D and create a new clean OOP API for whatever your needs 
> are.

I've actually done it this way initially, as well, however there 
are certain problems.

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)...


More information about the Digitalmars-d-learn mailing list