Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C API function callbacks into C++ member function code

So, I'm using the FMOD api and it really is a C api.

Not that that's bad or anything. Its just it doesn't interface well with C++ code.

For example, using

FMOD_Channel_SetCallback( channel, callbackFunc ) ;

It wants a C-style function for callbackFunc, but I want to pass it a member function of a class.

I ended up using the Win32 trick for this, making the member function static. It then works as a callback into FMOD.

Now I have to hack apart my code to make some of the members static, just to account for FMOD's C-ness.

I wonder if its possible in FMOD or if there's a work around to link up the callback to a specific C++ object's instance member function (not a static function). It would be much smoother.

like image 799
bobobobo Avatar asked Mar 10 '10 20:03

bobobobo


People also ask

How are callbacks implemented in C?

Callbacks in C are usually implemented using function pointers and an associated data pointer. You pass your function on_event() and data pointers to a framework function watch_events() (for example). When an event happens, your function is called with your data and some event-specific data.

What is callback function in C with example?

We can define it in other words like this: If the reference of a function is passed to another function argument for calling, then it is called the callback function. In C we have to use the function pointer to call the callback function. The following code is showing how the callback function is doing its task.

Why callback function is used in C?

What is the callback function in C? In C or any other programming language, we will give the function address to another function or any other code. So, that code can call the function at any time whenever it needs. A callback function is a function that is called by using a function pointer.


2 Answers

You cannot directly pass a member function. A member function has the implicit parameter this and C functions don't.

You'll need to create a trampoline (not sure the signature of the callback, so just doing something random here).

extern "C" int fmod_callback( ... args ...)
{
    return object->member();
}

One issue is where does that object pointer come from. Hopefully, fmod gives you a generic context value that will be provided to you when your callback is made (you can then pass in the object pointer).

If not, you'll just need to make it a global to access it.

like image 54
R Samuel Klatchko Avatar answered Oct 15 '22 07:10

R Samuel Klatchko


I guess it supposed to work like this:
You can assign some user data to channel by calling FMOD_Channel_SetUserData. This user data should be a pointer to your C++ object that handles events. Then you should write C-style callback that extracts that object by calling FMOD_Channel_GetUserData and then calls your C++ instance method on that object.

like image 20
Denis K Avatar answered Oct 15 '22 07:10

Denis K