[Prev][Next][Index][Thread]

Re: Using C structs with FD 2.0 FFI



oodl@my-deja.com writes:

> Can someone tell me what I am doing wrong?  

Cut down to a simple case:

---------------8<--------------
define c-struct <statistics>
  slot count :: <c-double>;
  pointer-type-name: <statistics*>;
end;

define method main () => ()
    pointer-value(make(<statistics*>));
end;
---------------8<--------------

This produces the error of no next-method(). It looks like no
pointer-value method is being added for <statistics*>. I'm pretty sure
this code should work but it doesn't. The pointer-value for a
<statistics*> should return an instance of  <statistics>.

By the way, with the code you posted, I think your output parameter
should not be an output parameter. The C code:

> int ReadStats (Statistics* stats) {
>   stats->count = 123;
>   return 1;
> }

does not actually return a statistics* - it sets a value in the member
of the statistics*. So your Dylan interface should look like:

  define C-function read-stats
    parameter stats :: <Statistics*>;
    result value :: <C-int>;
    c-name: "ReadStats"
  end;

And call it with something like:

  with-stack-structure( stats :: <statistics*>)
    let result = read-stats(stats);
    format-out("%= %=\n", result, stats.count);
  end;

I tested this just now and it works fine. If you wanted an output
parameter type code, it would be used in a situation like:

  extern "C" int ReadStats (Statistics** stats) {
     Statistics* s = new Statistics;
     s->count = 123;
     *stats = s;
     return 1;
  }

Hope that helps,
Chris.
-- 
http://www.double.co.nz/dylan


Follow-Ups: References: