// Stub code to access libcell from OCaml extern "C" { #include /* for debugging purposes (=> printf) only */ #include #include #include #include #include #include } #include "libcell.h" #define CELL_PTR(v) (*((Cell **) Data_custom_val(v))) #define CELL_VAL(v) (* CELL_PTR(v)) static void free_cell(value v) { printf("(cell_stub1.cpp) free_cell called on %p -> %i\n", CELL_PTR(v), CELL_VAL(v).get() ); delete CELL_PTR(v); } static int compare_cell(value v1, value v2) { return CELL_VAL(v1).get() - CELL_VAL(v2).get(); } static struct custom_operations cell_ops = { /* identifier */ "Cell/v0.1", /* finalize */ &free_cell, /* compare */ &compare_cell, /* hash */ custom_hash_default, /* serialize */ custom_serialize_default, /* deserialize */ custom_deserialize_default }; extern "C" value cell_create(value v) { CAMLparam1(v); CAMLlocal1(res); extern struct custom_operations cell_ops; res = alloc_custom(&cell_ops, sizeof(Cell *), 1, 1000); CELL_PTR(res) = /* store a pointer to the object in the custom block */ new Cell(Int_val(v)); CAMLreturn(res); } extern "C" value cell_set(value v1, value v2) { CAMLparam2(v1, v2); try { CELL_VAL(v1).set( Int_val(v2) ); } catch (Exc e) { failwith((char *) e.msg); } CAMLreturn(Val_unit); } extern "C" value cell_get(value v) { CAMLparam1(v); CAMLreturn( Val_int(CELL_VAL(v).get()) ); } static struct custom_operations cell_global_ops = { /* identifier */ "Cell/v0.1", /* finalize */ NULL, /* compare */ &compare_cell, /* hash */ custom_hash_default, /* serialize */ custom_serialize_default, /* deserialize */ custom_deserialize_default }; extern "C" value cell_global(value v) { CAMLparam1(v); CAMLlocal1(res); extern struct custom_operations cell_ops; extern Cell global_cell; res = alloc_custom(&cell_global_ops, sizeof(Cell *), 1, 1000); CELL_PTR(res) = &global_cell; CAMLreturn(res); }