FFI definitions - structs

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

FFI definitions - structs

recursive
Hi,

The FFI documentation states the following in terms of defining a C struct fields when defining subclasses of ExternalStructure:

#####################################

For example, a color structur might implemented so:

  • first, create a new class named color in the System Browser (subclass of ExternalStructure)
  • add class method 'fields' to color
maybe, like this:

fields 

^#((red   'byte')
   (green 'byte')
   (blue  'byte'))                

##########################################

It only mentions simple data types, int, byte etc, as do the all the examples I've been able to find. 

How do you define pointers to other structs that are fields that are part of the C struct. E.g.


C definition:

struct Statement
{
    Stmt         *stmt;              /* statement handle */
    Resultset  **rsts;              /* pointer to resultset list */

....

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: FFI definitions - structs

Schwab,Wilhelm K
Try something like what appears below.  If you search the Squeak archives, you'll probably find some useful posts by Andreas Raab.  Don't expect too much documentation.  Overall, Squeak/Pharo's FFI still falls far short of Dolphin's (sorry, but it's true), and structure mapping is a big area of relative weakness.

Note that you *might* have to add an instance variable and accessors to allow the struct to hold a reference to the "real" target object - otherwise the GC might pull things out from under you.  Somewhere in the Dolphin image, there are examples of the phenomenon.

HTH.

Bill

==============================

fields
    "
        Gsl_wavelet defineFields
    struct


{
const gsl_wavelet_type *type;
const double *h1;
const double *g1;
const double *h2;
const double *g2;
size_t nc;
size_t offset;
}"
^#(
     ( type 'Gsl_wavelet_type*' )
     ( h1 'double*' )
     ( g1 'double*' )
     ( h2 'double*' )
     ( g2 'double*' )
     ( nc 'long' )
     ( offset 'long' )
)





From: [hidden email] [[hidden email]] on behalf of [hidden email] [[hidden email]]
Sent: Monday, April 09, 2012 8:04 AM
To: [hidden email]
Subject: [Pharo-project] FFI definitions - structs

Hi,

The FFI documentation states the following in terms of defining a C struct fields when defining subclasses of ExternalStructure:

#####################################

For example, a color structur might implemented so:

  • first, create a new class named color in the System Browser (subclass of ExternalStructure)
  • add class method 'fields' to color
maybe, like this:

fields 

^#((red   'byte')
   (green 'byte')
   (blue  'byte'))                

##########################################

It only mentions simple data types, int, byte etc, as do the all the examples I've been able to find. 

How do you define pointers to other structs that are fields that are part of the C struct. E.g.


C definition:

struct Statement
{
    Stmt         *stmt;              /* statement handle */
    Resultset  **rsts;              /* pointer to resultset list */

....

Thanks