Skip to content

documentation: add examples and documentation of ecl_make_foreign_data and similar

It seems that these functions aren't documented (probably missed because they are C-world functions, not Lisp. Something like this:

> void* ptr = new MyClass(); // Assume default constructor
>
> What is the proper way to turn ptr into a cl_object?
> What is the proper way to decode the encoded cl_object back to C++'s
> void pointer so I can recast it?

cl_object foreign = ecl_make_foreign_data(ECL_NIL, sizeof(MyClass*), &obj);

notice, that it doesn't have to be a pointer, in that case the object
will be serialized with a cast to char*.

MyClass *foo = ecl_foreign_data_pointer_safe(foreign);

Moreover you may add your own finalizer to your object:

cl_object poof_my_class(cl_object h) {
          MyClass *foo = ecl_foreign_data_pointer_safe(foreign);
          printf("Poofing foo!\n");
          delete foo;
          return ECL_T;
}

si_set_finalizer(foreign, poof_my_class);