2003-08-19 15:19:38 -04:00
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
This trivial Elk extension demonstrates encapsulation of a C++ class in
|
|
|
|
a first-class Scheme type, and encapsulation of member functions in
|
|
|
|
Scheme primitives.
|
|
|
|
|
2003-08-25 09:03:52 -04:00
|
|
|
See constructor.cpp in this directory for compilation instructions.
|
2003-08-19 15:19:38 -04:00
|
|
|
|
2003-08-20 18:18:34 -04:00
|
|
|
Here is a transcript showing a test run under Linux using the
|
2003-08-19 15:19:38 -04:00
|
|
|
GNU g++ compiler:
|
|
|
|
|
2003-08-26 21:11:36 -04:00
|
|
|
% g++ -shared -fPIC -I/usr/include/elk class.cpp -o class.so -lelk
|
2003-08-19 15:19:38 -04:00
|
|
|
%
|
|
|
|
% scheme
|
2003-08-20 18:18:34 -04:00
|
|
|
> (load 'class.so)
|
2003-08-19 15:19:38 -04:00
|
|
|
|
|
|
|
> (define x (make-foo))
|
|
|
|
x
|
|
|
|
> (read-val x)
|
|
|
|
1234
|
|
|
|
> (write-val! x 11)
|
|
|
|
|
|
|
|
> (read-val x)
|
|
|
|
11
|
|
|
|
> (exit)
|
2003-08-20 18:18:34 -04:00
|
|
|
%
|
2003-08-19 15:19:38 -04:00
|
|
|
|
|
|
|
-----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
class foo {
|
|
|
|
int val;
|
|
|
|
public:
|
|
|
|
int read_val(void);
|
|
|
|
void write_val(int);
|
|
|
|
foo() { val = 1234; };
|
|
|
|
};
|
|
|
|
|
|
|
|
int foo::read_val(void) {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void foo::write_val(int newval) {
|
|
|
|
val = newval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------- */
|
|
|
|
|
|
|
|
#include "scheme.h"
|
|
|
|
|
|
|
|
struct S_Foo {
|
|
|
|
Object tag; class foo foo;
|
|
|
|
};
|
|
|
|
|
|
|
|
int T_Foo;
|
|
|
|
|
|
|
|
#define FOO(x) ((struct S_Foo *)POINTER(x))
|
|
|
|
|
|
|
|
Object P_Make_Foo(void) {
|
|
|
|
Object f = Alloc_Object(sizeof (struct S_Foo), T_Foo, 0);
|
|
|
|
FOO(f)->foo.write_val(1234); /* FOO(f)->foo.foo() is not allowed?! */
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object P_Read_Val(Object x) {
|
|
|
|
Check_Type(x, T_Foo);
|
|
|
|
return Make_Integer(FOO(x)->foo.read_val());
|
|
|
|
}
|
|
|
|
|
|
|
|
Object P_Write_Val(Object x, Object y) {
|
|
|
|
Check_Type(x, T_Foo);
|
|
|
|
FOO(x)->foo.write_val(Get_Integer(y));
|
|
|
|
return Void;
|
|
|
|
}
|
|
|
|
|
2003-08-20 18:18:34 -04:00
|
|
|
int Foo_Print(Object h, Object port, int raw, int depth, int length) {
|
2003-08-19 15:19:38 -04:00
|
|
|
Printf(port, "#[foo %d]", FOO(h)->foo.read_val());
|
2003-08-20 18:18:34 -04:00
|
|
|
return 0;
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int Foo_Equal(Object x, Object y) {
|
|
|
|
return FOO(x)->foo.read_val() == FOO(y)->foo.read_val();
|
|
|
|
}
|
|
|
|
|
2003-08-20 18:18:34 -04:00
|
|
|
extern "C" void elk_init_foo() {
|
2003-08-19 15:19:38 -04:00
|
|
|
T_Foo = Define_Type(0, "foo", NOFUNC, sizeof(struct S_Foo),
|
2003-09-02 04:12:11 -04:00
|
|
|
Foo_Equal, Foo_Equal, Foo_Print, NOFUNC);
|
2003-08-19 15:19:38 -04:00
|
|
|
Define_Primitive((Object(*)(...))P_Make_Foo, "make-foo", 0, 0, EVAL);
|
|
|
|
Define_Primitive((Object(*)(...))P_Read_Val, "read-val", 1, 1, EVAL);
|
|
|
|
Define_Primitive((Object(*)(...))P_Write_Val, "write-val!", 2, 2, EVAL);
|
|
|
|
}
|
2003-08-20 18:18:34 -04:00
|
|
|
|