我想用cython将现有的c ++库移植到Python,使用模板的C++库.在这种情况下,它是adevs库.
问题是如何使用Cython将Python对象存储在C++容器中?对于引用计数问题,我知道这有点 气馁,但是可以这样做,如果是的话,怎么做?
我知道Gauthier Boaglios对类似问题的回答.但是,这并没有解决引用计数的问题,显然,我尝试了以下方法:
让我们说'cadevs.pxd'我有以下代码:
cdef extern from "adevs/adevs_digraph.h" namespace "adevs":
cdef cppclass PortValue[VALUE, PORT]:
PortValue() except +
PortValue(PORT port, const VALUE& value) except +
PORT port
VALUE value
Run Code Online (Sandbox Code Playgroud)
在'adevs.pyx'中:
from cpython.ref cimport PyObject
cimport cadevs
ctypedef PyObject* PythonObject
cdef class PortValue:
cdef cadevs.PortValue[PythonObject, PythonObject]* _c_portvalue
def __cinit__(self, object port, object value):
self._c_portvalue = new cadevs.PortValue[PythonObject, PythonObject](
<PyObject *>port, <PyObject *>value
)
def …Run Code Online (Sandbox Code Playgroud)