cython包装cpp结构和参数为结构数组的函数

2024-09-30 02:18:18 发布

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

我正在使用Cython用python包装一个c++库。不幸的是,我没有访问c++库的权限。因此,我必须找到一种方法来包装libapi公开的结构和函数。你知道吗

我的问题是包装一个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)

我已经测试了上面的代码,编译成功了。我还设法修改了每个结构的成员。然而,这不是我想要达到的。我的问题是如何从这一点上我可以传递一个数组与c++结构封装在每个pyTestData对象(通过自身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结构?你知道吗

如果我做了一些根本错误的事,我会提前表示感谢和歉意。你知道吗


Tags: to函数testselfiddatavalue结构
1条回答
网友
1楼 · 发布于 2024-09-30 02:18:18

既然你想要一个数组^ {< CD1>}传递给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
<>在创建^ {< CD4>}类时有一点额外的努力,但是它以一种直接从C++中使用的格式存储数据,因此我认为这是最好的解决方案。你知道吗

相关问题 更多 >

    热门问题