views:

124

answers:

2

I am trying to figure out the syntax to register a callback with this 3rd party software. I think it is probably a basic question, I just am not too familiar with c++. They have a method for registering a callback function so their code can call a function in my code when an event happens. They provided a working example that registers the callback from the main file, but I want to know how to do it when working inside a class

Their method signature:

smHTRegisterHeadPoseCallback(smEngineHandle engine_handle, void *user_data, smHTHeadPoseCallback callback_fun);

Working example from the main file:

void STDCALL receiveHeadPose(void *,smEngineHeadPoseData head_pose, smCameraVideoFrame video_frame)
{
 ...
}

void main()
{
 ...
 smHTRegisterHeadPoseCallback(engine_handle,0,receiveHeadPose)
 ...
}

But I want to use this from my class

MyClass.h

class FaceEngine
{
public:
 void STDCALL receiveFaceData(void *, smEngineFaceData face_data, smCameraVideoFrame video_frame);
    ...

MyClass.cpp

void FaceEngine::Start(void)
{
 rc = smHTRegisterFaceDataCallback(hFaceAPIEngine,0,&FaceEngine::receiveFaceData);
        ...

Results in this compiler error:

Error 1 error C2664: 'smHTRegisterFaceDataCallback' : cannot convert parameter 3 from 'void (__stdcall FaceEngine::* )(void *,smEngineFaceData,smCameraVideoFrame)' to 'smHTFaceDataCallback' d:\stuff\programming\visual studio 2008\projects\tut02_vertices\faceengine.cpp 43 Beard

If my question isn't clear please let me know how I can clarify.

+1  A: 

To use a class member as a function pointer parameter you must declare it as a static method. If you think about it you will understand why. Normal class methods are invisibly passed a pointer to the instance when they are invoked (this pointer). A callback mechanism has no way to know the address of an instance of the class whose member method you wish to call. So, unfortunately, you do lose the automatic ability to access instance data in a callback arrangement.

Amardeep
A: 

You need to use a trampoline function as member functions can't be called without an instance:

void STDCALL trampoline(void* data, smEngineHeadPoseData hp, smCameraVideoFrame vf) {
    static_cast<FaceEngine*>(data)->receiveFaceData(hp, vf);
}

// ... register:
smHTRegisterFaceDataCallback(hFaceAPIEngine, static_cast<void*>(this), &trampoline);

Note that this uses the user-data parameter to pass the this pointer, so the trampoline function knows what instance to call the member function on.

Georg Fritzsche
Ok, I get it. Thats somewhat clever. I hadn't heard of trampoline functions before. Thanks for the answer
Mr Bell