[C++-sig] Policies again

Roman Yakovenko roman.yakovenko at gmail.com
Fri May 4 21:23:30 CEST 2007


On 5/3/07, Piotr Jaroszynski <p.jaroszynski at gmail.com> wrote:
> On Wednesday 02 of May 2007 22:56:51 Piotr Jaroszynski wrote:
> > Let me show you my current problem:
> To be even more precise:
>
> #include <boost/python.hpp>
> namespace bp = boost::python;
>
> struct Blah;
>
> struct Foo {
>     Blah * b;
>     Foo(Blah * blah) : b(blah) { }
> };
>
> struct Blah_f {
>     Foo * f;
>     Blah_f(Blah * blah) : f(new Foo(blah)) { }
> };
>
> struct Blah : Blah_f {
>     Blah() : Blah_f(this) { }
> };
>
> BOOST_PYTHON_MODULE(policies)
> {
>     bp::register_ptr_to_python<Foo*>();
>     bp::class_<Foo, boost::noncopyable> f("Foo", bp::no_init);
>     bp::class_<Blah> b("Blah");
>     b.add_property("f", bp::make_getter(&Blah::f,
>          bp::return_internal_reference<>()));
> }
>
> You will probably ask where is the big difference... So when using the
> return_internal_reference policy, which is probably wrong anyway, I get:
> Traceback (most recent call last):
>   File "./test.py", line 5, in ?
>     print Blah().f
> Boost.Python.ArgumentError: Python argument types in
>     None.None(Blah)
> did not match C++ signature:
>     None(Blah_f {lvalue})
>
> I think I should use one of the with_custodian_and_ward variations, but I
> couldn't make any of them to even compile.

No,  return_internal_reference should be fine. I don't remember why,
but Py++ generates a wrapper class for you use-case and adds accessor
function. Take a look on member variables tester. I attached the
generated code.

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: member_variables.cpp
Type: text/x-c++src
Size: 20869 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070504/6baf2626/attachment.cpp>


More information about the Cplusplus-sig mailing list