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
f54ded74
Commit
f54ded74
authored
Apr 20, 2016
by
Wenzel Jakob
Browse files
added caster for std::reference_wrapper (fixes #171)
parent
afb9c177
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
31 additions
and
9 deletions
+31
-9
example/issues.cpp
example/issues.cpp
+20
-9
example/issues.py
example/issues.py
+3
-0
example/issues.ref
example/issues.ref
+1
-0
include/pybind11/cast.h
include/pybind11/cast.h
+7
-0
No files found.
example/issues.cpp
View file @
f54ded74
...
...
@@ -8,6 +8,7 @@
*/
#include "example.h"
#include <pybind11/stl.h>
struct
Base
{
virtual
void
dispatch
(
void
)
const
=
0
;
...
...
@@ -19,15 +20,10 @@ struct DispatchIssue : Base {
}
};
void
dispatch_issue_go
(
const
Base
*
b
)
{
b
->
dispatch
();
}
PYBIND11_PLUGIN
(
mytest
)
{
pybind11
::
module
m
(
"mytest"
,
"A test"
);
struct
Placeholder
{
int
i
;
};
void
dispatch_issue_go
(
const
Base
*
b
)
{
b
->
dispatch
();
}
return
m
.
ptr
();
}
void
init_issues
(
py
::
module
&
m
)
{
py
::
module
m2
=
m
.
def_submodule
(
"issues"
);
...
...
@@ -38,10 +34,25 @@ void init_issues(py::module &m) {
m2
.
def
(
"print_char"
,
[](
char
c
)
{
std
::
cout
<<
c
<<
std
::
endl
;
});
// #159: virtual function dispatch has problems with similar-named functions
py
bind11
::
class_
<
DispatchIssue
>
base
(
m2
,
"DispatchIssue"
);
py
::
class_
<
DispatchIssue
>
base
(
m2
,
"DispatchIssue"
);
base
.
alias
<
Base
>
()
.
def
(
py
bind11
::
init
<>
())
.
def
(
py
::
init
<>
())
.
def
(
"dispatch"
,
&
Base
::
dispatch
);
m2
.
def
(
"dispatch_issue_go"
,
&
dispatch_issue_go
);
py
::
class_
<
Placeholder
>
(
m2
,
"Placeholder"
)
.
def
(
"__repr__"
,
[](
const
Placeholder
&
p
)
{
return
"Placeholder["
+
std
::
to_string
(
p
.
i
)
+
"]"
;
});
// #171: Can't return reference wrappers (or STL datastructures containing them)
m2
.
def
(
"return_vec_of_reference_wrapper"
,
[]
{
Placeholder
*
p1
=
new
Placeholder
{
1
};
Placeholder
*
p2
=
new
Placeholder
{
2
};
Placeholder
*
p3
=
new
Placeholder
{
2
};
std
::
vector
<
std
::
reference_wrapper
<
Placeholder
>>
v
;
v
.
push_back
(
std
::
ref
(
*
p1
));
v
.
push_back
(
std
::
ref
(
*
p2
));
v
.
push_back
(
std
::
ref
(
*
p3
));
return
v
;
});
}
example/issues.py
View file @
f54ded74
...
...
@@ -5,6 +5,7 @@ sys.path.append('.')
from
example.issues
import
print_cchar
,
print_char
from
example.issues
import
DispatchIssue
,
dispatch_issue_go
from
example.issues
import
return_vec_of_reference_wrapper
print_cchar
(
"const char *"
)
print_char
(
'c'
)
...
...
@@ -26,3 +27,5 @@ class PyClass2(DispatchIssue):
b
=
PyClass2
()
dispatch_issue_go
(
b
)
print
(
return_vec_of_reference_wrapper
())
example/issues.ref
View file @
f54ded74
...
...
@@ -2,3 +2,4 @@ const char *
c
Failed as expected: Tried to call pure virtual function "dispatch"
Yay..
[Placeholder[1], Placeholder[2], Placeholder[2]]
include/pybind11/cast.h
View file @
f54ded74
...
...
@@ -241,6 +241,13 @@ protected:
static
void
*
copy_constructor
(
const
void
*
)
{
return
nullptr
;
}
};
template
<
typename
type
>
class
type_caster
<
std
::
reference_wrapper
<
type
>>
:
public
type_caster
<
type
>
{
public:
static
handle
cast
(
const
std
::
reference_wrapper
<
type
>
&
src
,
return_value_policy
policy
,
handle
parent
)
{
return
type_caster
<
type
>::
cast
(
&
src
.
get
(),
policy
,
parent
);
}
};
#define PYBIND11_TYPE_CASTER(type, py_name) \
protected: \
type value; \
...
...
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