Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
36f00b4e
Commit
36f00b4e
authored
Jan 13, 2018
by
Myles Hollowed
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add a C++ extention example to the available code base
parent
42b2984a
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
4313 additions
and
0 deletions
+4313
-0
docs/examples/tutorial/rectangle_cpp/Rectangle.cpp
docs/examples/tutorial/rectangle_cpp/Rectangle.cpp
+40
-0
docs/examples/tutorial/rectangle_cpp/Rectangle.h
docs/examples/tutorial/rectangle_cpp/Rectangle.h
+17
-0
docs/examples/tutorial/rectangle_cpp/rect.cpp
docs/examples/tutorial/rectangle_cpp/rect.cpp
+4176
-0
docs/examples/tutorial/rectangle_cpp/rect.pyx
docs/examples/tutorial/rectangle_cpp/rect.pyx
+56
-0
docs/examples/tutorial/rectangle_cpp/setup.py
docs/examples/tutorial/rectangle_cpp/setup.py
+9
-0
docs/examples/tutorial/rectangle_cpp/test_import.py
docs/examples/tutorial/rectangle_cpp/test_import.py
+15
-0
No files found.
docs/examples/tutorial/rectangle_cpp/Rectangle.cpp
0 → 100644
View file @
36f00b4e
#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
;
}
}
docs/examples/tutorial/rectangle_cpp/Rectangle.h
0 → 100644
View file @
36f00b4e
#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 // RECTANGLE_H
docs/examples/tutorial/rectangle_cpp/rect.cpp
0 → 100644
View file @
36f00b4e
This source diff could not be displayed because it is too large. You can
view the blob
instead.
docs/examples/tutorial/rectangle_cpp/rect.pyx
0 → 100644
View file @
36f00b4e
# disutils: language = c++
# disutils: sources = Rectangle.cpp
# 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
)
# 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
)
# Attribute access
@
property
def
x0
(
self
):
return
self
.
c_rect
.
x0
@
x0
.
setter
def
x0
(
self
,
x0
):
self
.
c_rect
.
x0
=
x0
def
main
():
rec_ptr
=
new
Rectangle
(
1
,
2
,
3
,
4
)
# Instantiate a Rectangle object on the heap
try
:
recArea
=
rec_ptr
.
getArea
()
finally
:
del
rec_ptr
# delete heap allocated object
cdef
Rectangle
rec_stack
# Instantiate a Rectangle object on the stack
if
__name__
==
"__main__"
:
main
()
docs/examples/tutorial/rectangle_cpp/setup.py
0 → 100644
View file @
36f00b4e
from
distutils.core
import
setup
,
Extension
from
Cython.Build
import
cythonize
setup
(
ext_modules
=
cythonize
(
Extension
(
"rect"
,
# the extension name
sources
=
[
"rect.pyx"
,
"Rectangle.cpp"
],
# the Cython source and
# additional C++ source files
language
=
"c++"
,
# generate and compile C++ code
)))
docs/examples/tutorial/rectangle_cpp/test_import.py
0 → 100644
View file @
36f00b4e
#!/usr/bin/env python
# -*- coding: utf-8 -*-
try
:
import
rect
except
ImportError
:
print
(
"Module rect has not yet been built"
)
print
(
"Please run $python setup.py build_ext --inplace"
)
print
(
"Then try again"
)
x0
,
y0
,
x1
,
y1
=
1
,
2
,
3
,
4
rect_obj
=
rect
.
PyRectangle
(
x0
,
y0
,
x1
,
y1
)
print
(
dir
(
rect_obj
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment