[C++-sig] Exposing members of members

Ricardo Abreu gumbeto at gmail.com
Tue Jan 20 11:30:30 CET 2009


Thank you very much for your answer, but what I really wanted was to 
expose to Python just the first element of the pair and not the whole 
pair, so that the fact that it is a pair in c++ would be transparent to 
python. For instance, if I had struct A{std::pair<int,bool> a;};, I just 
wanted to expose a.first, as a simple int...
Any ideas for this?


Ralf W. Grosse-Kunstleve wrote:
> I'd define to/from Python conversions for the std::pair, mapping to
> Python tuples, then use .add_property().
> Alternatively, wrap std::pair the normal way via class_, then you
> can use .def_readwrite().
> You need to handle each std::pair<A,B> combination of A,B separately,
> like any other template class you want to wrap or convert to/from a
> Python object.
> See also:
> http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/faq.html#custom_string
> This could be a starting point for std::pair<A,B> conversions. Probably you want
> to make it into a converter template if you have more than one combination of A,B.
> Here is code we use for the to_python conversions:
>   http://cci.lbl.gov/cctbx_sources/boost_adaptbx/
>     std_pair_conversion.h
>     std_pair_ext.cpp
> We don't have corresponding from_python conversions.
> If you add them, let me know!
> The next item in the FAQ is also relevant.
> ----- Original Message ----
> From: Ricardo Abreu <gumbeto at gmail.com>
> To: cplusplus-sig at python.org
> Sent: Monday, January 19, 2009 7:27:46 AM
> Subject: [C++-sig]  Exposing members of members
> Hello,
> Suppose that I have a class A with a member of type std::pair, called a. If I want to expose class A to python while allowing read/write access to the object pointed to by a.first, what to I put in def_readwrite?
> I tried something like class<A>("A").def_readwrite("a", &A::a::first); but it doesn't work. Also been trying with boost::bind but didn't have any luck :s
> I know I could do getters and setters, but there would be a lot of them because my "A" has a lot of members like "a".
> Thank you in advance,
> Ricardo

More information about the Cplusplus-sig mailing list