[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Question on Chickens C++ interface.
From: |
felix |
Subject: |
Re: [Chicken-users] Question on Chickens C++ interface. |
Date: |
Mon, 26 Apr 2004 14:18:49 +0200 |
User-agent: |
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6) Gecko/20040113 |
Matthias Heiler wrote:
Hi,
I'm currently trying to integrate a C++ library into Chicken. This
library works a lot with passing around C-arrays containing C++
objects. I don't currently understand how to build and pass around
such arrays from Chicken.
>
>[...]
Here I essentially store the pointers on C++ objects in a s32vector
and try to pass a pointer to a container class. Unfortunately, this
doesn't work (but segfaults).
Ugh. Well, I wouldn't recommend this approach. Converting object pointers
to/from integer addresses screams for trouble... ;-)
So my questions are:
1.) How do I create a C array of C++ objects and pass it around properly?
One possible way would be to use TinyCLOS wrapper classes and use
a Scheme vector, but that would complicate things if you want to
access the vector in C as well.
Another way would be to add pointer-vectors to Chicken. But that won't
help you now.
2.) How do I make sure it doesn't get garbage collected while the C++
container is still using it? Is it dangerous if the C++ container
deletes the vector? (Unfortunately, the C++ code I have to use does
many things a little "unconventionally".)
Yes, this is another issue.
So, assuming the vector containing pointers has to be accessible for
C/C++ code, I would create the array completely in C, and define
accessor functions in C (including their Scheme wrappers) that
let you access elements of the array.
But I may misunderstanding your exact requirements. If you need
special functionality (vector should be garbage collectablem, accessed
C++ instances should map to TinyCLOS objects, etc.), please give
more details.
3.) What are the differences between locatives, pointers, and addresses
in Chicken? When do I use which?
An address is just an integer. A pointer is a first class object containing
an address. A locative is a first class object containing an address and
additional data that lets the locative refer to arbitrary positions inside
another Scheme data object, so for example:
(define x (vector 'a 'b 'c))
(define y (make-locative x 2))
(locative-ref y) ==> c
Locatives can be used in place of pointers, which gets handy for passing
a pointer to the Nth element of a SRFI-4 bytevector to foreign code, for example
(the OpenGL egg uses this: some GL procedures accept a void * to data,
where GLenum specifies the type of the data. So we pass a locative, which is
untyped. This also takes care of the case when data moves:
(let ([loc (make-locative X)])
... ; lots of stuff, causing GC
(my-func loc) )
This will work, since a locative will automatically update the pointer after
each GC.
I wouldn't recommend using addresses directly.
Pointers are fine when using them to address C data.
Locatives are best when passing pointers to Scheme data to C
(but note that those pointers normally point to Scheme data structures,
which C has to decode manually. Byte-vectors and SRFI-4 bytevectors are
an exception to this rule, since they store unboxed numbers directly
in the format used by C.
Sorry if this all sounds a bit confusing. The documentation is way
to incomplete...
cheers,
felix