Commit 4d7de983 authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2448 from gabrieldemarmiesse/test_wrapping_cplusplus_7

Added tests for "Using C++ in Cython" part 7
parents 09e9b502 70c5dca1
# distutils: language = c++
from libcpp.vector cimport vector
cdef class VectorStack:
cdef vector[int] v
def push(self, x):
self.v.push_back(x)
def pop(self):
if self.v.empty():
raise IndexError()
x = self.v.back()
self.v.pop_back()
return x
...@@ -438,20 +438,9 @@ Simplified wrapping with default constructor ...@@ -438,20 +438,9 @@ Simplified wrapping with default constructor
If your extension type instantiates a wrapped C++ class using the default If your extension type instantiates a wrapped C++ class using the default
constructor (not passing any arguments), you may be able to simplify the constructor (not passing any arguments), you may be able to simplify the
lifecycle handling by tying it directly to the lifetime of the Python wrapper lifecycle handling by tying it directly to the lifetime of the Python wrapper
object. Instead of a pointer attribute, you can declare an instance:: object. Instead of a pointer attribute, you can declare an instance:
cdef class VectorStack: .. literalinclude:: ../../examples/userguide/wrapping_CPlusPlus/wrapper_vector.pyx
cdef vector[int] v
def push(self, x):
self.v.push_back(x)
def pop(self):
if self.v.empty():
raise IndexError()
x = self.v.back()
self.v.pop_back()
return x
Cython will automatically generate code that instantiates the C++ object Cython will automatically generate code that instantiates the C++ object
instance when the Python object is created and deletes it when the Python instance when the Python object is created and deletes it when the Python
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment