2012-03-20 5 views
6

cython으로 C++ 라이브러리를 래핑했습니다. 헤더 파일에서 다른 구조체에서 상속 일부 구조체는과 같이,이 있습니다 :Cython의 C++ Struct 상속

struct A { 
    int a; 
}; 
struct B : A { 
    int b; 
}; 

어떻게해야 내 cdef extern... 블록의 표정이?

답변

5

Using C++ in Cython 특별한 아무것도 언급하지 않습니다

#file: pya.pyx 
cdef extern from "a.h": 
    cdef cppclass A: 
     int a 
    cdef cppclass B(A): 
     int b 

래퍼 클래스 :

#file: pya.pyx 
cdef class PyB: 
    cdef B* thisptr 
    def __cinit__(self): 
     self.thisptr = new B(); 
    def __dealloc__(self): 
     del self.thisptr 
    property a: 
     def __get__(self): return self.thisptr.a 
     def __set__(self, int a): self.thisptr.a = a 
    property b: 
     def __get__(self): return self.thisptr.b 
     def __set__(self, int b): self.thisptr.b = b 

예 :

import pyximport; pyximport.install(); # pip install cython 

from pya import PyB 

o = PyB() 
assert o.a == 0 and o.b == 0 
o.a = 1; o.b = 2 
assert o.a == 1 and o.b == 2 

당신이 C++를 사용하는 pyximport을 지시 할 필요를 빌드하려면 :

#file: pya.pyxbld 
import os 
from distutils.extension import Extension 

dirname = os.path.dirname(__file__) 

def make_ext(modname, pyxfilename): 
    return Extension(name=modname, 
        sources=[pyxfilename, "a.cpp"], 
        language="c++", 
        include_dirs=[dirname]) 
+0

구조체에 cppclass를 사용할 수 있습니까? 그렇다면 클래스 상속을 할 수있는 것처럼 보입니다. 내 문제를 해결해야합니다. http://wiki.cython.org/gsoc09/daniloaf/progress#Inheritance – colinmarc

+0

@colinmarc : 저는 cython 0.15에서 시도해 보았습니다. 공장; 문서는 이전 버전을 나타낼 수 있습니다. 큰'struct {..};는 C++에서'class {public : ..};와 같습니다. – jfs

+0

도와 주셔서 감사합니다! – colinmarc