简体   繁体   English

将C ++成员函数映射到C回调

[英]Map C++ member function to C callback

In a C++ project I am using a C library to access some hardware. 在C ++项目中,我使用C库访问某些硬件。 The C-Bindings include callbacks to notify about changes on some input pins. C绑定包括回调,用于通知某些输入引脚上的更改。 The function for the callback looks like this: void callback(char port, uint8_t interrupt_mask, uint8_t value_mask, void *user_data) 回调函数如下所示: void callback(char port, uint8_t interrupt_mask, uint8_t value_mask, void *user_data)

In my implementation I have a SensorController class which has a member function that should receive the callback. 在我的实现中,我有一个SensorController类,该类具有一个应接收回调的成员函数。 The member function looks like this void SensorController::pinChanged(char port, uint8_t interrupt_mask, uint8_t value_mask, void *user_data) . 成员函数如下所示: void SensorController::pinChanged(char port, uint8_t interrupt_mask, uint8_t value_mask, void *user_data)

Now I was wondering, what would be the cleanest way to avoid making the pinChanged function static to be able to assign the function to the callback? 现在我想知道,什么是避免将pinChanged函数pinChanged静态函数以便能够将函数分配给回调的最干净的方法?

Use the user_data pointer to give you back a pointer to the class that owns the callback. 使用user_data指针可以为您提供一个指向拥有回调的类的指针。 For example. 例如。

void MyCallback(char port, uint8_t interrupt_mask, uint8_t value_mask, void *user_data)
{
    static_cast<Example *>(user_data)->Callback(port, interrupt_mask, value_mask);
}

class Example
{
public:

    Example()
    {
        //setup callback here - i dont know what library you're using but you should be able to pass a this pointer as user_data
        init_lib(fictionalparameter1, fictionalparameter2, this);
    }

private:

     void   Callback(char port, uint8_t interrupt_mask, uint8_t value_mask)
     {
         //callback handler here...
     }

     friend void MyCallback(char port, uint8_t interrupt_mask, uint8_t value_mask, void *user_data);
};

Function that receives callback can only be: 接收回调的函数只能是:

  1. Ordinary function 普通功能
  2. Static function 静态功能

Why it can't be a member class function? 为什么不能成为成员类函数?

It's all about pointers. 这都是关于指针的。 Pointer to ordinary function or static function is an address to memory. 指向普通函数或静态函数的指针是存储器的地址。 Pointer to a member class function is an offset - number which should be added to object pointer (this) to access the method. 指向成员类函数的指针是一个偏移量-应该添加到对象指针(this)以访问该方法的数字。 [see jv110 comment] [请参阅jv110评论]

In C++, member functions have an implicit parameter which points to the object (the this pointer inside the member function). 在C ++中,成员函数具有指向对象的隐式参数(成员函数内部的this指针)。 Normal C functions can be thought of as having a different calling convention from member functions, so the types of their pointers (pointer-to-member-function vs pointer-to-function) are different and incompatible. 普通C函数可以被认为与成员函数具有不同的调用约定,因此其指针的类型(指针到成员函数与指针到函数)是不同且不兼容的。 C++ introduces a new type of pointer, called a pointer-to-member, which can be invoked only by providing an object. C ++引入了一种新型的指针,称为成员指针,只有提供一个对象才能调用它。 https://isocpp.org/wiki/faq/pointers-to-members#addr-of-memfn https://isocpp.org/wiki/faq/pointers-to-members#addr-of-memfn

You could use user_data in callback to access class member methods like user1320881 said. 您可以在回调中使用user_data来访问类成员方法,如user1320881所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM