diff options
Diffstat (limited to 'docs/examples/userguide/wrapping_CPlusPlus')
13 files changed, 151 insertions, 145 deletions
diff --git a/docs/examples/userguide/wrapping_CPlusPlus/Rectangle.pxd b/docs/examples/userguide/wrapping_CPlusPlus/Rectangle.pxd index 68f949122..a26e69b51 100644 --- a/docs/examples/userguide/wrapping_CPlusPlus/Rectangle.pxd +++ b/docs/examples/userguide/wrapping_CPlusPlus/Rectangle.pxd @@ -1,7 +1,7 @@ cdef extern from "Rectangle.cpp":
pass
-# Decalre the class with cdef
+# Declare the class with cdef
cdef extern from "Rectangle.h" namespace "shapes":
cdef cppclass Rectangle:
Rectangle() except +
diff --git a/docs/examples/userguide/wrapping_CPlusPlus/cython_usage.pyx b/docs/examples/userguide/wrapping_CPlusPlus/cython_usage.pyx index 24192bf96..d074fa5ab 100644 --- a/docs/examples/userguide/wrapping_CPlusPlus/cython_usage.pyx +++ b/docs/examples/userguide/wrapping_CPlusPlus/cython_usage.pyx @@ -1,12 +1,12 @@ -# distutils: language = c++
-
-from Rectangle cimport Rectangle
-
-def main():
- rec_ptr = new Rectangle(1, 2, 3, 4) # Instantiate a Rectangle object on the heap
- try:
- rec_area = rec_ptr.getArea()
- finally:
- del rec_ptr # delete heap allocated object
-
- cdef Rectangle rec_stack # Instantiate a Rectangle object on the stack
+# distutils: language = c++ + +from Rectangle cimport Rectangle + +def main(): + rec_ptr = new Rectangle(1, 2, 3, 4) # Instantiate a Rectangle object on the heap + try: + rec_area = rec_ptr.getArea() + finally: + del rec_ptr # delete heap allocated object + + cdef Rectangle rec_stack # Instantiate a Rectangle object on the stack diff --git a/docs/examples/userguide/wrapping_CPlusPlus/function_templates.pyx b/docs/examples/userguide/wrapping_CPlusPlus/function_templates.pyx index 13c75426e..35d064fdd 100644 --- a/docs/examples/userguide/wrapping_CPlusPlus/function_templates.pyx +++ b/docs/examples/userguide/wrapping_CPlusPlus/function_templates.pyx @@ -1,7 +1,7 @@ -# distutils: language = c++
-
-cdef extern from "<algorithm>" namespace "std":
- T max[T](T a, T b)
-
-print(max[long](3, 4))
-print(max(1.5, 2.5)) # simple template argument deduction
+# distutils: language = c++ + +cdef extern from "<algorithm>" namespace "std": + T max[T](T a, T b) + +print(max[long](3, 4)) +print(max(1.5, 2.5)) # simple template argument deduction diff --git a/docs/examples/userguide/wrapping_CPlusPlus/iterate.pyx b/docs/examples/userguide/wrapping_CPlusPlus/iterate.pyx index ea0007e6a..cdce8910f 100644 --- a/docs/examples/userguide/wrapping_CPlusPlus/iterate.pyx +++ b/docs/examples/userguide/wrapping_CPlusPlus/iterate.pyx @@ -1,12 +1,12 @@ -# distutils: language = c++
-
-from libcpp.vector cimport vector
-
-def main():
- cdef vector[int] v = [4, 6, 5, 10, 3]
-
- cdef int value
- for value in v:
- print(value)
-
- return [x*x for x in v if x % 2 == 0]
+# distutils: language = c++ + +from libcpp.vector cimport vector + +def main(): + cdef vector[int] v = [4, 6, 5, 10, 3] + + cdef int value + for value in v: + print(value) + + return [x*x for x in v if x % 2 == 0] diff --git a/docs/examples/userguide/wrapping_CPlusPlus/nested_class.pyx b/docs/examples/userguide/wrapping_CPlusPlus/nested_class.pyx index e53f39b98..c5c764468 100644 --- a/docs/examples/userguide/wrapping_CPlusPlus/nested_class.pyx +++ b/docs/examples/userguide/wrapping_CPlusPlus/nested_class.pyx @@ -1,17 +1,17 @@ -# distutils: language = c++
-
-cdef extern from "<vector>" namespace "std":
- cdef cppclass vector[T]:
- cppclass iterator:
- T operator*()
- iterator operator++()
- bint operator==(iterator)
- bint operator!=(iterator)
- vector()
- void push_back(T&)
- T& operator[](int)
- T& at(int)
- iterator begin()
- iterator end()
-
-cdef vector[int].iterator iter #iter is declared as being of type vector<int>::iterator
+# distutils: language = c++ + +cdef extern from "<vector>" namespace "std": + cdef cppclass vector[T]: + cppclass iterator: + T operator*() + iterator operator++() + bint operator==(iterator) + bint operator!=(iterator) + vector() + void push_back(T&) + T& operator[](int) + T& at(int) + iterator begin() + iterator end() + +cdef vector[int].iterator iter #iter is declared as being of type vector<int>::iterator diff --git a/docs/examples/userguide/wrapping_CPlusPlus/python_to_cpp.pyx b/docs/examples/userguide/wrapping_CPlusPlus/python_to_cpp.pyx index 30bdb7bcb..b4be72c16 100644 --- a/docs/examples/userguide/wrapping_CPlusPlus/python_to_cpp.pyx +++ b/docs/examples/userguide/wrapping_CPlusPlus/python_to_cpp.pyx @@ -1,19 +1,19 @@ -# distutils: language = c++
-
-from libcpp.string cimport string
-from libcpp.vector cimport vector
-
-py_bytes_object = b'The knights who say ni'
-py_unicode_object = u'Those who hear them seldom live to tell the tale.'
-
-cdef string s = py_bytes_object
-print(s) # b'The knights who say ni'
-
-cdef string cpp_string = <string> py_unicode_object.encode('utf-8')
-print(cpp_string) # b'Those who hear them seldom live to tell the tale.'
-
-cdef vector[int] vect = range(1, 10, 2)
-print(vect) # [1, 3, 5, 7, 9]
-
-cdef vector[string] cpp_strings = b'It is a good shrubbery'.split()
-print(cpp_strings[1]) # b'is'
+# distutils: language = c++ + +from libcpp.string cimport string +from libcpp.vector cimport vector + +py_bytes_object = b'The knights who say ni' +py_unicode_object = u'Those who hear them seldom live to tell the tale.' + +cdef string s = py_bytes_object +print(s) # b'The knights who say ni' + +cdef string cpp_string = <string> py_unicode_object.encode('utf-8') +print(cpp_string) # b'Those who hear them seldom live to tell the tale.' + +cdef vector[int] vect = range(1, 10, 2) +print(vect) # [1, 3, 5, 7, 9] + +cdef vector[string] cpp_strings = b'It is a good shrubbery'.split() +print(cpp_strings[1]) # b'is' diff --git a/docs/examples/userguide/wrapping_CPlusPlus/rect.pyx b/docs/examples/userguide/wrapping_CPlusPlus/rect.pyx index e7c4423ef..d8eec16ef 100644 --- a/docs/examples/userguide/wrapping_CPlusPlus/rect.pyx +++ b/docs/examples/userguide/wrapping_CPlusPlus/rect.pyx @@ -8,7 +8,7 @@ from Rectangle cimport Rectangle cdef class PyRectangle: cdef Rectangle c_rect # Hold a C++ instance which we're wrapping - def __cinit__(self, int x0, int y0, int x1, int y1): + def __init__(self, int x0, int y0, int x1, int y1): self.c_rect = Rectangle(x0, y0, x1, y1) def get_area(self): diff --git a/docs/examples/userguide/wrapping_CPlusPlus/rect_ptr.pyx b/docs/examples/userguide/wrapping_CPlusPlus/rect_ptr.pyx index 508e55dc6..ec4b34ab4 100644 --- a/docs/examples/userguide/wrapping_CPlusPlus/rect_ptr.pyx +++ b/docs/examples/userguide/wrapping_CPlusPlus/rect_ptr.pyx @@ -1,12 +1,18 @@ -# distutils: language = c++
-
-from Rectangle cimport Rectangle
-
-cdef class PyRectangle:
- cdef Rectangle*c_rect # hold a pointer to the C++ instance which we're wrapping
-
- def __cinit__(self, int x0, int y0, int x1, int y1):
- self.c_rect = new Rectangle(x0, y0, x1, y1)
-
- def __dealloc__(self):
- del self.c_rect
+# distutils: language = c++ + +from Rectangle cimport Rectangle + +cdef class PyRectangle: + cdef Rectangle*c_rect # hold a pointer to the C++ instance which we're wrapping + + def __cinit__(self): + self.c_rect = new Rectangle() + + def __init__(self, int x0, int y0, int x1, int y1): + self.c_rect.x0 = x0 + self.c_rect.y0 = y0 + self.c_rect.x1 = x1 + self.c_rect.y1 = y1 + + def __dealloc__(self): + del self.c_rect diff --git a/docs/examples/userguide/wrapping_CPlusPlus/rect_with_attributes.pyx b/docs/examples/userguide/wrapping_CPlusPlus/rect_with_attributes.pyx index 1bac30dec..441292ace 100644 --- a/docs/examples/userguide/wrapping_CPlusPlus/rect_with_attributes.pyx +++ b/docs/examples/userguide/wrapping_CPlusPlus/rect_with_attributes.pyx @@ -5,7 +5,7 @@ from Rectangle cimport Rectangle cdef class PyRectangle: cdef Rectangle c_rect - def __cinit__(self, int x0, int y0, int x1, int y1): + def __init__(self, int x0, int y0, int x1, int y1): self.c_rect = Rectangle(x0, y0, x1, y1) def get_area(self): diff --git a/docs/examples/userguide/wrapping_CPlusPlus/setup.py b/docs/examples/userguide/wrapping_CPlusPlus/setup.py index 0c89865d6..09009d28d 100644 --- a/docs/examples/userguide/wrapping_CPlusPlus/setup.py +++ b/docs/examples/userguide/wrapping_CPlusPlus/setup.py @@ -1,4 +1,4 @@ -from distutils.core import setup +from setuptools import setup from Cython.Build import cythonize diff --git a/docs/examples/userguide/wrapping_CPlusPlus/templates.pyx b/docs/examples/userguide/wrapping_CPlusPlus/templates.pyx index 8e7383ca2..4ff232b82 100644 --- a/docs/examples/userguide/wrapping_CPlusPlus/templates.pyx +++ b/docs/examples/userguide/wrapping_CPlusPlus/templates.pyx @@ -1,30 +1,30 @@ -# distutils: language = c++
-
-# import dereference and increment operators
-from cython.operator cimport dereference as deref, preincrement as inc
-
-cdef extern from "<vector>" namespace "std":
- cdef cppclass vector[T]:
- cppclass iterator:
- T operator*()
- iterator operator++()
- bint operator==(iterator)
- bint operator!=(iterator)
- vector()
- void push_back(T&)
- T& operator[](int)
- T& at(int)
- iterator begin()
- iterator end()
-
-cdef vector[int] *v = new vector[int]()
-cdef int i
-for i in range(10):
- v.push_back(i)
-
-cdef vector[int].iterator it = v.begin()
-while it != v.end():
- print(deref(it))
- inc(it)
-
-del v
+# distutils: language = c++ + +# import dereference and increment operators +from cython.operator cimport dereference as deref, preincrement as inc + +cdef extern from "<vector>" namespace "std": + cdef cppclass vector[T]: + cppclass iterator: + T operator*() + iterator operator++() + bint operator==(iterator) + bint operator!=(iterator) + vector() + void push_back(T&) + T& operator[](int) + T& at(int) + iterator begin() + iterator end() + +cdef vector[int] *v = new vector[int]() +cdef int i +for i in range(10): + v.push_back(i) + +cdef vector[int].iterator it = v.begin() +while it != v.end(): + print(deref(it)) + inc(it) + +del v diff --git a/docs/examples/userguide/wrapping_CPlusPlus/vector_demo.pyx b/docs/examples/userguide/wrapping_CPlusPlus/vector_demo.pyx index d7fdfc969..f1697e1ec 100644 --- a/docs/examples/userguide/wrapping_CPlusPlus/vector_demo.pyx +++ b/docs/examples/userguide/wrapping_CPlusPlus/vector_demo.pyx @@ -1,15 +1,15 @@ -# distutils: language = c++
-
-from libcpp.vector cimport vector
-
-cdef vector[int] vect
-cdef int i, x
-
-for i in range(10):
- vect.push_back(i)
-
-for i in range(10):
- print(vect[i])
-
-for x in vect:
- print(x)
+# distutils: language = c++ + +from libcpp.vector cimport vector + +cdef vector[int] vect +cdef int i, x + +for i in range(10): + vect.push_back(i) + +for i in range(10): + print(vect[i]) + +for x in vect: + print(x) diff --git a/docs/examples/userguide/wrapping_CPlusPlus/wrapper_vector.pyx b/docs/examples/userguide/wrapping_CPlusPlus/wrapper_vector.pyx index 592e83ad9..4cdf12fc2 100644 --- a/docs/examples/userguide/wrapping_CPlusPlus/wrapper_vector.pyx +++ b/docs/examples/userguide/wrapping_CPlusPlus/wrapper_vector.pyx @@ -1,17 +1,17 @@ -# 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
+# 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 |