Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2324 from gabrieldemarmiesse/update_cpp_examples
Update cpp examples
- Loading branch information
Showing
9 changed files
with
319 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <iostream> | ||
#include "Rectangle.h" | ||
|
||
namespace shapes { | ||
|
||
// Default constructor | ||
Rectangle::Rectangle () {} | ||
|
||
// Overloaded constructor | ||
Rectangle::Rectangle (int x0, int y0, int x1, int y1) { | ||
this->x0 = x0; | ||
this->y0 = y0; | ||
this->x1 = x1; | ||
this->y1 = y1; | ||
} | ||
|
||
// Destructor | ||
Rectangle::~Rectangle () {} | ||
|
||
// Return the area of the rectangle | ||
int Rectangle::getArea () { | ||
return (this->x1 - this->x0) * (this->y1 - this->y0); | ||
} | ||
|
||
// Get the size of the rectangle. | ||
// Put the size in the pointer args | ||
void Rectangle::getSize (int *width, int *height) { | ||
(*width) = x1 - x0; | ||
(*height) = y1 - y0; | ||
} | ||
|
||
// Move the rectangle by dx dy | ||
void Rectangle::move (int dx, int dy) { | ||
this->x0 += dx; | ||
this->y0 += dy; | ||
this->x1 += dx; | ||
this->y1 += dy; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef RECTANGLE_H | ||
#define RECTANGLE_H | ||
|
||
namespace shapes { | ||
class Rectangle { | ||
public: | ||
int x0, y0, x1, y1; | ||
Rectangle(); | ||
Rectangle(int x0, int y0, int x1, int y1); | ||
~Rectangle(); | ||
int getArea(); | ||
void getSize(int* width, int* height); | ||
void move(int dx, int dy); | ||
}; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
cdef extern from "Rectangle.cpp": | ||
pass | ||
|
||
# Decalre the class with cdef | ||
cdef extern from "Rectangle.h" namespace "shapes": | ||
cdef cppclass Rectangle: | ||
Rectangle() except + | ||
Rectangle(int, int, int, int) except + | ||
int x0, y0, x1, y1 | ||
int getArea() | ||
void getSize(int*width, int*height) | ||
void move(int, int) |
12 changes: 12 additions & 0 deletions
12
docs/examples/userguide/wrapping_CPlusPlus/cython_usage.pyx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# distutils: language = c++ | ||
|
||
from Rectangle cimport Rectangle | ||
|
||
# Create a Cython extension type which holds a C++ instance | ||
# as an attribute and create a bunch of forwarding methods | ||
# Python extension type. | ||
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): | ||
self.c_rect = Rectangle(x0, y0, x1, y1) | ||
|
||
def get_area(self): | ||
return self.c_rect.getArea() | ||
|
||
def get_size(self): | ||
cdef int width, height | ||
self.c_rect.getSize(&width, &height) | ||
return width, height | ||
|
||
def move(self, dx, dy): | ||
self.c_rect.move(dx, dy) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# 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 |
52 changes: 52 additions & 0 deletions
52
docs/examples/userguide/wrapping_CPlusPlus/rect_with_attributes.pyx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# distutils: language = c++ | ||
|
||
from Rectangle cimport Rectangle | ||
|
||
cdef class PyRectangle: | ||
cdef Rectangle c_rect | ||
|
||
def __cinit__(self, int x0, int y0, int x1, int y1): | ||
self.c_rect = Rectangle(x0, y0, x1, y1) | ||
|
||
def get_area(self): | ||
return self.c_rect.getArea() | ||
|
||
def get_size(self): | ||
cdef int width, height | ||
self.c_rect.getSize(&width, &height) | ||
return width, height | ||
|
||
def move(self, dx, dy): | ||
self.c_rect.move(dx, dy) | ||
|
||
# Attribute access | ||
@property | ||
def x0(self): | ||
return self.c_rect.x0 | ||
@x0.setter | ||
def x0(self, x0): | ||
self.c_rect.x0 = x0 | ||
|
||
# Attribute access | ||
@property | ||
def x1(self): | ||
return self.c_rect.x1 | ||
@x1.setter | ||
def x1(self, x1): | ||
self.c_rect.x1 = x1 | ||
|
||
# Attribute access | ||
@property | ||
def y0(self): | ||
return self.c_rect.y0 | ||
@y0.setter | ||
def y0(self, y0): | ||
self.c_rect.y0 = y0 | ||
|
||
# Attribute access | ||
@property | ||
def y1(self): | ||
return self.c_rect.y1 | ||
@y1.setter | ||
def y1(self, y1): | ||
self.c_rect.y1 = y1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from distutils.core import setup | ||
|
||
from Cython.Build import cythonize | ||
|
||
setup(ext_modules=cythonize("rect.pyx")) |
Oops, something went wrong.