预期非成员函数指针时传递成员函数指针?

时间:2017-11-01 23:11:31

标签: c++ class c++11 callback portaudio

我目前在将成员函数回调函数传递给库portaudio定义的非成员函数时遇到了一些问题。

这是我的课程的定义方式:

class record {
public:
    record();
    void start_record();
    int recordCallback(const void *inputBuffer, void *outputBuffer,
                       unsigned long framesPerBuffer,
                       const PaStreamCallbackTimeInfo* timeInfo,
                       PaStreamCallbackFlags statusFlags, void *userData);
private:
    PaStreamParameters  inputParameters,
                        outputParameters;
    PaStream*           stream;
    PaError             err = paNoError;
    paTestData          data;
    int                 totalFrames;
    int                 numSamples;
    int                 numBytes;
    SAMPLE              max, val;
    double              average;
};

start_record()中,我将成员函数传递给非成员函数。意思是我将类记录(回调)的成员函数传递给非成员函数Pa_OpenStream()。

err = Pa_OpenStream(
          &this->stream,
          &this->inputParameters,
          NULL,                  /* &outputParameters, */
          SAMPLE_RATE,
          FRAMES_PER_BUFFER,
          paClipOff,      /* we won't output out of range samples so don't bother clipping them */
          this->recordCallback,
          &data );
if( err != paNoError )
{
    std::cout << "Something wrong  - open_stream check" << std::endl;
    exit(1);
}

portaudio需要一个类型为

的函数指针
int (*)(const void*, void*, long unsigned int, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void*)
and not

而不是

int (record::)(const void*, void*, long unsigned int, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void*) 

一个快速的解决方案是在类范围之外定义函数,并使它在类中使用和定义的变量全局化,但我想避免这种情况。那我该怎么处理呢?

2 个答案:

答案 0 :(得分:2)

duplicateprevious question列出了一些想法,但缺少具体的代码,所以这是一个样本。

首先,通过添加静态回调函数来修改class record

static int myCallback(const void *inputBuffer, void outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void *userData);

请注意,它与您现有的回调具有相同的签名,但它是一个静态函数,因此您可以将其传递给Pa_OpenStream

定义该函数以调用成员函数回调:

int record::myCallback(const void *inputBuffer, void outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
{
    record *pThis = (record *)userData;
    return pThis->recordCallback(inputBuffer, outputBuffer, framesPerBuffer, timeInfo, statusFlags, nullptr);
}

我们在此处传递nullptr作为用户数据指针,因为我们需要的所有用户数据都保存在类中。由于此回调仅在内部使用,因此您只需从此处和函数中删除最后一个参数。

注册回调时,请传入新的回调,并将this作为用户数据参数。

err = Pa_OpenStream(
      &this->stream,
      &this->inputParameters,
      NULL,                  /* &outputParameters, */
      SAMPLE_RATE,
      FRAMES_PER_BUFFER,
      paClipOff,      /* we won't output out of range samples so don't bother clipping them */
      &this->myCallback,
      this);

然后在recordCallback中,您可以像往常一样访问班级的所有成员。

答案 1 :(得分:2)

通常这样做的方法是在数据上下文中传递'this'指针,然后在回调中将其解压缩。您可以将回调声明为一个简单的私有静态存根。

示例:

class record
{
    // ...

     void start_record();
     {
         err = Pa_OpenStream(
                   &this->stream,
                   &this->inputParameters,
                   NULL,                  /* &outputParameters, */
                   SAMPLE_RATE,
                   FRAMES_PER_BUFFER,
                   paClipOff,      /* we won't output out of range samples so don't bother clipping them */
                   &record::recordCallbackStub,  // call back our static stub
                   this );   // 'this' is the data
     }

private:
    // our member function called back, can be virtual
    int recordCallback(
        const void *inputBuffer, void *outputBuffer,
        unsigned long framesPerBuffer,
        const PaStreamCallbackTimeInfo* timeInfo,
        PaStreamCallbackFlags statusFlags)  // no 'userData' param, since it's 'this'
    {
        // do your thing within the record context
    }

    // the stub to keep C happy, can be declared as extern "C" or whatever call 
    // convention is required from the library, WINAPI comes to mind.
    static int recordCallbackStub(
        const void *inputBuffer, 
        void *outputBuffer,
        unsigned long framesPerBuffer,
        const PaStreamCallbackTimeInfo* timeInfo,
        PaStreamCallbackFlags statusFlags
        void* userData)
    {
         auto pThis = reinterpret_cast<record*>(userData);  // get back the this pointer.
         return pThis->recordCallback( /* all parameters, except userData */);
    }
};