如何正确地将回调函数从swift传递到c++?

时间:2022-12-08 19:43:32

I am trying to pass a callback-function from swift-code to c++ code. I am using the current version of Xcode. It works for simple variables, but not for functions so far:

我正在尝试通过一个从swift代码到c++代码的回调函数。我正在使用当前版本的Xcode。它适用于简单的变量,但不适用于目前的函数:

cpp-class.cpp:

cpp-class.cpp:

bool * swiftvar;

void register_var(bool * varpntr){
    swiftvar = varpntr;
    cout<<"registered"<<endl;
}

void switch_var(){
    *swiftvar = !(*swiftvar);
    cout<<"switched"<<endl;
}

cpp-class.hpp:

cpp-class.hpp:

#ifdef __cplusplus
extern "C"{
#endif

    void register_var(bool *);
    void switch_var();

#ifdef __cplusplus
}
#endif

and in swift:

在迅速:

register_var(&testbool)
print(testbool)
switch_var()
print(testbool)

If i try the same for functions (which should be that simple in the current swift version as far as I know), I get errors. So how do I have to pass the function pointer in swift? All methods I found were causing errors. As i read, the old way of defining an UnsafeMutablePointer object an so on became obsolete, but how does it work now? I expect something like:

如果我对函数进行同样的尝试(就我所知,在当前的swift版本中应该是这么简单),就会出现错误。那么,如何才能在swift中传递函数指针呢?我发现的所有方法都导致错误。正如我所读到的,定义UnsafeMutablePointer对象的老方法已经过时了,但是它现在是如何工作的呢?我希望类似:

cpp-class.cpp:

cpp-class.cpp:

void run_func(void * funcpntr()){
    (*funcpntr)();
}

cpp-class.hpp:

cpp-class.hpp:

void run_func(void *);

(swift-pseudo-code):

(swift-pseudo-code):

run_func(testfunc())

Or is there even a better solution to run swift-functions from wrapped cpp-code?

或者是否有更好的解决方案来运行包装的cppcode中的快速函数?

Any help would be really appreciated. Thanks in advance

如有任何帮助,我们将不胜感激。谢谢提前

2 个解决方案

#1


1  

First of all, you need to declare the function type properly in .cpp/.hpp .

首先,需要在.cpp/中正确声明函数类型。高压泵。

.cpp:

. cpp:

void run_func(void (* funcpntr)()) {
    (*funcpntr)();
}

.hpp:

. hpp:

void run_func(void (* funcpntr)());

(You can omit the latter funcpntr as shown in Jarod42's answer. And this needs to be enclosed in extern "C" {...} as shown in the first "cpp-class.hpp:".)

(可以省略后面的funcpntr,如Jarod42的答案所示。这需要用extern "C"{…如第一个“cpp-class.hpp:”所示)。

And Swift side:

和迅速的一面:

Function definition:

函数定义:

//This needs to be a toplevel function, which means it cannot be a method.
func test_func() {
    //...
}

To pass it to run_func():

将其传递给run_func():

    run_func(test_func)

Or you can pass a closure to run_func:

或者可以将闭包传递给run_func:

    //This closure cannot have captured variables.
    run_func {
        //...
    }

#2


0  

The signature would be

签名会

void run_func(void (*)()); // Declaration

// Definition
void run_func(void (*funcpntr)()) {
    (*funcpntr)();
}

And usage

和使用

void testfunc(); 
run_func(&testfunc);

#1


1  

First of all, you need to declare the function type properly in .cpp/.hpp .

首先,需要在.cpp/中正确声明函数类型。高压泵。

.cpp:

. cpp:

void run_func(void (* funcpntr)()) {
    (*funcpntr)();
}

.hpp:

. hpp:

void run_func(void (* funcpntr)());

(You can omit the latter funcpntr as shown in Jarod42's answer. And this needs to be enclosed in extern "C" {...} as shown in the first "cpp-class.hpp:".)

(可以省略后面的funcpntr,如Jarod42的答案所示。这需要用extern "C"{…如第一个“cpp-class.hpp:”所示)。

And Swift side:

和迅速的一面:

Function definition:

函数定义:

//This needs to be a toplevel function, which means it cannot be a method.
func test_func() {
    //...
}

To pass it to run_func():

将其传递给run_func():

    run_func(test_func)

Or you can pass a closure to run_func:

或者可以将闭包传递给run_func:

    //This closure cannot have captured variables.
    run_func {
        //...
    }

#2


0  

The signature would be

签名会

void run_func(void (*)()); // Declaration

// Definition
void run_func(void (*funcpntr)()) {
    (*funcpntr)();
}

And usage

和使用

void testfunc(); 
run_func(&testfunc);