c++ - 如何将成员函数作为参数传递? (PortAudio)

标签 c++ c++17 mingw-w64 portaudio

我正在尝试在 portaudio 中创建多个流。 这是打开流所需要的

PaError Pa_OpenDefaultStream( PaStream** stream,
                              int numInputChannels,
                              int numOutputChannels,
                              PaSampleFormat sampleFormat,
                              double sampleRate,
                              unsigned long framesPerBuffer,
                              PaStreamCallback *streamCallback,
                              void *userData );

这是 PaStreamCallback 函数。

typedef int PaStreamCallback(
    const void *input, void *output,
    unsigned long frameCount,
    const PaStreamCallbackTimeInfo* timeInfo,
    PaStreamCallbackFlags statusFlags,
    void *userData );

当我尝试使用此方法传递我的回调函数时,编译器不接受:

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);

那么,有没有一种方法可以将我的成员函数作为参数传递?

最佳答案

有几种方法可以做到这一点。您可以使用 void* userData 参数从非成员(或静态)函数指向您的类,并从该函数访问您的类。或者您可以使用该非成员(或静态)函数来调用对象的成员函数:

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 传递。回调在回调函数中将该指针返回给您。您可以将它转换回您的类类型并通过它访问您的类或将调用转发给成员函数(如本例所示)。

关于c++ - 如何将成员函数作为参数传递? (PortAudio),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53951653/

相关文章:

c++ - 类未定义

c++ - 构建 64 位静态库

c++ - 这个程序崩溃了。我找不到错误

c++ - C++17和C++20在模板友元函数中一元和二元运算符的区别

c++ - 递归的可变参数模板函数

c++ - 指向局部变量生命周期结束后的指针的编译时比较

gcc - 用于 MinGW w64 的 OpenMP?

从 MATLAB 命令 Maxon Motors EPOS2 电机 Controller

c++ - 获取 vtkPoint 数据坐标的顺序指针

c++ - Qt 5.6 使用 EGLFS 为 Raspberry Pi 交叉编译