C to D bindings: how much do you D-ify the code?

Paulo Pinto pjmlp at progtools.org
Fri Oct 25 09:19:15 PDT 2013


Am 25.10.2013 15:10, schrieb Lionello Lunesu:
> There's a lot of expressiveness that can be added to D bindings, when
> compared to the C or C++ headers, for a particular library:
>
> 1. enum names vs prefixes
> enum FOO { FOO_A, FOO_B }; -> enum FOO {A,B}
>
> 2. enum vs int parameters (based on a lib's documentation)
> void takefoo(int) -> void takefoo(FOO)
>
> 3. "in" for input buffers (again, based on docs)
> int puts(char*) -> puts(in char*)
>
> 4. "out" or "ref" for output parameters
> void getdouble(double*) -> void getdouble(out double value)
>
> 5. D arrays vs length+pointer pairs
> void bar(size_t len, int* ptr) -> void bar(int[] a)
>
> 6. D array wrappers
> void bar(int* ptr, int size) ->
> void bar(int[] a) { bar(a.ptr, cast(int)a.length; }
>
> 6. library specific sized-int typedefs to D natives
> png_uint_16 -> short
>
>
> These are some of the more trivial ones, but I'd like to see how other
> people go about making bindings. Do you keep as close to C as possible?
> Or do you "add value" by using more D style constructs?
>
> L.

Speaking from my experience in other languages, try to map 1:1 to the C 
API as much as possible, given the language features offered for FFI.

Offer another layer that makes use of this binding API in a more 
canonical way for the given language.

--
Paulo


More information about the Digitalmars-d mailing list