Haskell FFI-C Array Data Fields

I am working on haskell bindings for my own library with a rather complicated interface. It has many structures as part of its interface, and I worked on creating interfaces with hsc2hs and the bindings-DSL package to help automate the binding of structures.

One problem that I have encountered is structures containing multidimensional arrays. The bindings-DSL documentation describes macros for binding to a structure such as

 struct with_array { char v[5]; struct test *array_pointer; struct test proper_array[10]; }; 

with macros like

 #starttype struct with_array #array_field v , CChar #field array_pointer , Ptr <test> #array_field proper_array , <test> #stoptype 

But this library has many structures with multidimensional arrays as fields more similar to

 struct with_multidimensional_array { int whatever; struct something big_array[10][25][500]; }; 

The #array_field macro seems to process only the first dimension of the array. Is it that bindings-DSL just doesn't have a macro to handle multidimensional arrays?

I would really like a macro to bind a (possibly multidimensional) array to a StorableArray arbitrary indices. It seems that the necessary information is possible in bindings-DSL macros provides - for this macro simply does not.

Has anyone added macros to bindings-DSL ? Has anyone added a macro for this in bindings-DSL ? I have already passed by what I should do with hsc2hs , and is there any other tool that will help me do what I want in a more concise way?

+4
source share
2 answers

Well, nobody came up with anything, so I'll go with the idea in my comment. I use the #field macro instead of the #field macro and set the type that wraps StorableArray to work properly.

Since I thought about this quite a bit, I realized that you can completely abstract the shell using the new level numbers at the level supported by GHC 7.6+. I put together a package called storable-static-array , which takes type-level measurements and provides the right instance of Storable to work with its own arrays, even multidimensional ones.

One thing that is still missing, which I really like, is to find a way to write a bindings-DSL macro that automatically retrieves the dimensions and takes care to correctly generate them. A brief look at the macros in bindings-DSL , though, convinced me that I did not know almost enough to manage it myself.

+1
source

The #array_field macro processes arrays of any size. The documentation has been updated to show this explicitly.

An equivalent Haskell entry will be a list. When you look and pop, the length and order of the elements of this list will correspond to the array, as it would be considered a one-dimensional array in C. Thus, the int example[2][3] field will correspond to the list with 6 elements ordered as example[0][0], example[0][1], example[0][2], example[1][0], example[1][1], example[1][2] . When you gouge out, if the list contains more than 6 elements, only the first 6 will be used.

This scheme was chosen for consistency with peekArray and pokeArray from the FFI standard library. Before version 1.0.17 from bindings-DSL was an error due to which the size of this list was underestimated when the array fields were larger than 1.

+1
source

Source: https://habr.com/ru/post/1494146/


All Articles