From f54ded74f170946d8c8987dee3d81384eec71d6d Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Wed, 20 Apr 2016 17:00:57 +0200 Subject: [PATCH] added caster for std::reference_wrapper (fixes #171) --- example/issues.cpp | 29 ++++++++++++++++++++--------- example/issues.py | 3 +++ example/issues.ref | 1 + include/pybind11/cast.h | 7 +++++++ 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/example/issues.cpp b/example/issues.cpp index b0f3a6beb..e2048b13a 100644 --- a/example/issues.cpp +++ b/example/issues.cpp @@ -8,6 +8,7 @@ */ #include "example.h" +#include struct Base { virtual void dispatch(void) const = 0; @@ -19,15 +20,10 @@ struct DispatchIssue : Base { } }; +struct Placeholder { int i; }; + void dispatch_issue_go(const Base * b) { b->dispatch(); } -PYBIND11_PLUGIN(mytest) -{ - pybind11::module m("mytest", "A test"); - - - 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 - pybind11::class_ base(m2, "DispatchIssue"); + py::class_ base(m2, "DispatchIssue"); base.alias() - .def(pybind11::init<>()) + .def(py::init<>()) .def("dispatch", &Base::dispatch); m2.def("dispatch_issue_go", &dispatch_issue_go); + + py::class_(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> v; + v.push_back(std::ref(*p1)); + v.push_back(std::ref(*p2)); + v.push_back(std::ref(*p3)); + return v; + }); } diff --git a/example/issues.py b/example/issues.py index 84752b0d3..4095daa6f 100644 --- a/example/issues.py +++ b/example/issues.py @@ -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()) diff --git a/example/issues.ref b/example/issues.ref index 55cbdda1d..74b34c28f 100644 --- a/example/issues.ref +++ b/example/issues.ref @@ -2,3 +2,4 @@ const char * c Failed as expected: Tried to call pure virtual function "dispatch" Yay.. +[Placeholder[1], Placeholder[2], Placeholder[2]] diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 802c6746b..1002ab1e7 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -241,6 +241,13 @@ protected: static void *copy_constructor(const void *) { return nullptr; } }; +template class type_caster> : public type_caster { +public: + static handle cast(const std::reference_wrapper &src, return_value_policy policy, handle parent) { + return type_caster::cast(&src.get(), policy, parent); + } +}; + #define PYBIND11_TYPE_CASTER(type, py_name) \ protected: \ type value; \