Skip to content

Commit 7dfdb7f

Browse files
committed
Initial draft implementation of the Cython support
1 parent 4f51e0a commit 7dfdb7f

13 files changed

Lines changed: 397 additions & 8 deletions

File tree

‎ElementsExamples/CMakeLists.txt‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,20 @@ elements_add_swig_binding(SwigExample ElementsExamples/SwigExample.i
211211
PUBLIC_HEADERS ElementsExamples)
212212
endif()
213213

214+
find_package(Cython QUIET)
215+
find_package(PythonLibs ${PYTHON_EXPLICIT_VERSION} QUIET)
216+
if(CYTHON_FOUND AND PYTHONLIBS_FOUND)
217+
elements_add_cython_module(PiCalculator src/Cython/lib/PiCalculator.pyx
218+
LINK_LIBRARIES ElementsExamples
219+
INCLUDE_DIRS ElementsExamples
220+
NO_PUBLIC_HEADERS)
221+
elements_add_cython_module(Rect src/Cython/lib/Rect.pyx
222+
LINK_LIBRARIES ElementsExamples
223+
INCLUDE_DIRS ElementsExamples/ElementsExamples ElementsExamples
224+
PUBLIC_HEADERS ElementsExamples)
225+
endif()
226+
227+
214228
#---Python modules, Scripts, conf and aux files ------------------------------------------------------------
215229

216230
elements_install_python_modules(TEST_TIMEOUT 30)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cdef extern from "ElementsExamples/Rectangle.h" namespace "Elements::Examples":
2+
cdef cppclass Rectangle:
3+
Rectangle(int, int, int, int)
4+
int x0, y0, x1, y1
5+
int getLength()
6+
int getHeight()
7+
int getArea()
8+
void move(int, int)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef ELEMENTS_EXAMPLES_PICALC_H__
2+
#define ELEMENTS_EXAMPLES_PICALC_H__
3+
4+
#include "ElementsKernel/Export.h"
5+
6+
namespace Elements {
7+
namespace Examples {
8+
9+
class ELEMENTS_API PiCalculator
10+
{
11+
public:
12+
void calculate(unsigned int terms);
13+
14+
typedef void (*show_result_callback_type)(double pi);
15+
void setShowResultCallback(show_result_callback_type f);
16+
17+
private:
18+
show_result_callback_type m_show_result_callback;
19+
};
20+
21+
#endif
22+
23+
} // namespace Examples
24+
} // namespace Elements
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cimport ElementsExamples.CRectangle
2+
from ElementsExamples.CRectangle cimport Rectangle as CRectangle
3+
4+
cdef class Rectangle:
5+
cdef ElementsExamples.CRectangle.Rectangle *thisptr # hold a C++ instance which we're wrapping
6+
cpdef getLength(self)
7+
cpdef getHeight(self)
8+
cpdef getArea(self)
9+
cpdef move(self, dx, dy)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef ELEMENTS_EXAMPLES_RECTANGLE_H__
2+
#define ELEMENTS_EXAMPLES_RECTANGLE_H__
3+
4+
#include "ElementsKernel/Export.h"
5+
6+
namespace Elements {
7+
namespace Examples {
8+
9+
class ELEMENTS_API Rectangle {
10+
public:
11+
int x0, y0, x1, y1;
12+
Rectangle(int x0, int y0, int x1, int y1);
13+
~Rectangle();
14+
int getLength();
15+
int getHeight();
16+
int getArea();
17+
void move(int dx, int dy);
18+
};
19+
20+
21+
} // namespace Examples
22+
} // namespace Elements
23+
24+
#endif

‎ElementsExamples/ElementsExamples/__init__.pxd‎

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cdef extern from "ElementsExamples/PiCalculator.h" namespace "Elements::Examples":
2+
cdef cppclass PiCalculator:
3+
void calculate(unsigned int terms) nogil
4+
void setShowResultCallback(void * callback)
5+
6+
cdef void show_result_callback(double pi) with gil:
7+
print(float(pi))
8+
9+
def print_pi(terms):
10+
cdef PiCalculator pi_calc
11+
pi_calc.setShowResultCallback(&show_result_callback)
12+
cdef unsigned int terms_int = terms
13+
with nogil:
14+
pi_calc.calculate(terms_int)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
cimport ElementsExamples.CRectangle
2+
3+
from ElementsExamples cimport CRectangle
4+
5+
cdef class Rectangle:
6+
7+
def __cinit__(self, int x0, int y0, int x1, int y1):
8+
# self.thisptr = new ElementsExamples.c_rectangle.Rectangle(x0, y0, x1, y1)
9+
self.thisptr = new CRectangle.Rectangle(x0, y0, x1, y1)
10+
11+
def __dealloc__(self):
12+
del self.thisptr
13+
14+
cpdef getLength(self):
15+
return self.thisptr.getLength()
16+
17+
cpdef getHeight(self):
18+
return self.thisptr.getHeight()
19+
20+
cpdef getArea(self):
21+
return self.thisptr.getArea()
22+
23+
cpdef move(self, dx, dy):
24+
self.thisptr.move(dx, dy)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "ElementsExamples/PiCalculator.h"
2+
3+
namespace Elements {
4+
namespace Examples {
5+
6+
void PiCalculator::calculate(unsigned int terms){
7+
double pi = 0.0;
8+
// Leibniz formula for pi
9+
double numerator = -1.0;
10+
double denominator = -1.0;
11+
for( unsigned int ii = 0; ii < terms; ++ii ) {
12+
numerator *= -1;
13+
denominator += 2.0;
14+
pi += numerator / denominator;
15+
}
16+
pi *= 4.0;
17+
m_show_result_callback(pi);
18+
}
19+
20+
void PiCalculator::setShowResultCallback(show_result_callback_type f){
21+
m_show_result_callback = f;
22+
}
23+
24+
25+
} // namespace Examples
26+
} // namespace Elements
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "ElementsExamples/Rectangle.h"
2+
3+
namespace Elements {
4+
namespace Examples {
5+
6+
Rectangle::Rectangle(int X0, int Y0, int X1, int Y1){
7+
x0 = X0;
8+
y0 = Y0;
9+
x1 = X1;
10+
y1 = Y1;
11+
}
12+
13+
Rectangle::~Rectangle(){
14+
}
15+
16+
int Rectangle::getLength(){
17+
return (x1 - x0);
18+
}
19+
20+
int Rectangle::getHeight(){
21+
return (y1 - y0);
22+
}
23+
24+
int Rectangle::getArea(){
25+
return (x1 - x0) * (y1 - y0);
26+
}
27+
28+
void Rectangle::move(int dx, int dy){
29+
x0 += dx;
30+
y0 += dy;
31+
x1 += dx;
32+
y1 += dy;
33+
}
34+
35+
} // namespace Examples
36+
} // namespace Elements

0 commit comments

Comments
 (0)