[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: how to wrap arrays?
From: |
John Steele Scott |
Subject: |
Re: how to wrap arrays? |
Date: |
Mon, 04 Sep 2006 22:44:41 +0900 |
User-agent: |
Pan/0.14.2.91 (As She Crawled Across the Table (Debian GNU/Linux)) |
On Mon, 04 Sep 2006 10:29:50 +0200, Ludovic Courtès wrote:
> Hi,
>
> Andreas Rottmann <address@hidden> writes:
>
>> John Steele Scott <address@hidden> writes:
>
>>> Just for fun, I am using g-wrap to interface the cairo graphics library
>>> with guile. Thankfully this library has quite a wrapper-friendly
>>> interface, but I don't know how to do this one:
>>>
>>> void cairo_set_dash(cairo_t *cr,
>>> const double *dashes,
>>> int num_dashes,
>>> double offset);
>>>
>>> The num_dashes argument gives the number of values pointed to by
>>> dashes, and it may be 0.
>>>
>>> Ideally I'd like to be able to have a Scheme function which could be
>>> called like:
>>>
>>> (cairo-set-dash cr (list 1.0 3.0 1.5 2.5) 2.5)
>>>
>>> Where dashes and num_dashes can be supplied based on the values and
>>> length of the list. But at the moment I don't know see how to handle
>>> the dashes argument at all. Any help would be appreciated.
>>>
>> G-Wrap currently doesn't support this (yet); a half-manual wrapper
>> must be written, consisting of an override and a C part:
>>
>> The override:
>>
>> (define-function gtk_stock_add
>> (c-name "_wrap_gtk_stock_add")
>> (overrides "gtk_stock_add")
>> (return-type "none")
>> (parameters
>> '("SCM" "items")))
>
> This is not "standard" G-Wrap: it's the higher-level layer used in
> `guile-gnome' (and possibly others).
Thanks for pointing this out, I might have become (even more) confused
otherwise.
> If you want to use standard G-Wrap to produce your semi-manual wrapper,
> you can create a C function like this:
>
> static void
> wrap_cairo_set_dash (cairo_t *c, SCM the_list, double offset) {
> /* Traverse the list and turn it into a C array... */ return
> cairo_set_dash (c, array, array_size, offset);
> }
Thanks for the suggestion. I ended up with this, which is awkward
(especially the second assert), but it works:
void wrap_cairo_set_dash (cairo_t * cr, SCM dash_list, double offset)
{
long num_dashes, i;
double * dashes;
SCM item;
SCM_ASSERT(SCM_NFALSEP(scm_list_p(dash_list)),
dash_list, SCM_ARG2, "cairo-set-dash");
num_dashes = scm_ilength(dash_list);
if (num_dashes == 0) {
dashes = NULL;
} else {
dashes = scm_must_malloc(sizeof(double) * num_dashes, "array for dashes");
}
for (i = 0; i < num_dashes; i++) {
item = scm_list_ref(dash_list, SCM_MAKINUM(i));
SCM_ASSERT(SCM_NUMBERP(item), item, SCM_ARG2, "cairo-set-dash");
dashes[i] = scm_num2dbl(item, "");
}
cairo_set_dash(cr, dashes, num_dashes, offset);
if (dashes != NULL)
free(dashes);
}
> Now, I'd rather use a Scheme vector to represent a C vector so that it
> wouldn't need to be traversed at "marshalling"-time.
Yes, I notice a deprecated gh_scm2doubles (SCM VECTOR, double *RESULT) in
the manual, that would have made this much simpler.
Do you know the purpose of the second argument to scm_num2dbl (called
"why")? I notice that guile 1.8 has a scm_to_double which does not have
this second argument, but I'm stuck with 1.6 for the time being.
Thanks,
John