Hey Ryan,
Ryan writes:
Hello all,
I'm working on a C++ project. For example let's say I have a class called MyClass
In C++ I might have
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);
I've read the documentation and tried a bunch of things. Finally giving in and asking for a bit of direction since it's not working as I'd expect.
For finalizers description see: https://common-lisp.net/project/ecl/manual/ch30s05.html
Here you will find how to use arbitrary CL library in your C application: https://common-lisp.net/project/ecl/static/ecldoc/Extensions.html#System-bui...
For FFI description see: https://common-lisp.net/project/ecl/static/ecldoc/Extensions.html#Foreign-Fu...
I've added a ticket about documenting thing you ask for: https://gitlab.com/embeddable-common-lisp/ecl/issues/324
I was using ecl_make_pointer(ptr) to try and make a cl_object I was using ecl_to_pointer([formerly created cl_object here]) to try and convert the cl_object pointer to a C++ void*
Maybe the bug is elsewhere or I'm not understanding...
ECL documentation needs more embedding examples, but we are working on that. Hope these advices help.
Sincerely, Ryan
Best regards, Daniel