如何将成员函数作为参数传递? (PortAudio) [英] How to pass a member function as a parameter? (PortAudio)
问题描述
我正在尝试在portaudio中创建多个流. 这就是打开流所需要的
I am trying to create multiple streams in portaudio. This is what it requires for opening a stream
PaError Pa_OpenDefaultStream( PaStream** stream,
int numInputChannels,
int numOutputChannels,
PaSampleFormat sampleFormat,
double sampleRate,
unsigned long framesPerBuffer,
PaStreamCallback *streamCallback,
void *userData );
这是PaStreamCallback函数.
and this is the PaStreamCallback function.
typedef int PaStreamCallback(
const void *input, void *output,
unsigned long frameCount,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData );
当我尝试使用以下方法传递回调函数时,编译器不接受:
The compiler doesn't accept when I try to pass my callback function using this:
std::bind(&MyStreamClass::OutputRouting, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, std::placeholders::_6)
这是我得到的错误:
error: cannot convert 'std::_Bind_helper<false, int (MyStreamClass::*)(const void*, void*, long unsigned int, const PaStreamCallbackTimeInfo*, long unsigned int, void*), MyStreamClass*, const std::_Placeholder<1>&, const std::_Placeholder<2>&, const std::_Placeholder<3>&, const std::_Placeholder<4>&, const std::_Placeholder<5>&, const std::_Placeholder<6>&>::type' {aka 'std::_Bind<int (MyStreamClass::*(MyStreamClass*, std::_Placeholder<1>, std::_Placeholder<2>, std::_Placeholder<3>, std::_Placeholder<4>, std::_Placeholder<5>, std::_Placeholder<6>))(const void*, void*, long unsigned int, const PaStreamCallbackTimeInfo*, long unsigned int, void*)>'} to 'int (*)(const void*, void*, long unsigned int, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void*)' {aka 'int (*)(const void*, void*, long unsigned int, const PaStreamCallbackTimeInfo*, long unsigned int, void*)'}
Pa_OpenDefaultStream(&stream, 0, 0, paFloat32, 0, 0, std::bind(&MyStreamClass::OutputRouting, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, std::placeholders::_6), nullptr);
那么,有什么方法可以将我的成员函数作为参数传递?
So, is there a way I can pass my member-function as a paramter?
推荐答案
有两种方法可以做到这一点.您可以使用void* userData
参数从非成员(或静态)函数指向您的类,并从该函数访问您的类.或者,您可以使用该非成员(或静态)函数在对象上调用成员函数:
There are a couple of ways you can do this. You can use the void* userData
parameter to point to your class from a non member (or static) function and access your class from that function. Or you can use that non member (or static) function to call a member function on your object:
class MyClass
{
public:
void register_callback_here()
{
Pa_OpenDefaultStream(stream, 2, 2, PaSampleFormat::whatever, 44100,
1024, &MyClass::pa_callback_mapper, this); // PASS this!!!
}
private:
// The actual call back
int callback(
const void* input, void* output,
unsigned long frameCount,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags)
{
// actual callback here
return 0;
}
// a pass-through function that reinterprets the userData as the this pointer
// then cals the member function using this.
static int pa_callback_mapper(
const void* input, void* output,
unsigned long frameCount,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void* userData)
{
if(auto self = reinterpret_cast<MyClass*>(userData))
return self->callback(input, output, frameCount, timeInfo, statusFlags);
return 0;
}
};
注册回叫时,您将this
作为userData
传递.回调在回调函数中将指针返回给您.您可以将其强制转换为您的类类型,然后通过它访问您的类,或将调用转发给成员函数(如本例所示).
When you register the call back you pass this
as the userData
. The callback gives that pointer back to you in your callback function. You can cast it back to your class type and either access your class through it or forward the call to a member function (as in this example).
这篇关于如何将成员函数作为参数传递? (PortAudio)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!