I need to wrap a C ++ library with C. This C ++ library defines callback functions. For example:
In the C shell, I can define new C callbacks that cause C ++ callbacks. Something like that:
// in C wrapper extern "C" { typedef int CallbackFnC(int n, ... ); CallbackFnC *callbackFnC; int addCallback(void* handle, CallbackFnC* fn) // handle is pointer to Z instance { callbackFnC = fn; ((Z*)handle)->addCallback(callbackFnCPP); } } X callbackFnCPP(Y y) { int rtn = callbackFnC(yn, ...); return X(rtn); }
where I assume that I can match the corresponding elements of Y with the arguments to the C callback function and that I can just build the return type X from the return of C.
Will this work? No way to define new C callbacks?
Should the new C callback be inside extern "C" , and the specified C ++ callback instance should be outside?
jensph
source share