cython包装cpp结构和带有结构数组的参数

时间:2019-02-12 15:20:23

标签: python c++ cython

我正在使用Cython在python中包装c ++库。不幸的是,我无权访问c ++库。因此,我必须找到某种方式包装lib API公开的结构和函数。

我的问题涉及包装c ++结构的最佳方法,并且;随后,如何在python中创建内存视图并将其指针(第一个元素地址)传递给参数为cpp结构数组的c ++函数。

例如,假设我有以下h文件:

//test.h

struct cxxTestData
{
   int m_id;
   double m_value;
};

void processData(cxxTestData* array_of_test_data, int isizeArr)

我的pyx文件如下所示

cdef extern from "Test.h":
    cdef struct cxxTestData:
        int m_id
        double m_value

cdef class pyTestData:
    cdef cxxTestData cstr

    def __init__(self, id, value):
        self.cstr.m_id = id
        self.cstr.m_value = value

    @property
    def ID(self):
        return self.cstr.m_id

    @property
    def Value(self):
        return self.cstr.m_value

现在,我想创建许多pyTestData并将它们存储在dtype对象的数组中。然后我想在cython / python函数中将此数组作为内存视图传递。

包装功能将具有以下签名

cpdef void pyProcessData(pyTestData[::1] test_data_arr)

我已经测试了上面的内容,并且编译成功。我还设法修改了每个结构的成员。但是,这不是我要实现的目标。我的问题是从这一点出发,我如何才能通过封装在每个pyTestData对象中的c ++结构(通过self.cstr)传递一个数组。

例如,请查看以下列表:

cpdef void pyProcessData(pyTestData[::1] test_data_arr):
    cdef int isize test_data_arr.shape[0]

    # here I want to create/insert an array of type cxxTestData to pass it 
    # to the cpp function
    # In other words, I want to create an array of [test_data_arr.cstr]
    # I guess I can use cxxTestData[::1] or cxxTestData* via malloc and
    # copy each test_data_arr[i].cstr to this new array
    cdef cxxTestData* testarray = <cxxTestData*>malloc(isize*sizeof(cxxTestData))

    cdef int i
    for i in range(isize):
        testarray[i] = test_data_arr[i].cstr
    processData(&testarray[0], isize)

    for i in range(isize):
        arrcntrs[i].pystr = testarray[i]

    free(testarray)

有人遇到过这种情况吗?有没有更好的方法可以在上述功能中传递我的python对象,而不必在内部复制cxx结构?

如果我做错了根本的事,请多谢并深表歉意。

1 个答案:

答案 0 :(得分:1)

由于您希望将cxxTestData的数组传递给C ++函数,因此最好的做法是将其分配为数组。一些未经测试的代码说明了该方法:

cdef class TestDataArray:
    cdef cxxTestData* array:
    def __init__(self, int length):
        self.array = <cxxTestData*>calloc(length,sizeof(cxxTestData))
    def __dealloc__(self):
        free(self.array)
    def __getitem__(self, int idx):
        return PyTestData.from_pointer(&self.array[idx],self) # see later
    def __setitem__(self, int idx, PyTestData pyobj): # this is optional
        self.array[idx] = deref(pyobj.cstr)

然后,您需要稍微修改PyTestData类,以使其保留一个指针而不是直接保留该类。它也应该有一个代表数据最终拥有者的字段(例如数组)。这样可以确保阵列保持活动状态,并且还可以考虑PyTestData拥有自己的数据的情况:

cdef class PyTestData:
    cdef cxxTestData* cstr
    cdef object owner

    def __init__(self, id, value):
        self.owner = None
        self.cstr = <cxxTestData*>malloc(sizeof(cxxTestData))
        self.cstr.m_id = id
        self.cstr.m_value = value

    def __dealloc__(self):
        if self.owner is None: # i.e. this class owns it
             free(self.cstr)

    @staticmethod
    cdef PyTestData from_pointer(cxxTestData* ptr, owner):
        # calling __new__ avoids calling the constructor
        cdef PyTestData x = PyTestData.__new__(PyTestData)
        x.owner = owner
        x.cstr = ptr
        return x

创建TestDataArray类时需要花费一些额外的精力,但是它以可直接从C ++使用的格式存储数据,因此我认为这是最好的解决方案。

相关问题