mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-25 22:52:01 +00:00
completed implicit type casters for reference_wrapper
This commit is contained in:
parent
f54ded74f1
commit
dbe43ffcce
@ -16,6 +16,7 @@ Changelog
|
|||||||
* Added a ``get_include()`` function to the Python module that returns the path
|
* Added a ``get_include()`` function to the Python module that returns the path
|
||||||
of the directory containing the installed pybind11 header files
|
of the directory containing the installed pybind11 header files
|
||||||
* Documentation improvements: import issues, symbol visibility, pickling, limitations
|
* Documentation improvements: import issues, symbol visibility, pickling, limitations
|
||||||
|
* Added casting support for ``std::reference_wrapper<>``
|
||||||
|
|
||||||
1.4 (April 7, 2016)
|
1.4 (April 7, 2016)
|
||||||
--------------------------
|
--------------------------
|
||||||
|
@ -20,7 +20,7 @@ struct DispatchIssue : Base {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Placeholder { int i; };
|
struct Placeholder { int i; Placeholder(int i) : i(i) { } };
|
||||||
|
|
||||||
void dispatch_issue_go(const Base * b) { b->dispatch(); }
|
void dispatch_issue_go(const Base * b) { b->dispatch(); }
|
||||||
|
|
||||||
@ -42,17 +42,19 @@ void init_issues(py::module &m) {
|
|||||||
m2.def("dispatch_issue_go", &dispatch_issue_go);
|
m2.def("dispatch_issue_go", &dispatch_issue_go);
|
||||||
|
|
||||||
py::class_<Placeholder>(m2, "Placeholder")
|
py::class_<Placeholder>(m2, "Placeholder")
|
||||||
|
.def(py::init<int>())
|
||||||
.def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; });
|
.def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; });
|
||||||
|
|
||||||
// #171: Can't return reference wrappers (or STL datastructures containing them)
|
// #171: Can't return reference wrappers (or STL datastructures containing them)
|
||||||
m2.def("return_vec_of_reference_wrapper", [] {
|
m2.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<Placeholder> p4){
|
||||||
Placeholder *p1 = new Placeholder{1};
|
Placeholder *p1 = new Placeholder{1};
|
||||||
Placeholder *p2 = new Placeholder{2};
|
Placeholder *p2 = new Placeholder{2};
|
||||||
Placeholder *p3 = new Placeholder{2};
|
Placeholder *p3 = new Placeholder{3};
|
||||||
std::vector<std::reference_wrapper<Placeholder>> v;
|
std::vector<std::reference_wrapper<Placeholder>> v;
|
||||||
v.push_back(std::ref(*p1));
|
v.push_back(std::ref(*p1));
|
||||||
v.push_back(std::ref(*p2));
|
v.push_back(std::ref(*p2));
|
||||||
v.push_back(std::ref(*p3));
|
v.push_back(std::ref(*p3));
|
||||||
|
v.push_back(p4);
|
||||||
return v;
|
return v;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ sys.path.append('.')
|
|||||||
|
|
||||||
from example.issues import print_cchar, print_char
|
from example.issues import print_cchar, print_char
|
||||||
from example.issues import DispatchIssue, dispatch_issue_go
|
from example.issues import DispatchIssue, dispatch_issue_go
|
||||||
from example.issues import return_vec_of_reference_wrapper
|
from example.issues import Placeholder ,return_vec_of_reference_wrapper
|
||||||
|
|
||||||
print_cchar("const char *")
|
print_cchar("const char *")
|
||||||
print_char('c')
|
print_char('c')
|
||||||
@ -28,4 +28,4 @@ class PyClass2(DispatchIssue):
|
|||||||
b = PyClass2()
|
b = PyClass2()
|
||||||
dispatch_issue_go(b)
|
dispatch_issue_go(b)
|
||||||
|
|
||||||
print(return_vec_of_reference_wrapper())
|
print(return_vec_of_reference_wrapper(Placeholder(4)))
|
||||||
|
@ -2,4 +2,4 @@ const char *
|
|||||||
c
|
c
|
||||||
Failed as expected: Tried to call pure virtual function "dispatch"
|
Failed as expected: Tried to call pure virtual function "dispatch"
|
||||||
Yay..
|
Yay..
|
||||||
[Placeholder[1], Placeholder[2], Placeholder[2]]
|
[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]
|
||||||
|
@ -246,6 +246,8 @@ public:
|
|||||||
static handle cast(const std::reference_wrapper<type> &src, return_value_policy policy, handle parent) {
|
static handle cast(const std::reference_wrapper<type> &src, return_value_policy policy, handle parent) {
|
||||||
return type_caster<type>::cast(&src.get(), policy, parent);
|
return type_caster<type>::cast(&src.get(), policy, parent);
|
||||||
}
|
}
|
||||||
|
template <typename T> using cast_op_type = std::reference_wrapper<type>;
|
||||||
|
operator std::reference_wrapper<type>() { return std::ref(*((type *) this->value)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
#define PYBIND11_TYPE_CASTER(type, py_name) \
|
#define PYBIND11_TYPE_CASTER(type, py_name) \
|
||||||
|
Loading…
Reference in New Issue
Block a user