[prev in list] [next in list] [prev in thread] [next in thread]
List: python-cpp-sig
Subject: [C++-sig] add_property with policy?
From: roman.yakovenko () gmail ! com (Roman Yakovenko)
Date: 2006-05-22 18:07:43
Message-ID: 7465b6170605221107p49b52204n42dd819a1f3c9f98 () mail ! gmail ! com
[Download RAW message or body]
On 5/22/06, Neal Becker <ndbecker2 at gmail.com> wrote:
> I'd like to use add_property with a reference data member.
>
> In the following, "getA" will work fine. But, I'd like to make this work
> with attribute access syntax:
>
> python code:
> b = B()
> an_A = b.a
>
> Anyway to make this work? The add_property below won't compile:
>
> #include <boost/python/module.hpp>
> #include <boost/python/def.hpp>
> #include <boost/python/class.hpp>
> #include <boost/python/init.hpp>
> #include <boost/python/return_internal_reference.hpp>
>
> struct A {};
>
> struct B {
> A& a;
> };
>
> A& getA (B const& b) {
> return b.a;
> }
>
> using namespace boost::python;
>
> BOOST_PYTHON_MODULE (test1) {
> class_<B> ("B")
> .def ("getA", &getA, return_internal_reference<>()) << this is OK
> .add_property ("A", &getA) << this is NOT OK
> ;
> }
>
> /usr/local/src/boost.cvs/boost/python/detail/invoke.hpp:75: error: no match
> for call to '(const
> boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<A&>)
> (A&)'
You should use boost::python::make_function.
class_<B>("B")
.add_property( "a", make_function( &::getA, return_internal_reference<> ) );
This should work. Consult make_function documentation:
http://boost.org/libs/python/doc/v2/make_function.html
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic