[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Question on User-Defined Types (Embedding)
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;
};
Now I might have a few C routines added to Scheme's global namespace:
proc = scheme_make_prim_w_arity(do_get_name, "get-name", 1, 1);
scheme_add_global("get-name", proc, loc_env);
proc = scheme_make_prim_w_arity(do_get_salary, "get-salary", 1, 1);
scheme_add_global("get-salary", proc, loc_env);
The idea being to do something like:
Scheme>(define mytest (make-test "Brent" 1000))
Scheme>(get-name mytest)
Brent
Scheme>(get-salary mytest)
1000
I would probably implement them something like this:
static Scheme_Object* do_get_name(int argc, Scheme_Object** argv)
{
struct* test = (SOME_CASTING_FOO)argv[0];
return scheme_make_string_value(test->name);
}
And I'd probably have some kind of "new" function:
static Scheme_Object* do_make_test(int argc, Scheme_Object** argv)
{
char* title = SCHEME_STR_VAL(argv[0]);
char* name = SCHEME_STR_VAL(argv[1]);
long salary = SCHEME_INT_VAL(argv[2]);
struct temp;
temp.title = title;
temp.name = name; // probably copy this actually
temp.salary = salary;
Scheme_Object* obj = SOMEHOW_MAKE_TYPE_FROM(temp);
scheme_add_global(title, obj, env);
return obj;
}
And I'd have previously created a type:
Scheme_Type test_type;
test_type = scheme_make_type("<test type>");
***
So, I've gotten this far, but I am now stuck as to how to go about actually
creating instances of a type.
In my real case, I wish to pass Scheme a type representing some global
data that *should not* be collected by Scheme. In fact, Scheme will not
be creating my type, but it might inspect it, or call methods that use
it.
So how do I go about casting in and out of a Scheme_Object* ?
How do I convert a pointer to a C struct into something that MzScheme will
recognize as a "<test>" type or similar?
How should I define work-alike predicate and value-getting macros?
Help! Help! :-)
Thanks,
-Brent