访问类C++ C 样式回调



以下C样式API函数由PiGPIO库提供:

typedef void (*gpioAlertFuncEx_t)(int, int, uint32_t, void *);  // assumed  
int gpioSetAlertFuncEx(unsigned user_gpio, gpioAlertFuncEx_t f, void *userdata)

基本上,它允许您通过回调函数处理引脚状态更改。

目前为止,一切都好。问题是将此回调包装到 c++ 类中。

我的方法如下:

class Pin
{
public:
Pin(_GpioPin)
{
gpioSetAlertFuncEx(_GpioPin, &PushButton::internal_gpio_callback, this );
}
void internal_callback_func(int pin, int level, uint32_t tick)
{
cout << "New level: " << pin << " " << level;
}
}

问题是回调函数类型不同(因为它是非静态的(。并提示错误:

error: cannot convert 'void (Pin::*)(int, int, uint32_t, void*) {aka void (Pin::*)(int, int, unsigned int, void*)}' to 'gpioAlertFuncEx_t {aka void (*)(int, int, unsigned int, void*)}' for argument '2' to 'int gpioSetAlertFuncEx(unsigned int, gpioAlertFuncEx_t, void*)'
gpioSetAlertFuncEx(this->GpioPin, &Pin::internal_gpio_callback), this );

诀窍是什么?如何铸造&PushButton::internal_gpio_callback以匹配所需的模板?

稍后编辑:我不想使回调方法成为静态的。

指向成员函数的指针与指向非成员函数的指针不同。不同之处在于成员函数需要调用一个对象,这是 C 无法处理的。

有一些方法可以解决这个问题,尤其是在您已经将this作为userdata指针传递的情况下。解决方案是简单地将真实成员函数包装在静态成员函数中(因为这些函数可以作为 C 回调函数传递(。

例如:

class Pin
{
public:
Pin(_GpioPin)
{
gpioSetAlertFuncEx(_GpioPin, &Pin::static_internal_callback_func, this );
}
private:
static void static_internal_callback_func(int pin, int level, uint32_t tick, void* userdata)
{
static_cast<Pin*>(userdata)->internal_callback_func(pin, level, tick);
}
void internal_callback_func(int pin, int level, uint32_t tick)
{
cout << "New level: " << pin << " " << level;
}
};