summaryrefslogtreecommitdiff
path: root/ext/pybind11/tests/test_callbacks.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ext/pybind11/tests/test_callbacks.cpp')
-rw-r--r--ext/pybind11/tests/test_callbacks.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/ext/pybind11/tests/test_callbacks.cpp b/ext/pybind11/tests/test_callbacks.cpp
index 31d4e39aa..f89cc1c79 100644
--- a/ext/pybind11/tests/test_callbacks.cpp
+++ b/ext/pybind11/tests/test_callbacks.cpp
@@ -74,6 +74,27 @@ struct Payload {
/// Something to trigger a conversion error
struct Unregistered {};
+class AbstractBase {
+public:
+ virtual unsigned int func() = 0;
+};
+
+void func_accepting_func_accepting_base(std::function<double(AbstractBase&)>) { }
+
+struct MovableObject {
+ bool valid = true;
+
+ MovableObject() = default;
+ MovableObject(const MovableObject &) = default;
+ MovableObject &operator=(const MovableObject &) = default;
+ MovableObject(MovableObject &&o) : valid(o.valid) { o.valid = false; }
+ MovableObject &operator=(MovableObject &&o) {
+ valid = o.valid;
+ o.valid = false;
+ return *this;
+ }
+};
+
test_initializer callbacks([](py::module &m) {
m.def("test_callback1", &test_callback1);
m.def("test_callback2", &test_callback2);
@@ -136,6 +157,7 @@ test_initializer callbacks([](py::module &m) {
return [p]() {
/* p should be cleaned up when the returned function is garbage collected */
+ (void) p;
};
});
@@ -146,4 +168,15 @@ test_initializer callbacks([](py::module &m) {
m.def("test_dummy_function", &test_dummy_function);
// Export the payload constructor statistics for testing purposes:
m.def("payload_cstats", &ConstructorStats::get<Payload>);
+
+ m.def("func_accepting_func_accepting_base",
+ func_accepting_func_accepting_base);
+
+ py::class_<MovableObject>(m, "MovableObject");
+
+ m.def("callback_with_movable", [](std::function<void(MovableObject &)> f) {
+ auto x = MovableObject();
+ f(x); // lvalue reference shouldn't move out object
+ return x.valid; // must still return `true`
+ });
});