[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Question on User-Defined Types (Embedding)
Quoting Brent Fulgham:
> Let's say I have a C++ class/structure that I want to pass in and out
> of the MzScheme runtime.
>
> For example, let's say I have a simple structure:
>
> struct test
> {
> char name[10];
> long salary;
> };
If you have the luxury of changing the test struct, add a
Scheme_Type element at the beginning:
struct test
{
Scheme_Type tag; /* MzScheme needs this tag */
char name[10];
long salary;
};
> And I'd have previously created a type:
>
> Scheme_Type test_type;
>
> test_type = scheme_make_type("<test type>");
Use test_type as the value of `tag' in a test struct.
Because of the type tag, a test* can be safely cast to a
Scheme_Object*. And any Scheme_Object* o where *(Scheme_Type*)o ==
test_type can be safely cast to a test*.
If you can't change the test struct, you'll have to define a wrapper
struct:
struct wrapped_test {
Scheme_Type tag;
struct test t;
};
> In my real case, I wish to pass Scheme a type representing some global
> data that *should not* be collected by Scheme.
MzScheme will happily work with a Scheme_Object* value that isn't a
pointer to GC land.
See also plt/collects/mzscheme/examples/bitmatrix.c. (Probably we
should have another example with a simpler C-defined structure.)
Matthew