| 30 | struct SimpleCppDerived : SimpleBase {}; |
| 31 | |
| 32 | void wrap(py::module m) { |
| 33 | py::class_<SimpleBase, SimpleBaseTrampoline>(m, "SimpleBase") |
| 34 | .def(py::init<>()) |
| 35 | .def_readwrite("num", &SimpleBase::num) |
| 36 | .def(py::pickle( |
| 37 | [](const py::object &self) { |
| 38 | py::dict d = py::getattr(self, "__dict__", py::dict()); |
| 39 | return py::make_tuple(self.attr("num"), d); |
| 40 | }, |
| 41 | [](const py::tuple &t) { |
| 42 | if (t.size() != 2) { |
| 43 | throw std::runtime_error("Invalid state!"); |
| 44 | } |
| 45 | auto cpp_state = std::unique_ptr<SimpleBase>(new SimpleBaseTrampoline); |
| 46 | cpp_state->num = t[0].cast<int>(); |
| 47 | auto py_state = t[1].cast<py::dict>(); |
| 48 | return std::make_pair(std::move(cpp_state), py_state); |
| 49 | })); |
| 50 | |
| 51 | m.def("make_SimpleCppDerivedAsBase", |
| 52 | []() { return std::unique_ptr<SimpleBase>(new SimpleCppDerived); }); |
| 53 | m.def("check_dynamic_cast_SimpleCppDerived", [](const SimpleBase *base_ptr) { |
| 54 | return dynamic_cast<const SimpleCppDerived *>(base_ptr) != nullptr; |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | } // namespace exercise_trampoline |
| 59 |
no test coverage detected