[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Strings from C?
From: |
Matthias Koeppe |
Subject: |
Re: Strings from C? |
Date: |
Wed, 17 Oct 2001 17:19:54 +0200 |
User-agent: |
Gnus/5.090004 (Oort Gnus v0.04) Emacs/20.6 |
"Robert A. Uhl" <address@hidden> writes:
> On Wed, Oct 17, 2001 at 11:21:11AM +0200, Matthias Koeppe wrote:
>>
>> You might want to consider using an interface generator like SWIG 1.3
>> (http://www.swig.org) or g-wrap. This may save some work, and both
>> tools know how to perform standard conversions between C and Scheme
>> values.
>
> Well, part of my problem is that in several places my methods take
> GSList* (a singly-linked list from glib, for those who don't use that
> library) as either an argument or return value. I was tempted to use
> SWIG, but everything I saw indicated that there was no way to convert
> between _my_ lists and guile's lists. Given that my application is
> list-intensive, and that scheme is list-intensive, it would seem
> pointless to simply wrap up more functions to create C lists from
> scheme lists; something like:
>
> (trav-mapobject-set-owners obj (gslist-new obj2 obj3 obj4))
>
> or
>
> (car (gslist-split (trav-mapobject-get-owners obj)))
>
> is more than a little ugly, at least to my eyes.
Indeed.
> Perh. there is a way to do this from SWIG after all? Obviously
> ideal would be able to write:
>
> (trav-mapobject-set-owners obj '(obj2 obj3 obj4))
>
> and
>
> (car (trav-mapobject-get-owners obj))
In SWIG we have user-defined type conversions between arbitrary C and
Scheme types, so-called "typemaps". The SWIG code for your specific
conversion problem would look like this:
%typemap(in) GSList * {
GSList *list = $source;
SCM scheme_list = ... /* build the SCM list here */
$target = scheme_list;
}
%typemap(out) GSList * {
SCM scheme_list = $source;
GSList *list = ... /* build GSList here */
$target = list;
}
If you make these typemap declarations, all functions taking or
returning GSList * values will automatically perform the conversion,
so you can write your Scheme code in the "ideal" way.
--
Matthias Köppe -- http://www.math.uni-magdeburg.de/~mkoeppe
SWIG makes Guile wrappers for C/C++ libs -- http://www.swig.org
ILISP does module-aware Emacs/Guile interaction -- http://sf.net/projects/ilisp