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

Passing member function pointer when a non-member function pointer was expected?

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

我的 class 是这样定义的:

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() 中,我将成员函数传递给非成员函数。这意味着我将 class 记录(回调)的成员函数传递给非成员函数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*) 

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

duplicate of your previous 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 作为用户数据指针,因为我们需要的所有用户数据都保存在 class 中。由于此回调仅在内部使用,因此您可以从此处和函数中删除最后一个参数。

注册回调时,传入新的回调,用户数据参数为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 中,您可以像往常一样访问 class 的所有成员。

通常这样做的方法是在数据上下文中传递 '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 */);
    }
};