|
| 1 | +/// Copyright 2023 LAAS-CNRS, INRIA |
| 2 | +#include <eigenpy/eigenpy.hpp> |
| 3 | + |
| 4 | +using std::shared_ptr; |
| 5 | +namespace bp = boost::python; |
| 6 | + |
| 7 | +// fwd declaration |
| 8 | +struct MyVirtualData; |
| 9 | + |
| 10 | +/// A virtual class with two pure virtual functions taking different signatures, |
| 11 | +/// and a polymorphic factory function. |
| 12 | +struct MyVirtualClass { |
| 13 | + MyVirtualClass() {} |
| 14 | + virtual ~MyVirtualClass() {} |
| 15 | + |
| 16 | + // polymorphic fn taking arg by shared_ptr |
| 17 | + virtual void doSomethingPtr(shared_ptr<MyVirtualData> const &data) const = 0; |
| 18 | + // polymorphic fn taking arg by reference |
| 19 | + virtual void doSomethingRef(MyVirtualData &data) const = 0; |
| 20 | + |
| 21 | + virtual shared_ptr<MyVirtualData> createData() const { |
| 22 | + return std::make_shared<MyVirtualData>(*this); |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +struct MyVirtualData { |
| 27 | + MyVirtualData(MyVirtualClass const&) {} |
| 28 | + virtual ~MyVirtualData() {} // virtual dtor to mark class as polymorphic |
| 29 | +}; |
| 30 | + |
| 31 | +shared_ptr<MyVirtualData> callDoSomethingPtr(const MyVirtualClass &obj) { |
| 32 | + auto d = obj.createData(); |
| 33 | + printf("Created MyVirtualData with address %p\n", (void *)d.get()); |
| 34 | + obj.doSomethingPtr(d); |
| 35 | + return d; |
| 36 | +} |
| 37 | + |
| 38 | +shared_ptr<MyVirtualData> callDoSomethingRef(const MyVirtualClass &obj) { |
| 39 | + auto d = obj.createData(); |
| 40 | + printf("Created MyVirtualData with address %p\n", (void *)d.get()); |
| 41 | + obj.doSomethingRef(*d); |
| 42 | + return d; |
| 43 | +} |
| 44 | + |
| 45 | +void throw_virtual_not_implemented_error() { |
| 46 | + throw std::runtime_error("Called C++ virtual function."); |
| 47 | +} |
| 48 | + |
| 49 | +/// Wrapper classes |
| 50 | +struct VirtualClassWrapper : MyVirtualClass, bp::wrapper<MyVirtualClass> { |
| 51 | + void doSomethingPtr(shared_ptr<MyVirtualData> const &data) const override { |
| 52 | + if (bp::override fo = this->get_override("doSomethingPtr")) { |
| 53 | + /// shared_ptr HAS to be passed by value. |
| 54 | + /// Boost.Python's argument converter has the wrong behaviour for |
| 55 | + /// reference_wrapper<shared_ptr<T>>, so boost::ref(data) does not work. |
| 56 | + fo(data); |
| 57 | + return; |
| 58 | + } |
| 59 | + throw_virtual_not_implemented_error(); |
| 60 | + } |
| 61 | + |
| 62 | + /// The data object is passed by mutable reference to this function, |
| 63 | + /// and wrapped in a @c boost::reference_wrapper when passed to the override. |
| 64 | + /// Otherwise, Boost.Python's argument converter will convert to Python by |
| 65 | + /// value and create a copy. |
| 66 | + void doSomethingRef(MyVirtualData &data) const override { |
| 67 | + if (bp::override fo = this->get_override("doSomethingRef")) { |
| 68 | + fo(boost::ref(data)); |
| 69 | + return; |
| 70 | + } |
| 71 | + throw_virtual_not_implemented_error(); |
| 72 | + } |
| 73 | + |
| 74 | + shared_ptr<MyVirtualData> createData() const override { |
| 75 | + if (bp::override fo = this->get_override("createData")) return fo(); |
| 76 | + return default_createData(); |
| 77 | + } |
| 78 | + |
| 79 | + shared_ptr<MyVirtualData> default_createData() const { |
| 80 | + return MyVirtualClass::createData(); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +/// This "trampoline class" does nothing but is ABSOLUTELY required to ensure |
| 85 | +/// downcasting works properly with non-smart ptr signatures. Otherwise, |
| 86 | +/// there is no handle to the original Python object ( @c PyObject *). |
| 87 | +/// Every single polymorphic type exposed to Python should be exposed through such a trampoline. |
| 88 | +/// Users can also create their own wrapper classes by taking inspiration from boost::python::wrapper<T>. |
| 89 | +struct DataWrapper : MyVirtualData, bp::wrapper<MyVirtualData> { |
| 90 | + /// we have to use-declare non-defaulted constructors |
| 91 | + /// (see https://en.cppreference.com/w/cpp/language/default_constructor) |
| 92 | + /// or define them manually. |
| 93 | + using MyVirtualData::MyVirtualData; |
| 94 | +}; |
| 95 | + |
| 96 | +/// Take and return a const reference |
| 97 | +const MyVirtualData &iden_ref(const MyVirtualData &d) { |
| 98 | + // try cast to holder |
| 99 | + return d; |
| 100 | +} |
| 101 | + |
| 102 | +/// Take a shared_ptr (by const reference or value, doesn't matter), return by const reference |
| 103 | +const MyVirtualData &iden_shared(const shared_ptr<MyVirtualData> &d) { |
| 104 | + // get boost.python's custom deleter |
| 105 | + // boost.python hides the handle to the original object in there |
| 106 | + // dter being nonzero indicates shared_ptr was wrapped by Boost.Python |
| 107 | + auto *dter = std::get_deleter<bp::converter::shared_ptr_deleter>(d); |
| 108 | + if (dter != 0) printf("> input shared_ptr has a deleter\n"); |
| 109 | + return *d; |
| 110 | +} |
| 111 | + |
| 112 | +/// Take and return a shared_ptr |
| 113 | +shared_ptr<MyVirtualData> copy_shared(const shared_ptr<MyVirtualData> &d) { |
| 114 | + auto *dter = std::get_deleter<bp::converter::shared_ptr_deleter>(d); |
| 115 | + if (dter != 0) printf("> input shared_ptr has a deleter\n"); |
| 116 | + return d; |
| 117 | +} |
| 118 | + |
| 119 | +BOOST_PYTHON_MODULE(bind_virtual_factory) { |
| 120 | + assert(std::is_polymorphic<MyVirtualClass>::value && |
| 121 | + "MyVirtualClass should be polymorphic!"); |
| 122 | + assert(std::is_polymorphic<MyVirtualData>::value && |
| 123 | + "MyVirtualData should be polymorphic!"); |
| 124 | + |
| 125 | + bp::class_<VirtualClassWrapper, boost::noncopyable>( |
| 126 | + "MyVirtualClass", bp::init<>(bp::args("self"))) |
| 127 | + .def("doSomething", bp::pure_virtual(&MyVirtualClass::doSomethingPtr), |
| 128 | + bp::args("self", "data")) |
| 129 | + .def("doSomethingRef", bp::pure_virtual(&MyVirtualClass::doSomethingRef), |
| 130 | + bp::args("self", "data")) |
| 131 | + .def("createData", &MyVirtualClass::createData, |
| 132 | + &VirtualClassWrapper::default_createData, bp::args("self")); |
| 133 | + |
| 134 | + bp::register_ptr_to_python<shared_ptr<MyVirtualData> >(); |
| 135 | + /// Trampoline used as 1st argument |
| 136 | + /// otherwise if passed as "HeldType", we need to define |
| 137 | + /// the constructor and call initializer manually. |
| 138 | + bp::class_<DataWrapper, boost::noncopyable>("MyVirtualData", bp::no_init) |
| 139 | + .def(bp::init<MyVirtualClass const&>(bp::args("self", "model"))); |
| 140 | + |
| 141 | + bp::def("callDoSomethingPtr", callDoSomethingPtr, bp::args("obj")); |
| 142 | + bp::def("callDoSomethingRef", callDoSomethingRef, bp::args("obj")); |
| 143 | + |
| 144 | + bp::def("iden_ref", iden_ref, bp::return_internal_reference<>()); |
| 145 | + bp::def("iden_shared", iden_shared, bp::return_internal_reference<>()); |
| 146 | + bp::def("copy_shared", copy_shared); |
| 147 | +} |
0 commit comments