Wrapping C code?

bearophile bearophileHUGS at lycos.com
Sat Mar 29 12:15:31 PDT 2014


CJS:

> I've been using a medium-sized C library. Most of the library 
> calls look something like
>
> XXXXaction(context, arg1, arg2)
>
> Where XXXX is the name of the library, action is the action to 
> take, and context is an opaque pointer defined by the library 
> to keep all the state related to these actions.
>
> Is there a simple way to wrap this library so I could make 
> calls that look like
>
> context.action(arg1, arg2)?
>
> I'm hoping for something with less manual work than having to 
> wrap each individual function call. Especially since the types 
> involved in the library can be somewhat complex and require 
> trawling through multiple header files to find out the true 
> definition.

If you give context a precise type (so no void*), you can use 
UFCS (uniform function call syntax) and define aliases like this:

alias context = XXXXaction;

You can define such aliases in bulk at compile-time in some way.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list