Using C++ in Cython nie wspomina nic szczególnego:
#file: pya.pyx
cdef extern from "a.h":
cdef cppclass A:
int a
cdef cppclass B(A):
int b
klasa Wrapper:
#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
Przykład:
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
Aby zbudować trzeba instruować pyximport użyć C++:
#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])
Mogę po prostu użyć cppclass dla struct? Jeśli tak, wygląda na to, że mogę zrobić dziedziczenie klasowe, a to powinno rozwiązać mój problem: http://wiki.cython.org/gsoc09/daniloaf/progress#Inheritance – colinmarc
@colinmarc: Próbowałem go na cytoncie 0.15 i Prace; dokumenty mogą opisywać starszą wersję. W dużym 'struct {..};' jest równoważne 'class {public: ..};' w C++. – jfs
dzięki za pomoc! – colinmarc