Cython: How to expose void* and function pointer in struct?

霸气de小男生 提交于 2019-12-06 06:15:33

It seems you can expose cb_func and cb_info doing something similar to this toy example:

import numpy as np
cimport numpy as np

ctypedef void (*f_type)(int, double*, double*)

ctypedef struct IntOptCP:
    int a
    double *b
    double *c
    f_type f

cdef class MyClass:
    cdef IntOptCP _iocp
    def exec_f(self):
        self._iocp.f(self._iocp.a, self._iocp.b, self._iocp.c)

cdef void myfunc(int a, double *b, double *c):
    cdef int i
    for i in range(a):
        b[i] += 1
        c[i] += 1

def main():
    cdef f_type f
    cdef np.ndarray[np.float64_t, ndim=1] b, c
    cdef int a
    a = 100
    b = np.zeros(a, dtype=np.float64)
    c = np.zeros(a, dtype=np.float64)
    test = MyClass()
    test._iocp.a = a
    test._iocp.b = &b[0]
    test._iocp.c = &c[0]
    test._iocp.f = myfunc
    print 'before', a, b, c
    test.exec_f()
    print 'after', a, b, c
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!