Wrapping C code?

Artur Skawina art.08.09 at gmail.com
Sat Mar 29 12:23:23 PDT 2014


On 03/29/14 20:01, CJS wrote:
> 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.
> 

The problem description is a little vague, at least w/o more context. If
you control D's view of that opaque structure then the simpliest solution
would be something like:

   struct XXXXContext {
      auto ref opDispatch(string NAME, Args...)(Args args) {
         return mixin("XXXX"~NAME~"(&this, args)");
      }
   }

artur


More information about the Digitalmars-d-learn mailing list