有没有办法在Elm中使用指针和回调函数?

2024-05-29 04:49:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在使用Elmer在DLL中构建一些python代码(有关Elmer的详细信息,请参见http://elmer.sourceforge.net/)。你知道吗

我试图找出是否有一种方法来构造.elm文件,以便在elmer中使用指针参数或设置回调函数。你知道吗

在.elm文件中,而是检索如下值:

double  get(int id)

我可能想做一些类似的事情:

void  get(int id, double* val)

或者设置回调

void registerCallback(int id, void (*MyCb)(double value) )

只是澄清一下,这是在.elm文件中,该文件告诉elmer如何在dll的c代码中包装python函数,而不是在c或python源代码中。你知道吗


Tags: 文件函数代码idhttpget详细信息int
1条回答
网友
1楼 · 发布于 2024-05-29 04:49:19

在搜索了elmer源代码之后,我找到了如何进行回调。似乎没有任何方法可以传递指针(除了用于字符串类型的char*)。你知道吗

在.elm文件中,首先需要定义回调函数原型,前面是callback关键字。然后使用回调的名称作为.elm中函数原型的参数

#snipped from mytest.elm

#define callbacks types
callback int MyCb(int arg1, int arg2)

#function prototypes
int  register_callback(string someOtherArgs,  callback MyCb)

python代码将以任何其他参数的形式接收回调函数,并且可以自由地调用它,就好像它是一个带有声明参数的本机python函数一样。如果您想连续调用回调(像大多数事件处理程序一样),就必须在python代码中构建自己的循环机制。一个选项是循环,直到回调返回零;例如:

#snipped from mytest.py

def register_callback(someOtherArg, callbackFunc):
    cbArg1, cbArg2 = (1,2) #just some dummy values
    while(callbackFunc(cbArg1, cbArg2) == TRUE):
        print("I'm calling the callback...")
        time.sleep(.1)

要使用它,您的c/c++代码中应该有:

//snipped from mytest.c

//define the callback function.
//based on example python code, this would be called continuously until it
//returns 0
int MyAwesomeCallbackFunction(int arg1, int arg2) {/*definition goes here*/ return 1};

//register the callback and start the python based loop that calls MyAwesomeCallbackFunction
register_ui_callback("LadaDeDa", &myAwesomeCallbackFunction);

相关问题 更多 >

    热门问题