bind_virtual_factory.cpp
Go to the documentation of this file.
1 #include <eigenpy/eigenpy.hpp>
3 
4 using std::shared_ptr;
5 namespace bp = boost::python;
6 
7 // fwd declaration
8 struct MyVirtualData;
9 
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 {
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 
46  throw std::runtime_error("Called C++ virtual function.");
47 }
48 
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")) {
56  fo(data);
57  return;
58  }
60  }
61 
66  void doSomethingRef(MyVirtualData &data) const override {
67  if (bp::override fo = this->get_override("doSomethingRef")) {
68  fo(boost::ref(data));
69  return;
70  }
72  }
73 
74  shared_ptr<MyVirtualData> createData() const override {
75  if (bp::override fo = this->get_override("createData")) {
76  bp::object result = fo().as<bp::object>();
77  return bp::extract<shared_ptr<MyVirtualData> >(result);
78  }
79  return default_createData();
80  }
81 
82  shared_ptr<MyVirtualData> default_createData() const {
84  }
85 };
86 
93 struct DataWrapper : MyVirtualData, bp::wrapper<MyVirtualData> {
98 };
99 
102  // try cast to holder
103  return d;
104 }
105 
108 const MyVirtualData &iden_shared(const shared_ptr<MyVirtualData> &d) {
109  // get boost.python's custom deleter
110  // boost.python hides the handle to the original object in there
111  // dter being nonzero indicates shared_ptr was wrapped by Boost.Python
112  auto *dter = std::get_deleter<bp::converter::shared_ptr_deleter>(d);
113  if (dter != 0) printf("> input shared_ptr has a deleter\n");
114  return *d;
115 }
116 
118 shared_ptr<MyVirtualData> copy_shared(const shared_ptr<MyVirtualData> &d) {
119  auto *dter = std::get_deleter<bp::converter::shared_ptr_deleter>(d);
120  if (dter != 0) printf("> input shared_ptr has a deleter\n");
121  return d;
122 }
123 
124 BOOST_PYTHON_MODULE(bind_virtual_factory) {
126  "MyVirtualClass should be polymorphic!");
128  "MyVirtualData should be polymorphic!");
129 
130  bp::class_<VirtualClassWrapper, boost::noncopyable>(
131  "MyVirtualClass", bp::init<>(bp::args("self")))
132  .def("doSomething", bp::pure_virtual(&MyVirtualClass::doSomethingPtr),
133  bp::args("self", "data"))
134  .def("doSomethingRef", bp::pure_virtual(&MyVirtualClass::doSomethingRef),
135  bp::args("self", "data"))
136  .def("createData", &MyVirtualClass::createData,
137  &VirtualClassWrapper::default_createData, bp::args("self"));
138 
139  bp::register_ptr_to_python<shared_ptr<MyVirtualData> >();
143  bp::class_<DataWrapper, boost::noncopyable>("MyVirtualData", bp::no_init)
144  .def(bp::init<MyVirtualClass const &>(bp::args("self", "model")));
145 
146  bp::def("callDoSomethingPtr", callDoSomethingPtr, bp::args("obj"));
147  bp::def("callDoSomethingRef", callDoSomethingRef, bp::args("obj"));
148 
149  bp::def("iden_ref", iden_ref, bp::return_internal_reference<>());
150  bp::def("iden_shared", iden_shared, bp::return_internal_reference<>());
151  bp::def("copy_shared", copy_shared);
152 }
BOOST_PYTHON_MODULE
BOOST_PYTHON_MODULE(bind_virtual_factory)
Definition: bind_virtual_factory.cpp:124
MyVirtualClass::MyVirtualClass
MyVirtualClass()
Definition: bind_virtual_factory.cpp:13
boost::python
Definition: alignment.hpp:49
VirtualClassWrapper::default_createData
shared_ptr< MyVirtualData > default_createData() const
Definition: bind_virtual_factory.cpp:82
MyVirtualClass::doSomethingRef
virtual void doSomethingRef(MyVirtualData &data) const =0
setup.data
data
Definition: setup.in.py:48
VirtualClassWrapper
Wrapper classes.
Definition: bind_virtual_factory.cpp:50
ref
Eigen::TensorRef< Tensor > ref(Eigen::TensorRef< Tensor > tensor)
Definition: tensor.cpp:55
VirtualClassWrapper::createData
shared_ptr< MyVirtualData > createData() const override
Definition: bind_virtual_factory.cpp:74
MyVirtualClass::doSomethingPtr
virtual void doSomethingPtr(shared_ptr< MyVirtualData > const &data) const =0
MyVirtualData
Definition: bind_virtual_factory.cpp:26
test_type_info.d
d
Definition: test_type_info.py:3
MyVirtualClass::createData
virtual shared_ptr< MyVirtualData > createData() const
Definition: bind_virtual_factory.cpp:21
MyVirtualClass
Definition: bind_virtual_factory.cpp:12
test_matrix.value
float value
Definition: test_matrix.py:161
iden_ref
const MyVirtualData & iden_ref(const MyVirtualData &d)
Take and return a const reference.
Definition: bind_virtual_factory.cpp:101
VirtualClassWrapper::doSomethingPtr
void doSomethingPtr(shared_ptr< MyVirtualData > const &data) const override
Definition: bind_virtual_factory.cpp:51
eigenpy.hpp
throw_virtual_not_implemented_error
void throw_virtual_not_implemented_error()
Definition: bind_virtual_factory.cpp:45
DataWrapper
Definition: bind_virtual_factory.cpp:93
iden_shared
const MyVirtualData & iden_shared(const shared_ptr< MyVirtualData > &d)
Definition: bind_virtual_factory.cpp:108
VirtualClassWrapper::doSomethingRef
void doSomethingRef(MyVirtualData &data) const override
Definition: bind_virtual_factory.cpp:66
callDoSomethingPtr
shared_ptr< MyVirtualData > callDoSomethingPtr(const MyVirtualClass &obj)
Definition: bind_virtual_factory.cpp:31
copy_shared
shared_ptr< MyVirtualData > copy_shared(const shared_ptr< MyVirtualData > &d)
Take and return a shared_ptr.
Definition: bind_virtual_factory.cpp:118
MyVirtualData::MyVirtualData
MyVirtualData(MyVirtualClass const &)
Definition: bind_virtual_factory.cpp:27
callDoSomethingRef
shared_ptr< MyVirtualData > callDoSomethingRef(const MyVirtualClass &obj)
Definition: bind_virtual_factory.cpp:38
MyVirtualClass::~MyVirtualClass
virtual ~MyVirtualClass()
Definition: bind_virtual_factory.cpp:14
MyVirtualData::~MyVirtualData
virtual ~MyVirtualData()
Definition: bind_virtual_factory.cpp:28


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Nov 2 2024 02:14:45