I am exposing my C++ classes via Boost.Python. My intention is to expose member variables that are of a user-defined class type with an internal reference. This worked fine until I decided to introduce a member variable of type boost::optional<T>.
There are several great posts that show how to expose boost::optional<T> as return by value. Specifically, I have implemented this converter. The other relevant pieces of my code look like this:
struct Bar {}
struct Foo {
boost::optional<Bar> bar;
}
BOOST_PYTHON_MODULE(mymodule) {
using namespace boost::python;
python_optional<Bar>(); //registering the converter
class_<Foo>("Foo")
.add_property ( "bar", make_getter(&Foo::bar, return_value_policy<return_by_value>()), make_setter(&Foo::bar) )
;
}
I tried to replace return_value_policy<return_by_value>()
with either return_value_policy<reference_existing_object>()
or return_internal_reference<>()
. Both produced a Python TypeError:
>> import mymodule
>> foo = mymodule.Foo()
>> bar = foo.bar
TypeError: No Python class registered for C++ class boost::optional<Bar>
My understanding is that I now I am getting a reference to the boost::optional<T> object. The converter I registered is, however, not called because it expects a boost::optional<T> object and not a reference to such an object. I thought about changing the converter but I am new to this and I really do not know how. Any suggestions?