unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Mike Gran <spk121@yahoo.com>
Cc: Guile Users <guile-user@gnu.org>
Subject: Re: C structures
Date: Sat, 31 Dec 2005 08:00:52 -0800 (PST)	[thread overview]
Message-ID: <20051231160052.18006.qmail@web32501.mail.mud.yahoo.com> (raw)
In-Reply-To: <87zmmhbe7r.fsf@ossau.uklinux.net>

Yeah.  There's a bit of overhead in getting a SMOB set up, but, it has
the advantage that you aren't keeping duplicate copies (a C copy and a
Scheme copy) of your information in memory.

--- Neil Jerram <neil@ossau.uklinux.net> wrote:

> 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 <leonardolopespereira@gmail.com> 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
> 
> 
> 
> _______________________________________________
> Guile-user mailing list
> Guile-user@gnu.org
> http://lists.gnu.org/mailman/listinfo/guile-user
> 



	
		
__________________________________ 
Yahoo! for Good - Make a difference this year. 
http://brand.yahoo.com/cybergivingweek2005/


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


  reply	other threads:[~2005-12-31 16:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-30 13:11 C structures Leonardo Lopes Pereira
2005-12-30 15:04 ` Neil Jerram
     [not found]   ` <20051230125804.f764063f.leonardolopespereira@gmail.com>
2005-12-31 13:14     ` Neil Jerram
2005-12-31 16:00       ` Mike Gran [this message]
2005-12-30 15:58 ` Mike Gran
2005-12-30 16:19   ` Leonardo Lopes Pereira
2005-12-30 17:01     ` Mike Gran
2005-12-30 19:45     ` klaus schilling
     [not found] <E1EsL1F-0006hc-Lg@pat.uio.no>
2006-01-04 21:10 ` Kjetil S. Matheussen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20051231160052.18006.qmail@web32501.mail.mud.yahoo.com \
    --to=spk121@yahoo.com \
    --cc=guile-user@gnu.org \
    --cc=spikegran@earthlink.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).