c++ - boost.python caching wrapped class members -
i have trivial classes dependency:
class { ... // constructor omitted public: const std::string str1; }; class b { public: std::shared_ptr<a> a; } boost_python_import(wrapped) { class_<a, std::shared_ptr<a>>("apy") .def_readonly("str1", &a::str1); class_<b>("bpy") .def_readwrite("a", &b::a); }
in python
import wrapped wr b = wr.bpy() s1 = b.a.str1 // apy wrapper created s2 = b.a.str1 // new apy wrapper created though object same
is there way create apy wrapper once object? especially, because inner object immutable(in particular case). otherwise, there considerable overhead of creating lots of such temporary objects.
the apy
wrappers temporaries because s1, s2
strings. once s1
created, python doesn't care if setup created used create s2
. b.a
gets discarded because isn't stored. same thing happen when like
a1 = b.a s1 = a1.str1 s2 = a1.str1
?
update:
we're trying find out b.a in python.
your invocation
boost_python_import(wrapped) { class_<a, std::shared_ptr<a>>("apy") .def_readonly("str1", &a::str1); class_<b>("bpy") .def_readwrite("a", &b::a); }
the definition of def_readwrite
in boost/python/class.hpp
is
template <class d> self& def_readwrite(char const* name, d const& d, char const* doc=0) { return this->def_readwrite_impl(name, d, doc boost_python_data_member_helper(d)); } // translates class_<b>& def_readwrite(char const* name, &d, char const* doc=0, detail::is_data_member_pointer<b>()){...}
where best matching implementation of def_readwrite_impl
is
class_<b>& def_readwrite_impl(char const* name, b::*a, char const* doc, mpl::true_)
because b::*a
should member of b not function pointer. in turn invokes
class_<b>& def_readwrite_impl(char const* name, b::*pm_, char const* doc, mpl::true_)
pm_
unbound pointer object of type b's member. now, let's move on add_property
.
class_<b>& add_property(char const* name, &fget = b::*a, &fset = b::*a, char const* docstr = 0)
from here go boost/python/objects/class.hpp , @ class_base::add_property
:
void add_property(char const* name, object const& fget, object const& fset, char const* docstr);
implementation unfortunately hidden. signature shows magic happens in make_getter
.
template <class f> object make_getter(f f) { typedef typename api::is_object_operators<f>::type is_obj_or_proxy; return this->make_fn_impl( detail::unwrap_wrapper((w*)0) , f, is_obj_or_proxy(), (char*)0, detail::is_data_member_pointer<f>() ); }
unwrap_wrapper
not doing pointer, because std::shared_ptr
doesn't derive boost::python::wrapper
. is_data_member_pointer
part of first macro. i'm not sure if broken part in case because @ point digging more decorators became tedious.
looking decorator definitions stumbled upon bad news. added comment original question.
Comments
Post a Comment