Passing C++ pointer as argument into Cython function

后端 未结 4 2080
萌比男神i
萌比男神i 2021-02-04 00:36
cdef extern from \"Foo.h\":
    cdef cppclass Bar:
        pass

cdef class PyClass:
    cdef Bar *bar

    def __cinit__(self, Bar *b)
        bar = b

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 01:26

    For each cdef class create a global cdef function that acts as a constructor, CefResponse is a C++ object, PyResponse a python equivalent of a c++ object:

    cdef object CreatePyResponse(CefRefPtr[CefResponse] cefResponse):
    
        pyResponse = PyResponse()
        pyResponse.cefResponse = cefResponse
        return pyResponse
    
    cdef class PyResponse:
    
        cdef CefRefPtr[CefResponse] cefResponse
    
        def GetStatus(self):
    
            return ((self.cefResponse.get())).GetStatus()
    

    So instead of resp = PyResponse(cppObject) call resp = CreatePyResponse(cppObject).

    Example taken from CEF Python: https://code.google.com/p/cefpython/source/browse/cefpython/response.pyx?r=0250b65e046a

提交回复
热议问题