在需要非成员函数指针时传递成员函数指针

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

本文关键字:指针 函数 成员      更新时间:2023-10-16

>我目前在将成员函数回调函数传递给库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*) 

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

您上一个问题的副本列出了一些想法,但缺乏具体的代码,所以这里有一个示例。

首先,通过添加静态回调函数来修改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,您可以像往常一样访问类的所有成员。

通常完成

此操作的方式是在数据上下文中传递"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 */);
    }
};