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
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cython
Commits
d8cea9ae
Commit
d8cea9ae
authored
Jul 18, 2017
by
Robert Bradshaw
Committed by
GitHub
Jul 18, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1588 from tadeu/add-std-function
Add basic support for std::function
parents
19d890ec
954f0ecd
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
204 additions
and
0 deletions
+204
-0
Cython/Includes/libcpp/functional.pxd
Cython/Includes/libcpp/functional.pxd
+13
-0
tests/run/cpp_function_lib.cpp
tests/run/cpp_function_lib.cpp
+50
-0
tests/run/cpp_function_lib.h
tests/run/cpp_function_lib.h
+35
-0
tests/run/cpp_function_lib.pxd
tests/run/cpp_function_lib.pxd
+19
-0
tests/run/cpp_stl_function.pyx
tests/run/cpp_stl_function.pyx
+87
-0
No files found.
Cython/Includes/libcpp/functional.pxd
0 → 100644
View file @
d8cea9ae
cdef
extern
from
"<functional>"
namespace
"std"
nogil
:
cdef
cppclass
function
[
T
]:
function
()
except
+
function
(
T
*
)
except
+
function
(
function
&
)
except
+
function
(
void
*
)
except
+
function
operator
=
(
T
*
)
function
operator
=
(
function
&
)
function
operator
=
(
void
*
)
function
operator
=
[
U
](
U
)
bint
operator
bool
()
tests/run/cpp_function_lib.cpp
0 → 100644
View file @
d8cea9ae
#include "cpp_function_lib.h"
double
add_one
(
double
a
,
int
b
)
{
return
a
+
(
double
)
b
+
1.0
;
}
double
add_two
(
double
a
,
int
b
)
{
return
a
+
(
double
)
b
+
2.0
;
}
AddAnotherFunctor
::
AddAnotherFunctor
(
double
to_add
)
:
to_add
(
to_add
)
{
}
double
AddAnotherFunctor
::
operator
()(
double
a
,
int
b
)
const
{
return
a
+
(
double
)
b
+
this
->
to_add
;
};
FunctionKeeper
::
FunctionKeeper
(
std
::
function
<
double
(
double
,
int
)
>
user_function
)
:
my_function
(
user_function
)
{
}
FunctionKeeper
::~
FunctionKeeper
()
{
}
void
FunctionKeeper
::
set_function
(
std
::
function
<
double
(
double
,
int
)
>
user_function
)
{
this
->
my_function
=
user_function
;
}
std
::
function
<
double
(
double
,
int
)
>
FunctionKeeper
::
get_function
()
const
{
return
this
->
my_function
;
}
double
FunctionKeeper
::
call_function
(
double
a
,
int
b
)
const
{
if
(
!
this
->
my_function
)
{
throw
std
::
runtime_error
(
"Trying to call undefined function!"
);
}
return
this
->
my_function
(
a
,
b
);
};
tests/run/cpp_function_lib.h
0 → 100644
View file @
d8cea9ae
#ifndef CPP_FUNCTION_LIB_H
#define CPP_FUNCTION_LIB_H
#include <functional>
// Functions, functor and a holder of std::function used by cpp_stl_function.pyx tests.
double
add_one
(
double
a
,
int
b
);
double
add_two
(
double
a
,
int
b
);
class
AddAnotherFunctor
{
double
to_add
;
public:
AddAnotherFunctor
(
double
to_add
);
double
operator
()(
double
a
,
int
b
)
const
;
};
class
FunctionKeeper
{
std
::
function
<
double
(
double
,
int
)
>
my_function
;
public:
FunctionKeeper
(
std
::
function
<
double
(
double
,
int
)
>
user_function
);
virtual
~
FunctionKeeper
();
void
set_function
(
std
::
function
<
double
(
double
,
int
)
>
user_function
);
std
::
function
<
double
(
double
,
int
)
>
get_function
()
const
;
double
call_function
(
double
a
,
int
b
)
const
;
};
#endif
tests/run/cpp_function_lib.pxd
0 → 100644
View file @
d8cea9ae
from
libcpp.functional
cimport
function
cdef
extern
from
"cpp_function_lib.cpp"
:
# CPP is include here so that it doesn't need to be compiled externally
pass
cdef
extern
from
"cpp_function_lib.h"
:
double
add_one
(
double
,
int
)
double
add_two
(
double
a
,
int
b
)
cdef
cppclass
AddAnotherFunctor
:
AddAnotherFunctor
(
double
to_add
)
double
call
"operator()"
(
double
a
,
int
b
)
cdef
cppclass
FunctionKeeper
:
FunctionKeeper
(
function
[
double
(
double
,
int
)]
user_function
)
void
set_function
(
function
[
double
(
double
,
int
)]
user_function
)
function
[
double
(
double
,
int
)]
get_function
()
double
call_function
(
double
a
,
int
b
)
except
+
tests/run/cpp_stl_function.pyx
0 → 100644
View file @
d8cea9ae
# distutils: extra_compile_args=-std=c++0x
# mode: run
# tag: cpp
from
libcpp.functional
cimport
function
cimport
cpp_function_lib
def
test_simple_function
():
'''
>>> test_simple_function()
6.0
'''
return
cpp_function_lib
.
add_one
(
2.0
,
3
)
def
test_AddAnotherFunctor
(
n
):
'''
>>> test_AddAnotherFunctor(5.0)
10.0
'''
return
cpp_function_lib
.
AddAnotherFunctor
(
5.0
).
call
(
2.0
,
3
)
cdef
class
FunctionKeeper
:
"""
>>> fk = FunctionKeeper('add_one')
>>> fk(2.0, 3)
6.0
>>> fk = FunctionKeeper('add_two')
>>> fk(2.0, 3)
7.0
>>> fk = FunctionKeeper('AddAnotherFunctor5')
>>> fk(2.0, 3)
10.0
>>> fk = FunctionKeeper('default')
>>> bool(fk)
False
>>> fk(2.0, 3)
Traceback (most recent call last):
...
RuntimeError: Trying to call undefined function!
>>> fk.set_function('AddAnotherFunctor5')
>>> fk(2.0, 3)
10.0
>>> bool(fk)
True
>>> fk.set_function('NULL')
>>> bool(fk)
False
"""
cdef
cpp_function_lib
.
FunctionKeeper
*
function_keeper
cdef
function
[
double
(
double
,
int
)]
*
_get_function_ptr_from_name
(
self
,
function_name
):
cdef
function
[
double
(
double
,
int
)]
*
f
if
function_name
==
'add_one'
:
f
=
new
function
[
double
(
double
,
int
)](
cpp_function_lib
.
add_one
)
elif
function_name
==
'add_two'
:
f
=
new
function
[
double
(
double
,
int
)](
cpp_function_lib
.
add_two
)
elif
function_name
==
'AddAnotherFunctor5'
:
f
=
new
function
[
double
(
double
,
int
)]()
f
[
0
]
=
cpp_function_lib
.
AddAnotherFunctor
(
5.0
)
elif
function_name
==
'NULL'
:
f
=
new
function
[
double
(
double
,
int
)](
NULL
)
elif
function_name
==
'default'
:
f
=
new
function
[
double
(
double
,
int
)]()
return
f
def
__cinit__
(
self
,
function_name
):
cdef
function
[
double
(
double
,
int
)]
*
f
=
self
.
_get_function_ptr_from_name
(
function_name
)
self
.
function_keeper
=
new
cpp_function_lib
.
FunctionKeeper
(
f
[
0
])
del
f
def
__dealloc__
(
self
):
del
self
.
function_keeper
def
__call__
(
self
,
a
,
b
):
return
self
.
function_keeper
.
call_function
(
a
,
b
)
def
__bool__
(
self
):
return
<
bint
>
self
.
function_keeper
.
get_function
()
def
set_function
(
self
,
function_name
):
cdef
function
[
double
(
double
,
int
)]
*
f
=
self
.
_get_function_ptr_from_name
(
function_name
)
self
.
function_keeper
.
set_function
(
f
[
0
])
del
f
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