I ran into a problem when using a C audio library (PortAudio built with ASIO SDK, but this isn\'t really relevant to this question; the technical details would only hinder m
As far as I can see, the callbacks in PortAudio, such as this one, take a void*
argument for passing user data to them. You can use this to call member functions by passing a class pointer as user data, and writing a small static or non-member function to register as the C-compatible callback. For example:
// C library function
typedef void (*c_library_callback)(void * user_data);
void c_library_start_stuff(c_library_callback, void * user_data);
// C++ class using it
class audio_thing {
public:
virtual ~audio_thing() {} // Better have one of these in an abstract class
void start_stuff() {
c_library_start_stuff(&audio_thing::static_callback, this);
}
private:
// C-compatible callback forwards to the implementation
static void static_callback(void * thing) {
static_cast(thing)->callback();
}
// Derived classes implement the callback behaviour
virtual void callback() = 0;
};