[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: C structures
From: |
Neil Jerram |
Subject: |
Re: C structures |
Date: |
Sat, 31 Dec 2005 13:14:48 +0000 |
User-agent: |
Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux) |
Leonardo,
Based on your replies to other people on the list, it sounds as though
you probably want a SMOB. A SMOB is a way of passing a C pointer
(such as to an arbitrary struct) around opaquely in Scheme, and the
way to use them in quite well documented in a couple of places in the
Guile manual (1.7/CVS version): nodes "Defining New Types (Smobs)" and
"Dia Smobs".
In case something simpler would do, however, I've also added a few
comments below.
Leonardo Lopes Pereira <address@hidden> writes:
> Let me give a example. I can pass some simple data...
>
> --- scheme.scm ---
> (define number 1)
>
> --- C program ---
> #include <stdio.c>
> #include <libguile.h>
> #include <guile/gh.h>
>
> int main ()
> {
> int number
> SCM s_symbol, s_value;
> scm_init_guile ();
>
> scm_c_primitive_load ("scheme.scm");
>
> s_symbol = scm_c_lookup("number");
> s_value = scm_variable_ref(s_symbol);
> number = gh_scm2int(s_value, 0, "main");
> }
>
> ---
>
> This convert the Scheme data in C data, but I do not know how to pass
> a group of data together. ex.: How to convert a (cons x y) into a
> struct?
Well, for example:
(define number (cons 3 4))
instead of (define number 1), and then
struct point number
SCM s_symbol, s_value;
scm_init_guile ();
scm_c_primitive_load ("scheme.scm");
s_symbol = scm_c_lookup("number");
s_value = scm_variable_ref(s_symbol);
number.x = gh_scm2int(SCM_CAR(s_value), 0, "main");
number.y = gh_scm2int(SCM_CDR(s_value), 0, "main");
instead of the C code above. Does that make sense?
A scheme list can be as long as you like, so can hold more than 2
values, and each value can be a different type. So you can equally
well have 2 numbers and a string, for example.
>
> I want to create a wrap to a function that has a sruct as arg, so, I
> need to create that struct in scheme and convert it to C, how to do
> that?
It depends what the C code expects as regards the lifetime of the
struct. If the struct only needs to be value for the duration of the
call to your C func, you can allocate it on the stack, initialize it
from a Scheme pair/list as shown above, and then call the C func. If
the struct needs to be longer lived than that, the SMOB approach is
probably best.
Regards,
Neil