我一直在使用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) )
为了清楚起见,这是在告诉elmer如何在dll的c代码中包装python函数的.elm文件中,而不是在c或python源代码中。
发布于 2014-09-09 23:53:28
在搜索了elmer源代码之后,我想出了如何做回调。似乎没有任何方法可以传递指针(除了用于字符串类型的char *)。
在.elm文件中,首先需要定义回调函数原型,前面加上回调关键字。然后使用该回调的名称作为.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);
https://stackoverflow.com/questions/25021587
复制相似问题