I have a function that returns vector<MyClass>
; what's the best way to change this into something FFI-appropriate?
I'm thinking a type like :: [CIntPointer]
might be a nice compromise, if possible to obtain.
You could define your own C functions to alloc, free, insert, remove, etc. These functions can wrap the C++ container you want to access. For example:
extern "C" {
Obj * obj_create()
{
return new Obj();
}
void obj_destroy(Obj * schema)
{
delete obj;
obj = NULL;
}
...
...
}
then declare them in the FFI and wrap them any way you'd like.
data SomeObject
type Obj = Ptr SomeObject
foreign import ccall unsafe "obj_create"
createObj :: IO Obj
foreign import ccall unsafe "obj_destroy"
destroyObj_ :: Obj -> IO ()
foreign import ccall unsafe "&obj_destroy"
destroyObj :: FunPtr (Obj -> IO ())
Some Gotchas:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With