Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
1e3be73a
Commit
1e3be73a
authored
May 24, 2016
by
Wenzel Jakob
Browse files
PYBIND11_OVERLOAD_NAME and PYBIND11_OVERLOAD_PURE_NAME (fixes #205)
parent
b4378673
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
24 additions
and
12 deletions
+24
-12
docs/advanced.rst
docs/advanced.rst
+8
-2
example/example12.ref
example/example12.ref
+1
-1
example/issues.ref
example/issues.ref
+1
-1
include/pybind11/pybind11.h
include/pybind11/pybind11.h
+14
-8
No files found.
docs/advanced.rst
View file @
1e3be73a
...
@@ -268,8 +268,14 @@ helper class that is defined as follows:
...
@@ -268,8 +268,14 @@ helper class that is defined as follows:
The macro :func:`PYBIND11_OVERLOAD_PURE` should be used for pure virtual
The macro :func:`PYBIND11_OVERLOAD_PURE` should be used for pure virtual
functions, and :func:`PYBIND11_OVERLOAD` should be used for functions which have
functions, and :func:`PYBIND11_OVERLOAD` should be used for functions which have
a default implementation. The binding code also needs a few minor adaptations
a default implementation.
(highlighted):
There are also two alternate macros :func:`PYBIND11_OVERLOAD_PURE_NAME` and
:func:`PYBIND11_OVERLOAD_NAME` which take a string-valued name argument
after the *Name of the function* slot. This is useful when the C++ and Python
versions of the function have different names, e.g. ``operator()`` vs ``__call__``.
The binding code also needs a few minor adaptations (highlighted):
.. code-block:: cpp
.. code-block:: cpp
:emphasize-lines: 4,6,7
:emphasize-lines: 4,6,7
...
...
example/example12.ref
View file @
1e3be73a
Constructing Example12..
Constructing Example12..
Original implementation of Example12::run(state=10, value=20)
Original implementation of Example12::run(state=10, value=20)
30
30
Caught expected exception: Tried to call pure virtual function "pure_virtual"
Caught expected exception: Tried to call pure virtual function "
Example12::
pure_virtual"
Constructing Example12..
Constructing Example12..
ExtendedExample12::run(20), calling parent..
ExtendedExample12::run(20), calling parent..
Original implementation of Example12::run(state=11, value=21)
Original implementation of Example12::run(state=11, value=21)
...
...
example/issues.ref
View file @
1e3be73a
const char *
const char *
c
c
Failed as expected: Tried to call pure virtual function "dispatch"
Failed as expected: Tried to call pure virtual function "
Base::
dispatch"
Yay..
Yay..
[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]
[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]
[3, 5, 7, 9, 11, 13, 15]
[3, 5, 7, 9, 11, 13, 15]
...
...
include/pybind11/pybind11.h
View file @
1e3be73a
...
@@ -1184,19 +1184,25 @@ inline function get_overload(const void *this_ptr, const char *name) {
...
@@ -1184,19 +1184,25 @@ inline function get_overload(const void *this_ptr, const char *name) {
return
overload
;
return
overload
;
}
}
#define PYBIND11_OVERLOAD_INT(ret_type,
class_name,
name, ...) { \
#define PYBIND11_OVERLOAD_INT(ret_type, name, ...) { \
pybind11::gil_scoped_acquire gil; \
pybind11::gil_scoped_acquire gil; \
pybind11::function overload = pybind11::get_overload(this,
#
name); \
pybind11::function overload = pybind11::get_overload(this, name); \
if (overload) \
if (overload) \
return overload(__VA_ARGS__).template cast<ret_type>(); }
return overload(__VA_ARGS__).template cast<ret_type>(); }
#define PYBIND11_OVERLOAD(ret_type, c
lass_
name, name, ...) \
#define PYBIND11_OVERLOAD
_NAME
(ret_type, cname, name,
fn,
...) \
PYBIND11_OVERLOAD_INT(ret_type,
class_name,
name, __VA_ARGS__) \
PYBIND11_OVERLOAD_INT(ret_type, name, __VA_ARGS__) \
return c
lass_
name::n
ame
(__VA_ARGS__)
return cname::
f
n(__VA_ARGS__)
#define PYBIND11_OVERLOAD_PURE(ret_type, class_name, name, ...) \
#define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...) \
PYBIND11_OVERLOAD_INT(ret_type, class_name, name, __VA_ARGS__) \
PYBIND11_OVERLOAD_INT(ret_type, name, __VA_ARGS__) \
pybind11::pybind11_fail("Tried to call pure virtual function \"" #name "\"");
pybind11::pybind11_fail("Tried to call pure virtual function \"" #cname "::" name "\"");
#define PYBIND11_OVERLOAD(ret_type, cname, fn, ...) \
PYBIND11_OVERLOAD_NAME(ret_type, cname, #fn, fn, __VA_ARGS__)
#define PYBIND11_OVERLOAD_PURE(ret_type, cname, fn, ...) \
PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, #fn, fn, __VA_ARGS__)
NAMESPACE_END
(
pybind11
)
NAMESPACE_END
(
pybind11
)
...
...
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