简体   繁体   English

C ++静态成员函数作为C回调需要访问非静态引用

[英]C++ static member function as C callback needs to access non static reference

In my C++ code I have a dependecy on a C library. 在我的C ++代码中,我依赖于C库。 This C library lets me define a callback with 3 arguments. 这个C库允许我用3个参数定义一个回调。 Example: 例:

file.c: file.c:

#ifdef __cplusplus
extern "C"{
#endif
     typedef void(*callback)(argument* 1, argument* 2, argument* 3);
...
     void set_callback(ARG1, callback name_of_callback);
...

In the C++ library i'm developing i want this callback to be a member function of a class, since I cannot pass directly a member function as the callback to the C library I created a static function to be the callback and inside this static function I want to have a reference to a class object and call its member function to do the work. 在我正在开发的C ++库中,我希望这个回调是一个类的成员函数,因为我无法直接传递成员函数作为对C库的回调我创建了一个静态函数作为回调并且在这个静态函数中我希望有一个类对象的引用,并调用其成员函数来完成工作。

Now my problem is that my static functions needs to have the 3 arguments specified by the C library, so i'm not finding a way of having a reference to an object of the class i'm developing inside that static function. 现在我的问题是我的静态函数需要有C库指定的3个参数,所以我没有找到一种方法来引用我正在该静态函数内部开发的类的对象。 Example of I wanted to do: 我想做的例子:

MyClass.h: MyClass.h:

class MyClass:
public:
   MyClass();
   void my_function(argument*1, argument* 2, argument* 3);
   static void my_callback(argument* 1, argument* 2, argument*3);

MyClass.cpp: MyClass.cpp:

MyClass::MyClass(){
    set_callback(ARG1, my_callback);
}

void MyClass::my_function(argument*1, argument* 2, argument* 3){ 
      /* Do something */}
void MyClass::my_callback(argument* 1, argument* 2, argument*3){
    Object->my_function(1, 2, 3);
}

The reason to not set the class members as static is that i want to be able to create and destroy objects of this class for testing purposes and control the state of the object within the tests. 不将类成员设置为静态的原因是我希望能够创建和销毁此类的对象以用于测试目的并控制测试中对象的状态。

I'm using g++ and gcc, hope someone can give some help with this. 我正在使用g ++和gcc,希望有人可以提供一些帮助。 Thanks in advance. 提前致谢。

You can create a static member function or a pure 'C' function to do this, consider my example below: 您可以创建静态成员函数或纯'C'函数来执行此操作,请考虑下面的示例:

/**
 * C++ part
 */
class obj
{
public:
    obj() {}
    ~obj() {}

    void doSomething(int a, int b) { cout << "result = " << a+b << endl; }
};

/**
 * C Part
 */
#ifdef __cplusplus
extern "C"
#endif
{
typedef void (*callback)(void* one, void* two, void* three);

callback g_ptr = NULL;
void* arg = NULL;

void c_lib_core (void)
{
    printf ("C Lib core...\n");
    if (g_ptr)
    {
        g_ptr(arg, NULL, NULL);
    }
}

void set_callback (void* arg, callback ptr)
{
    g_ptr = ptr;
}

void my_callback (void* one, void* two, void* three)
{
    obj* my_obj = (obj*) one;

    my_obj->doSomething(1,2);
}

#ifdef __cplusplus
}
#endif



int main (void)
{
    obj a;

    set_callback (&a, my_callback);

    c_lib_core ();
}

Output: 输出:

C Lib core...
result = 3

I pass a pointer to the C++ object as argument 1. This allows the callback to cast it back to C++ and call it. 我将指向C ++对象的指针作为参数1传递。这允许回调将其强制转换回C ++并调用它。 The c_lib_core stuff is just so I can simulate the callback of the callback function for testing purposes. c_lib_core就是这样我可以模拟回调函数的回调以进行测试。 This, along with the global variables will be in the C code somewhere. 这与全局变量一起将在C代码中的某个地方。 You only need to implement a single C function and set ARG1 to your object and you are good to go. 你只需要实现一个C函数并将ARG1设置为你的对象,你就可以了。

Assuming no concurrency concerns, which i don't know if it's the case, the simpler solution could be to define an static instance and use that in your static function. 假设没有并发问题,我不知道是否是这种情况,更简单的解决方案可能是定义静态实例并在静态函数中使用它。

Something like this. 像这样的东西。

MyClass.h MyClass.h

class Object;

class MyClass
{
public:
    MyClass();
    static Object* object;
    static void callback();
    ~MyClass();

};

MyClass.cpp MyClass.cpp

Object* MyClass::object;

MyClass::MyClass()
{
    object = new Object();
}

void MyClass::callback() {
    object->sayHello();
}

MyClass::~MyClass()
{
    delete object;
}

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

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